var nextImage = new Image;
var checkPreload, tmphref, checkURL;
var active = 1;
var running = 0;
var activeAnc = "";

function addLoadFunction(addme)
{	
	var oldonload = window.onload;
	window.onload = typeof window.onload != "function" ? addme : function(){ oldonload(); addme(); };
}

function fadeImage(){
	if(nextImage.complete == true){
		window.clearInterval(checkPreload);
		$("#loader").css({right:0, left:"auto"});
		$("#backgroundimage")[0].src = nextImage.src;
		$("#loader").animate({width: "0"}, 1000, "swing",
			function(){
				$("#loader").css({left:0, right:"auto"});
				active = running;
			}
		);
	}
}

function initGallery(){
	nextImage.src = $("#backgroundimage")[0].src;
	$("#loader").css({width:0});
	$("#gallery ul li a")
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0 -160px)"}, {duration:1});
		})
		.mouseout(function(){
			if(this.className!="active")
				$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:800});
		})
		.click(function(){
			if(active != parseInt(this.innerHTML) && this.className != "active"){
				running = parseInt(this.innerHTML);
				nextImage.src = "img/backgrounds/"+running+".jpg";
				$("#loader").stop();
				$("#loader").css({width:0, left:0, right:"auto"});
				$("#loader").animate({width: "100%"}, 2000, "swing",
					function(){
						checkPreload = window.setInterval("fadeImage()", 200);
					}
				);
				hideContent();
				$("#navigation li.active").removeClass("active");
				$("#gallery ul li a").removeClass("active");
				$("#gallery ul li a").stop().animate({backgroundPosition:"(0 0)"}, 800);
				this.className = "active";
				$(this).stop().animate({backgroundPosition:"(0 -160px)"}, 1);
			}
		})
}

function hideContent(){
	$("#page div").stop().animate({opacity:0}, 500, "swing", function(){$("#page").stop().animate({opacity:0}, 500, "swing", function(){$("#page div")[0].innerHTML="";});});
}

function showContent(text){
	if(!$("#"+text)[0])
		return false;
		
	if($("#page").css("opacity") < 1){
		$("#page div")[0].innerHTML = $("#"+text)[0].innerHTML;
		$("#page").stop().animate({opacity:1}, 500, "swing", function(){$("#page div").stop().animate({opacity:1}, 500);});	
	} else {
		$("#page div").stop().animate({opacity:0}, 500, "swing", function(){$("#page div")[0].innerHTML = $("#"+text)[0].innerHTML; $("#page div").animate({opacity:1}, 500);});
	}
}

function getAnchor(obj){
	var startpos = obj.search(/#.+/)+1;
	if(startpos < 0)
		return false;
	var endpos = obj.length;
	return obj.slice(startpos, endpos);
}

function checkURL(){
	
}

function initMenue(){
	$("#page").css({opacity:0});
	var loadanc = getAnchor(document.location.href);
	if(loadanc){
		if(isNaN(loadanc)){
			showContent(loadanc);
			$("#navigation ul li a").each(
				function(){
					if(getAnchor(this.href) == loadanc){
						this.parentNode.className = "active";
						this.parentNode.parentNode.parentNode.className = "active";
					}
				}
			)
		} else {
			$("#backgroundimage")[0].src = "img/backgrounds/"+loadanc+".jpg";
			$("#gallery a").each(
				function(){
					if(getAnchor(this.href) == loadanc){
						this.className = "active";
						$(this).css({"background-position":"0 -160px"});
					} else {
						this.className = "";
					}
				}
			)
		}
	}
	$("#navigation a")
		.click(
			function(){
				if(this.parentNode.className == "active"){
					hideContent();
					$(this.parentNode).removeClass("active");
				}else{
					if(this.parentNode.parentNode.parentNode.id == "navigation"){
						$("#navigation li.active").removeClass("active");
						this.parentNode.className = "active";
					}else{
						$("#navigation li.active li.active").removeClass("active");
					}
					
					var newanc = getAnchor(this.href);
					showContent(newanc);
					document.getElementById("btn_"+newanc).className = "active";
				}
			}
		);
}

addLoadFunction(initGallery);
addLoadFunction(initMenue);