$(document).ready(function(){
	var changeEvery=5,itvl=null,current=1,totSlides=0,prev=0;
	$('#slides').hover(function(){clearInterval(itvl)},function(){itvl=setInterval(function(){autoAdvance()},changeEvery*1000)});
	var positions=new Array();
	$('#slides .slide').each(function(i){
		if(totSlides!=0)$(this).fadeOut(0);
		totSlides++;
		positions[i]=$(this);
		if(!$(this).width()){alert("Please, fill in width & height for all your images!");return false;}
	});
	$('#slides .slide .slideimg img').each(function(i){
		$(this).css('margin-top',(($(this).parent().height()-$(this).height())/2)+'px');
	});
	$('#gallerymenu ul li a').click(function(e,keepScroll){
			$('#gallerymenu li').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
			var pos=$(this).parent().prevAll('li').length;
			positions[pos].fadeIn(500,'linear');
			positions[prev].fadeOut(250,'linear');
			prev=pos;
			e.preventDefault();
			/* Prevent the default action of the link */
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) clearInterval(itvl);
	});
	$('#gallerymenu ul li:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	function autoAdvance(){
		if(current==-1) return false;
		$('#gallerymenu ul li a').eq(current%totSlides).trigger('click',[true]);
		current++;
	}
	itvl=setInterval(function(){autoAdvance()},changeEvery*1000);
	/* End of customizations */
});
