
var sliderIntervalID;
var $next;
var slidetimer = 0;

function cleartimer() {
	slidetimer = 0;
}

function slideSwitch(sl) {
	clearInterval(sliderIntervalID);
	var $active = jQuery('#slider li.active');

	if ( $active.length == 0 ) $active = jQuery('#slider li:last-child');

	if(sl >= 0) {
		$next =  $active.prev().length ? $active.prev() : jQuery('#slider li:last-child');
	} else {
		$next =  $active.next().length ? $active.next() : jQuery('#slider li:first-child');
	}
	$active.addClass('lastactive');
	$active.children('p').stop().animate({bottom: '-' + ($active.children('p').height() + 30) + 'px'}, 500);
	$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1500, function() {
		$active.removeClass('active lastactive');
		$next.children('p').stop().animate({bottom: '0'}, 500);
	});
	
	sl = -1;
	
	sliderIntervalID  = setInterval('slideSwitch()', 8000 );
};

jQuery(document).ready(function($) {
	
	
	if ($('#slider li').size() > 0) {
		$('#slider li').each(function(){
			$(this).css({opacity: 0.0}).show();
			$(this).children('p').css({bottom: '-' + ($(this).children('p').height() + 30) + 'px'});
		});
		$('#slider li:first-child').animate({opacity: 1.0}, 1500, function() {			
			$(this).children('p').stop().animate({bottom: '0'}, 500);
		}).addClass('active');
		if($('#slider li').size() > 1) {
			/* start slide */
			sliderIntervalID  = setInterval('slideSwitch()', 8000 );
			
			$('.slidenav-left, .slidenav-right').show();
			$('.slidenav-left a').click(function() {
				if(slidetimer == 0) {
					slideSwitch(1);
					slidetimer = window.setTimeout(cleartimer, 1800);
				}
				return false;
			});
			$('.slidenav-right a').click(function() {
				if(slidetimer == 0) {
					slideSwitch();
					slidetimer = window.setTimeout(cleartimer, 1800);
				}
				return false;
			});
			/* end slide */
		}
	}
	
});
