jQuery(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var thispage = document.location.pathname.split("/");
	var page = 0;
	var starting_points = [];
	starting_points["cooking-wine"] = 0;
	starting_points["fortified-wine"] = 1;
	starting_points["vinegars"] = 2;
	starting_points["speciality-vinegars"] = 3;
	starting_points["cooking-oils"] = 4;
	starting_points["cooking-spirit"] = 5;
	starting_points["cooking-gels"] = 6;
	starting_points["bulk"] = 7;
	
	if( thispage.length > 2 ) {
		page = starting_points[thispage[thispage.length-2]];
	}
	
	var totWidth=0;
	var positions = new Array();
	
	jQuery('#slider_slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += jQuery(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!jQuery(this).width())
		{
			//	alert("Please, fill in width for all your images!");
			return false;
		}
	});
	
	jQuery('#slider_slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	jQuery('#slider_menu ul li a').bind('mouseover', function(e,keepScroll){

			/* On a thumbnail click */

			jQuery('li.menuItem').removeClass('act').addClass('inact');
			jQuery(this).parent().addClass('act');
			
			var pos = jQuery(this).parent().prevAll('.menuItem').length;
			
			jQuery('#slider_slides').stop().animate({marginLeft:-positions[pos]+'px'},2500);
			/* 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);
	});
	
	jQuery('#slider_menu ul li a').bind('click', function(e,keepScroll){

			/* On a thumbnail click */

			jQuery('li.menuItem').removeClass('act').addClass('inact');
			jQuery(this).parent().addClass('act');
			
			var pos = jQuery(this).parent().prevAll('.menuItem').length;
			
			jQuery('#slider_slides').stop().animate({marginLeft:-positions[pos]+'px'},0);
			/* Start the sliding animation */
			
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) clearInterval(itvl);
	});
	
	/* On page load, mark the relevant thumbnail as active */
	jQuery('#slider_menu ul li.menuItem:eq(' + page + ')').addClass('act').siblings().addClass('inact');
	
	jQuery('#slider_menu ul li a').eq(page%jQuery('#slider_menu ul li a').length).trigger('click',[true]);
	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		//	jQuery('#slider_menu ul li a').eq(current%jQuery('#slider_menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		//current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 3;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});
