;(function($) {
	$.fn.youtubeRewrite = function() {
		var elements = this;

		elements.each(function() {
			var t = $(this);
			var id = t.attr("id");
			var arr = t.html().split(",");
			var params = {wmode:"transparent"};
			swfobject.embedSWF(arr[0], id, arr[1], arr[2], arr[3] + ".0.0", '/js/expressInstall.swf', "", params);
		});

		return this;
	};
})(jQuery);

$(function(){
	// prettyPhoto
	$("a[rel^='prettyPhoto']").livequery(function(){$(this).prettyPhoto();});
	// youtubeRewrite
	$("div.div_youtube_func").livequery(function(){$(this).youtubeRewrite();});
	$('div.div_input_comment>textarea').livequery("focus", function(){
		var obj = $(this);
		var defValue = 'ひとことを書く';
		var bWidth = (document.URL.match("/my_top|my_topics/")) ? '475px' : '395px';
		var tWidth = (document.URL.match("/my_top|my_topics/")) ? '350px' : '395px';

		obj.css("height","60px").css("width", tWidth);
		if (obj.val() == defValue) obj.val("");
		obj.parent().find("p.p_btn_comment,p.p_explain,p.p_cmt_twitter").show();

		obj.parent().find('p>input:visible').blur(function(){
			if ($.trim(obj.val()) == "") {
				obj.val(defValue).css("height","20px").css("width", bWidth).parent().parent().find("p.p_btn_comment,p.p_explain,p.p_cmt_twitter").hide();
				$("p.comment_error").remove();
			}
		});
	});
	// ひとこと送信
	$('p.p_btn_comment>input').livequery("click", function (){
		var btnObj = $(this).eq(0);
		var name = btnObj.attr("disabled","disabled").attr("name").split("_");
		var id = '#comment_'+name[1];
		var commentObj = $(id);
		var comment = $.trim(commentObj.attr("disabled","disabled").val());
		var list_id = '#topicReslist_'+name[1];
		var chk_id = '#chkCmtTwtr_'+name[1];
		$("p.comment_error").remove();
		if (comment.length > 0 && comment.length <= 100) {
			$.ajax({
				type: "POST",
				dataType: "json",
				async: false,
				scriptCharset: "UTF-8",
				data: {
					"submit_type": 'SAVE',
				    "e": name[2],
					"t": name[1],
					"mode": (document.URL.match("/my_top|my_topics/")) ? 'mypage' : 'enterprise',
					"twitter_flg": ($(chk_id).attr("checked")) ? 1 : 0,
					"content": comment
				},
				cache: false,
				url: "/ajax_get_topic_comment.php",
				success: function (data) {
					var bWidth = (document.URL.match("/my_top|my_topics/")) ? '475px' : '395px';
					$(list_id).empty().append(data.html);
					commentObj.attr("value","");
					btnObj.parent().parent().find("p.p_btn_comment,p.p_explain,p.p_cmt_twitter").hide();
					commentObj.val("ひとことを書く").css("height","20px").css("width",bWidth);
				}
			});
		} else if (comment.length > 100) {
			commentObj.parent().after('<p class="comment_error err-text">100文字以内で入力してください。</p>');
		} else if (comment.length == 0) {
			commentObj.parent().after('<p class="comment_error err-text">入力してください。</p>');
		}
		commentObj.attr("disabled","");
		btnObj.attr("disabled","");
	});

	//Read More
	$("a.topic_more,a.comment_more").livequery("click",function(){
		var obj = $(this);
		obj.hide().parent().children("span").toggle();
	});

	$('p.p_cmt_twitter input:checkbox').livequery("click",function(){
		var obj = $(this).siblings("label");
		var over;
		var out;
		if ($(this).attr("checked")) {
			over = 'onro';
			out = 'on';
		} else {
			over = 'offro';
			out = 'off';
		}
		obj.removeClass().addClass(out);
		obj.blur(function(){
			obj.mouseover(function(){
				var obj = j$(this);
				obj.removeClass().addClass(over);
			}).mouseout(function(){
				var obj = j$(this);
				obj.removeClass().addClass(out);
			});
		});
	});
	$('p.p_cmt_twitter label').livequery("mouseover",function(){
		var obj = $(this);
		var over;
		if (obj.siblings("input:checkbox").attr("checked")) over = 'onro';
		else over = 'offro';
		obj.removeClass().addClass(over);
	}).livequery("mouseout", function(){
		var obj = $(this);
		var out;
		if (obj.siblings("input:checkbox").attr("checked")) out = 'on';
		else out = 'off';
		obj.removeClass().addClass(out);
		
	});

	// タブクリック
	$('#topic_attach>span').click(function (){
		var spanID = $(this).attr('id');;

		// エントリー部分
		$('#topic_forms .topic_form').hide();
		$('#form_' + spanID).show();
		// 値挿入
		$("input:hidden[name='topic_type']").val(spanID);
		$("#topic_attach").hide();
	});

	$('#add_form_image_file').click(function (){
		var limit = 5;
		var n = $('#form_image_file > p:has(input)').size();
		var end_id = '#topic_img_file_' + n;
		var id = 'topic_img_file_' + (n + 1);
		var html = '<p class="input_image">画像ファイル：<input type="file" id="'+id+'" name="'+id+'" size="40" /></p>';
		if (n < limit) {
			$("#form_image_file > p").eq(n-1).after(html);
		}
		if (n + 1 == limit) {
			$('#add_form_image_file').hide();
		}
	});

	$('#add_form_youtube_url').click(function (){
		var limit = 5;
		var n = $('#form_youtube_url > p:has(input)').size();
		var end_id = '#topic_youtube_url_' + n;
		var id = 'topic_youtube_url_' + (n + 1);
		var name = 'topic_youtube_url[' + (n + 1) + ']';
		var html = '<p class="input_url">動画URL：<input type="text" id="'+id+'" name="'+name+'" size="40" /></p>';
		if (n < limit) {
			$("#form_youtube_url > p").eq(n-1).after(html);
		}
		if (n + 1 == limit) {
			$('#add_form_youtube_url').hide();
		}
	});

	$("a.a_attach_close").livequery(function(){
		$(this).click(function(){
			$('#topic_forms .topic_form').hide();
			var i = 0;
			$('#form_image_file > p:has(input)').each(
				function () {
					if (i > 0) $(this).remove();
					else $(this).children('input').val('');
					i++;
				}
			);
			$('#form_image_file > p:hide').show();
			var i = 0;
			$('#form_youtube_url > p:has(input)').each(
				function () {
					if (i > 0) $(this).remove();
					else $(this).children('input').val('');
					i++;
				}
			);
			$("p.err-text").remove();
			$('#add_form_youtube_url').show();
			$('#add_form_image_file').show();
			$('#topic_web_url').val("");
			$('#form_web_url_result').empty();
			$("#topic_attach").show();
		});
	});

});

