

var changeEvery = 8;
var stopped=0;
var totWidth=0;
var pos=0;
var current=1;
var positions = new Array();

$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	$('#slides').cycle({
		timeout: 0,
		fx: 'scrollHorz',
		next: '#next',
		prev: '#prev' 
	});
	if ($('#slides .slide')){
	$('#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){
		stopped = 1;
			/* On a thumbnail click */

			$('li').removeClass('act').addClass('inact');
			$(this).parent().addClass('act');
					
			pos = $(this).parent().prevAll('.menuItem').length;
						
			$("#slides").cycle(pos);
					
			e.preventDefault();
			/* Prevent the default action of the link */
	});
	
	/*$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');*/
	/* On page load, mark the first thumbnail as active */

	}	
	$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
});

$(document).ready(function() {

var divchildren= $("#slides > div").size();
var lastslide = divchildren - 1;

var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

function autoAdvance()
	{
		if(stopped == 1)
	{return false;}

	$("#slides").cycle(current);
	$('li').removeClass('act').addClass('inact');
	$("#menu"+ current).addClass('act');
	current++;
	
	if(current > lastslide)
	{				
		pos=0;
		current=0;
	}
	}
		
	$("#slides").touchwipe({		
 					
		wipeLeft: function() {
			stopped=1;	
			if(pos == lastslide)
			{
				
				$("#slides").cycle(pos);
				$('li').removeClass('act').addClass('inact');
				$("#menu"+ pos).addClass('act');
				pos=-1;
			}
			
			pos++;
			$("#slides").cycle(pos);
			$('li').removeClass('act').addClass('inact');
			$("#menu"+ pos).addClass('act');		
			
		if(pos >lastslide)
		{
			pos=0;	
		}
		
 		},
 		wipeRight: function() {
			stopped=1;
			
		/*if(pos ==-1)
		{
			pos=2;
			$("#slides").cycle(pos);
			$('li').removeClass('act').addClass('inact');
			$("#menu"+ pos).addClass('act');	
		}*/
		
		if(pos == 0)
		{
			$("#slides").cycle(pos);
			$('li').removeClass('act').addClass('inact');
			$("#menu" + pos).addClass('act'); 	
			pos=divchildren;
		}
			pos--;
			//alert("slidenumber:" + slidenum + ", count" + count);
			$("#slides").cycle(pos);	
			$('li').removeClass('act').addClass('inact');
			$("#menu" + pos).addClass('act'); 
		}
	});	
});
