var rotator = {
	init:function(){			
		
		
		
		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator .item').each(function(){
			$(this).attr('id', 'image-'+num);
			num++;
		});
		
		// make first image visible
		$('#image-1').css('display', 'block').addClass('current');
		rotator.current = 1;
		
		// store value of last image
		rotator.lastImage = $('#rotator .item:last').attr('id').split('-')[1];		
		
		// set the order for the next slide to appear
		rotator.prepNextSlide();
		
		$('#controls a').bind('click', rotator.rotatorControls);			
		
		
		setTimeout(function(){
			$('#controls, #rotator .caption').animate({ bottom:'-100px'});
		}, 1500);
		
		// on hover, show the controls and caption		
		$('#banner').hover(function(){
			$('#rotator .current .caption').animate({ bottom:0});
			$('#controls').animate({bottom:'16px'});
		}, function(){
			$('#rotator .caption, #controls').animate({ bottom:'-72px'});
		});
		
		
	},
	
	prepNextSlide:function(doFade){
		// set var img depending on whether last image is selected or not
		if(rotator.current == rotator.lastImage){
			rotator.nextImg = '#image-1';
			rotator.next 	= 1;
		} else {
			rotator.next 	= parseInt(rotator.current)+1;			
			rotator.nextImg = '#image-'+rotator.next;
		}		
		rotator.getRotatorSpeed();
	},
	
	getRotatorSpeed:function(){
		if($('#image-'+rotator.current).attr('class'))
			rotator.speed = parseInt($('#image-'+rotator.current).attr('class').split('-')[1] );	
		
		if(rotator.speed)
			rotator.rotatorSpeed = rotator.speed*1000;
		else
			rotator.rotatorSpeed = 5000;		
			
		// trigger the fading function
		rotator.timer = setTimeout(function(){ rotator.fadeIt(); }, rotator.rotatorSpeed);
	},
	
	fadeIt:function(click){	
		
		// fade out current image
		$('#image-'+rotator.current).fadeOut('slow').removeClass('current');
		
		// set next image to be new image
		$(rotator.nextImg).addClass('current');
		$(rotator.nextImg).fadeIn('slow');
		
		rotator.current = rotator.next;
		
		rotator.prepNextSlide();
		
		rotator.moveIndicator();
		
		if(!click){
			$('#rotator .caption, #controls').animate({ bottom:'-72px'});
		} else {
			$('#rotator .caption').animate({bottom:0 });
			$('#controls').animate({bottom:'16px'});
		}

	},
	
	moveIndicator:function(){
		$('#controls a.current').removeClass('current');
		$('#control-'+rotator.current).addClass('current');
	},
	
	rotatorControls:function(){
		if(rotator.timer) clearTimeout(rotator.timer);		

		switch(this.id){
			case 'controls-nxt':
				if(rotator.current == rotator.lastImage){
					rotator.nextImg = '#image-1';
					rotator.next	= 1;
				} else {
					rotator.next	= parseInt(rotator.current)+1;
					rotator.nextImg = '#image-'+rotator.next;					
				}				
			break;
			
			case 'controls-prev':
				if(rotator.current == '1'){
					rotator.next	= rotator.lastImage;
					rotator.nextImg	= '#image-'+rotator.lastImage;
				} else {
					rotator.next	= parseInt(rotator.current)-1;
					rotator.nextImg	= '#image-'+rotator.next;
				}
			break;
			
			default:
				rotator.next		= $(this).text();
				rotator.nextImg		= '#image-'+rotator.next;
			break;			

		}
		
		rotator.timer = setTimeout(function(){ rotator.fadeIt('click'); }, 50);				
		return false;
	}
}

$(document).ready(function(){	
	rotator.init();
});
