$(document).ready(function(){
	
	var path = window.location.pathname;
	var curPage = path.substring(path.lastIndexOf('/') + 1);
	if(curPage == ''){
		curPage = 'index.php';
	}

	$('#nav ul li a').each(function(){
		if($(this).attr('href') == curPage)
		{
			$(this).addClass('selected');
		}
	});
	
	function distance(page, totalPages, width)
	{
		if(page < 1 || page > totalPages)
		{
			return -width * totalPages + width; 	
		} else {
			return width;
		}
	}
	
	function slide(direction, page, move)
	{
		$('#slideshow .caption').fadeOut(200);
		
		$('#slideshow ul').animate({left:direction + move}, 200, function(){
			if($('#slideshow .caption:eq(' + (page - 1) + ')').text().length > 0)
			{
				$('#slideshow .caption:eq(' + (page - 1) + ')').addClass('visible').slideDown(300);
			}
			
			$('#dots .dot').removeClass('selected');
			$('#dots .dot:eq(' + (page - 1) + ')').addClass('selected');
			
			$('#arrow-right').stopTime();
			$("#arrow-right").everyTime(7000, function() {
				$(this).click();
			});
		});
	}
	
	

	var width = $('#slideshow ul li').width();
	var page = 1;
	var totalPages = $('#slideshow ul li').length;
	
	$('#slideshow ul').css('width', width * totalPages);
	
	for(var i=0; i<totalPages; i++)
	{
		$('#dots').append('<div class="dot"></div>');
	}
	
	$('#dots .dot:first').addClass('selected');
	
	$('#slideshow .caption a').each(function(){
		if($(this).text().length > 0)
		{
			$(this).parent().show();
		}
	});
	
	$('#arrow-left').click(function(){
		page--
		var move = distance(page, totalPages, width);
		if(page < 1)
		{
			page = totalPages;
		}
		
		slide('+=', page, move);
	});
	
	$('#arrow-right').click(function(){
		page++
		var move = distance(page, totalPages, width);	
		if(page > totalPages)
		{
			page = 1;
		}
		slide('-=', page, move);
	});
	

	$("#arrow-right").everyTime(7000, function() {
		$(this).click();
	});
	
});
