// JavaScript Document

var projectOpacity = 0.4;
var projectFirstDelay  = 1000;
var projectDelay  = 300;

var projectSpeed = 1000;
var projectTimer;
var prerollerTimer;
var prerollerTime = 10000;

$(document).ready(function(){
    hideAbout();
    setupPreroller();
 	setupProjectLinks();
	setupInfoLinks();
	
	setupCloseInfoButton();
	
	// For testing
	//$("#infocontent").load("info/intro.html");
	//$("#info").show();
	
	
	
	prerollerTimer = setTimeout("hidePreroller()", prerollerTime);
	
	setupLogoLink();
	
	});

    function hideAbout() {
	   $("#about").css({"position" : "absolute", "left" : "-3000px", "width" : "300px"});
	}

	function setupLogoLink() {
		$("#logo").click(function() {showProject("Thorbjorn")})
	}

	function setupPreroller() {
		$("#projectmenu img.project").each(function(i) {
			var img = this.src.substr(this.src.lastIndexOf("/")+1)
			var large = new Image();
			large.src = "images_big/" + img;
			$(large).load(function() {/*alert(this +" is loaded") */});
		});
		$("#preroller").click( function() {hidePreroller()});
	}

	function hidePreroller() {
		clearTimeout(prerollerTime);
		$("#preroller").fadeOut(150, function() {setupProjectEffects()});
		
	}
	function setupProjectLinks() {
	
		$("#projectmenu img.project").each ( function(i) {
													  
		   var img = this.src.substr(this.src.lastIndexOf("/")+1);
		   img = img.substr(0, img.lastIndexOf("."))
		   var titleImg = "titles/" + img +".png";
		   $(this).click(function() {
			showProject(img);
		    })
		   
		   $(this).hover(function() {setTitleImage(titleImg)}, function() {});
			

		});	
	}

	function setTitleImage(titleImg) {
		if (!$.browser.msie){
			$("#projecttitle").css({"background-image":"url(" +titleImg +")"});
		} else {
			document.getElementById("projecttitle").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + titleImg + "',sizingMethod='scale')";
		}
	}
	

	function showProject(img) {
		var titleImg = "titles/" + img +".png";
		hideInfo();
		$("#backimage").attr("src",  "images_big/" +img + ".jpg");
		$("#projecttitle").attr({"projecttitle": titleImg});
		setTitleImage(titleImg);
	}
	
	function setupInfoLinks() {
	
		$("#mainmenu img").each ( function(i) {
			
			
		   $(this).click(function() {
			$("#mainmenu img").each ( function(i) { $(this).removeClass("selected")})
			$(this).addClass("selected")
			var img = this.src.substr(this.src.lastIndexOf("/")+1);
			img = img.substr(0, img.lastIndexOf("."));
			$("#infocontent").load("info/" +img +".html");
			$("#info").show();
			});	
		   });	
	}
	
	function setupCloseInfoButton() {
		$("#closeinfo").hover(function() {$(this).attr({"src":"images/close_hover.png"})}, function() {$(this).attr({"src":"images/close.png"})});
		$("#closeinfo").click(function() {hideInfo()});
	
	}
	
	function hideInfo() {
		$("#info").hide()
		$("#mainmenu img").each ( function(i) { $(this).removeClass("selected")})
	}
	
	function setupProjectEffects() {
		projectTimer = setTimeout("fadeProjectMenu()", projectFirstDelay);
		$("#projects").hover(function() {clearTimeout(projectTimer); $(this).fadeTo(200, 1.0)}, 
			   function() {
				    var titleImg = $("#projecttitle").attr("projecttitle");
				   if(titleImg) {
				   	setTitleImage(titleImg);
				   }
				   clearTimeout(projectTimer)
				   projectTimer = setTimeout("fadeProjectMenu()", projectDelay);
				})
	}
	
	function fadeProjectMenu() {
		
		$("#projects").fadeTo(projectSpeed, projectOpacity);
	}
