$(document).ready(function(){
	var timer = null;
	
	var intervalSpeed = 5000;//5 secs
	var photos = $(".slideshow li:not(.tabs)");
	var tabs = $(".slideshow li.tabs");
	var counter = 0;
	var currentPhoto = null;
	var nextPhoto = null;
	var total = photos.length;//number of li elements. exclude li.left and li.right
	
	photos.css({opacity: 0.0});
	photos.hide();
	photos.eq(0).show();
	photos.eq(0).css({opacity: 1.0});
	
	$('.slideshow li#tab-1').click(function(){
		if(counter!=0){
			currentPhoto = photos.eq(counter);
			counter = 0;
			nextPhoto = photos.eq(counter);
			move();
		}
		return false;
	});
	$('.slideshow li#tab-2').click(function(){
		if(counter!=1){
			currentPhoto = photos.eq(counter);
			counter = 1;
			nextPhoto = photos.eq(counter);
			move();
		}
		return false;
	});
	$('.slideshow li#tab-3').click(function(){
		if(counter!=2){
			currentPhoto = photos.eq(counter);
			counter = 2;
			nextPhoto = photos.eq(counter);
			move();
		}
		return false;
	});
	$('.slideshow li#tab-4').click(function(){
		if(counter!=3){
			currentPhoto = photos.eq(counter);
			counter = 3;
			nextPhoto = photos.eq(counter);
			move();
		}
		return false;
	});
	
	function prev(){
		currentPhoto = photos.eq(counter);
		
		counter--;
		if(counter<0){
			counter=total-1;
		}	

		nextPhoto = photos.eq(counter);
		
		move();
	}
	
	function next(){
		currentPhoto = photos.eq(counter);
		
		counter++;
		if(counter>total-1){
			counter=0;
		}	

		nextPhoto = photos.eq(counter);
		
		move();
	}
	
	function move(){
		//hide & show
		currentPhoto.animate({opacity: 0.0}, 500, function () {
			//Hide the content
			currentPhoto.hide();
		});	
		nextPhoto.show();
		nextPhoto.animate({opacity: 1.0}, 500);	
		tabs.removeClass('tab-selected');
		tabs.eq(counter).addClass('tab-selected');
		
	}
	timer = setInterval(next, intervalSpeed);
	$('.slideshow li.tabs').click(function(){
		if(timer!=null){
			clearInterval(timer);	//reset
			timer = setInterval(next, intervalSpeed);
		}
		return false;
	});
});
