// JavaScript Document




function doSlide(slideshow) {
	var count = $("#slider li.slide").length;
	var index = $(".action").attr("rel");
	//alert(index+" - "+count);
	
	if(slideshow=='show') {
		if(count <= index || index == 0) {
			$("a.prev").attr("rel", parseInt(count));
			$("a.next").attr("rel", 2);
			$("a.action").attr("rel", 1);
		} else {
			$("a.prev").attr("rel", index);
			$("a.next").attr("rel", parseInt(index)+2);
			$("a.action").attr("rel", parseInt(index)+1);
		}
	} else {
		if(index == 1 || index <= 0) {
			$("a.prev").attr("rel", 2);
			$("a.next").attr("rel", 1);
			$("a.action").attr("rel", parseInt(count));
		} else {
			$("a.prev").attr("rel", parseInt(index)-2);
			$("a.next").attr("rel", parseInt(index));
			$("a.action").attr("rel", parseInt(index)-1);
		}
	}
 	$("li.slide").removeClass("active").fadeOut();
	$("#slide"+index).addClass("active").fadeIn();
}

$(document).ready(function() {
	var delayLength = 4000;
	var i = 1;
	
	$("#slider li.slide").each(function() {
		if(i == 1) $(this).fadeIn();
		$(this).attr("id","slide"+i);
		i++;
	});
	
	var count = $("#slider li.slide").length;	
	$("a.prev").attr("rel", parseInt(count));
	$("a.next").attr("rel", 3);
	$("a.action").attr("rel", 2);
	
	sliderIntervalID = setInterval(function() {
		doSlide('show');
	}, delayLength);
	
	$("a.action").click(function() {
		if ($(this).is(".pause")) {
			clearInterval(sliderIntervalID);
		 	$(this).removeClass("pause").addClass("play");
		} else {
			sliderIntervalID = setInterval(function() {													
				doSlide('show');
			}, delayLength);
		 	$(this).removeClass("play").addClass("pause");
		}
	});
	
	$("a.next").click(function() {
		doSlide('show');
	});
	
	$("a.prev").click(function() {
		doSlide('prev');
	});
	
});



