/*
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

// define Shockwave player version for writeShockwavePlayer (unless already defined)
if ( SHOCKWAVE_PLAYER_VERSION == null ) {
	var SHOCKWAVE_PLAYER_VERSION = "8,5,1,0";
}

//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);
}

//set the cookie
function setCookie(name, value, expires, path, domain, secure) {
	// name - name of the cookie
	// value - value of the cookie
	// [expires] - expiration date of the cookie (defaults to end of current session if set to null) Example of date code: "var expdate = new Date ();  expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000*20)); //expires 20 days from now"
	// [path] - path for which the cookie is valid (defaults to path of calling document)
	// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
	// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
	// * an argument defaults when it is assigned null as a placeholder
	// * a null placeholder is not required for trailing omitted arguments

	var curCookie = name + "=" + escape(value) +
	  ((expires) ? "; expires=" + expires.toGMTString() : "") +
	  ((path) ? "; path=" + path : "") +
	  ((domain) ? "; domain=" + domain : "") +
	  ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

//function to read the cookie
function getCookie(name) {
	// name - name of the desired cookie
	// * return string containing value of specified cookie or null if cookie does not exist
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}


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);
}
