var RotatingVideo = {
	
	current: 0, // current video that is playing
	videos: [], // list of videos to play
	
	/*
		Start up the video rotation
	*/
	start: function() {
		// randomize the order:
		RotatingVideo.videos.sort(function() {
			return (0.5 - Math.random());
		});
		
		// display the first video to the screen:
		RotatingVideo.display(true);
	},
	
	/*
		Choose a video to play... but when called from HTML/JS
		
			i: the index of the video selected (zero-based index)
	*/
	choose: function(i) {
		RotatingVideo.current = i;
		
		// the SWF's playlist includes the prerolls ads, but the JS
		// playlist does not.  We need to manipulate the index to 
		// make sure that we load up the proper video, but with
		// a preroll playing first
		//var index = (i * 2) - 1;
		//if(index <= 0) index = 0;
		
		var index = i;
		
		RotatingVideo.getSWF().service('choose', {index: index});
	},
	
	/*
		Display the next video in the play list.  If we do not have
		any more videos in the list, then stop playing videos.
	*/
	next: function() {
		if(RotatingVideo.current + 1 >= RotatingVideo.videos.length) {
			RotatingVideo.current = 0;
			// we are only going through the videos once... do not
			// start the videos again.  Let the user do that if
			// they want to.  We will put the first video
			// back in the player:
			RotatingVideo.choose(RotatingVideo.current);
			return;
		}
		
		RotatingVideo.current++;
		RotatingVideo.choose(RotatingVideo.current);
	},
	
	/*
		Display the video player (and start the videos)
	*/
	display: function(autoStart) {		
		var flashvars = {
			method: 'json',
			externalService: 'RotatingVideo.externalService',
			dontHideControls: true
		};
		var params = {
			allowFullScreen: true,
			allowScriptAccess: 'always',
			bgcolor: '#000000'
		};
		
		swfobject.embedSWF(
			'/static/swf/VideoPlayer.swf',
			'video-area', 
			'100%',
			'100%',
			"10.0.0",
			"", 
			flashvars, 
			params
		);
	},
	
	externalService: function(service, data) {		
		if(service == 'playerReady') {
			RotatingVideo.setPlaylist();
		}
	},
	
	setPlaylist: function() {
		
		var playlist = {
			autostart:true,
			locale: 'en',
			still: '',
			playlist: []
		}
		
		for(var i = 0, len = RotatingVideo.videos.length; i < len; i++) {
			var v = {
				type: 'video',
				secure: true,
				filename: RotatingVideo.videos[i].filename
			};
			
			if(RotatingVideo.videos[i].url != '') {
				v.clickURL = RotatingVideo.videos[i].url;
			}
			
			playlist.playlist.push(v);
			
			//playlist.playlist.push({
			//	type: 'preroll',
			//	prerollType: 'short',
			//	zone: 'atnimasdktest',
			//	site: 'sri.testing'
			//});
		}
		
		// remove the last preroll, as we are only running prerolls _between_ videos
		playlist.playlist.pop();
		
		RotatingVideo.getSWF().service('setPlaylist', playlist);
	},
	
	getSWF: function() {
		return $j('#video-area')[0];
	},
	
	/*
		Build the filename string that will be used to pass to the SWF.
		
		The SWF expects the video filesnames to be separated by a comma:
			ex: 'video1,video2,video3'
	*/
	buildFilename: function() {
		if(RotatingVideo.videos[RotatingVideo.current]) {
			return RotatingVideo.videos[RotatingVideo.current].filename;
		}
		
		return '';
	},
	
	buildClickTag: function() {
		if(RotatingVideo.videos[RotatingVideo.current]) {
			return RotatingVideo.videos[RotatingVideo.current].url;
		}
		
		return '';
	},
	
	buildStill: function() {		
		if(RotatingVideo.videos[RotatingVideo.current]) {
			return RotatingVideo.videos[RotatingVideo.current].image;
		}
		
		return '';
	}
	
};

