$(document).ready(function() {
	

	
	//	SLIDESHOW
	
	$('#slideshow ul.buttons li.prev a').click(
		function () {
			slide_prev();
			return false;
		}
	);
	
	$('#slideshow ul.buttons li.next a').click(
		function () {
			slide_next();
			return false;
		}
	);
	
	$('#slideshow ul.dots-nav li a').click(
		function () {
			slide_index( $('#slideshow ul.dots-nav li a').index( this ) );
			return false;
		}
	);
	

	
});

function slide_mark_dot(index) {
	
	 $('#slideshow .dots-nav a').removeClass('active');
	 $('#slideshow .dots-nav a:eq(' + index + ')').addClass('active');
	
}

function slide_prev() {
	
	var pos = $('#slideshow .photos').position();
	if (parseInt(pos.left) + parseInt($('#slideshow .photos img:first').width()) > 0 ) {
			$('#slideshow .photos').animate({
				'left':'-=' + (parseInt($('#slideshow .photos img:first').width()) * (parseInt($('#slideshow .photos img').length)-1) ) + 'px' 
			},
			function () 
			{ 
				var pos = $('#slideshow .photos').position();
				slide_mark_dot( Math.round(parseInt(pos.left) / parseInt(-$('#slideshow .photos img:first').width())) );
			}
		);	
	} else {
		$('#slideshow .photos').animate(
			{
				'left':'+=' + $('#slideshow .photos img:first').width() + 'px' 
			},
			function () 
			{ 
				var pos = $('#slideshow .photos').position();
				slide_mark_dot( Math.round(parseInt(pos.left) / parseInt(-$('#slideshow .photos img:first').width())) );				
			}
		);
	}
	
	clearTimeout(slideTimeout);
	slideTimeout = setTimeout('slide_next()', slideTimer);
	
}

function slide_next() {
	
	var pos = $('#slideshow .photos').position();
	if (parseInt(pos.left) - parseInt($('#slideshow .photos img:first').width()) <= (-parseInt($('#slideshow .photos img:first').width()) * (parseInt($('#slideshow .photos img').length)) ) ) {
		$('#slideshow .photos').animate({
			'left':'+=' + Math.abs(pos.left) + 'px' 
		},
			function () 
			{ 
				var pos = $('#slideshow .photos').position();
				slide_mark_dot( Math.round(parseInt(pos.left) / parseInt(-$('#slideshow .photos img:first').width())) );				
			}
		
		);	
	} else {
		$('#slideshow .photos').animate({
			'left':'-=' + $('#slideshow .photos img:first').width() + 'px' 
		},
			function () 
			{ 
				var pos = $('#slideshow .photos').position();
				slide_mark_dot( Math.round(parseInt(pos.left) / parseInt(-$('#slideshow .photos img:first').width())) );				
			});
	}
	
	
	
	clearTimeout(slideTimeout);
	slideTimeout = setTimeout('slide_next()', slideTimer);
	
}

function slide_index(index) {
	
	var pos = $('#slideshow .photos').position();
	
	if ( parseInt(pos.left) <= parseInt(-$('#slideshow .photos img:first').width() * index) ) {
		$('#slideshow .photos').animate({
			'left':'+=' + Math.abs((Math.abs(parseInt(pos.left)) - parseInt($('#slideshow .photos img:first').width() * index))) + 'px' 
		});
	} else {
		$('#slideshow .photos').animate({
			'left':'-=' + Math.abs((Math.abs(parseInt(pos.left)) - parseInt($('#slideshow .photos img:first').width() * index))) + 'px' 
		});
	}
	
	slide_mark_dot( index );
	
	clearTimeout(slideTimeout);
	slideTimeout = setTimeout('slide_next()', slideTimer);
	
}

slideTimer = 4000; // 4 sec * 1000
slideTimeout = setTimeout('slide_next()', slideTimer);
