var rotator = {
	init:function(){
	
		// center the rotator
		var width = $('#controls ul').width();
		var ban_width = $('#rotator').width();
		var middle = ( ( ban_width - width) /2);
		$('#controls').css('left', middle+'px');
		
		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator p').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 p:last').attr('id').split('-')[1];		
		
		// set the order for the next slide to appear
		rotator.prepNextSlide();
		
		$('#controls a').bind('click', rotator.rotatorControls);
		
		
		$('#rotator a').click( function() {
			
			if( $(this).attr('href').match(/vimeo/)){
				
				clearTimeout(rotator.timer);

				var id = $(this).attr('href').split('/').pop();
												
				// replace the image with a video embed and start playing
				var embed = '<div style="position:absolute; top:0; left:0; z-index:100" class="video" id="video-'+id+'"><object width="880" height="463">'+
				'<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="880" height="463" wmode="transparent"></embed>'+
				'</div></object>';	
				
				$(this).before(embed);
				
				return false;
									
			}
			
		});
				
	},
	
	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 = 9000;		
			
		// 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');
	},
	
	embedControl:function() {
	
		if(rotator.timer) clearTimeout(rotator.timer);
		$('#controls').css('display','none');
		$('#rotator').html( '<object width="880" height="463"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + $('#embed').html() + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1" /><embed type="application/x-shockwave-flash" width="880" height="463" src="http://vimeo.com/moogaloop.swf?clip_id=' + $('#embed').html() + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1&amp;autoplay=1" allowscriptaccess="always" allowfullscreen="true"></embed></object>' );
		
	},
	
	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();
});

