// JavaScript Document

var CreateUniversalPlayer = new Class({
        options: {
				//Div Name Options
				wrapper: $('universalVideoWrapper'),
				videoPreLoader: $('preLoader'),
				videoPlayer: $('universalVideoPlayer'),
				//Basic Options
				playerType: 'Universal',
				playerTitle: 'Cnet Universal Player',
				autoPlay: false,
				externalPlayer: false,
				watchedVideoRotation: false,
				//Lumiere Options
				lumiereQueryType: 'orderBy',
				lumiereQueryValue: 'productionDate+desc',
				//Interface Options
				startVolume: 30,
				expandTabs: false,
				showVideoInfo: false,
				hideTabs: false,
				hideHeader: false,
				useCurrentPageUrl: false,
				userAutoplay: false,
				displayMuteBtn: false,
				//Ad Options
				adInterval: 2,
				preRollAd: false,
				firstVideoPostAd: true,
				contentPostRollAd: false,
				//Related Video Options
				relatedVideo: true,
				externalLinks: false,
				vendorVideo: false,
				//Page Variables
				si: '',//Defined in PageVars
                pt: '',//Defined in PageVars
                nd: '',//Defined in PageVars
                oid: '',//Always needs to be entered by developer
                edid: '',//Defined in PageVars
                br: '',//Always needs to be entered by developer
                ip: '',//Always needs to be entered by developer
                ncat: '',//Always needs to be entered by developer
                ua: '',//Developer should never have to enter this in
				cid: '',
				aid: '',//Defined in PageVars
				//CPN Options
				ePartner: '',
				cpnModule: '',
				refDomain: ''
        },
		
        initialize: function(options){
				this.setOptions(options);
				this.setReferencePaths();
				this.setAutoPlayProperties();
				this.setDisplayProperties();
				this.processPlayerData();
				this.convertPageVars();
				this.buildFlashPlayer(this.playerDimensions.width, this.playerDimensions.height);
				this.playerStartUp();
        },
		
		setDisplayProperties: function(){
				//Set Player and wrapper div styles based on whether or not the header is hidden
				this.options.wrapper.setStyle('text-align', 'left');
				
				if(this.options.hideHeader){
					this.playerDimensions = {
										width: 325,
										height: 320
					};
					
					this.options.videoPreLoader.setStyles({
										background: 'url(http://i.i.com.com/cnwk.1d/i/vid/uPlayerBGsmall.gif) no-repeat',
										height: this.playerDimensions.height+'px',
										width: this.playerDimensions.width+'px',
										padding: '20px 0px 0px 3px',
										position: 'absolute',
										zIndex: '100'
					});	
					
				}else{
					this.playerDimensions = {
										width: 335,
										height: 360
					};
					
					this.options.videoPreLoader.setStyles({
										background: 'url(http://i.i.com.com/cnwk.1d/i/vid/uPlayerBG.gif) no-repeat',
										height: this.playerDimensions.height+'px',
										width: this.playerDimensions.width+'px',
										padding: '55px 0px 0px 8px',
										position: 'absolute',
										zIndex: '100'
					});	
				}
				
				if(this.options.playerType=='Wide'){
					this.playerDimensions = {
										width: 620,
										height: 330
					};
					
					this.options.videoPreLoader.setStyles({
										background: 'url(http://i.i.com.com/cnwk.1d/i/vid/uPlayerWide.gif) no-repeat',
										height: this.playerDimensions.height+'px',
										width: this.playerDimensions.width+'px',
										padding: '55px 0px 0px 7px',
										position: 'absolute',
										zIndex: '100'
					});
					
					this.options.videoPreLoader.setHTML('');
				}
				
				if(this.options.playerType=='Widget'){
					this.playerDimensions = {
										width: 300,
										height: 390
					};
					
					this.options.videoPreLoader.setStyles({
										background: 'url(http://i.i.com.com/cnwk.1d/i/vid/uPlayerWidget.gif) no-repeat',
										height: this.playerDimensions.height+'px',
										width: this.playerDimensions.width+'px',
										padding: '2px',
										position: 'absolute',
										zIndex: '100'
					});
					
					this.options.videoPreLoader.setHTML('');
				}
				
				//Display Pre Loader
				if(this.options.playerType=='Widget'){
					this.options.videoPreLoader.setHTML('<img src="http://i.i.com.com/cnwk.1d/i/vid/uPlayerWidgetLoader.gif" alt="Loading Video Player" />');
				}else{
					this.options.videoPreLoader.setHTML('<img src="http://i.i.com.com/cnwk.1d/i/vid/uPlayerLoader.gif" alt="Loading Video Player" />');
				}
								
		},
		
		setReferencePaths: function(){
			
			if (this.options.externalPlayer == true){
				this.referencePath = {
					basePath: 'http://www.cnet.com'
				}
			}else{
				this.referencePath = {
					basePath:''
				}
			}
		},
		
		setAutoPlayProperties: function(){
			//Check for autoplay defined in URL
			this.options.autoPlay = (window.getQueryStringValue("autoplay")) ? window.getQueryStringValue("autoplay") : this.options.autoPlay;
			
			//Check for autoplay cookie
			if(Cookie.get("userAutoPlay")=="disabled") this.options.autoPlay = false;
		},
		
		processPlayerData: function(){
			//Set the copy URL to the current page URL if this.options.useCurrentPageUrl = true
			if(this.options.useCurrentPageUrl) this.options.useCurrentPageUrl = document.URL;
				
			//Parses the Reviews and Download video xml call and passes it into the player
			if(this.options.playerType=="Reviews" || this.options.playerType=="Download"){
				var reviewsUrl = this.options.lumiereQueryValue.split("-");
				var splitReviewsURL = reviewsUrl[2].split(".");
				this.options.lumiereQueryValue = splitReviewsURL[0];
			}
			
		},
		
		displayUniversalPlayer: function(){
			this.options.videoPlayer.setStyle('visibility', 'visible');
			this.options.videoPreLoader.setStyle('display', 'none');
		},
		
		convertPageVars: function(){
			PageVars.ncat = (this.options.ncat!='') ? this.options.ncat : PageVars.ncat;
			PageVars.userIP = (this.options.ip!='') ? this.options.ip : PageVars.userIP;
			PageVars.siteId = (this.options.si!='') ? this.options.si : PageVars.siteId;
			PageVars.brandId = (this.options.br!='') ? this.options.br : PageVars.brandId;
			PageVars.assetId = (this.options.aid!='') ? this.options.aid : PageVars.assetId;
			PageVars.oid = (this.options.oid!='') ? this.options.oid : PageVars.oid;
			PageVars.editionId = (this.options.edid!='') ? this.options.edid : PageVars.editionId;
			PageVars.nodeId = (this.options.nd!='') ? this.options.nd : PageVars.nodeId;
			PageVars.pageType = (this.options.pt!='') ? this.options.pt : PageVars.pageType;
			PageVars.cid = (this.options.aid!='') ? this.options.aid : PageVars.aid;
		},
		
		buildFlashPlayer: function(width, height){
			
			var MM_contentVersion = 8;
			var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
			if ( plugin ) {
					var words = navigator.plugins["Shockwave Flash"].description.split(" ");
					for (var i = 0; i < words.length; ++i)
					{
					if (isNaN(parseInt(words[i])))
					continue;
					var MM_PluginVersion = words[i];
					}
				var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
			}
			else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0
			   && (navigator.appVersion.indexOf("Win") != -1)) {
				document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
				document.write('on error resume next \n');
				document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
				document.write('</SCR' + 'IPT\> \n');
			}
			
			if (!MM_FlashCanPlay) {
				this.options.videoPreLoader.setStyle('display', 'none');
				this.options.videoPreLoader.setStyle('visibility', 'visible');
				this.options.videoPlayer.setHTML('<div id="ctv_noflash">We&rsquo;ve detected that you need to upgrade or install Flash 8 or later to view CNET TV and our library of thousands of tech videos. Installing Flash is fast, free, and easy! Simply follow the instructions from your browser, or <a href="http://www.macromedia.com/go/getflashplayer">click here to install/upgrade the free Flash player</a>.<br />&nbsp;<br />Thanks,<br />The CNET TV Team</div>');
			}
			
			//Check for live vs. stage ad environment
			var urlString = document.URL;
			
			if(urlString.search(".com:") != -1 || urlString.search(".uat.") != -1 || urlString.search("preview1.") != -1 || urlString.search("dev-server-test.dev.sub.classes") != -1){
				var liveAdEnv = false;
			}else{
				var liveAdEnv = true;
			}			

			
			if(this.options.watchedVideoRotation == true){
				if(Cookie.get("cnetWatchedVideos") != false){
					var cnetWatchedVideos = Cookie.get("cnetWatchedVideos").split(",");
					for(r=0;r<cnetWatchedVideos.length;r++){
						if(cnetWatchedVideos.length > 5){
							cnetWatchedVideos.splice(0,1)
						}
					}
					var enteredVideos = this.options.lumiereQueryValue.split(",");
					
					
					
					for(var i=0;i<cnetWatchedVideos.length;i++){
						var videoCheck = cnetWatchedVideos[i];
						for(var c=0;c<enteredVideos.length;c++){
							if(videoCheck == enteredVideos[c]){
								enteredVideos.splice(c,1);
								enteredVideos.push(videoCheck);
								var videosWatched = true;
							}
						}
					}
					
					var queryValue = (videosWatched == true) ? enteredVideos : escape(this.options.lumiereQueryValue);
				}else{
					var queryValue = escape(this.options.lumiereQueryValue);
				}
			}else{
				var queryValue = escape(this.options.lumiereQueryValue);
			}
			
			if(this.options.playerType=='Wide'){
				var player = new SWFObject(this.referencePath.basePath+"/av/video/flv/newPlayers/widePlayer.swf", "universalplayer", width, height, "8", "#ffffff");
			}else if(this.options.playerType=='Widget'){
				var player = new SWFObject(this.referencePath.basePath+"/av/video/flv/newPlayers/widget.swf", "universalplayer", width, height, "8", "#ffffff");
			}else{
				var player = new SWFObject(this.referencePath.basePath+"/av/video/flv/newPlayers/universal.swf", "universalplayer", width, height, "8", "#ffffff");
			}
			//***Define Parameters***/
			player.addParam("wmode", "transparent");												
			player.addParam("scale", "noscale");
			player.addParam("salign", "lt");
			player.addParam("swLiveConnect", "true");
			player.addParam("allowScriptAccess", "always");
			player.addParam("allowFullScreen", "true");			

			/***Define Variables***/
			player.addVariable("ddom", location.host);
			player.addVariable("parent", true);
			player.addVariable("live", liveAdEnv);
			
			//Basic Options
			player.addVariable("playerType", this.options.playerType);
			player.addVariable("name", this.options.playerTitle);
			player.addVariable("autoplay", this.options.autoPlay);
			player.addVariable("externalPlayer", this.options.externalPlayer);
			//Lumiere Options
			player.addVariable("type", this.options.lumiereQueryType);
			player.addVariable("value", queryValue);
			//Interface Options
			player.addVariable("startVolume", this.options.startVolume);
			player.addVariable("expandTabs", this.options.expandTabs);
			player.addVariable("showVideoInfo", this.options.showVideoInfo);
			player.addVariable("hideHeader", this.options.hideHeader);
			player.addVariable("copyUrl", this.options.useCurrentPageUrl);
			player.addVariable("userAutoplay", this.options.userAutoplay);
			player.addVariable("displayMuteBtn", this.options.displayMuteBtn);
			player.addVariable("hideTabs", this.options.hideTabs);
			//Ad Options
			player.addVariable("adInterval", this.options.adInterval);
			player.addVariable("firstVideoPreRoll", this.options.preRollAd);
			player.addVariable("firstVideoPostRoll", this.options.firstVideoPostAd);
			player.addVariable("contentPostRoll", this.options.contentPostRollAd);
			//Related Video Options
			player.addVariable("relatedVideo", this.options.relatedVideo);
			player.addVariable("externalLinks", this.options.externalLinks);
			player.addVariable("vendorVideo", this.options.vendorVideo);
			//Page Variables
			player.addVariable("si", PageVars.siteId);
			player.addVariable("br", PageVars.brandId);
			player.addVariable("ip", PageVars.userIP);
			player.addVariable("ua", this.options.ua);
			player.addVariable("cid", PageVars.assetId);
			player.addVariable("oid", PageVars.oid);
			player.addVariable("edid", PageVars.editionId);
			player.addVariable("nd", PageVars.nodeId);
			player.addVariable("pt", PageVars.pageType);
			player.addVariable("ncat", PageVars.ncat);
			if (PageVars.guid!=undefined) {
				player.addVariable("guid", PageVars.guid);
			} else {
				try {
					if (PageVars.sentSpecs.guid!=undefined) {
						player.addVariable("guid", PageVars.sentSpecs.guid);
					}
				} catch(e) {
					// can't find a guid
				}
			}
			
			//CPN Options
			player.addVariable("ePartner", this.options.ePartner);
			player.addVariable("cpnModule", this.options.cpnModule);
			player.addVariable("refDomain", this.options.refDomain);
			
			/***Write Player***/
			player.write(this.options.videoPlayer);
				
		},
		
		playerStartUp: function(){
			/*var thisMovie = function(movieName) {
				var isIE = navigator.appName.indexOf("Microsoft") != -1;
				return (isIE) ? window[movieName] : document[movieName];
			}
									
			var startUpInterval = new Number();
			
			window.addEvent('domready', function(){
				startUpInterval = setInterval(startPlayerWhenAvailable, 1000);
			});
									
			var startPlayerWhenAvailable = function() {
				try {
					thisMovie("universalplayer").startUp();
					clearInterval(startUpInterval);
				} catch(error) {
					dbug.log("not ready");
				}
			}*/
		}
			
});

CreateUniversalPlayer.implement(new Options, new Events);

var trackEvent = function(cval) {
	//DW.redir({ ctype: 'event', cval: cval});
	cval = escape(cval);
	var dwUrl = "http://dw.com.com/redir?destUrl=" + escape("http://i.i.com.com/cnwk.1d/b.gif?" + Math.floor(Math.random()*1000));
	dwUrl += "&edid=" + PageVars.editionId;
	dwUrl += "&oid=" + PageVars.oid;
	dwUrl += "&onid=" + PageVars.nodeId;
	dwUrl += "&siteid=" + PageVars.siteId;
	dwUrl += "&ptid=" + PageVars.pageType;
	dwUrl += "&ctype=event";
	dwUrl += "&cval="+ cval;
									
	var dwImg = new Image;
	dwImg.src = dwUrl;
					
	dbug.log("trackEvent: " + "&cval="+ cval );
}