var Video = {	
	detect: function(type){
		var fVersion = Browser.Plugins.Flash.version;
		var fBuild = Browser.Plugins.Flash.build;

		if (fVersion >= 9) {
			this.versionApproved = true;
			this.build(type);
		} else if (fVersion >= 6 && fBuild >= 65) {
			this.install();
		} else {
			$(this.container).set('html','You need the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Plugin</a> to view this video.');
		}
	},
	
	install: function(){
		var playerType = (Browser.Engine.trident === true) ? "ActiveX" : "PlugIn";
		
		new Swiff('http://image.com.com/gamespot/images/cne_flash/production/express/playerProductInstall.swf', {
			id: 'proteus-player',
			width: 480,
			height: 420,
			container: 'video-player',
			params: {
				bgcolor: '#000000'
			},
			vars: {
				MMredirectURL: window.location,
				MMplayerType: playerType,
				MMdoctitle: document.title
			}
		});
	},
	
	build: function(type){
		if (type == 'embed') {
			this.embed.width = (this.embed.width) ? this.embed.width : 450;
			this.embed.height = (this.embed.height) ? this.embed.height : 440;
			this.embed.container = (this.embed.container) ? this.embed.container : 'video-player';
			
			new Swiff(this.embed.swf, {
				id: 'embed-player',
				container: this.embed.container,
				width: this.embed.width,
				height: this.embed.height,
				params: this.embed.params,
				vars: this.embed.vars
			}); 
		} else {
			this.width = (this.width) ? this.width : 480;
			this.height = (this.height) ? this.height : 434;
			this.container = (this.container) ? this.container : 'video-player';
			this.autoplay = (this.autoplay) ? this.autoplay : 'true';
			this.ads = (this.ads) ? this.ads : 'all';
			this.ad_freq = (this.ad_freq) ? this.ad_freq : '3';
			this.clip = (this.clip) ? this.clip : '';
			this.cs_id = (this.cs_id) ? this.cs_id : '';		

			new Swiff('http://image.com.com/gamespot/images/cne_flash/production/media_player/proteus/bnet/proteus2_bnet.swf', {
				id: 'proteus-player',
				container: this.container,
				width: this.width,
				height: this.height,
				params: {
					wmode: 'transparent',
					allowFullScreen: 'true',
					allowScriptAccess: 'always',
					bgcolor: '#000000'
				},
				vars: {
					playerMode: 'in_page',
					allowFullScreen: 1,
					flavor: '480Version',
					showOptions: 0,
					skin: this.skin,
					autoPlay: this.autoplay,
					movieAspect: '16.9',
					embeddingAllowed: 'true',
					clockColor: this.clockColor,
					chromeColor: this.chromeColor,
					marqueeColor: this.marqueeColor,
					ads: this.ads,
					ad_freq: this.ad_freq,
					clipURI: this.clip,
					cs_id: this.cs_id,
					paramsURI: this.params
				}
			}); 
		}
	},
	
	updatePlaylist: function(vidId,newBuild,type){
		if (newBuild) {
			if (this.versionApproved) {
				if (type != 'embed') {
					this.getParams(vidId);
				} else {
					this.getEmbedParams(vidId);
				}
				this.build(type);
			} else {
				this.detect();
			}
		}
		this.nowPlaying(vidId);
	},
	
	nowPlaying: function(vidId){
		if ($('now-playing')) {
			$('now-playing').dispose();
			var npExists = true;
		}
		$('video-playlist').getElement('.on').removeClass('on');
		
		var selected = this.getEl(vidId);
		if(npExists) {
			new Element('em', {'id': 'now-playing','text': 'Now Playing'}).inject(selected,'top');
		}
		selected.addClass('on');
	},
	
	initPlaylist: function(){
		this.watchCount = 0;
		$$('#video-playlist li').each(function(el){
			var type = (el.hasClass('embedded')) ? "embed" : "";
			el.getElements('a').each(function(ela){
				if (!ela.hasClass('external')) {
					ela.addEvent('click', function(e){
						e = new Event(e).stop();
						this.watchCount++;
						this.updatePlaylist(this.getId(el),true,type);
					}.bind(this));
				}
			}.bind(this));
		}.bind(this));	
	},
	
	getParams: function(vidId){
		//gotta rebuild params when updating playlist
		var ps1 = this.params.split('?');
		var ps2 = ps1[1].split('&');
		var paramsUrl = ps1[0] + "?";
	
		if (ps2.length) ps2.each(function(val) {
			if (val.match('playlist')) {
				var ps3 = val.split('=');
				var playlistIds = ps3[1].split(',').erase('');	
				var oldIds = playlistIds.slice(0,playlistIds.indexOf(vidId));
				var newIds = playlistIds.slice(playlistIds.indexOf(vidId));
				this.newPlaylistIds = newIds.extend(oldIds); //move old ids to the back of array
				playlistVal = val;	
			} else if (val.match('ads')) {
				adsVal = val;
			}
		}.bind(this));
		
		ps2.erase(playlistVal);
		ps2.erase(adsVal);
		
		var ads;
		if (this.watchCount < this.ad_freq) {
			//set to 0 to remove ads
			ads = 'ads=' + this.ads;
		} else {
			ads = 'ads=' + this.ads;
			this.watchCount = 0;
		}

		var staticParams = '&' + ps2.join('&');
		var newPlaylist = "playlist=" + this.newPlaylistIds.join(',') + '&';
		this.params = paramsUrl + newPlaylist + ads + staticParams;
	},

	getEmbedParams: function(vidId){
		this.embed  = eval('embed' + vidId);
	},	
	
	getId: function(el){
		//return video id from id - slice off 'playlist-'
		var vidId = el.get('id').slice(9);
		return (vidId);
	},
	
	getEl: function(vidId){
		//return element from video id
		var elName = 'playlist-' + vidId;
		return($(elName));
	}	
};

