$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	var totWidth=0;
	var positions = new Array();
	var pos = 0;

	$('#slides .slide').each(function(i){

		/* Traverse through all the slides and store their accumulative widths in totWidth */

		positions[i]= totWidth;
		totWidth += 1030;

	});

	$('#slides').width(totWidth);


	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#gallery_menu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('li.menuItem').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');

			pos = $(this).parent().prevAll('.menuItem').length;

			var disp_width = document.documentElement.clientWidth;
			$('#slides').stop().animate({marginLeft:(document.documentElement.clientWidth-1030)/2-positions[pos]+'px'},495);

			/* 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);
	});

	$('#gallery_prev a').click(function(){
			next_pos = $('#gallery_menu ul li').index($('#gallery_menu ul li.act')) - 1;
			if (next_pos == -1) next_pos = $('#gallery_menu ul li a').length -1;
			$('#gallery_menu ul li a').eq(next_pos).trigger('click',[false]);
	});

	$('#gallery_next a').click(function(){
			next_pos = ($('#gallery_menu ul li').index($('#gallery_menu ul li.act')) + 1)%$('#gallery_menu ul li a').length;
			$('#gallery_menu ul li a').eq(next_pos).trigger('click',[false]);
	});


	$('#gallery_menu ul li.menuItem: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;

		$('#gallery_menu ul li a').eq(current%$('#gallery_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 = 20;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*200);

	$('#slides').css("margin-left",(document.documentElement.clientWidth-1030)/2);
	$(window).resize(function() {
		$('#slides').stop().animate({marginLeft:(document.documentElement.clientWidth-1030)/2-positions[pos]+'px'},'fast','linear');
	});

	/* End of customizations */
});
