var rotator = {
	init:function(){
		
		var imgs = $('#rotator .item');		
				
		$('#rotator-links li:first').addClass('current');
		
		rotator.current = 1;
		
		// store value of last image
		rotator.lastImage = imgs.length;	
		
		// set the order for the next slide to appear
		rotator.prepNextSlide();
		
		$('#rotator-links a').bind('click', rotator.rotatorControls);		
		
	},
	
	prepNextSlide:function(doFade){
		// set var img depending on whether last image is selected or not
		if(rotator.current == 3){
			rotator.nextImg = '#item1';
			rotator.next 	= 1;
		} else {
			rotator.next 	= parseInt(rotator.current)+1;			
			rotator.nextImg = '#item'+rotator.next;
		}
		rotator.timer = setTimeout(function(){ rotator.fadeIt(); }, 5000);	
		//rotator.getRotatorSpeed();
	},	
	
	
	fadeIt:function(){	
		
		// slide the slider
		if(rotator.current == rotator.lastImage){
			var left = 0;			
		} else {
			var left = -(rotator.current)*428;			
		}
		$('#rotator-images').animate({left:left+'px'}, 1000);		
		
		$('#rotator-links li.current').removeClass('current');
		$('#rotator-links li').eq(rotator.next-1).addClass('current');
		
		rotator.current = rotator.next;
		rotator.prepNextSlide();		

	},
	
	
	rotatorControls:function(){
		if(rotator.timer) clearTimeout(rotator.timer);	

		var index	= $('#rotator-links a').index($(this));
		var id 		= index+1;						
		var next	= (id == 4) ? 1 : id+1;

		rotator.current = index;
		rotator.next 	= next-1;
		rotator.timer 	= setTimeout(function(){ rotator.fadeIt(); }, 50);
		return false;
	}
}

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

