var rotate = {
	current: "",
	next: "",
	next_number: "",
	current_number: "",
	interval: "",
	ms: 6000,
	speed: 2000,
	
	init: function(pauseTime){
		this.ms = pauseTime;
		
		//Set the opacity of all images to 0
		$('div#image-rotate ul li').css({opacity: 0.0});
		
		//Get the first image and display it (gets set to full opacity)
		$('div#image-rotate ul li:first').css({opacity: 1.0});
		
		//Set first link to bold
		$('div#image-rotate-navigation ul li:first a').css("fontWeight","bold");
		
		//Call the rotator function to run the slideshow
		this.interval = setInterval('rotate.rotateit()',this.ms);
		$('div#image-rotate-navigation ul li a').each(function(){
			$(this).click(function(){
				
				if($(this).parent("li").attr("class") != rotate.current_number){
					
					rotate.changeButton("play");
					rotate.pause();
					
					rotate.current = ($('div#image-rotate ul li.show') ? $('div#image-rotate ul li.show') : $('div#image-rotate ul li:first'));
					rotate.next = $('div#image-rotate ul li img.'+$(this).parent("li").attr("class")).parent("li");
					rotate.next_number = rotate.next.find("img").attr("alt");
					rotate.current_number = rotate.current.find("img").attr("alt");
					
					$('div#image-rotate-navigation ul li.' + rotate.next_number).addClass("selected");
					$('div#image-rotate-navigation ul li.' + rotate.current_number).removeClass("selected");
					
					rotate.next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, rotate.speed / 2);

					//Hide the current image
					rotate.current.animate({opacity: 0.0}, rotate.speed).removeClass('show');

					rotate.interval = setInterval('rotate.rotateit()',rotate.ms);
					
				}
			});
		});
	
	},
	
	changeButton: function(cb){
		if(cb == "pause"){
			$("#image-rotate-buttons ul li.pause").hide();
			$("#image-rotate-buttons ul li.play").show();
		}else{
			$("#image-rotate-buttons ul li.pause").show();
			$("#image-rotate-buttons ul li.play").hide();
		}
	},
	
	pause: function(){
		clearInterval(this.interval);
	},
	
	play: function(){
		this.interval = setInterval('rotate.rotateit()',this.ms);
	},

	rotateit: function(numbers){	
		
		if(numbers === undefined){
			//Get the first image
			rotate.current = ($('div#image-rotate ul li.show') ? $('div#image-rotate ul li.show') : $('div#image-rotate ul li:first'));

			//Get next image, when it reaches the end, rotate it back to the first image
			rotate.next = ((rotate.current.next().length) ? ((rotate.current.next().hasClass('show')) ? $('div#image-rotate ul li:first') :rotate.current.next()) : $('div#image-rotate ul li:first'));	
			
			//Get the number of the next image, so we can set the next button to 
			rotate.next_number = rotate.next.find("img").attr("alt");
			rotate.current_number = rotate.current.find("img").attr("alt");
			
			$('div#image-rotate-navigation ul li.' + rotate.next_number).addClass("selected");
			$('div#image-rotate-navigation ul li.' + rotate.current_number).removeClass("selected");
		}
		
		//Set the fade in effect for the next image, the show class has higher z-index
		rotate.next.css({opacity: 0.0})
		.addClass('show')
		.animate({opacity: 1.0}, rotate.speed / 2);

		//Hide the current image
		rotate.current.animate({opacity: 0.0}, rotate.speed)
		.removeClass('show');
		
	}
};