$.fn.showPanel = function(panel,dir) {
	return $(this).each(function() {

		if (dir == "right")
		{
			$(this).css('margin-left',-900);
			$(panel).prependTo(this).addClass("show");
			$(this).animate({
				'margin-left':0
			},500,function() {
				$('.panel',this).filter(":visible").not(panel).removeClass("show");
			});
		}
		else if (dir == "left")
		{
			$(panel).appendTo(this).addClass("show");
			$(this).animate({
				'margin-left':-900
			},500,function() {
				$('.panel',this).filter(":visible").not(panel).removeClass("show");
				$(this).css('margin-left',0);
			});
		}
	});
};

var akt_panel = null;
var panels = new Array();

naviSet = function(panel) {
	$("#navi_button_"+panel).each(function() {
		akt_panel = panel;
		_gaq.push(['_trackEvent', 'panel', 'view', panel]);
		$("#navi img[src$=navi_on.png]").attr("src","/images/new/navi_off.png");
		$("img",this).attr("src","/images/new/navi_on.png");
		var prev = null;
		var next = null;
		for (x in panels)
		{
			x = x*1;
			if (panels[x] == panel)
			{
				prev = panels[x-1] ? panels[x-1] : null;
				next = panels[x+1] ? panels[x+1] : null;
			}
		}
		$("#navi_prev").attr({'href': "#"+(prev ? "panel:"+prev : "")});
		$("#navi_prev img").attr({'src': "/images/new/navi_left"+(prev ? "" : "_off")+".png"});
		$("#navi_next").attr({'href': "#"+(next ? "panel:"+next : "")});
		$("#navi_next img").attr({'src': "/images/new/navi_right"+(next ? "" : "_off")+".png"});
	});
};

$(function() {

	$(".panel").each(function() {
		panels.push($(this).attr("id").split("_")[1]);
	});

	$("a[href^=#panel]").add("#navi_next").add("#navi_prev").click(function() {
		$(this).blur();
		var panel = $(this).attr("href").split(":")[1];
		if (panel && panel != akt_panel)
		{
			location.hash = $(this).attr("href");
			var megvan = false;
			for (x in panels)
			{
				if (panels[x] == akt_panel) megvan = true;
				if (panels[x] == panel) dir = megvan ? "left" : "right";
			}

			target = "#panel_"+panel;
			$("#panel_c").showPanel(target,dir);
			naviSet(panel);
		}
		return false;
	});

	var lochash = location.hash ? location.hash.split("#")[1] : null;
	var panel = lochash ? lochash.split(":")[1] : panels[0];
	panel = $("#panel_"+panel).size() > 0 ? panel : panels[0];
	$(".panel").each(function() {
		if ($(this).is("#panel_"+panel))
		{
			$(this).addClass("show");
			naviSet(panel);
		}
		else
		{
			$(this).removeClass("show");
		}
	});
	
});