function delComment(thisObj){
	$(function() { 
		var obj = $(thisObj);
		var tmp = obj.attr("id").split("_");
		var tid = tmp[1];
		var trid = tmp[2];
		var ecd = tmp[3];
		var list_id = '#topicReslist_'+ tid;
		$.ajax({
			type: "POST",
			dataType: "json",
			scriptCharset: "UTF-8",
			data: {
				"submit_type": 'DELETE',
				"mode": (document.URL.match("/my_top|my_topics/")) ? 'mypage' : 'enterprise',
			    "e": ecd,
			    "e": ecd,
				"t": tid,
				"tr": trid
			},
			cache: false,
			url: "/ajax_get_topic_comment.php",
			success: function (data) {
				$(list_id).empty().append(data.html);
			}
		});
	});
}

function aplComment(thisObj){
	$(function() { 
		var obj = $(thisObj);
		var tmp = obj.attr("id").split("_");
		var tid = tmp[1];
		var trid = tmp[2];
		var ecd = tmp[3];
		var list_id = '#topicReslist_'+ tid;
		$.ajax({
			type: "POST",
			dataType: "json",
			scriptCharset: "UTF-8",
			data: {
				"submit_type": 'APPROVAL',
				"mode": (document.URL.match("/my_top|my_topics/")) ? 'mypage' : 'enterprise',
			    "e": ecd,
				"t": tid,
				"tr": trid,
				"ap": 1
			},
			cache: false,
			url: "/ajax_get_topic_comment.php",
			success: function (data) {
				$(list_id).empty().append(data.html);
			}
		});
	});
}

