﻿var biw;
var bih;
var ratio
	$(document).ready(function() {
		
		$('.slideshownav_btn').each(
			function(){
				//alert('hmm');
				$(this).attr('src',nonselected_btn);
		
			}
		);
		
		
		/* Define the function that triggers to fade in the background as soon as the image has loaded */
		$("img#bg").load(function()
			{
		
				biw=$("img#bg").width();
				bih=$("img#bg").height();
			
				var dh=$(document).height();
				var dw=$(document).width();
				
				ratio = bih / biw;	
					
				// Get browser window size
				
				// Scale the image
				if ((dh/dw) > ratio){
		   		 	$("img#bg").height(dh);
		   		 	$("img#bg").width(dh / ratio);
				} else {
		   			$("img#bg").width(dw);
		    			$("img#bg").height(dw * ratio);
				}
				/*
				// Center the image
				$("img#bg")).css('left', (dw - $("img#bg")).width())/2);
				$("img#bg")).css('top', (dh - $("img#bg")).height())/2);
	*/
				/* Fade in during 3 seconds */
				dimmBtns(1);
				$("img#bg").fadeTo(700,1);
				
				/* Animate the picture description during 1 second */
				descriptionTimer=setTimeout(fadeInImageDescription, 200);
			
				/* Set the timeout to fade out the image and the description after 10 seconds*/
				if(frontpageslides.length>1 && fpsh_playing){	
					fadeOutTimer=setTimeout(fadeImageOut,8500);
				}
			}
			)
	
		/* Start the slideshow one second after the page is ready */ 
		startTimer=setTimeout(LoadImages,1000);
			
	});
	
	function fadeInImageDescription(){
			slideIn=true;
			$("#showreelText").fadeTo(1000,1)
	
	}
	function fadeImageOut(forceload){
			dimmBtns(.8);
			slideIn=false;
			if(forceload){
				$("#showreelText").fadeOut(200);
				$("img#bg").fadeOut(400);
			
			
			
				loadTimer=setTimeout(LoadImages,600);
			
			}else{
				$("#showreelText").fadeOut(200);
				$("img#bg").fadeOut(400);
			
			
			if(fpsh_playing ){
				loadTimer=setTimeout(LoadImages,1000);
			}
			}
	}
	
	function closeFrontText(){
		$("#primary").html('');
		$("#primary").attr("id", "primary-closed");
	}
	function dimmBtns(a){
	
		$('.slideshownav_btn').each(
			function(){
				//alert('hmm');
				$(this).fadeTo(400,a);
		
			}
		);
		
	}
	
	function showreelNav(rRnd){
	
		if(slideIn){
			iRnd=rRnd-1;
			if(iPrev!=-1){
					$("img#slidebtn_"+(iPrev+1)).attr("src", nonselected_btn);
				}
				
			try{
				window.clearTimeout(fadeOutTimer);
			} catch (e){
				//alert('fadeOutTimer not defined');
			}
			try{
				
				window.clearTimeout(loadTimer);
			} catch (e){
				//alert('loadTimer not defined');
			}
				
			if(!fpsh_playing && iRnd==iPrev){
				fpsh_playing=true;
				
				
				fadeImageOut(true);
			}else{
				
				
				
				fpsh_playing=false;
				iRnd=iRnd-1;
				//alert('fadeOut, and show '+iRnd);
				
				fadeImageOut(true);
			}
		}
		
		
		
		/* LoadImage(iRnd);*/
			
			
	}
	
	function LoadImage(iNr){
		
		
		
			if(fpsh_playing){
				$("img#slidebtn_"+(iRnd+1)).attr("src", selected_btn);
			}else{
				$("img#slidebtn_"+(iRnd+1)).attr("src", mouseselected_btn);
			}
			
			
			/* Assign the new image to the background */
			$("img#bg").attr("src", frontpageslides[iNr].image);
			
			
			
			/* Assign the artist name to the description */
			$("#image_title").html('<h3>'+frontpageslides[iNr].title+'</h3>');
			$("#image_text").html(frontpageslides[iNr].text);
			
			/* Assign the image url to the description */
		   $("a#image_url").attr("href", frontpageslides[iNr].url);
		  
						
	};
    
    function LoadImages(){
    		
		/* Select a random image number and make sure this is not equal to the previous image */
		if(frontpageslides.length>1){
		
			if(iPrev!=-1){
				$("img#slidebtn_"+(iPrev+1)).attr("src", nonselected_btn);
			}
		
			if(fpsh_order=="random"){
			while(iPrev == iRnd){
				iRnd = Math.floor(Math.random()*frontpageslides.length);
			}
			}else{
				iRnd++;
				if(iRnd>=frontpageslides.length){
					iRnd=0;
				}
			}
			iPrev = iRnd;
			/* Show the selected image */
			LoadImage(iRnd);
		
			
		}else if(frontpageslides.length>0 && iRnd==-1){
			iRnd=0;
			iPrev=iRnd;
			LoadImage(iRnd);
		
		}
    	
    };
