var Rating = {
	rate: function(url, type) {
		var self = this;
		if (!self.done) {
			$.post(url, {}, function(data) {
				$("#rating ."+type+" > span").text(data[type]).Highlight("slow", "#D9FF00");
				self.done = true;
			}, 'json');
		} else {
			alert("連続して評価することはできません");
		}
	}
};

var CommentList = {
	moving: false,
	highLightColor: "#D9FF00",
  maxSize: 10,
  currentPage: 1,

	init: function() {
		CommentList.setEventDelete();
		CommentList.resetBgColor();
		CommentList.moving = false;
	},

	updateTotal: function(total) {
		$(".user-comment .comment h4 span").text(total).Highlight("slow", self.highLightColor);
		$("#last_position").val(total);
	},

	resetBgColor: function() {
		bgColor = "gray";
		flag = true;
		$("#comment-content").children("div").each(function() {
			if (flag) {
				$(this).addClass(bgColor);
			} else {
				$(this).removeClass(bgColor);
			}
			flag = !flag;
		});
	},

	removeComment: function(objId) {
		$(objId).parent().parent().fadeOut("slow", function() {
			$(this).remove();
			total = $(".user-comment .comment h4 span").text();
			total--;
			CommentList.updateTotal(total);
			CommentList.resetBgColor();
			CommentList.moving = false;
		});
	},

	setEventDelete: function() {
		$("#comment-content").find("a.user-favorite-delete").click(function() {
			if (CommentList.moving == true) return false;
			CommentList.moving = true;

			window['del-comment-id'] = '#' + $(this).attr('id');
			$.get($(this).attr("aurl"), null, function(data) {
				if (data != "OK") {
					alert("削除できませんでした");
				} else {
					CommentList.removeComment(window['del-comment-id']);
					window['del-comment-id'] = '';
				}
			});
			return false;
		}).end();
	}
};

$(function() {

	$("#submit-report").click(function() {
		var fm_report_set = $("#fm-channel-report");
		if(fm_report_set.length){
			var self = this;
			if (!self.done) {
				var seccode = fm_report_set.find("input[name='seccode']").val();
				var channel_id = fm_report_set.find("input[name='channel_id']").val();
				var reason = fm_report_set.find("select[name='reason']").val();
				$('#report .error-area').hide();
				if (!seccode){
					$('#report #reportErrorSeccode').show();
				} else if(!channel_id || !reason){
					$('#report #reportErrorReason').show();
				} else {
					$.post(fm_report_set[0].action, fm_report_set.serialize(), function(data) {
		                if(data['result']){
							//success
							self.done = true;
							$('#report .error-area').hide();
							$('#report .fin').show();
						}
						else{
							if (data['error'] == 'seccode') {
								$('#report #reportErrorSeccode').show();
							} else {
								$('#report #reportErrorReason').show();
							}
						}
					}, 'json');
				}
			} else {
				alert("連続して通報することはできません");
			}
		}
		return false;
	});

	$("div.report a").click(function() {
		if ($('#report').css("display") == "none") {
			$("#fm-channel-report").find("input[name='seccode']").val('');
			$('#report .error-area').hide();
			$("#report #reportSeccode").attr('src', '/seccode.php?'+Math.random(1));
			video_index = window['current_playing_video_index'];
			video_id = window['current_playing_video_id'];
			var objVideoSite = $('#videolist_idx_' + video_index + ' #video_original_' + video_index);
			if (objVideoSite.length) {
				$('#report #current_video_original').html('<strong>再生している動画のオリジナルサイト</strong>：<a href="'+objVideoSite.attr("href")+'" target="_blank">'+objVideoSite.text()+'</a>');
			} else {
				$('#report #current_video_original').html('');
			}
			var objVideoTitle = $('#videolist_idx_' + video_index + ' div.text-area a.ttl');
			if (objVideoTitle.length) {
				var video_title = $.trim(objVideoTitle.text())
				$("#report #fm-channel-report input[name='video_id']").val(video_id);
				$("#report #fm-channel-report input[name='video_title']").val(video_title);
			}
		}
		$('#report').toggle('fast');
		return false;
	});

	$("a.button-good").click(function() {
		Rating.rate(this.href, "good");
		return false;
	});

	$("a.button-bad").click(function() {
		Rating.rate(this.href, "bad");
		return false;
	});

	$("#form-channel-comment").submit(function() {
		author = $.trim($("input[name='comment[author]']").val());
		comment = $.trim($("textarea[name='comment[content]']").val());
		if(!$("input[name='agree']").attr("checked"))
		{
			$(".error-area").show();
			return false;
		}

		error_str = "";
		if (author.length > 30) error_str = error_str + "お名前は30文字以内で入力してください。" + "\n";
		if (comment == "") error_str = error_str + "コメントを入力してください。" + "\n";
		else if (comment.length > 400) error_str = error_str + "コメントは400字以内で入力してください。" + "\n";
		if ("" != error_str) {
			alert(error_str);
			return false;
		}

		if (CommentList.moving == true) return false;
		CommentList.moving = true;

		params = $(this).serialize();
		$("#form-channel-comment input:submit").attr("disabled", true).val("コメントを送信中・・・");

		$.post(this.action, $(this).serialize(), function(data) {
			$("div.comment-text-area.empty").fadeOut("slow", function () { $(this).remove(); });
      
			$("#comment-text-area-wrap").prepend(data);
      
			total = $("#comment-content div.comment-update #comment_update_total").text();
			$("#comment_update_total").remove();
			//l = $("div.comment-text-area:not(.empty)").length;
			$("#last_position").val(total);
			$(".user-comment .comment h4 span").text(total).Highlight("slow", "#D9FF00");
			$("div.comment-update div.comment-text-area").Highlight("slow", "#D9FF00", function() {
				//$("div.comment-update").removeClass("comment-update");
				$realDiv = $("div.comment-update").children("div").clone();
				$("div.comment-update").replaceWith($realDiv);
				//CommentList.setEventDelete();
			});

      var commentSize = $(".comment-text-area").size();
      if (commentSize > CommentList.maxSize) {
        $("#comment-text-area-wrap div.comment-text-area:last-child").fadeOut("slow", function() {
          $(this).remove();
        });
      } else {
      }
			CommentList.init();
			$("#form-channel-comment input:submit").attr("disabled", false).val("コメントを投稿");
			$("textarea[name='comment[content]']").val("");
			$(".error-area").hide();
		});

		return false;
	});

	CommentList.setEventDelete();
	//CommentList.init();

	var channelInfoMore = function(obj) {
		$('#channel-info div.contents-more').toggle();
		if (obj.hasClass("btn-more")) {
			obj.removeClass("btn-more");
			obj.addClass("close");
		} else{
			obj.removeClass("close");
			obj.addClass("btn-more");
		}
		return false;
	}
	$('#channel-info div.channel-title a.btn-more').click(function() {
		return channelInfoMore($(this));
	});
	var videoMore = function(obj) {
		alert('ccccc')
		obj.parent().parent().find('div.movie-more').toggle();
		if (obj.hasClass("btn-more")) {
			obj.removeClass("btn-more");
			obj.addClass("close");
		} else{
			obj.removeClass("close");
			obj.addClass("btn-more");
		}
		return false;
	}
	/*$("#tab-content-search")
		.find("div.video-results-area div.text-area a.btn-more").click(function() {
		return videoMore($(this));
	});*/

	controllerTabResize();

});