function daplComment(thisObj){
	$(function() { 
		var obj = $(thisObj);
		var tmp = obj.attr("id").split("_");
		var tid = tmp[1];
		var trid = tmp[2];
		var ecd = tmp[3];
		var list_id = '#topicReslist_'+ tid;
		$.ajax({
			type: "POST",
			dataType: "json",
			scriptCharset: "UTF-8",
			data: {
				"submit_type": 'APPROVAL',
				"mode": (document.URL.match("/my_top|my_topics/")) ? 'mypage' : 'enterprise',
			    "e": ecd,
				"t": tid,
				"tr": trid,
				"ap": 2
			},
			cache: false,
			url: "/ajax_get_topic_comment.php",
			success: function (data) {
				$(list_id).empty().append(data.html);
			}
		});
	});
}

function readComment(list_id){
	$(function() {
		$(list_id).find('span.spn_read_check').each(
			function(){
				var name = $(this).eq(0).attr("id").split("_");
				$.ajax({	
					type: "POST",
					dataType: "json",
					scriptCharset: "UTF-8",
					data: {
						"submit_type": 'READ',
					    "e": name[3],
						"t": name[1],
						"tr": name[2]
					},
					cache: false,
					url: "/ajax_get_topic_comment.php",
					success: function (data) {
					}
				});
			}
		);
	});
}

function updateTopics(ecd, page){
	$(function() {
		if (page > 0) var pData = {"e": ecd, "p": page};
		else var pData = {"e": ecd};
		$("#ajTopicMore").remove();
		$("#more_topic_loading,#more_topic_back").show();
		$.ajax({
			type: "POST",
			dataType: "json",
			scriptCharset: "UTF-8",
			data: pData,
			cache: false,
			url: "/ajax_get_topics.php?e=" + ecd,
			success: function (data) {
				$("#more_topic_loading").parent("p.p_pagenum").remove();
				if (page > 1) $("#topic_list").append(data.html);
				else $("#topic_list").empty().append(data.html);
			}
		});
	});
}

function clearTopicForm(){
	$(function() {
		$("form[name='topic']").find("input[type='text'],input[type='file'],textarea").val("");
		$("p.err-text").remove();
		var i = 0;
		$('#form_image_file > p:has(input)').each(
			function () {
				if (i > 0) $(this).remove();
				i++;
			}
		);
		$('#form_image_file > p:hide').show();
		var i = 0;
		$('#form_youtube_url > p:has(input)').each(
			function () {
				if (i > 0) $(this).remove();
				i++;
			}
		);
		$('#form_youtube_url > p:hide').show();
		$('#topic_web_url').val("");
		$('#form_web_url_result').empty().hide();
		$('#topic_forms .topic_form').hide();
		$("#topic_attach").show();
	});
}

function getCommentList(){
	$(function() { 
		$("div.div_topic_commentlist").each(
			function(){
				var name = $(this).eq(0).attr("id").split("_");
				var list_id = '#topicReslist_'+name[1];
				$.ajax({	
					type: "GET",
					dataType: "json",
					scriptCharset: "UTF-8",
					data: {
					    "e": name[2],
						"t": name[1]
					},
					cache: false,
					url: "/ajax_get_topic_comment.php",
					success: function (data) {
						$(list_id).empty().append(data.html);
						readComment(list_id);
					}
				});
			}
		);
	});
}

function getUrlPhoto(){
	$(function(){
		var url = $("#topic_web_url").val();
		$("p.err-text").remove();
		if (url != "" && /^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(url)) {
			$("#form_web_url").hide();
			$("#form_web_url_result").empty().show();
			$("#form_ajax_loading").clone(true).appendTo("#form_web_url_result").show();
			$.getJSON("/ajax_get_url_photo.php", { "url": url}, function(json){
				if (json.img_num > 0) {
					$("div.topic_form").hide();
					$("#form_web_url_result").empty().append(json.html).show();
					$("#title").val(json.title);
					$("#content").val(json.contents);
					// サイト画像選択
					$('#entry_image_url img').livequery(function(){
						$(this).hover(
							function(){
								$(this).removeClass('image_base');
								$(this).addClass('image_hover');
							},
							function(){
								$(this).removeClass('image_hover');
								$(this).addClass('image_base');
							}
						);
					});
					// サイト画像クリック
					$('#entry_image_url img').livequery("click", function(){
						$('#preview_image').attr('src', ($(this).attr('src')));
						$('#entry_image_url .post').val($(this).attr('src'));
					});
				} else {
					$("#form_web_url_result").empty().hide();
					$("#form_web_url").show();
					$("#form_web_url input:button").after('<p class="err-text">画像を取得できませんでした。</p>');
				}
			});
		} else {
			$("#form_web_url input:button").after('<p class="err-text">URLを正しい形式で入力してください。</p>');
		}
	});

}