var rotator = {
	init:function(){	
		
		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator .item').each(function(){
			$(this).attr('id', 'image-'+num);
			num++;
		});
		$('#slider').css('width', (num*482)+'px');
		// make first image visible
		$('#image-1').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);	
			
		$('.item a').each(function(){
			
			// check for onclick attr
			if($(this).attr('onclick')){
				var onclick = $(this).attr('onclick').split(',')[0].replace('waspPopup(\'', '');				
				var url = onclick.replace('\'', '');
				$(this).attr('onclick', '');
				$(this).attr('href', url);				
			}
			
			var file = $(this).attr('href').split('.');
			var file = file[file.length-1];			
							
			if(file == 'flv' || file == 'mov' || file == 'mp4'){
				$(this).bind('click', 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(){	
		
		// if the new current item is the last item, slide to first
		if(rotator.current == rotator.lastImage){

			$('#slider').animate({left:'0px'}, 400);

		} else {		
		
			var left = parseInt($('#slider').css('left'))-482;
			$('#slider').animate({left: left+'px' }, 1000);
			
		}
		
		// set next image to be new current image
		$('#image-'+rotator.current).removeClass('current');		
		$(rotator.nextImg).addClass('current');		
		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);		
		var move = true;
		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;					
				}
				$('#controls-pse').removeClass('paused');
			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;
				}
				$('#controls-pse').removeClass('paused');
			break;
			
			case 'controls-pse':
				// pause rotator
				if($(this).hasClass('paused')){
					rotator.timer = setTimeout(function(){ rotator.fadeIt(); }, rotator.rotatorSpeed);
					$(this).removeClass('paused');
				} else {
					clearTimeout(rotator.timer);
					$(this).addClass('paused');									
				}	
				move = false;		
			break;
			
			default:
				rotator.next		= $(this).text();
				rotator.nextImg		= '#image-'+rotator.next;
				$('#controls-pse').removeClass('paused');
			break;
		}
		
		if(move){
			$('#rotator .current').removeClass('current');
			$(rotator.nextImg).addClass('current');
			rotator.current = rotator.next;		
			
			var item = rotator.nextImg.split('-')[1];
			var left = (item-1)*(-482);		
			rotator.timer = setTimeout(function(){
				$('#slider').animate({left:left+'px'}, 400);
			}, 50);
			
			rotator.prepNextSlide();		
			rotator.moveIndicator();		
			
			$('#rotator .video').remove();
		}
		return false;
	}
}

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

function launchVideo(){
	var ext = $(this).attr('href').split('.');
	var ext = ext[ext.length-1];
	
	if(ext == 'mp4'){
		var parts 		= $(this).attr('href').split('/');
		var filetype 	= parts[parts.length-1];
		var url 		= 'http://media.monkserve.com/EKK/1124/'+filetype.replace('mp4', 'flv');
	} else {
		var url = $(this).attr('href');
	}
	
	if(rotator.timer) clearTimeout(rotator.timer);
	var div = $(this).parent().attr('id').split('-')[1];

	$(this).parent().append('<div id="video'+div+'" class="video"></div>')
	$('#video'+div).css({position:'absolute', top:'0', left:'0', zIndex:'10'});
	
	var s1 	= new SWFObject('/js/player.swf','player','482','329','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'+div);
	
	return false;			
}