var rotator = {
	init:function(){		
		
		// Add in the controls
		rotator.addControls();	
		
		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator img').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 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.imageClick);		
		
		$('#home-banner').mouseenter(function(){
			$('#controls').fadeIn();
		}).mouseleave(function(){
			$('#controls').fadeOut();
		});
				
	},
	
	addControls:function(){
		var imgs = $('#rotator img').length;
		if(imgs > 0){
			var $html = 		'<div id="controls">'+
								'<div id="controller">'+
								'<ul>'+
								'<li id="controls-previous"><a href="#" id="controls-prev">Previous</a></li>'+
								'<li id="controls-next"><a href="#" id="controls-nxt">Next</a></li>'+
								'</ul>'+
								'</div>'+
								'</div><!-- controls -->';
			$('#home-banner').append($html);				
			
			for(var i=1; i<=imgs; i++){	
				var crnt = (i == 1) ? ' class="current"' : '';
				$('#controls-next').before('<li><a href="#" id="control-'+i+'"'+crnt+'>'+i+'</a></li>');
			}
			
			// center the rotator
			var width = $('#controls').width();
			var ban_width = $('#home-banner').width();
			var middle = ( ( ban_width - width) /2);
			$('#controls').css('left', middle+'px');
		}
	},
	
	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');
	},
	
	imageClick:function(){
		// remove any vimeo video
		$('#rotator .video').remove();
		
		var $lnk = $(this);
		var $url = $lnk.attr('href');
		
		if($url.match(/vimeo/)){
			
			// video details
			var id 		= $url.split('/').pop();
			var $width 	= $('#home-banner').css('width');
			var $height	= $('#home-banner').css('height');
													
			// replace the image with a video embed and start playing
			var embed = '<div style="position:absolute; top:0; left:0; z-index:100; width:'+$width+'px; height:'+$height+'px; background:#000;" class="video" id="video-'+id+'">'+
			'<object width="'+$height+'" height="'+$height+'">'+
			'<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="'+$width+'" height="'+$height+'" wmode="transparent">'+
			'</embed>'+
			'</object>'+
			'</div>';				
			if(rotator.timer) clearTimeout(rotator.timer);

			$('#rotator').append(embed);
			return false;
		} 
	},
	
	rotatorControls:function(){
		// Stop rotation
		if(rotator.timer) clearTimeout(rotator.timer);		
		
		// remove any vimeo video
		$('#rotator .video').remove();
			
		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();	
});
