var reference = ['one','two','three','four','five','six'];
var timer="";
var currentWindow=0;
var movement=false;

$(document).ready(function(){
	timer = setInterval('$.rotateWindow({currentWindow: currentWindow})', 10000);
	
	$(".button-one, .button-two, .button-three, .button-four, .button-five, .button-six").live('click', function() {
		if ($(this).attr("name") != currentWindow && !movement) {
			clearInterval(timer);
			var newWindow = $(this).attr("name");
			var oldWindow = currentWindow;
				currentWindow = newWindow;
				movement=true;

			$("."+reference[oldWindow]).css({"z-index":"1"});
			$("."+reference[oldWindow]+" .overlay").css({"z-index":"1"});
			$(".button-"+reference[oldWindow]).css({"backgroundPosition":"0 0"});
			$("."+reference[$(this).attr("name")]).animate({
				"left": "0"
			}, 2000, function() {
				$("."+reference[oldWindow]).css({"z-index":"100", "left":"657px"});
				$("."+reference[oldWindow]+" .overlay").css({"z-index":"100"});
				clearInterval(timer);
				timer = setInterval('$.rotateWindow({currentWindow: currentWindow})', 10000);
				movement=false;;
			});
			$(".button-"+reference[$(this).attr("name")]).css({"backgroundPosition":"0 -41px"});
		}
    });
});

(function($) {
	$.rotateWindow = function(arguments) {
		var config = {
			"currentWindow" : null
		};
		
		if (arguments) { $.extend(config, arguments); }
		
		clearInterval(timer);
		movement=true;
		
		if(config.currentWindow < 5){
			currentWindow++;
		} else {
			currentWindow = 0;
		}
		
		$("."+reference[config.currentWindow]).css({"z-index":"1"});
		$("."+reference[config.currentWindow]+" .overlay").css({"z-index":"1"});
		$(".button-"+reference[config.currentWindow]).css({"backgroundPosition":"0 0"});
		$("."+reference[currentWindow]).animate({
			"left": "0"
		}, 2000, function() {
			$("."+reference[config.currentWindow]).css({"z-index":"100", "left":"657px"});
			$("."+reference[config.currentWindow]+" .overlay").css({"z-index":"100"});
			movement=false;
		});
		$(".button-"+reference[currentWindow]).css({"backgroundPosition":"0 -41px"});
		timer = setInterval('$.rotateWindow({currentWindow: currentWindow})', 10000)
	};
})(jQuery);
