$(document).ready(function(){
	$("#slider").css("overflow", "hidden");
	
	var array = new Array();
	
	$("#sliderContent > img").each(function(i){
		$(this).css({'position': 'absolute', 'left': i * 885});
		array.push($(this));
	});
	
	// user's control
	var index = 0;
	var pos = 0;
	var indexLeft = 0;
	
	function moveRight(){
		$("#sliderContent").animate({
			left: "-=885px"
		}, 500, function(){
			$("#sliderContent > img:eq(" + index + ")").css("left", (pos * 885) + (array.length * 885));
			if(index == array.length - 1) index = 0;
			else index++;
			
			pos++;
			
		});
	}
	
	function moveLeft(){
		if(index + 2 == array.length + 1) indexLeft = 1;
		else if(index + 2 == array.length) indexLeft = 0;
		else indexLeft = index + 2;
		
		$("#sliderContent > img:eq(" + indexLeft + ")").css("left", (pos - 1) * 885);
		
		$("#sliderContent").animate({
			left: "+=885px"
		}, 500, function(){
			if(index == 0) index = array.length - 1;
			else index--;
			
			pos--;
			
		});
	}
	
	$("#arrowRight").click(moveRight);
	$("#arrowLeft").click(moveLeft);
	
	
	// timer control
	$(document).everyTime(5000, "interval", moveRight);
	
	$("#slider > img").hover(
		function(){
			togglePause();
		},
		function(){
			togglePause();
		}
	);
	
	
	// play - pause
	$("#playpause").css("display", "block");
	var play = true;
	var src = "";
	function togglePause(){
		src = $("#playpause img").attr("src");
		if(play == true){
			play = false;
			$("#playpause img").attr({src: src.replace(/pause/, "play"), alt: "Play"});
			$(document).stopTime("interval");
		} else {
			play = true;
			$("#playpause img").attr({src: src.replace(/play/, "pause"), alt: "Pause"});
			$(document).everyTime(5000, "interval", moveRight);
		}
	}
	
	$("#playpause").click(togglePause);
	
	
	// fix Safari & Opera containerMid height bug
	if($.browser.safari || $.browser.opera){
		$(window).load(function(){
			var h = $("#containerMid").css("height");
			$("#containerMid").css("height", h);
		});
	}
});