/* GLOBALS */
var IS_SNIPPET_DISPLAYED = 0;
var IS_SIDEBAR_DISPLAYED = 1;
var IS_SIDEBAR_DISPLAY_LOCKED = 0;
var IS_SIDEBOX_DISPLAYED = 1;
var IS_SIDEBOX_DISPLAY_LOCKED = 0;
var IS_SNIPPET_ENABLED = 1;
var TIMER_SNIPPET_FADEOUT = 0;
var TIMER_SIDEBAR_FADEOUT = 0;
var TIMER_SIDEBOX_FADEOUT = 0;
var SIDE_FADEOUT_DELAY = 4000;
var SIDEBAR_OPACITY = 0.1;

$("document").ready(function() {
		
		TIMER_SIDEBAR_FADEOUT = setTimeout(function(){
			$("#sidebar").fadeTo("slow", SIDEBAR_OPACITY);
			IS_SIDEBAR_DISPLAYED = 0;
		}, SIDE_FADEOUT_DELAY);

		TIMER_SIDEBOX_FADEOUT = setTimeout(function(){
			$("#sideboxSection").fadeTo("slow", SIDEBAR_OPACITY);
			IS_SIDEBOX_DISPLAYED = 0;
		}, SIDE_FADEOUT_DELAY);
		
		
		$("#wrapper").mousemove(function(e){
		
			if(IS_SIDEBAR_DISPLAYED == 0)
			{
				$("#sidebar").fadeTo("fast", 1);
				$("#sideboxSection").fadeTo("fast", 1);

				IS_SIDEBAR_DISPLAYED = 1;
				IS_SIDEBOX_DISPLAYED = 1;
			}

			clearTimeout(TIMER_SIDEBAR_FADEOUT);
			
			if(IS_SIDEBAR_DISPLAY_LOCKED == 0)
			{
				TIMER_SIDEBAR_FADEOUT = setTimeout(function(){
					$("#sidebar").fadeTo("slow", SIDEBAR_OPACITY);
					IS_SIDEBAR_DISPLAYED = 0;
				}, SIDE_FADEOUT_DELAY);
			}
			
			clearTimeout(TIMER_SIDEBOX_FADEOUT);
			if(IS_SIDEBOX_DISPLAY_LOCKED == 0)
			{
				TIMER_SIDEBOX_FADEOUT = setTimeout(function(){
					$("#sideboxSection").fadeTo("slow", SIDEBAR_OPACITY);
					IS_SIDEBOX_DISPLAYED = 0;
				}, SIDE_FADEOUT_DELAY);
			}
		
		});
		
		$("#sidebar").hover(
			function(){
				clearTimeout(TIMER_SIDEBAR_FADEOUT);
				$(this).fadeTo("fast", "1");
				IS_SIDEBAR_DISPLAY_LOCKED = 1;
				//IS_SIDEBOX_DISPLAY_LOCKED = 1;
			},
			
			function(){
				clearTimeout(TIMER_SIDEBAR_FADEOUT);
				
				IS_SIDEBAR_DISPLAY_LOCKED = 0;
				//IS_SIDEBOX_DISPLAY_LOCKED = 0;
				
				TIMER_SIDEBAR_FADEOUT = setTimeout(function(){
					$("#sidebar").fadeTo("slow", SIDEBAR_OPACITY);
					IS_SIDEBAR_DISPLAYED = 0;
				}, SIDE_FADEOUT_DELAY);
			}
		);
		
		$("#sideboxSection").hover(
			function(){
				clearTimeout(TIMER_SIDEBOX_FADEOUT);
				$(this).fadeTo("fast", "1");
				//IS_SIDEBAR_DISPLAY_LOCKED = 1;
				IS_SIDEBOX_DISPLAY_LOCKED = 1;
			},
			
			function(){
				clearTimeout(TIMER_SIDEBOX_FADEOUT);
				
				//IS_SIDEBAR_DISPLAY_LOCKED = 0;
				IS_SIDEBOX_DISPLAY_LOCKED = 0;
				
				TIMER_SIDEBOX_FADEOUT = setTimeout(function(){
					$("#sideboxSection").fadeTo("slow", SIDEBAR_OPACITY);
					IS_SIDEBOX_DISPLAYED = 0;
				}, SIDE_FADEOUT_DELAY);
			}
		);
		
		/* FORM EFFECTS */
		/*$("table.form tr td input").focus(function(){
			$(this).parent().parent().find("th").animate({"paddingLeft": "10px"}, "fast");
		});
		$("table.form tr td input").blur(function(){
			$(this).parent().parent().find("th").animate({"paddingLeft": "0"}, "fast");
		});
		
		$("table.form tr").hover(
			function(){
				$(this).find("th").animate({"paddingLeft": "10px"}, "fast");
			},
			
			function(){
				$(this).find("th").animate({"paddingLeft": "0"}, "fast");
			}
		);*/
		/* END FORM EFFECTS */

		/* INFORMATIVE FIELD */
		$('.informativeField').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
		$('.informativeField-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'));
			}
		})
		
		$('.informativeField, .informativeField-alt').bind('focus', function(){
			if ($(this).attr('value') == $(this).attr('title')) {
				$(this).removeClass('inactive');
				$(this).attr('value', '');
			}
		});
		
		$('.informativeField, .informativeField-alt').bind('blur', function(){
			if ($(this).attr('value') == "") {
				$(this).addClass('inactive');
				$(this).attr('value', $(this).attr('title'));
			}
		});
		
		/* END INFORMATIVE FIELD */
		
		/* AUTO EXPAND */
		$("textarea.autoExpand").bind("keypress", function(){
		
			var charLength = $(this).attr("value").length;
			var inputLength = Number($(this).attr("cols"));
			if( charLength > inputLength)
			{
				$(this).attr("rows", Math.ceil(charLength/inputLength + 1))
			}
		});
		/* END AUTO EXPAND */
		
		/* RANKINGS */
		$("#headline").hover(
			function(){
				if (IS_SNIPPET_ENABLED)
				{
					if(TIMER_SNIPPET_FADEOUT)
					{
						clearTimeout(TIMER_SNIPPET_FADEOUT);
					}
	
					TIMER_SNIPPET_FADEIN = setTimeout(function(){
						$("#snippets").parent().fadeIn("fast");								
					}, 100);
				}
			},
			
			function(){
				TIMER_SNIPPET_FADEOUT = setTimeout(function(){
					$("#snippets").parent().fadeOut("slow");
				}, 400);
			}
		);
		
		$('#snippetsWrapper').hover(
			function(){
				clearTimeout(TIMER_SNIPPET_FADEOUT);
			},
			
			function(){
				TIMER_SNIPPET_FADEOUT = setTimeout(function(){
					$("#snippets").parent().fadeOut("slow");
				}, 400);
			}
		);
		
		$("#headline").find("li").each(function(){
				var snippets =  $("#snippets");
				var regbox =  $("#sidebar");
				var rank_name = $(this).attr("class");
				var spacer = $(this).find("a span.spacer");
				var spacer_width = parseFloat($(spacer).css("width").split(".")[0]);
				
				$(this).find("a span.title span.author .profileLink").click(function(){
					location.href = document.base_url + "profil/" + $(this).attr("title") + ".html";
					return false;
				});
				
				$(this).find("a span.avatar").click(function(){
					location.href = document.base_url + "profil/" + $(this).attr("title") + ".html";
					return false;
				});
				
				$(this).hover(
					function(){
						$(this).find("a span.spacer").animate({width: spacer_width + 10 + "px"}, "fast");
						$(snippets).html("<span class=\"" + rank_name + "\">" + $(this).find("a span.snippet").html() + "</span>");
					},
					
					function(){
						$(this).find("a span.spacer").animate({width: spacer_width + "px"}, "fast");
					}			
				);		
		});
		/* END RANKINGS */
		
		/* HOVERMENU */
		$(".hoverMenu").each(function(){
			$(this).hover(
				function(){
					$(this).find(".hoverMenuItems").show();
				}, 
				
				function(){
					$(this).find(".hoverMenuItems").hide();
				}
			);
			$(this).click(function(){
				$(this).find(".hoverMenuItems").hide();
			});

		});
		
		/* END HOVERMENU */
		
		/* POPMENU */
		$(".popMenu").each(function(){
			$(this).hover(
				function(){
					$(this).find(".popMenuItems").show();
				}, 
				
				function(){
					$(this).find(".popMenuItems").hide();
				}
			);
		});
		
		/* END HOVERMENU */
		
});
/* END RACING DISPLAY */

/* 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 */

