var rotator = {
	init:function(){
	
		// center the rotator
		var width = $('#controls ul').width();
		var middle = ( ( 901 - width) /2) + 10;
		$('#controls').css('left', middle+'px');

		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator img, #rotator object').each(function(){
			var item = $(this);
			if(item.attr('nodeName') == 'OBJECT'){
				item.wrap('<div class="item" id="image-'+num+'" />');
			} else {
				item.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 img:last').attr('id').split('-')[1];		
		
		// set the order for the next slide to appear
		rotator.prepNextSlide();
		
		$('#controls a').bind('click', rotator.rotatorControls);
		$('#rotator a').bind('click', rotator.launchVideo);
	},
	
	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(){	
		
		// 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();

	},
	
	moveIndicator:function(){
		$('#controls a.current').removeClass('current');
		$('#control-'+rotator.current).addClass('current');
	},
	
	rotatorControls:function(){		
		// remove any embedded videos
		$('#rotator embed, #rotator object').remove();
		
		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;
	},
	
	launchVideo:function(){
		var $url = $(this).attr('href');
		if($url.match(/vimeo/)){
			var id = $url.split('/').pop();
			
			// replace the image with a video embed and start playing
			var embed = '<object width="900" height="480">'+
			'<param name="allowfullscreen" value="true" />'+
			'<param name="wmode" value="transparent" />'+
			'<param name="allowscriptaccess" value="always" />'+
			'<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=vimeo.com&amp;autoplay=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />'+
			'<embed src="http://vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=vimeo.com&amp;autoplay=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="900" height="480" wmode="transparent"></embed>'+
			'</object>';
			if(rotator.timer) clearTimeout(rotator.timer);
						
			// hide the image
			$(this).find('img').hide();
			
			// inject the embed code
			$(this).before(embed);
			
			// if the browser is IE, barf in a bag. no, no, ensure the controls remain visible.
			
							
			
			
			return false;
		}	

		/*
		if($url.match(vimeo)){
			var id = $url.split('/').pop();
			console.log(id);
		}		
		return false;
		*/
	}
}

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

