/*
Collection of methods used throughout Playhouse pages
- writeShockwavePlayer: creates Shockwave Player for both NS and IE.
- FYI: cookie functions can be found in http://disney.go.com/detect/scripts/master_flash_full.js
*/

//GLOBALS

// A few variables to help figure out what platform we're on
var dc_ie  = (navigator.appName.toLowerCase().indexOf("microsoft") != -1);
var dc_ns  = (navigator.appName.toLowerCase().indexOf("netscape") != -1);
var dc_win = (navigator.platform.toLowerCase().indexOf("win") != -1);
var dc_mac = (navigator.platform.toLowerCase().indexOf("mac") != -1);
var dc_browserVer = parseFloat(dc_ie ? navigator.appVersion.substring(navigator.appVersion.toLowerCase().indexOf("msie") + 4) : navigator.appVersion);

//END GLOBALS


//DETECT FLASH - functions found in http://disney.go.com/detect/scripts/master_flash_full.js
try{
	CreateInterface();
	DetectFlash();
	}
	catch(e){
		//detect functions not found
	}
//END DETECT FLASH

// define Shockwave player version for writeShockwavePlayer (unless already defined)
if ( SHOCKWAVE_PLAYER_VERSION == null ) {
	var SHOCKWAVE_PLAYER_VERSION = "8,5,1,0";
}

function getQueryString (obj) {
	var args = new Object();
	var qString = obj.location.search.substring(1);
	var argsArray = qString.split("&");

	for (var i = 0; i < argsArray.length; i++)
	{
		var pos = argsArray[i].indexOf('=');
		if (pos == -1) continue;
		var argName = argsArray[i].substring(0, pos);
		var value = argsArray[i].substring(pos + 1);
		args[argName] = unescape(value);
	}
	return args;
}

//This is the global popDisclaimer function for ad served elements
function deliverPopDisclaimer(mLink){
	window.open("http://home.disney.go.com/guestservices/disclaimers/redirect?destination="+mLink);
}

function writeShockwavePlayer (sSrc,  iWidth, iHeight, sID, settings, doc) {

	if ( sSrc == null || iWidth == null || iWidth < 1 || iHeight == null || iHeight < 1 ) {
		// quit if required parameters are not set
		return;
	}
	
	// set defaults for optional parameters
	if ( sID == null ) {
		sID = '';
	}		
	if ( settings == null ) {
		settings = {};
	}		
	if (settings.quality == null ) {
		settings.quality = 'high';
	}
	if (settings.bgcolor == null ) {
		settings.bgcolor = '#FFFFFF';
	}
	
	if (doc == null ) {
		doc = document;
	}
	
	var sPlayer = '';
	var prop;
	
	// define object tag with required parameters
	sPlayer += '<OBJECT id="' + sID + '" classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=' + SHOCKWAVE_PLAYER_VERSION + '" ';
	sPlayer += 'WIDTH="' + iWidth + '" HEIGHT="' + iHeight + '" >';
	sPlayer += '<PARAM NAME="src" VALUE="'+ sSrc +'" />';
	
	// add additional parameters
	for ( prop in settings ) {
		sPlayer += '<PARAM NAME="' + prop + '" VALUE="' + settings[prop] + '" /> ';
	}

	// define embed tag with required parameters
	sPlayer += '<EMBED SRC="' + sSrc + '" WIDTH="' + iWidth + '" HEIGHT="' + iHeight +'" ';
	//sPlayer += 'swliveconnect="FALSE" ';
	sPlayer += 'TYPE="application/x-director" ';
	sPlayer += 'PLUGINSPAGE="http:/'+'/www.macromedia.com/shockwave/download/" ';

	// define optional parameters
	for ( prop in settings ) {
		sPlayer += prop + '="' + settings[prop] + '" ';
	}
	
	// close embed & object tags
	sPlayer += '></EMBED></OBJECT>';

	doc.write (sPlayer);
}

// opens a window and returns the window
popUp = function (newwin, name, url,  options ) {
	// alert ( url +' '+ name +' '+ options);
	if ((newwin == null) || (newwin.closed == true)) {
		newwin = window.open( url, name, options);
	}

	// alert (newwin);
	newwin.focus();
	return newwin;
}

popTermsOfUse = function (  ) {
	if (this.windowTerms != null && !this.windowTerms.closed ) {
		this.windowTerms.close();
		this.windowTerms = null;
	}

	var width = 320;
	var height = 260;
	// positon on screen
	var screenXW = 0;
	var screenYW = 0;

	var options = "width="+width+",height="+height+",left=" + screenXW + ",top=" + screenYW + ",toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,screenX=" + screenXW + ",screenY=" + screenYW;

	var termsUrl = "http://tv.disney.go.com/playhouse/global/legal.html";
	termsUrl += "?width=" + width;
	termsUrl += "&height=" + height;

	this.windowTerms = popUp (this.windowTerms, "TermsOfUse", termsUrl, options );
}

// opens a window
function openNewWindow(URLtoOpen,windowName,windowFeatures) {
	newWindow=window.open(URLtoOpen,windowName,windowFeatures);
}

function aReplace (path) {
	if (path.substr(0,7) != "http://") { //put "http://" at the beginning of the url if it doesn't exist
		path = "http://" + path;
	}

	//UPDATE 030919:  Windows 98 running IE 5 doesn't like the replace function, so we need to check for that combination and use location.href instead
	var windows98 = 0;
	agt = navigator.userAgent.toLowerCase();
	if ((agt.indexOf("win98")!=-1)||(agt.indexOf("windows 98")!=-1)){
		windows98 = 1;
	}

	if (windows98 == 1 && dc_browserVer == 5){
		top.location.href = path;
	}
	else{
		// replace is supported
		if (dc_browserVer>=4 || window.location.replace) {
			window.location.replace(path);
		}
		else {
			top.location.href = path;
		}
	}
}

