//Variables to be set by installers
if(videoFile == null) 		{ alert('MMWalkout: No video file defined, please use variable "videoFile".'); }
if(videoTop == null) 		{ var videoTop 		= '275px'; }
if(videoLeft == null) 		{ var videoLeft 	= '300px'; }
if(videoWidth == null) 		{ var videoWidth 	= '500px'; }
if(videoHeight == null) 	{ var videoHeight 	= '420px'; }

if(playTop== null) 		{ var playTop		= '600px'; }
if(playLeft== null) 		{ var playLeft		= '180px'; }

if(flvSkin == null) 		{ var flvSkin 		= 'MojaveOverNoVol.swf'; }
if(flvMode == null) 		{ var flvMode		= 'transparent'; }
if(flvVolume == null) 		{ var flvVolume 	= '100'; }
if(flvBuggerTime == null) 	{ var flvBuggerTime	= '4'; }
if(flvAutoPlay == null) 	{ var flvAutoPlay 	= 'true'; }
if(flvAutoSize == null) 	{ var flvAutoSize	= 'true'; }
if(flvAspectRatio == null) 	{ var flvAspectRatio	= 'true'; }
if(flvAutoHide == null) 	{ var flvAutoHide	= 'true'; }

if(cookieName == null)		{ var cookieName	= 'DoWalkout'; }
if(cookieDays == null) 		{ var cookieDays	= '7'; }

if(playButton == null)		{ var playButton	= 'play.jpg'; }

//Required variables, do not change
var datasize=70000; 
var startTime=0;
var endTime=0;
var date=0;
var textMessage="";
var doWalkout=0;

//Write basic 3 divs that will handle all the walkout videos
document.write('<div id="walkOut" name="walkOut" style="position:absolute;width:' + videoWidth + ';height:' + videoHeight + ';top:' + videoTop + ';left:' + videoLeft + ';z-index:1;display:none;"><div id="walkoutDiv"></div></div>');
document.write('<div id="walkOutPlay" name="walkOutPlay" style="display:block;position:absolute;width:99px;height:22px;top:' + playTop + ';left:' + playLeft + ';z-index:1;">');
document.write('<a href="javascript:walkoutPlay();"><img border="0" src="/walkout/' + playButton + '" alt="Play Video Again" /></a></div>');

function XHConn() {
	var xmlhttp, bComplete = false;
	
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { xmlhttp = false; }}}
	
	if (!xmlhttp) return null;
	
	this.connect = function(sURL, sMethod, sVars, fnDone) {
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		
		try {
			if (sMethod == "GET") {
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			} else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && !bComplete) {
				if (xmlhttp.status == 200) {
					bComplete = true;
					if (fnDone != '') { fnDone(xmlhttp); }
				} else {
					alert('MMWalkout: XMLHTTP: Communication Error. Please use the refresh button on your browser to try again.');
					return null;
				}
			}};
			xmlhttp.send(sVars);
		}
		catch(z) { return false; }
		return true;
	};
	return this;
} //End Function

var myConn = new XHConn();

if (!myConn) alert('MMWAlkout: XMLHTTP: Unable to establish a connection with the remote server.');

function createWalkoutSWF() {
	 var so = new SWFObject("/scripts/walkout/WalkoutPlayer.swf", "Walkout", videoWidth, videoHeight, "8", "#ffffff");
	 so.addParam("quality", "high");
	 so.addParam("wmode", flvMode);
	 so.addParam("swliveconnect", "true");
	 so.addParam("salign", "t");
	 so.addVariable("FlvFile", 'http://media.mediamarketers.com/' + videoFile);
	 so.addVariable("FlvSkin", '/scripts/walkout/' + flvSkin);
	 so.addVariable("FlvBufferTime", flvBuggerTime);
	 so.addVariable("FlvSkinAutoHide", flvAutoHide);
	 so.addVariable("FlvVolume", flvVolume);
	 so.addVariable("FlvKeepAspectRatio", flvAspectRatio);
	 so.addVariable("FlvAutoSize", flvAutoSize);
	 so.addVariable("FlvAutoPlay", flvAutoPlay);
	 so.write("walkoutDiv");
}  //End Function

function SetCookie(cookieName,cookieValue,nDays) {
	var today		= new Date();
	var expire		= new Date();
	
	if (nDays==null || nDays==0) nDays=1;
	
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}  //End Function

function ReadCookie(cookieName) {
	var theCookie	= "" + document.cookie;
	var ind		= theCookie.indexOf(cookieName);
	
	if (ind==-1 || cookieName=="") return ""; 
	var ind1		= theCookie.indexOf(';',ind);
	
	if (ind1==-1) ind1=theCookie.length; 
	
	return unescape(theCookie.substring(ind + cookieName.length + 1,ind1));
}  //End Function

function walkoutPlay() { //If a user clicks on the play button for the walkout video
    document.getElementById('walkOut').style.display = 'block';
    document.getElementById('walkOutPlay').style.display = 'none';
    createWalkoutSWF();
} //End Function

function Walkout_DoFSCommand(command, args) {
	if(command == "hideWalkout") {
		document.getElementById("walkOut").style.display = 'none';
		document.getElementById("walkOutPlay").style.display = 'block';
	}
} //End Function

function okayToRun() { //Read the cookies to make sure it's okay to play
	if (ReadCookie(cookieName) == "1" || ReadCookie(cookieName) == "") {
		return true;
	} else {
		return false;
	} //End If
} //End Function

function runBandwidthTest() { //Check to see if the user is able to view video
	if(myConn) {
		date = new Date();
		startTime=date.getTime();
		myConn.connect('/walkout/walkout.bandwidth.test.txt', 'GET', 'a=test', runBandwidthTestFinished);
	    date = new Date();
	} else {
		return false; //Return that the user can't view the video, because we can't connect to the XMLHTTP
	} //End If
} //End Function

function runBandwidthTestFinished() {
	endTime=date.getTime();
	
    var diffTimeMilliseconds = endTime - startTime;
    var diffTimeSeconds = diffTimeMilliseconds/1000;
    var bits = (datasize*8);   // convert Bytes to bits,
    var kbits = bits/1024;     // convert bits to kbits
    var throughput = kbits/(diffTimeSeconds);
    throughput = throughput * .93;  // account for IP packet header overhead - averages about 7%
    
     if (throughput > 568) {
    	doWalkout = 1;
    	
		if(ReadCookie(cookieName) == "1") {
			SetCookie(cookieName, "2", cookieDays);
		} //End If
	
		if(ReadCookie(cookieName) == "") {
			SetCookie(cookieName, "1", cookieDays);
			walkoutPlay();
		} //End If

    } //End If
} //End Function

if(okayToRun() == true) { runBandwidthTest(); }