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

$(document).ready(function(){

    $('.thum_img').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            extraClass: 'ttip',
            positionLeft: true,
            opacity: .1
    });
    
    // load the progress bar
    $('#progbar').fadeIn(500).progressbar(function() {
        interval: 500;
        width: 250;
        range: true;
        increment: 1;
    });
    $('#thum_carousel').jcarousel({
   		vertical: true,
		scroll: 1,
        initCallback: initCallback
    });
	$.preload('a img',{
        onComplete:advanceXPercent
    });
    // set a slight timeout on the progbar for better visuals
    setTimeout("initialProgressing();",50);
    setTimeout("advanceXPercent();",3000);
   
});

/**
 * use the initCallback callback
 * to assign functionality to the carousel controls
 */
function initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });

    $('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = $.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    $('#forward').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#back').bind('click', function() {
        carousel.prev();
        return false;
    });
    $('#thum_carousel').hide(1);
};

var progress = 8;
var increment = $('#progbar').attr('name');
var carouselStarted = false;
	
function startCarousel()
{

	$('#prevent_flicker').css("height","590px");
	$('#thum_carousel').fadeIn(1).fadeTo(800,1);
}
	
function increase(){
		//$('#percent').html(progress);
	    progress++;
	    
	    if( progress > 100 && !carouselStarted ){
	    
	        carouselStarted = true;
	        // a small delay before starting
	        setTimeout(function(){
	            // destroys the progress bar and fades out the hand
	            $("#progbar").hide(1).progressbar("destroy");
	            setTimeout("startCarousel();",100);
	        },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 advanceXPercent(){	
	    var delay = 0;
	    for(var i = 0; i < increment; i++){
	        setTimeout( "increase();" , delay );
	        delay = delay + 200;
	    }
	}