﻿$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var totBannerWidth=0;
	var bannerPositions = new Array();
	
	$('#Diapos .diapo').each(function(i){
		
		/* Traverse through all the Diapos and store their accumulative widths in totBannerWidth */
		
		bannerPositions[i]= totBannerWidth;
		totBannerWidth += $(this).width();
		
		/* The bannerPositions array contains each diapo's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}

        $(this).click(function() {
          var url = $(this).find('a').attr('href');
          if (url == "" || url == null)
            url = "#";
          window.navigate(url);
        });
	});
	
	$('#Diapos').each(function(i){
		/* Pausing when mouse is over */
	    $(this).hover(function() {
	      clearInterval(itvlBanner);
          $(this).css('cursor','hand');
          //$('#clickAnim').animate({top:'0px'}, {easing:'swing',queue:false,duration:500});
        }, function() { 
          itvlBanner = setInterval(function(){autoAdvanceBanner()},changeBannerEvery*1000);       
          $(this).css('cursor','default');
          //$('#clickAnim').animate({top:'-47px'}, {easing:'swing',queue:false,duration:500});
        });
	});
	
	$('#Diapos').width(totBannerWidth);
	
	/* Change the cotnainer div's width to the exact width of all the Diapos combined */

	$('#markers ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('li.marker').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().prevAll('.marker').length;
			
			$('#Diapos').stop().animate({marginLeft:-bannerPositions[pos]+'px'}, 450);
			/* Start the sliding animation */
			
			e.preventDefault();
			/* Prevent the default action of the link */
			
			// Stopping the auto-advance if an icon has been clicked:
			//if(!keepScroll) clearInterval(itvlBanner);
			
			currentBanner=$(this).parent().index();
	});
	
	$('#markers ul li.marker:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var currentBanner=1;
	function autoAdvanceBanner()
	{
		if(currentBanner==-1) return false;
		
		// [true] will be passed as the keepScroll parameter of the click function on line 28
		$('#markers ul li a').eq(currentBanner%$('#markers ul li a').length).trigger('click',[true]);	
		currentBanner++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeBannerEvery = 10;

	var itvlBanner = setInterval(function(){autoAdvanceBanner()},changeBannerEvery*1000);

	/* End of customizations */
});
