/*
Collection of methods used throughout Disney Channel pages
- writeFlashPlayer: creates Flash Player for both NS and IE, requires to define parameters only once
- popUp : create popup window
- popSendToFriend : create popup for send to friend, populates with emailPage.swf/xml
- writeShockwavePlayer: creates Shockwave Player for both NS and IE.
*/

//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 Flash player version for writeFlashPlayer (unless already defined)
if ( FLASH_PLAYER_VERSION == null ) {
	var FLASH_PLAYER_VERSION = "8,0,0,0";
}

// define Shockwave player version for writeShockwavePlayer (unless already defined)
if ( SHOCKWAVE_PLAYER_VERSION == null ) {
	var SHOCKWAVE_PLAYER_VERSION = "8,5,1,0";
}


var windowSendToFriend;

function getVar (search_str, varName ) {
	var value = "";

	if ( search_str == null || varName == null ) {
	 return value;
	}
	var str = (new String (search_str) ).toLowerCase();
	var variable = (new String ( varName ) ).toLowerCase();

	var pos = str.indexOf ( variable );
	if ( pos == -1 ) {
		// variable not found
		return value;
	}

	var startPos = str.toLowerCase().indexOf ( "=", pos ) + 1;
	var endPos = str.toLowerCase().indexOf ( "&", pos );
	if (endPos == -1 ) {endPos = str.length;}

	value = (new String (search_str) ).substring ( startPos, endPos );

	return value;
}

//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 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
//taken from http://disney.go.com/sponsors/javascript/sponsorsGlobal.js
var mLinkDest = "", popDisclaimer;
function sendToClientSide(){window.open(mLinkDest);popDisclaimer.window.focus();}
function deliverPopDisclaimer(mLink){
	mLinkDest = mLink;
	popDisclaimer=window.open("","disclaimer","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=370,height=250,top=100,left=150,screenX=180,screenY=100");
	popDisclaimer.document.write ('<html><head><title>Disney.com - Disclaimer</title>');
	popDisclaimer.document.write ('<scr'+'ipt type="text/javascript" language="javascript">');
	popDisclaimer.document.write ('closeTimer = setT'+'imeout("window.close()",18000);');
	popDisclaimer.document.write ('</scr'+'ipt>');
	popDisclaimer.document.write ('</head><body bgcolor="#FFFFFF" text="#0065CE" link="#0065CE" vlink="#0065CE" alink="#0065CE" leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>');
	popDisclaimer.document.write ('<table width="370" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="http://disney.go.com/sponsors/images/disclaimer_top.gif" width="370" height="27" alt="" border="0"></td></tr><tr><td><table width="330" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td><br><font face="Verdana,sans-serif" size="2" color="#0065CE"><b>The web site you are about to link to is not controlled by Disney Online and different terms of use and privacy policy will apply. By proceeding you agree and understand that Disney Online is not responsible for the site you are about to access.<br><br>');
	popDisclaimer.document.write ('If your page does not load within a few seconds, please <a href="javascript:window.open(\''+mLink+'\',\'popup\',\'height='+parseInt(screen.availHeight-150)+',width='+parseInt(screen.availWidth-50)+',left=0,top=0,location=yes,directories=yes,menubar=yes,resizable=yes,scrollbars=yes,toolbar=yes,status=yes\');void(0);self.close();">click here</a> to open it.</b></font><br><br></td></tr></table>');
	popDisclaimer.document.write ('</td></tr><tr bgcolor="#0000CC"><td align="center" height="20"><a href="javascript:window.close();"><font face="Verdana,sans-serif" size="1" color="#FFFF00"><b>Close this window</b></font></a></td></tr></table>');
	popDisclaimer.document.write ('</body></html>');
	popDisclaimer.document.close();
	sendTimer = setTimeout("sendToClientSide()",1500);
}

function writeFlashPlayer (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 (settings.allowScriptAccess == null ) {settings.allowScriptAccess = 'always';}
	if (doc == null ) {doc = document;}

	var fPlayer = '';
	var prop;

	// define object tag with required parameters
	fPlayer += '<OBJECT id="' + sID + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + FLASH_PLAYER_VERSION + '" ';
	fPlayer += 'WIDTH="' + iWidth + '" HEIGHT="' + iHeight + '" >';
	fPlayer += '<PARAM NAME=movie VALUE="'+ sSrc +'">';

	// add additional parameters
	for ( prop in settings ) {
		fPlayer += '<PARAM NAME="' + prop + '" VALUE="' + settings[prop] + '"> ';
	}

	// define embed tag with required parameters
	fPlayer += '<EMBED name="' + sID + '" src="' + sSrc + '"  WIDTH="' + iWidth + '" HEIGHT="' + iHeight +'" ';
	//fPlayer += 'swliveconnect="True" ';
	fPlayer += 'TYPE="application/x-shockwave-flash" ';
	fPlayer += 'PLUGINSPAGE="http:/'+'/www.macromedia.com/go/getflashplayer" ';

	// define optional parameters
	for ( prop in settings ) {
		if (prop != "wmode"){
			fPlayer += prop + '="' + settings[prop] + '" ';
		}
		else{
			if (settings[prop].toLowerCase() != "opaque" && settings[prop].toLowerCase() != "transparent"){ //we need to remove the opaque and transparent parameter from the embed tag
				fPlayer += prop + '="' + settings[prop] + '" ';
			}
		}
	}
	// close embed & object tags
	fPlayer += '></EMBED></OBJECT>';

	doc.write (fPlayer);
}

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;
}

popSendToFriend = function () {

	if (this.windowSendToFriend != null && !this.windowSendToFriend.closed ) {
		this.windowSendToFriend.close();
		this.windowSendToFriend = null;
	}

	var width = 320;
	var height = 240;
	// positon on screen
	var screenXW = screen.availWidth - width-10;
	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 pageUrl  = escape (self.location);
	var pageName = escape (self.document.title);

	var emailUrl = "http://www.disney.go.com/disneychannel/global/emailPage.html";
	emailUrl += "?pageUrl=" + pageUrl;
	emailUrl += "&pageName=" + pageName;
	emailUrl += "&width=" + width;
	emailUrl += "&height=" + height;

	this.windowSendToFriend = popUp (this.windowSendToFriend, "SendToFriend", emailUrl, options );

}

popTermsOfUse = function (  ) {
	if (this.windowTerms != null && !this.windowTerms.closed ) {
		this.windowTerms.close();
		this.windowTerms = null;
	}

	var width = 380;
	var height = 320;
	// 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://www.disneylatino.com/DisneyChannel/summersite/High_School_Musical_2/videos/popup/pop_up.html";
	termsUrl += "?width=" + width;
	termsUrl += "&height=" + height;

	this.windowTerms = popUp (this.windowTerms, "TermsOfUse", termsUrl, options );
}

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;
		}
	}
}

function nsRedirect(path) { //called when Netscape has trouble redirecting.

	if (parseInt(dc_browserVer) <= 4) {
		nsInterceptPage = "http://www.disney.go.com/disneychannel/global/nsIntercept.html?query=" + escape(path);

		aReplace(nsInterceptPage);
	}
	else {
		aReplace(path);
	}
}