var rotator = {
	init:function(){
	
		// center the rotator
		var width = $('#controls ul').width();
		var middle = ( (643 - width) /2) + 10;
		$('#controls').css('left', middle+'px');
		
		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator .item').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 .item:last').attr('id').split('-')[1];		
		
		// set the order for the next slide to appear
		rotator.prepNextSlide();
		
		$('#controls a').bind('click', rotator.rotatorControls);				
		
	},
	
	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(){
		var $current_image = $('#image-'+rotator.current);
		
		if($current_image.attr('class'))
			rotator.speed = parseInt($current_image.attr('class').split('-')[1]);
		
		if(rotator.speed)
			rotator.rotatorSpeed = rotator.speed*1000;
		else
			rotator.rotatorSpeed = 5000;				
		
		// if item link is video, play video.		
		var $curr 		= $('#image-'+rotator.current).find('a:first');		
		if($curr.attr('href')){
			var parts 	= $curr.attr('href').split('.');
			var ext		= parts.pop();
		}
			
		// replace image with video		
		if(ext == 'mpg' || ext == 'flv' || ext == 'mov'){

			var parts 		= $curr.attr('href').split('/');
			var filetype 	= parts[parts.length-1];
			var url 		= 'http://media.monkserve.com/EKK/2625/'+filetype.replace('mp4', 'flv');
			
			//if(rotator.timer) clearTimeout(rotator.timer);
		
			$curr.replaceWith('<div id="video'+rotator.current+'" class="video"></div>')
			//$('#video'+rotator.current).css({position:'absolute', top:'0', left:'0', zIndex:'10'});
			
			var s1 	= new SWFObject('/js/player.swf','player','616','284','9');
			s1.addParam('allowfullscreen','true');
			s1.addParam('allowscriptaccess','always');
			s1.addParam('flashvars','file='+url+'&autostart=true&frontcolor=ffffff&lightcolor=cc9900&skin=http://www.longtailvideo.com/jw/upload/overlay.swf&controlbar=over');
			s1.write('video'+rotator.current);				
		}
		
		// 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(){
		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();
});

