﻿/**
 * Creates the LivePlayer "class"
 */
function LivePlayer(){
	this.selectedStream;
	this.swf = "";
	this.lcSuffix = new Date().getMilliseconds();
}

/*************
****EVENTS****
*************/

/**
 * Handles body onload event
 */
LivePlayer.prototype.handleOnLoad = function(){
	fixLoadingNotice();
	this.selectedStream = getQueryParamValue("stream").replace(/stream/gi, "");
	if(this.selectedStream.length == 0 || (this.isNumeric(this.selectedStream) && this.selectedStream < 1 || this.selectedStream > settings.MAXIMUM_STREAM_INDEX)) this.selectedStream = settings.DEFAULT_STREAM_INDEX;
	
	if(settings.FORCE_POPUP && !opener){
		var win = this.openLivePlayer(this.selectedStream);
		if(!win){
			alert("The video window has been blocked by your popup blocker. Please turn off your popup blocker or allow this site and refresh the page.");
		}else{
		}
		return;
	}
	
	if(settings.FORCE_RESIZE) this.resizeWindowTo("NORMAL");
	
	liveplayer.embedPlayer();
	liveplayer.embedImageDecoder();
	
	//if(window.opener) window.opener.location.reload();
}

/*************
****GENERAL***
*************/

LivePlayer.prototype.openLivePlayer = function(index){
	var url = settings.HTML_FILENAME + '?stream=' + index;
	
	return this.openChromelessPopup(url, "liveplayer", settings.WINDOW_SIZES.NORMAL, settings.WINDOW_SIZES.NORMAL);
}

LivePlayer.prototype.openChromelessPopup = function(url, name, width, height){
	var x = window.screen.availWidth/2 - width/2;
	var y = window.screen.availHeight/2 - height/2;
	
	return window.open(url, name, "width="+width+",height="+height+",menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,top=" + y + ",left=" + x + ",", true);
}

/**
 * Embeds the player into the document using swfobject
 */
LivePlayer.prototype.embedPlayer = function(){
	var ignoreOcto = !this.isOctoCapable();
	var version = settings.FLASH_VERSION;
	if(tkutils.isMacOS() && navigator.userAgent.indexOf("10.5") > -1) version = settings.OSX_FLASH_VERSION;
	if(ignoreOcto) version = settings.POWERPC_FLASH_VERSION;
	
	this.swf = new SWFObject(settings.SWF_URL+settings.PLAYER_FILENAME, "LivePlayer", settings.PLAYER_SIZE.width, settings.PLAYER_SIZE.height, version, settings.FLASH_BACKGROUND_COLOR);
    	this.swf.useExpressInstall(settings.SWF_URL+settings.EXPRESS_INSTALL_FILENAME+"?htmlVersionURL="+settings.HTML_VERSION_URL+"&installURL="+settings.ADOBE_INSTALL_PING_URL+"&cancelURL="+settings.ADOBE_CANCEL_PING_URL);
	this.swf.addParam("allowScriptAccess", "always");
	this.swf.addParam("allowFullScreen", "true");
	this.swf.addParam("wmode", "window");
	this.swf.addVariable("lcSuffix", this.lcSuffix);
   	this.swf.addVariable("defaultStream", this.selectedStream);
	this.swf.addVariable("configURL", settings.CONFIG_URL);
	this.swf.addVariable("edition", this.getEdition());
	this.swf.addVariable("geo", this.isGeoRestricted());
	this.swf.addVariable("ignoreOcto", ignoreOcto)
	try{
		this.swf.addVariable("currentTime", cnnCurrTime);
		this.swf.addVariable("currentMinute", cnnCurrMin);
		this.swf.addVariable("currentHour", cnnCurrHour);
		this.swf.addVariable("currentDay", cnnCurrDay);
		this.swf.addVariable("omniTime", cnnOmniTime);
	}catch(e){ /*playing catch*/ }
	
	var isEmbedded = this.swf.write(settings.PLAYER_DIV);
	
	//set the proper div to visible
	this.$(isEmbedded ? settings.PLAYER_PARENT_DIV : settings.PLAYER_NO_FLASH).style.display = "inline";
}

LivePlayer.prototype.embedImageDecoder = function(){
	var decoderSWF = new SWFObject(settings.SWF_URL+settings.IMAGE_DECODER_FILENAME, "ImageDecoder", "1", "1", settings.FLASH_VERSION, settings.FLASH_BACKGROUND_COLOR);
	decoderSWF.addVariable("lcSuffix", this.lcSuffix);
	decoderSWF.write("imageDecoder");
}

/**
 * Verifies the system to see if Octoshape will work properly
 * @return Boolean
 */
LivePlayer.prototype.isOctoCapable = function(){
	var os = navigator.userAgent.toLowerCase();
	return !(os.indexOf("mac") > -1 && os.indexOf("10.3") > -1);
}

