$(document).ready(function() {
	$(".jp-playlist-player").show();
	if(window.location.hash.substr(1)!= ""){
		siteID = window.location.hash.substr(1);
	}
	if(siteID=="") siteID = "home";
	var pa = '';
	if(window.location.href.indexOf('?')!=-1){
			pa = '&'+window.location.href.substr(window.location.href.indexOf('?')+1);
		}
	parse_links(siteID+'.html', pa);
	function get_content(p){
		$.get("app.php?cmd=content&siteID="+siteID+p, function(data) {
			$("#content").html(data);
			switch_topimg();
			//pageTracker._trackPageview("/"+siteID+".html"+p+"" );
			$(".intern").click(function(e) {
				e.preventDefault();
				parse_links($(this).attr("href"));
			});
			$(".npage").click(function(e) {
				e.preventDefault();
				parse_links($(this).attr("href"),'&start='+$(this).attr("id").substr(2));
			});
			$(".gal").click(function(e) {
				e.preventDefault();
				parse_links($(this).attr("href"),'&id='+$(this).attr("id"));
			});
			$(".gal_img").click(function(e) {
				e.preventDefault();
				$('<div id="trans"></div>').appendTo("body");
				$('#trans').height($('html')[0].scrollHeight);
				img = $(this).attr("href");
				thumbID = $(this+' .img_gal').index() + 1;
				$('<div id="gallery"><p style="text-align:right"><a name="close">close</a></p><p style="text-align:center" id="bimg"></p><p id="iswitch"><a name="left">&lt;&lt;</a> <a name="right">&gt;&gt;</a></p></div>').appendTo("body");
				$("#gallery").css("top", $('html')[0].scrollTop + 20);
				$("#bimg").html('<img src="'+img+'">');
				$("#gallery a").click(function() {
					switch($(this).attr("name")){
						case 'left':
							thumbID--;
							if(thumbID==-1) thumbID = $("#content .img_gal").length - 1;
							switch_image();	
						break;
						case 'right':
							thumbID++;
							if(thumbID==$("#content .img_gal").length) thumbID = 0;
							switch_image();
						break;
						case 'close':
							$("#trans").remove();
							$("#gallery").remove();
						break;
					}
				});
				
			});
			$(".big_img").click(function(e) {
				$('<div id="trans"></div>').appendTo("body");
				$('#trans').height($('html')[0].scrollHeight);
				img = $(this).attr("src").replace(".jpg", "_big.jpg");
				$('<div id="gallery"><p style="text-align:right"><a name="close">close</a></p><p style="text-align:center" id="bimg"></p></div>').appendTo("body");
				$("#gallery").css("top", $('html')[0].scrollTop + 20);
				$("#bimg").html('<img src="'+img+'">');
				$("#gallery a").click(function() {
					switch($(this).attr("name")){
						case 'close':
							$("#trans").remove();
							$("#gallery").remove();
						break;
					}
				});
				
			});
		});
	}

	$("#menu a, #home").click(function(e) {
		e.preventDefault();
		parse_links($(this).attr("href"));
	});

	$(".clr").click(function(e) {
		$(".num").css("color", "#d2bd9e");
		$("#menu .num:eq("+$(this).index()+")").css("color", "#7f1816");
		$("#hand").css("top", 333 + $(this).index() * 25);
	});

	function switch_image(){
		var img = $("#content .img_gal:eq("+thumbID+")").attr("src").replace("/th", "");
		$("#bimg").html('<img src="'+img+'">');
	}

	function switch_topimg(){
		var z = 1;
		if(siteID!="news") z = Math.ceil(Math.random() * 3);
		$("#top").attr("src", "tmpl/img/top_"+z+".jpg");
	}

	function parse_links(site,p){
		var para = '';
		if(typeof p != 'undefined'){
			para = p;
		}
		var cut = 5;

		if(site.indexOf('?')!=-1){
			cut = site.length - site.indexOf('?') + 5;
			para = '&'+site.substr(site.indexOf('?')+1,site.length);
		}
		siteID = site.substr(0, site.length - cut);
	
		window.location.hash = siteID;
		get_content(para);
	}
});
