$(document).ready(function() {
	
	//	MENU
	
	$('body, #content, #header h1').not( $('#header li, #header .dropdown, #header li .dropdown') ).hover(
		function () {
			$('#header li .dropdown').not(this).fadeOut();
		}
	);
	
	$('#header').children('ul').children('li').children('a').hover(
		function() {
			if (!$(this).parent().children('.dropdown').is(':visible')) {
				$('#header li .dropdown').not(this).fadeOut();
				$(this).parent().children('.dropdown').fadeIn();
			}
		}
	);
	
	//	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;
		}
	);
	
	//	SLIDER
	
	$('#slider ul.buttons li.prev a').click(
		function () {
			
			if ($('#slider .inner ul').is(':animated')) return false;
			
			if ($('#slider .inner ul').css('left').replace('px','') <= -$('#slider .inner').width()  ) {
				$('#slider .inner ul').animate(
					{
						'left':'+=' + $('#slider .inner').width() + 'px'	
					},
					function () {
						if ($('#slider .inner ul').css('left').replace('px','') > 0 ) {
							$('#slider .inner ul').css({'left':'0px'});
						}
					}
				);
			} else {
				$('#slider .inner ul').animate(
					{
						'left':'-=' + $('#slider .inner').width() + 'px'	
					}
				);
				
			}
			return false;
		}
	);
	
	$('#slider ul.buttons li.next a').click(
		function () {
			
			if ($('#slider .inner ul').is(':animated')) return false;
			
			var pos = $('#slider .inner ul').position();
			

			if (pos.left > (-$('#slider .inner ul').width() + $('#slider .inner').width()) ) {
				$('#slider .inner ul').animate(
					{
						'left':'-=' + $('#slider .inner').width() + 'px'	
					}
				);
			} else {

				$('#slider .inner ul').animate(
					{
						'left':'+=' + Math.abs(pos.left) + 'px'
					}
				);
			}
			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();
	
	// alert( parseInt(pos.left) + ' - ' + parseInt($('#slideshow .photos img:first').width()) + ' <= ' + (-parseInt($('#slideshow .photos img:first').width()) * (parseInt($('#slideshow .photos img').length)) ) );
	
	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);
