﻿/*
 * thanks to http://devkick.com/talk/topic/304/galleria-and-progress-bar/
 *
*/

$(document).ready(function(){
	
	// prepare the hand animation and start preloading the images
    $('#slideshow').css({ "border" : "1px solid #444" }).fadeIn(100);
    
    $('#hand').preload().fadeTo(800,1,function()
    {
    
		$.preload('#s3slider img',{
	        onComplete:increaseCountImagesLoaded
	    });

	    // load the progress bar
	    $('#progbar').fadeIn(500).progressbar(function() 
	    {
		    interval: 500;
		    width: 250;
		    range: true;
		    increment: 1;
	    });
	    // set a slight timeout on the progbar for better visuals       
	    setTimeout("initialProgressing();",50);
	    setTimeout("advance10Percent();",3000);
	    setTimeout("advance10Percent();",8000);
    });
	
});

var progress = 0;
var countImagesLoaded = 0;
var firstImagesLoaded = false;
var ssStarted = false;
	
function startss()
{
	$('#hand').hide(1);
	$('#sig').fadeIn(700).fadeTo(700,1,function(){$('#s3slider').fadeIn(700);});
	$('#s3slider').s3Slider({timeout: 300});
	$('#slideshow').css({ "border" : "1px solid #000" });
}
	
function increase(){
	
	    progress++;
	    // checks if it's time to start ss
	    if( progress > 100 && !ssStarted && firstImagesLoaded ){
	    
	        ssStarted = true;
	        // a small delay before starting ss
	        setTimeout(function(){
	            // destroys the progress bar and fades out the hand
	            $("#progbar").hide(1).progressbar("destroy");
	            $('#hand img').fadeTo(1900,0,function(){
		            // waits and then starts the ss
		            setTimeout("startss();",50);
	            });
	        },450);
	    }
            // change the percent state of the progressbar
            $("#progbar").progressbar("option","value",progress);
	}
	 
function initialProgressing(){
	
	    var delay = 0;
	    for(var i = 0; i < 100; i++){
	        setTimeout( "increase();" , delay );    
	        delay = delay + 600;
	    }
	}
	
function advance10Percent(){	
	    var delay = 0;
	    for(var i = 0; i < 10; i++){
	        setTimeout( "increase();" , delay );
	        delay = delay + 200;
	    }
	}
	
function increaseCountImagesLoaded(){
	
	    countImagesLoaded++;
	    // this value determines how many of the first images you want
	    // to be loaded before ss starts.
	    if( countImagesLoaded == 3 ){
	        firstImagesLoaded = true;
	        advance10Percent();
	    }
	    advance10Percent();
		//alert("loaded: "+countImagesLoaded);
	}