$(document).ready(function() {
	
	//Css Sprites 2 Top Navigation.
	$("#top-nav").children("li").each(function() {
		var current = "nav current-" + ($(this).attr("class"));
		var parentClass = $("#top-nav").attr("class");
		
		attachNavEvents("#top-nav", $(this).attr("class"));
		
		if (parentClass != current) {
			$(this).children("a").css({backgroundImage:"none"});
		}
	});
	
	//SubAccordion
	$(".side-nav > a").click(function() {
		var href = $(this).attr("href");
		$(this).attr("href", "javascript: getSexyBack('" + href + "');");
		$(this).siblings().slideToggle("normal");
		var toggle = $(this).children("img").attr("src");
		if(toggle.indexOf("on") != -1) {
			toggle = "/rs/images/lever-off.png";
		} else {
			toggle = "/rs/images/lever-on.png";
		}
		$(this).children("img").attr("src", toggle);
	});
	
	//Zebra Stripes
	$("table.normal tbody tr:even").addClass("even")
		.mouseover(function(){$(this).addClass("hover");})
		.mouseout(function(){$(this).removeClass("hover");});
	$("table.normal tbody tr:odd").addClass("odd")
		.mouseover(function(){$(this).addClass("hover");})
		.mouseout(function(){$(this).removeClass("hover");});
	
	//Super-subAcoordion
	$(".sub-side > li > ul").css({display:"none"});	
	$("ul:has(#category).sub-side > li > a").click(function() {
		$(this).attr("href", "javascript: nothing();");
		$(this).siblings().slideToggle("fast");
	});
	
	//Search input form.
	$("#search_query").focus(function() {
		if($(this).val() == "pencarian...") {
			$(this).val("");
		}
	});
	
	//Give a nifty bottom corner for sidebars
	$(".sub-side").after('<div class="sidebar-bottom"></div>');
});

function getSexyBack(id) {
	$(id).attr("href", id);
}

function nothing() {
	$("ul:has(#category).sub-side > li").children("a").each(function() {
		$(this).attr("href", "#");
	});
}

function attachNavEvents(parent, myClass) {
	$(parent + " ." + myClass).mouseover(function() {
		$(this).before('<div class="nav-' + myClass + '"></div>');
		$("div.nav-" + myClass).css({display:"none"}).fadeIn(500);
	}).mouseout(function() {
		$("div.nav-" + myClass).fadeOut(500, function() {
			$(this).remove();
		});
	});
}

function changeCategory() {
	$("form[name=new_product]").submit();
}