var rotator = {
	init:function(){
	
		// center the rotator
		/*var width = $('#controls ul').width();
		var ban_width = $('#banner').width();
		var middle = ( (702 - width) /2) - 14;
		$('#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);		

		$('.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);
				$(this).parent().addClass('video-link');
			}			
		});
		
	},
	
	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();
		
		// remove any video players that are not currently in use
		$('#rotator .video-wrapper').remove();
	},
	
	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();
});

function launchVideo(){
	var ext = $(this).attr('href').split('.');
	var ext = ext[ext.length-1];
	
		var url = $(this).attr('href');

	
	if(rotator.timer) clearTimeout(rotator.timer);
	var div = $(this).parent().attr('id').split('-')[1];

	$(this).parent().append('<div class="video-wrapper"><div id="video'+div+'" class="video">flash video alternate text</div></div>');
	
	var flashvars = { 
		file: url,
		autostart:'true',
		frontcolor: 'ffffff',
		lightcolor: 'cc9900',
		skin: 'http://www.longtailvideo.com/jw/upload/overlay.swf',
		controlbar: 'over'
	};
	
	var params = { wmode:'transparent', allowfullscreen:'true', allowscriptaccess:'true' };
	var attributes = {  };
	
	if($(this).find('img').attr('src')){
		flashvars.image = $(this).find('img').attr('src');
	}
	swfobject.embedSWF("/js/player.swf", "video"+div, "432", "243", "9.0.0", "/js/expressInstall.swf", flashvars, params, attributes);		
	//$('#video'+div).css({position:'absolute', top:'0', left:'0', zIndex:'2000'});
	
	return false;			
}
