var POL_STATUS_IS_OPEN = 0;
var RATING_MENU_ACTIVE;
var RATING_MENU_TIMER;

$("document").ready(function() {
	$("#manualRating").remove();

	$(".hovermenu").hover(
		function(){
			$(this).find("ul").show();
			$(this).addClass("hovermenu-active");
		},
		
		function(){
			$(this).find("ul").hide();
			$(this).removeClass("hovermenu-active");
		}
	);
	
	$(".popupMenu").hover(
		function(){
			$(this).children(".popupMenuBody").css("display", "block");
			$(this).addClass("popupMenu-active");
		},
		
		function(){
			$(this).children(".popupMenuBody").css("display", "none");
			$(this).removeClass("popupMenu-active");
		}
	);
	
	/* INFORMATIVE FIELD */
	$('.informative-field').each(function(){
	
		// Mengisi informasi penjelasan
		if ($(this).attr('value') == "" || $(this).attr('value') == $(this).attr('title') ) {
			$(this).addClass('inactive');
			$(this).attr('value', $(this).attr('title'));
		}

		// Mengisi mencegah submit dengan default value
		var informfieldField = $(this);
		$(this).parents("form").submit(function(){
			if($(informfieldField).attr("value") == $(informfieldField).attr("title")) {
				return false;
			}
		});
	})
	
	//Untuk opsional
	$('.informative-field-alt').each(function(){
	
		// Mengisi informasi penjelasan
		if ($(this).attr('value') == "" || $(this).attr('value') == $(this).attr('title') ) {
			$(this).addClass('inactive');
			$(this).attr('value', $(this).attr('title'));
		}
	})
	
	$('.informative-field, .informative-field-alt').bind('focus', function(){
		if ($(this).attr('value') == $(this).attr('title')) {
			$(this).removeClass('inactive');
			$(this).attr('value', '');
		}
	});
	
	$('.informative-field, .informative-field-alt').bind('blur', function(){
		if ($(this).attr('value') == "") {
			$(this).addClass('inactive');
			$(this).attr('value', $(this).attr('title'));
		}
	});
	
	/* END INFORMATIVE FIELD */
	
	/* DEBATE */
	$("#debate-form div.buttons div, #sidebar-debate-form div.buttons div").click(function(){
		$("#debate-form .none, #sidebar-debate-form .none").show();
		$("#debate-form .none textarea, #sidebar-debate-form .none textarea").focus();
		$("#debate-form .none label, #sidebar-debate-form .none label").html($(this).html() + ":");
		$("#debate-form div.buttons, #sidebar-debate-form div.buttons").hide();
		
		var responseState = 0;
		switch($(this).html()) {
			case "Ya"        : responseState = "pro"; break;
         case "Tidak"    : responseState = "contra"; break;
         case "Netral"    : responseState = "neutral"; break;
  			default			: responseState = "0"; break;
		};
		$("#response-state, #sidebar-response-state").attr('value', responseState);

		return false;
	});

	$(".changestate").click(function(){
		$("#sidebar-debate-form .none").hide();
		$("#sidebar-debate-form div.buttons").show();
		$("#response-state").attr('value', "0");

		return false;
	});
	/* END DEBATE */
	
	/* DEBATE COMMENT */
	$(".debate-response-comment-toggler").one("click", function(){
		var responseDiv = $(this).parents("div");
		var responseId = $(responseDiv).attr("id").substr(9);
		var responseCommentsDiv = $(this).parent().siblings(".response-comments");
		
		if ($(responseCommentsDiv).children().size() == 0)
		{
			$(responseCommentsDiv).html("<dl class=\"response-comments-list\"><dd class=\"wait\"></dd></dl>");
			$.get(document.base_url+'ajax/is_logged/', function(data){
			  if(data){
					$(responseCommentsDiv).find("dl.response-comments-list").append("<dd class=\"debate-ajax none\"></dd><dd class=\"debate-comment-submitform\"><form onsubmit=\"setDebateResponseComments(this); return false;\"><fieldset><textarea rows=\"2\" name=\"debate_respond_comment_comment\"></textarea><br/><input class=\"response-comments-submit\" name=\"submit\" type=\"submit\" value=\"Komentar\" /><input type=\"hidden\" name=\"debate_respond_id\" value=\""+responseId+"\" /><input type=\"hidden\" name=\"last_debate_respond_comment_id\" value=\"\" class=\"debate-respond-comment-id\" /></fieldset></form></dd>");
				
				}else{
					$(responseCommentsDiv).find("dl.response-comments-list").append("<dd class=\"debate-ajax none\"></dd><dd>Mari <a href=\""+document.base_url+"registrasi\" class=\"recommend\"><b>register</b></a> atau <a href=\""+document.base_url+"login\" class=\"recommend\"><b>login</b></a> untuk beraktivitas di sini!</dd>");

				}
			});
			getDebateResponseComments(responseId, responseCommentsDiv);
		}
		$(this).parent().append($(this).html());
		$(this).empty().remove();
		return false;
	});
	
	/* END DEBATE COMMENT */
	
	/* PROFILE */
		$(".nextCommentsButton").click(function(){
			$(".nextCommentsButton").hide();
			$(".waitCommentLoad").show();
			getProfileComments(document.userName, document.lastCommentId, "#comments");
			return false;
		});
		$(".nextMentionsButton").click(function(){
			$(".nextMentionsButton").hide();
			$(".waitCommentLoad").show();
			getProfileMentions(document.userName, document.lastCommentId, "#comments");
			return false;
		});
	/* END PROFILE */
	/* STATUSBOX */
	$(".status-body").click(function(){
		if (POL_STATUS_IS_OPEN == 0)
		{
			$("#screen-blocker").css("display", "block");
			$(".status-info").slideDown('fast', function(){
				$(".status-body .excerpt").css('display', 'none');
				$(".status-body .text").css('display', 'block');
			});
			POL_STATUS_IS_OPEN = 1;
			$('#plurk_warning').empty().hide();
		}
		else
		{
			$("#screen-blocker").css("display", "none");
			$(".status-info").slideUp('fast', function(){
				$(".status-body .excerpt").css('display', 'block');
				$(".status-body .text").css('display', 'none');
			});
			POL_STATUS_IS_OPEN = 0;
			plurk();
		}
		return false;
	});
	
	$("#slide-status-toggle").click(function(){
		if (POL_STATUS_IS_OPEN == 0)
		{
			$(".status-info").slideDown('fast', function(){
				$(".status-body .excerpt").css('display', 'none');
				$(".status-body .text").css('display', 'block');
			});
			POL_STATUS_IS_OPEN = 1;
		}
		else
		{
			$(".status-info").slideUp('fast', function(){
				$(".status-body .excerpt").css('display', 'block');
				$(".status-body .text").css('display', 'none');
			});
			POL_STATUS_IS_OPEN = 0;
		}
		return false;
	});
	
	$("#screen-blocker").click(function(){
		$(".status-info").slideUp('fast', function(){
			$(".status-body .excerpt").css('display', 'block');
			$(".status-body .text").css('display', 'none');
		});
		POL_STATUS_IS_OPEN = 0;
		$(this).css("display", "none");
	});
	
	// Indicator to display how many characters left //
	$("#plurk_status_comment_text").keypress(function(){
		var numChars = ($(this).attr("value")).length;
		
		if (numChars > 82) {
			$(this).css("height", "6em");
		}
		
		if(numChars > 140) {
			$(this).parent().find(".post-info span").html( "Kelebihan " + (numChars - 140) + " huruf");
			$(this).parent().find(".post-info span").addClass("post-info-alert");
			$(this).parent().children("#plurk_save").attr("disabled", "disabled");
		}
		else {
			if ((140 - numChars) == 0)
				$(this).parent().find(".post-info span").html( "Sudah pas 140 karakter");
			else
				$(this).parent().find(".post-info span").html( "Masih muat " + (140 - numChars) + " huruf lagi");

			$(this).parent().children("#plurk_save").removeAttr("disabled");
			$(this).parent().find(".post-info span").removeClass("post-info-alert");
		}
		
	});
	/* END STATUSBOX */
	
	$(".toggler").click(function(){
		var node_id = $(this).attr("href");
		$(node_id).toggle();
		
		if(!$(this).filter(".nohide"))
			$(this).hide();
		return false;
	})
	
	/* RATING BUTTON */
	$(".ratingButton a ").hover(
		function(){
			RATING_MENU_ACTIVE = this;
			RATING_MENU_TIMER = setTimeout(function(){
				$("#rating-options div").appendTo(RATING_MENU_ACTIVE);
				$(RATING_MENU_ACTIVE).addClass("ratingButton-active");
			}
			, 100 )
		},
		function(){
			clearTimeout(RATING_MENU_TIMER);
			$(".ratingButton div").appendTo("#rating-options");
			$(this).removeClass("ratingButton-active");
		}
	);
	
	/* END RATING BUTTON */

	/* PHOTOSTORY */
	$(".photostory").each(function(){
		var ps_list = $(this).find(".list ul");
		var ps_caption = $(this).find(".caption");
		var ps_photo = $(this).find(".photo .photoimg");
		
		// event handler untuk navigasi foto
		$(this).find(".list ul li a").each(function(){
			$(ps_photo).parent().removeClass("wait")						
			$(this).click(function(){
				var change_a = $(this);
				
				//preload foto
				var img = new Image();
				img.src = $(change_a).attr("href");

				if (img.complete) {
					//transisikan foto
					$(ps_photo).fadeOut("slow", function(){
						//ubah foto
						//$(ps_photo).css("margin", Math.abs((380-Math.min(img.height, 375))/2) + "px auto");
						$(ps_photo).attr("src", img.src);
						$(ps_photo).fadeIn("slow");
					});
				}
				else {
					$(ps_photo).parent().addClass("wait")
					var img = new Image();
					//transisikan foto
					$(ps_photo).fadeOut("slow", function(){
						//ubah foto
						$(img).load(function(){
							$(ps_photo).attr("src", this.src);
							$(ps_photo).fadeIn("slow");
							$(ps_photo).parent().removeClass("wait")
						});
						img.src = $(change_a).attr("href");
					});
				}
								
				// ubah navigasi
				$(this).parent().siblings(".active").removeClass("active");
				$(this).parent().addClass("active");
				$(ps_caption).html("<a href=\"" + $(change_a).attr("href").replace("/medium/", "/large/") + "\">" + $(this).attr("title") + "</a>");
				return false;
			});
		});
		
		// event handler untuk previous photo
		$(this).find(".photo .photonav .prev-button").click(function(){
			$(ps_photo).parent().removeClass("wait");
			var change_li = $(ps_list).children("li.active").prev();
			if (!$(change_li).size())
				change_li = $(ps_list).children(":last-child");
			
			var change_a = $(change_li).find("a");
			
			//preload foto
			var img = new Image();
			img.src = $(change_a).attr("href");
			
			if (img.complete) {
				//transisikan foto
				$(ps_photo).fadeOut("slow", function(){
					//ubah foto
					$(ps_photo).attr("src", img.src);
					$(ps_photo).fadeIn("slow");
				});
			}
			else {
				$(ps_photo).parent().addClass("wait")
				
				var img = new Image();
				//transisikan foto
				$(ps_photo).fadeOut("slow", function(){
					//ubah foto
					$(img).load(function(){
						$(ps_photo).attr("src", this.src);
						$(ps_photo).fadeIn("slow");
						$(ps_photo).parent().removeClass("wait")
					});
					img.src = $(change_a).attr("href");
				});
			}
							
			// ubah navigasi
			$(ps_list).children("li.active").removeClass("active");
			$(change_li).addClass("active");
			$(ps_caption).html("<a href=\"" + $(change_a).attr("href").replace("/medium/", "/large/") + "\">" + $(change_a).attr("title") + "</a>");
			return false;
		});

		$(this).find(".photo .photonav .next-button").click(function(){
			$(ps_photo).parent().removeClass("wait");
			var change_li = $(ps_list).children("li.active").next();

			if (!$(change_li).size())
				change_li = $(ps_list).children(":first-child");
			
			var change_a = $(change_li).find("a");
			
			
			//preload foto
			var img = new Image();
			img.src = $(change_a).attr("href");
			
			if (img.complete) {
				//transisikan foto
				$(ps_photo).fadeOut("slow", function(){
					//ubah foto
					//$(ps_photo).css("margin", Math.abs((380-Math.min(img.height, 375))/2) + "px auto");
					$(ps_photo).attr("src", img.src);
					$(ps_photo).fadeIn("slow");
				});
			}
			else {
				$(ps_photo).parent().addClass("wait")
				var img = new Image();
				//transisikan foto
				$(ps_photo).fadeOut("slow", function(){
					//ubah foto
					$(img).load(function(){
						//$(ps_photo).css("margin", Math.abs((380-Math.min(img.height, 375))/2) + "px auto");
						$(ps_photo).attr("src", this.src);
						$(ps_photo).fadeIn("slow");
						$(ps_photo).parent().removeClass("wait")
					});
					img.src = $(change_a).attr("href");
				});
			}

			// ubah navigasi
			$(ps_list).children("li.active").removeClass("active");
			$(change_li).addClass("active");
			$(ps_caption).html("<a href=\"" + $(change_a).attr("href").replace("/medium/", "/large/") + "\">" + $(change_a).attr("title") + "</a>");

			return false;
		});
	});
	/* END PHOTOSTORY */
	
	/* NEWSTABS */
	
	var newsTabs = $(".newstabs");
	for (i = 0; i < newsTabs.length; i++)
	{
		var newsTab = $(newsTabs[i]);
		var tabButtons = $("<div class=\"tabButtons\">Tes");
		var tabPages = $(newsTab).children(".tabPages").children("div");
		
		$(tabButtons).prependTo(newsTab);
		
		for (j = 0; j < tabPages.length; j++)
		{
			tabPage = tabPages[j];
			tabId = $(tabPage).attr("id");
			tabName = $(tabPage).find("h3").text();
			tabButton = $("<a href=\"#\">" + tabName + "</a>");
			
			$(tabPage).find("h3").remove();
			$(tabButton).appendTo(tabButtons);
			$(tabButton).attr("id", "tabBtn-" + tabId);
			
			$(tabButton).click(function(){
				var activeTabId = "#" + $(this).attr("id").substr(7);

				$(this).siblings(".active").removeClass("active");
				$(this).addClass("active");
				$(this).parent().siblings(".tabPages").children().css("display", "none");
				$(this).parent().siblings().children(activeTabId).css("display", "block");
				return false;
			});
			
			if (j > 0){
				$(tabPage).css("display", "none");
			} else {
				$(tabButton).addClass("active");
			};
		}
		
		$("<div class=\"clear\"></div>").appendTo(tabButtons);
	}
	
	$(".newestPost").slideDown("slow", function(){
		$(this).animate({'backgroundColor': '#D4DDDD', 'borderBottomColor': '#D9EBE4'},3000);
	});
	/*
$(".newestPost").animate({'backgroundColor': '#f6e497'}, 2000, "easeOutCubic", function(){
		$(this).animate({'backgroundColor': '#d4dddd'},3000, "easeOutCubic");
	});
*/
	/* END NEWSTABS */
	
	/* WELCOMEBOX */
	$(".sidebar-message .messageBody h2").remove();
	$(".sidebar-message .messageBody .tentang").css("position", "static");
	$(".sidebar-message div.messageSummary").click(function(){
		$(this).slideUp(function(){
			$(this).siblings("div.messageBody").slideDown();
		});
		$.get(document.base_url + "ajax/helpbox_toggle/");
	});
	
	$(".sidebar-message div.messageBody .toggleButton").click(function(){
	
		$(this).parent().slideUp(function(){
			$(this).siblings("div.messageSummary").slideDown();
		});
		$.get(document.base_url + "ajax/helpbox_toggle/");
		return false;
	});
	
	//Hightlight Newsroom
	$("#newsroomHighlight").hover(function(){
		$(".newsroom-menu a").animate({opacity: "0.3"}, "fast", "linear")
			.animate({opacity: "1"}, "fast", "linear", function(){$(this).animate({opacity: "0.3"}, "fast", "linear", function(){$(this).animate({opacity: "1"}, "fast", "linear")})});
		return false;
	});
	/* END WELCOMEBOX */
	
	/* EQUALIZE COLUMN */
	
	
	/* Follow/Unfollow Article Comments */
	/*$("#follow").bind('click', function() {
	  if(this.checked) {
  		$.get(document.base_url + "setprefs/follow/" + this.value);
	  } else {
  		$.get(document.base_url + "setprefs/unfollow/" + this.value);
	  }
	});*/
	
	$("#follow").bind('click', function() {
	  if($(this).attr("class") == "checked") {
  		$.get(document.base_url + "setprefs/unfollow/" + $('#artikel').val());
	  	$(this).html("Pantau");
	  	$(this).attr("title", "Pantau artikel ini lewat email");
	  } else {
  		$.get(document.base_url + "setprefs/follow/" + $('#artikel').val());
	  	$(this).html("Stop Pantau");
	  	$(this).attr("title", "Stop pantau artikel ini lewat email");
	  }
	  $(this).toggleClass("checked");
	  return false;
	});
	
	$(".follow").bind('click', function() {
	  if($(this).is(".checked")) {
  		$.get(document.base_url + "setprefs/unfollow/" + this.id.substr(1));
	  	$(this).removeClass('checked').addClass('unchecked').parents('tr').addClass('unfollowed');
	  	$(this).attr("title", "Pantau artikel ini lewat email");
	  } else {
	    $.get(document.base_url + "setprefs/follow/" + this.id.substr(1));
	  	$(this).removeClass('unchecked').addClass('checked').parents('tr').removeClass('unfollowed');
	  	$(this).attr("title", "Stop pantau artikel ini lewat email");
	  }
	  return false;
	});
	
	/**reply on avatar**/
	$('.avatar-reply').bind('click', function(){
		$('#komentar-text').val() == 'Komentar Anda...' ? $('#komentar-text').val('') : '';
		var value = $('#komentar-text').val() + '@'+$(this).attr('value')+ ': ';
		$('#komentar-text').val(value).focus();
		$("#komentar-text").removeClass('inactive');
	});
	/**end reply on avatar**/
	
});

/* POPUP TRIGGER */
function evokePopup(popup_id) {
	$("#" + popup_id).css("top", window.scrollY);
	$("#" + popup_id).fadeIn("fast");
	$("#" + popup_id + " .closeTrigger").one('click', function(){$("#" + popup_id).fadeOut("fast"); return false});
	return false;
}
/* END POPUP TRIGGER */