/**
 * Gets the CNN edition
 * @return Edition
 */
LivePlayer.prototype.getEdition = function(){
	var domain = document.location.host;
	var edition = domain.replace(".cnn.com", "");
	if(edition == null || edition.length == 0 || settings.EDITIONS.indexOf(edition) == -1) edition = settings.DEFAULT_EDITION;
	return edition;
}

/**
 * DOM element retrieval
 * @param element Dom element name or array of element names
 * @return DOM element reference or array of references
 */
LivePlayer.prototype.$ = function(element){
	if (arguments.length > 1){
		for (var i = 0, elements = [], len = arguments.length; i < len; i++)
			elements.push(this.$(arguments[i]));
		return elements;
	}
	if (typeof element == "string") element = document.getElementById(element);

	return element;
}

/**
 * Validates numbers
 * @return boolean
 */
LivePlayer.prototype.isNumeric = function(value){
	return isNaN(parseInt(value));
}

/*************
******GEO*****
*************/

/**
 * Validates the geo cookie against the GEO_COOKIE_RESTRICTIONS
 * @returns boolean
 */
LivePlayer.prototype.isGeoRestricted = function(){
	//See this doc: http://wikiweb.turner.com/index.php/GeoTargeting
	//Also, to fake cookie values: http://ad8w1.turner.com/cookiepage.cgi
	
	var result = false;
	var cc = '';
	
	if(tkutils.Cookie.canUse()){
		var adDEmas = tkutils.Cookie.read('adDEmas');
		if(tkutils.isNonEmptyString(adDEmas)){
			var parts = adDEmas.split('&');
			if(parts.length >= 5) cc = parts[4];
			else if(parts.length == 1) cc = adDEmas;
		}
	}
	
	if(cc.length > 0){
		var cArray = settings.GEO_COOKIE_RESTRICTIONS.split(',');
		for(var i=0, len = cArray.length; i<len; i++){
			if(cArray[i] == cc){
				result = true;
				break;
			}
		}
	}
	
	return result;
}


/*************
****RESIZE****
*************/

/**
 * Resizes the browser
 * @param type Based on the WINDOW_SIZES types
 */
LivePlayer.prototype.resizeWindowTo = function(type){
	try{
		this.setWindowSize(settings.WINDOW_SIZES[type]);
	}catch(e){
		//alert(e);
	}
}

/**
 * Sets the window size
 * @param size Object in the form of: {height: Number, weight: Number}
 */
LivePlayer.prototype.setWindowSize = function(size){
	var point = this.getWindowPosition();
	var change = false;
	
	if((point.x+size.width) > window.screen.availWidth){
		point.x = window.screen.availWidth-size.width-70; //extra 20 is for padding
		change = true;
	}
	if((point.y+size.height) > window.screen.availHeight){
		point.y = window.screen.availHeight-size.height-70; //extra 20 is for padding
		change = true;
	}
	
	if (change === true){
		// Reposition the window, if needed
		
		window.moveTo(point.x, point.y);
		change = false;
	}
	
	// Resize window.
	window.resizeTo(size.width, size.height);
	
	// Retrieve window size.
	var tempSize = this.getWindowSize();
	// Do we need to resize again?
	if((tempSize.height < size.height) || (tempSize.width < size.width)){
	
		var newSize = {width: (size.width-tempSize.width), height: (size.height-tempSize.height)};
		var point = this.getWindowPosition();
		if((point.x+newSize.width) > window.screen.availWidth) {
			point.x = window.screen.availWidth-newSize.width-50; //extra 20 is for padding
			change = true;
		}
		if((point.y+newSize.height) > window.screen.availHeight){
			point.y = window.screen.availHeight-newSize.height-50; //extra 20 is for padding
				change = true;
		}
		if (change === true){
			
			window.moveTo(point.x, point.y);
			change = false;
		}
		window.resizeBy(newSize.width, newSize.height); 
	}
}

LivePlayer.prototype.getWindowPosition = function(){
	var point = {x: 0, y: 0};
	
	if (navigator.appName.toLowerCase().indexOf("microsoft") !== -1){
		point.x = window.screenLeft;
		point.y = window.screenTop; 
	}else{
		point.x = window.screenX;
		point.y = window.screenY; 
	}
	return point;
}

/**
 * Returns the current window size
 * @return size object in the form of: {height: Number, weight: Number}
 */
LivePlayer.prototype.getWindowSize = function(){
	var size = { width: 0, height: 0 };
	if (navigator.appName.toLowerCase().indexOf("microsoft") !== -1){
		// Retrieve width.
		size.width = document.documentElement.clientWidth;
		// Retrieve height.
		size.height = document.documentElement.clientHeight; 
	}else{
		// Retrieve width.
		size.width = window.innerWidth;
		// Retrieve height.
		size.height = window.innerHeight; 
	}
	return size;
}

//Initialize a class instance
var liveplayer = new LivePlayer();
