/* EMBEDDED VIDEO JS */

/* ------------------------------------------------------------------------ set variables */
// urls and window options for those urls
var prefsURL = "/common/apps/mediaplay/options.html";
var prefsOptions = "width=500,height=377,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0";
var detectURL = "http://www.discovery.com/utilities/techtools/detect.html";
var detectOptions = "width=688,height=436,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0";

// expiration date for the cookie [ (milliseconds) * (seconds) * (minutes) * (hours) * (days) ]
var thisDay = new Date();
var die = new Date();
die.setTime(thisDay.getTime() + 1000*60*60*24*365);

// are players installed ?
if (pluginlist.indexOf("Windows Media Player") != -1) { var wmpCheck = true; }
if (pluginlist.indexOf("RealPlayer") != -1) { var rpCheck = true; }



/* ------------------------------------------------------------------------ parent / prefs / detect [functions] */
// play video
function play(pageURL, mediaType, width, height, scroll) {
	// get the cookie
	var cookieValue = getCookie("videoPreferences");	
	
	// if no cookie launch options window to set it
	// else do series of checks: [1] launch detect plugins popup to install player, [2] launch video options to change cookie, [3] launch video popup
	if (!cookieValue) {
		var loc = window.location.pathname;
		var pos = loc.lastIndexOf("/");
		var url = loc.substring(0,pos+1) + pageURL;
		window.open(prefsURL + "?type=" + mediaType + "&page=" + url + "&w=" + width + "&h=" + height + "&s=" + scroll, "SetMediaPreferences", prefsOptions);
	}
	else {
		// pull data from cookie and split them into [2] variables
		valueArray = cookieValue.split("|");
		var mediaPrefs = valueArray[0];
		var conSpeed = valueArray[1];
		
		if (conSpeed == "") { window.open(prefsURL + "?type=" + mediaType + "&page=" + url, "SetMediaPreferences", prefsOptions); }
		else {
			// set connection speed variable
			loSpeed = new Array("28", "56", "64", "128");
			for (i = 0; i < loSpeed.length; i++) {
				if (conSpeed == loSpeed[i]) { conSpeed = "lo"; break; }
			}		
			hiSpeed = new Array("350", "400", "t1", "lan");
			for (i = 0; i < hiSpeed.length; i++) {
				if (conSpeed == hiSpeed[i]) { conSpeed = "hi"; break; }
			}
			
			// pull variables from [/common/js/browsers.js] / browser = [Internet Explorer] or else / OS = [Windows] or else
			var theBrowser = browser;
			var theOS = OS;
			
			// set URLS
			var hirpURL = pageURL + "_hirp.html";
			var hiwmpURL = pageURL + "_hiwmp.html";
			var lorpURL = pageURL + "_lorp.html";
			var lowmpURL = pageURL + "_lowmp.html";
			
			// set popup window options
			if (width) { var hiOptions = "width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scroll + ",resizable=0"; }
			else { var hiOptions = "width=640,height=516,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0"; }
			
			if (width) { var loOptions = "width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scroll + ",resizable=0"; }
			else { var loOptions = "width=560,height=441,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0"; }
						
			// do tests and launch player popup or detect page
			if (conSpeed == "hi") {
				if (mediaPrefs == "wmp" && (mediaType == "both" || mediaType == "wmp")) {
					if (wmpCheck) { window.open(hiwmpURL, "VideoPlayer", hiOptions); }
					else { window.open(detectURL, "DetectPopup", detectOptions); }
				} else {
					if (rpCheck) { window.open(hirpURL, "VideoPlayer", hiOptions); }
					else { window.open(detectURL, "DetectPopup", detectOptions); }
				}
			} else {
				if (mediaPrefs == "wmp" && (mediaType == "both" || mediaType == "wmp")) {
					if (wmpCheck) { window.open(lowmpURL, "VideoPlayer", loOptions); }
					else { window.open(detectURL, "DetectPopup", detectOptions); }
				} else {
					if (rpCheck) { window.open(lorpURL, "VideoPlayer", loOptions); }
					else { window.open(detectURL, "DetectPopup", detectOptions); }
				}
			}
		}
	}
}

// change video preferences
function changeVideoPrefs() {
	window.open(prefsURL, "SetMediaPreferences", prefsOptions);
	return false;
}

// set video preferences cookie
function setPrefs(choice) {
	for (i=0; i<choice.player.length; i++) {
		if (choice.player[i].checked) { break; }
	}
	
	var picked = choice.player[i].value + "|" + choice.speed.options[choice.speed.selectedIndex].value;
	setCookie("videoPreferences", picked, die, "/");
	
	var pageURL = getParam("page");
	var mediaType = getParam("type");
	var theW = getParam("w"); var theH = getParam("h"); var theS = getParam("s");
	
	if (theW != "") { if (pageURL) { play(pageURL, mediaType, theW, theH, theS); } }
	else { if (pageURL) { play(pageURL, mediaType); } }
	
	window.close();
}



/* ------------------------------------------------------------------------ video player [functions] */
// open / close (cycle-through) "more" absolutely positioned info panels
function openSlide (s) {
	if (document.getElementById) { document.getElementById(s).style.visibility = "visible"; }
	else { document.layers[s].visibility = "show"; }
}

function closeSlide (s) {
	if (document.getElementById) { document.getElementById(s).style.visibility = "hidden"; }
	else { document.layers[s].visibility = "hide"; }
}

/* END EMBEDDED VIDEO JS */