// these vars must be getting called in the swf
var proteus = 'geez';
var player = 'lousie';



/*
Script: Transcript.js
*/

var Transcript = new Class({
	Implements: Options,
	options: {
		buildLink: false,
		include: '/include/video-transcripts.php'
	},
	
	initialize: function(container,params,options){
		this.setOptions(options);
		this.container = $(container);
		this.file = this.options.include + '?url=' + escape(params);	
		if(this.options.buildLink) this.buildLink();
	},
	
	buildLink: function(){
		var linkContainer =  new Element ('p', {
			'class': 'transcript'
		}).inject(this.container,'after');
		
		var load = this.load.bind(this);
		
		this.link = new Element ('a', {
			'html': 'Transcript',
			'events': {
				'click': function(){
					load();	
				}
			}
		});
		
		this.link.inject(linkContainer,'bottom');
	},
	
	load: function(){
		if (typeof(CNB) != 'undefined') {
			var transcriptCont = new Element('div');

			var request = new Request.HTML({
				url: this.file,
				update: transcriptCont,
				onComplete: function() {
					new CNB.Layered(transcriptCont,{
						title: 'Transcript',
						className: 'layered-transcript'
					});
				}
			}).send();		
		} else {
			if ($('transcript')) this.remove();
		
			this.shell = new Element ('div', {
				'id': 'transcript'
			}).inject($(document.body),'bottom');
	
			var inner = new Element ('div', {
				'class': 'inner'
			}).inject(this.shell,'top').load(this.file);
		
			this.drag();
			this.close();
		
			new Position(this.shell,this.link);
		}
	},

	drag: function(){
		var tHandle = new Element ('h4', {
			'id': 'tHandle',
			'html': 'Full Transcript',
			'styles': {
				'cursor': 'move'
			}
		}).inject(this.shell,'top');
		new Drag(this.shell, {handle: tHandle} );
	},

	close: function(){
		var that = this;
		new Element ('a', {
			'class': 'close',
			'html': 'Close',
			'events':{
				'click': function(){ 
					that.remove(); 
				}
			}
		}).inject(this.shell,'bottom'); 
	},

	remove: function(){
		$('transcript').destroy();
	}

});

