$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var totWidthA = 0;
	var positionsA = new Array();
	var totWidthB = 0;
	var positionsB = new Array();

	
	$('#slidesA .slide').each(function(i){
		
		/* Traverse through all the A slides and store their cumulative widths in totWidth */
		
		positionsA[i]= totWidthA;
		totWidthA += $(this).width();
		
		/* The positions array contains each slide's cumulative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	$('#slidesB .slide').each(function(i){
		
		/* Traverse through all the A slides and store their cumulative widths in totWidth */
		
		positionsB[i]= totWidthB;
		totWidthB += $(this).width();
		
		/* The positions array contains each slide's cumulative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	$('#slidesA').width(totWidthA);
	$('#slidesB').width(totWidthB);
	

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#TPgallerymenu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('#TPgallerymenu li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().prevAll('.menuItem').length;
			
			$('#slidesA').stop().animate({marginLeft:-positionsA[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(itvl);
	});
	
	$('#TPgallerymenu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	
	/* On page load, mark the first thumbnail as active */
	
	$('#CSgallerymenu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('#CSgallerymenu li.menuItemB').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			
			var pos = $(this).parent().prevAll('.menuItemB').length;
			
			$('#slidesB').stop().animate({marginLeft:-positionsB[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(itvl);
	});
	
	$('#CSgallerymenu ul li.menuItemB:first').addClass('act').siblings().addClass('inact');
	
	/* On page load, mark the first thumbnail as active */
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		$('#TPgallerymenu ul li a').eq(current%$('#TPgallerymenu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		$('#CSgallerymenu ul li a').eq(current%$('#CSgallerymenu ul li a').length).trigger('click',[true]);
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 7;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});
