
$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	var cursor = "<div class='cursor'>&nbsp;</div>";

	$('.fbar').click(function(){
		var pos = $('#menu').find('li.act').prevAll('.menuItem').length;
		var totalpos = 0;
		$('#menu').find('ul li.menuItem').each(function(){
			totalpos++;
		});
		if($(this).hasClass('back')){
			if(pos == 0) pos = totalpos; 
			
			$('#menu').find('li.act').removeClass('act').addClass('inact').find('.cursor').detach();
			
			$('#menu').find('.point').find('li:eq('+(pos-1)+')').addClass('act');
			$(cursor).prependTo($('#menu').find('li.act'));
			pos--;
		}
		else{
			if(pos == (totalpos-1)) {
				pos = 0;
			}
			else{
				pos++;
			}
			$('#menu').find('li.act').removeClass('act').addClass('inact').find('.cursor').detach();
			$('#menu').find('.point').find('li:eq('+(pos)+')').addClass('act');
			$(cursor).prependTo($('#menu').find('li.act'));
		}
		
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
	});

	
	
	$('#slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide'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;
		}
		
	});
	
	$('#slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#menu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			$('li.menuItem').removeClass('act').addClass('inact').find('.cursor').detach();
			$(this).parent().addClass('act');
			$(cursor).prependTo($('#menu').find('li.act'));
			
			var pos = $(this).parent().prevAll('.menuItem').length;
			
			$('#slides').stop().animate({marginLeft:-positions[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);
	});
	
	$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	$(cursor).prependTo($('#menu').find('li.act'));
	/* On page load, mark the first thumbnail as active */
	
	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		$('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}

	//var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});
