var rotator = {
	init:function(){
			
		// add IDs to left side
		var id = 1;
		$('#feature-left li').each(function(){
			$(this).attr('id', 'feature-'+id);
			id++;
		});
		
		// add IDs to items
		var item = 1;
		$('#rotator .item').each(function(){
			$(this).attr('id', 'item-'+item);
			item++;
		});
		
		// make first one visible
		$('#rotator .item:first').css('display','block');
			
		// center the rotator
		rotator.items = $('#rotator .item').length;		
		rotator.current = 1;
		rotator.lastImage = $('#rotator .item:last').attr('id').split('-')[1];
		
		// start the rotating	    
	    rotator.prepNextSlide();
	    
		$('#rotator img').click(function(){
			if ( ! $(this).hasClass('external-link') ) {
				var id = $(this).parent().parent().attr('id').split('-')[1];
				var url = $('#feature-'+id).find('a').attr('href');
				window.location = url;
				return false;
			}
		});
		
		$('#feature-left li').bind('click', rotator.featureClick);
					
	},
	
	featureClick:function(){
		// if this item is already current, go to the page
		if($(this).hasClass('current')){
			window.location = $(this).find('a').attr('href');
		}			
		
		var id = $(this).attr('id').split('-')[1];
		
		// check if the item has a corresponding image in the rotator
		if($('#item-'+id).length > 0){	
		
			// stop the interval and start it over
			clearTimeout(rotator.timer);
			
			rotator.next		= id;
			rotator.nextImg		= '#item-'+rotator.next;

			rotator.timer = setTimeout(function(){ rotator.fadeIt(); }, 50);
					    			
		} else {
			window.location = $(this).attr('href');
		}
		// set current class on correct link
		$('#feature-left li.current').removeClass('current');		
		
		$(this).addClass('current');						
		
		return false;		
	
	},
	
	prepNextSlide:function(doFade){
		// set var img depending on whether last image is selected or not
		if(rotator.current == rotator.lastImage){
			rotator.nextImg = '#item-1';
			rotator.next 	= 1;
		} else {
			rotator.next 	= parseInt(rotator.current)+1;			
			rotator.nextImg = '#item-'+rotator.next;
		}		
		rotator.getRotatorSpeed();
	},
	
	getRotatorSpeed:function(){
		if($('#item-'+rotator.current).attr('class'))
			rotator.speed = parseInt($('#item-'+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(){	
		
		// fade out current image
		$('#item-'+rotator.current).fadeOut('slow').removeClass('current');
		
		// set next image to be new image
		$(rotator.nextImg).addClass('current');
		$(rotator.nextImg).fadeIn('slow');
		
		// get the item that is current
		$('#feature-left li.current').removeClass('current');		

		rotator.current = rotator.next;
		$('#feature-'+rotator.current).addClass('current');
		
		rotator.prepNextSlide();
		
		rotator.moveIndicator();

	},
	
	moveIndicator:function(){
		$('#rcontrols 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(); }, 50);		
		return false;
	}
}

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