function tellerror(msg, url, linenumber){
	errorCount += 1;
	writeDebug('<font color=red><BR>Error message= '+msg+'\nURL= '+url+'<BR>Line Number= '+linenumber + '</font>');
	return true;
}
window.onerror=tellerror;

/*--------------------------------------------------------*/
/*---------Utility Functions------------------------------*/


// Different playerStates...
// 1 - Playing
// 2 - Stopped
// 3 - Buffering
// 4 - Paused
// 5 - Closed
// 6 - Error

// Different playerAbility's
// false -- we can't speak with it
// true -- can use javascript

// Different buttonStates:
// play, stop, pause, ffw, rew


// Browser detection, image swapper, preload images

function Is() {
    var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.ns  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.ns2 = (this.ns && (this.major == 2));
    this.ns3 = (this.ns && (this.major == 3));
    this.ns4b = (this.ns && (this.minor < 4.04));
    this.ns4 = (this.ns && (this.major >= 4) && (this.major < 5));
	this.ns5 = (this.ns && (this.major >= 5));
    this.ie   = (agent.indexOf("msie") != -1);
    this.ie3  = (this.ie && (this.major == 2));
    this.ie4  = (this.ie && (this.major >= 4));
	this.ie6 = (this.ie && (this.major >= 6));
	this.ie7 = (this.ie && (this.major >= 7));
    this.op3 = (agent.indexOf("opera") != -1);
	//alert("" + this.major + " " + this.ns4 + " " + this.ns5);
	this.win   = ( (agent.indexOf("win")!=-1) || (agent.indexOf("16bit")!=-1) ); 
    // NOTE: On Opera 3.0, the userAgent string includes "Windows 95/NT4" on all 
    //        Win32, so you can't distinguish between Win95 and WinNT. 
    this.win95 = ((agent.indexOf("win95")!=-1) || (agent.indexOf("windows 95")!=-1)); 

    // is this a 16 bit compiled version? 
    this.win16 = ((agent.indexOf("win16")!=-1) || 
                  (agent.indexOf("16bit")!=-1) || (agent.indexOf("windows 3.1")!=-1) || 
                  (agent.indexOf("windows 16-bit")!=-1) ); 

    this.win31 = ((agent.indexOf("windows 3.1")!=-1) || (agent.indexOf("win16")!=-1) || 
                  (agent.indexOf("windows 16-bit")!=-1)); 

    // NOTE: Reliable detection of Win98 may not be possible. It appears that: 
    //       - On Nav 4.x and before you'll get plain "Windows" in userAgent. 
    //       - On Mercury client, the 32-bit version will return "Win98", but 
    //         the 16-bit version running on Win98 will still return "Win95". 
	this.winxp = ((agent.indexOf("winnt5.1")!=-1) || (agent.indexOf("windows nt 5.1")!=-1)); 
    this.win98 = ((agent.indexOf("win98")!=-1) || (agent.indexOf("windows 98")!=-1)); 
    this.winnt = ((agent.indexOf("winnt")!=-1) || (agent.indexOf("windows nt")!=-1)); 
    this.win32 = ( this.win95 || this.winnt || this.win98 || 
                   ((this.major >= 4) && (navigator.platform == "Win32")) || 
                   (agent.indexOf("win32")!=-1) || (agent.indexOf("32bit")!=-1) ); 

    this.os2   = ((agent.indexOf("os/2")!=-1) || 
                  (navigator.appVersion.indexOf("OS/2")!=-1) || 
                  (agent.indexOf("ibm-webexplorer")!=-1)); 

    this.mac    = (agent.indexOf("mac")!=-1); 
    this.mac68k = (this.mac && ((agent.indexOf("68k")!=-1) || 
                               (agent.indexOf("68000")!=-1))); 
    this.macppc = (this.mac && ((agent.indexOf("ppc")!=-1) || 
                               (agent.indexOf("powerpc")!=-1))); 


    this.vms   = ((agent.indexOf("vax")!=-1) || (agent.indexOf("openvms")!=-1)); 


}

var is = new Is();

if(is.ns4) {
    doc = "document";
    sty = "";
    htm = ".document"
    xpos = "e.contentX";
    ypos = "e.contentY";
} else if(is.ie4) {
    doc = "document.all";
    sty = ".style";
    htm = ""
    xpos = "event.x";
    ypos = "event.y";
}

function getObjectRef(objectName,subcontainer,container){



	if(document.all)
		return eval("document.all." + objectName);
		
	if (document.implementation &&document.implementation.hasFeature && document.implementation.hasFeature("html", "1.0")) 
		return document.getElementById(objectName);

	//alert(objectName.substring(1,2));
		
	//if(objectName == 'lyPlay'){
		//writeDebug("getting play");
		//alert("hi" + document['lyPlay']);	
	//}
	
	if(objectName.substring(0,2) == "ly")
		return eval("document['" + objectName + "']");

	
	if(objectName == "mmMedia")
		return eval("getObjectRef('lyMedia').document.embeds['mmMedia']");

	if(objectName == "imLoading")
		return eval("document['lySplash'].document.imLoading");
		
	if(objectName == "imLoadingBar")
		return eval("document['lySplash'].document.imLoadingBar");	
		
	if(objectName == "imRew")
		return eval("document['lyRew'].document.imRew");
		
	if(objectName == "imFfw")
		return eval("document['lyFfw'].document.imFfw");
	
	if(objectName == "imStop")
		return eval("document['lyStop'].document.imStop");
	
	if(objectName == "imPause")
		return eval("document['lyPause'].document.imPause");
	
	if(objectName == "imPlay")
		return eval("document['lyPlay'].document.imPlay");
		
	if(objectName == "imPrevSlide")
		return eval("document['lyPrevSlide'].document.imPrevSlide");
		
	if(objectName == "imNextSlide")
		return eval("document['lyNextSlide'].document.imNextSlide");
		
	if(objectName == "imFlag")
		return eval("getObjectRef('lyFlag').document.imFlag");
	
	if(objectName == "inSlideList")
		return eval("getObjectRef('lySlideList').document.forms[0]");	
	
	if(objectName == "inCurrentSlide")
		return eval("getObjectRef('inSlideList').elements[0]");
		
	if(objectName == "imSlide")
		return eval("document['lySlide'].document.images[0]");
		
	if(objectName == "imTracker")
		return eval("document['lyTracker'].document.images[0]");
		
	if(objectName == "imPlayerIcon")
		return eval("document['lySwitchMedia'].document.images[0]");
		
	if(container != null && subcontainer != null)
		return eval("document['"+subcontainer+"'].document."+container+".document."+objectName);
		
	if(subcontainer != null)
		return eval("document['"+subcontainer+"'].document."+objectName);
	
	
	writeDebug("Cant Find " + objectName);
		
	/*if(objectName == "imSlide")
		
		
	if(objectName == "imNextSlide")
	
	if(objectName == "imPrevSlide")
	 
	if(objectName == "inSlideForm")
	
	if(objectName == "inSlideForm")
	
	if(objectName == "inSlideForm")
	
	if(objectName == "inSlideForm")
	
	if(objectName == "inSlideForm")
	 
		return eval("getObjectRef('lySlideControl').document."+objectName+"");
		
	if(objectName == "inSlideForm")
		return eval("getObjectRef('lySlideControl').document.forms[0]");	
		
	if(objectName == "inSlideList")
		return eval("getObjectRef('inSlideForm').elements[0]");

	if(objectName == "mmMedia")
		return eval("getObjectRef('lyMedia').document.embeds['mmMedia']");
		
	if(objectName == "imStop" || objectName == "imPause" || objectName == "imPlay" || objectName == "imFfw" || objectName == "imRew")
		return eval("getObjectRef('lyMediaControlA').document."+objectName+"");
		
	
	if(objectName == "imFlag")
		return eval("getObjectRef('lyFlag').document."+objectName+"");
	
	if(objectName == "inCurrentSlideForm")
		return eval("getObjectRef('lyFlag').document.forms[0]");	
	
	if(objectName == "inCurrentSlide")
		return eval("getObjectRef('inCurrentSlideForm').elements[0]");
*/
	//alert("invalid id: "+objectName);
		
}

function getObjectStyleRef(objectName){

	var theObj = null;
	if (typeof objectName == "string"){
		if(is.ns4)
			theObj = getObjectRef(objectName);
		else
			theObj = getObjectRef(objectName).style;
	}else{
		if(is.ns4)
			theObj = objectName;
		else
			theObj = objectName.style;
	}
		
	return theObj;
}


function allLoaded(){

	//alert('checking loaded');

	var passed = true;

	if(playerAbility)
		passed = (passed && getObjectRef("lyMedia") !=null && getObjectRef("mmMedia") !=null);
	
	//passed = (getObjectRef("lySlide") != null && getObjectRef("lySlideControl") != null && getObjectRef("lyMedia") != null && getObjectRef("lyMediaControlA") != null && getObjectRef("lyMediaControlB") != null && getObjectRef("lyFlag") != null);
	//alert(passed);
	// All top levels are ok.  Just Check to see if one element in each of the second levels are loaded.
	//alert(getObjectRef("imSlide") + " " + getObjectRef("inSlideForm"));
	//passed = (passed && getObjectRef("imSlide") != null && getObjectRef("inSlideForm") != null && getObjectRef("mmMedia") != null && getObjectRef("imStop") != null && getObjectRef("inCurrentSlideForm") != null);
	//alert(passed);
	// All secondary levels ok.  Check to make sure that the 2 forms loaded ok.
	//passed = (passed && getObjectRef("inSlideList") != null && getObjectRef("inCurrentSlide") != null);
	
	
	//alert(passed);


	errorCount = 0;


	allsystemsgo = passed;

	if(!allsystemsgo && errorCount<5) 
		setTimeout("allLoaded();",1000);
	else if(errorCount > 4){
	
		if(currentPlayer == "wmp"){
			writeMedia('real');
			currentPlayer='real';
			errorCount=0;
			setTimeout("allLoaded();",1000);
		}else{
			writeMedia('wmp');
			currentPlayer='wmp';
			errorCount=0;
			setTimeout("allLoaded();",1000);
		}
	} else {
	
		//if(is.ns4 && iFrameInitSRC.length && useIFrame)
			
			//setTimeout("document.lyIFrameNS4.src='"+iFrameInitSRC+"';",2000);
		//setTimeout("masterLoop();",5000);
		
		
	
	}
	
}

function swapImage(imageName, imageFile){
	
	if((tmpImage = imageLookup[imageFile]) != null){
		//alert(registeredImages[tmpImage]);
		//writeDebug('swapImage: Found in cache: ' + imageFile);
		
		getObjectRef(imageName).src = imageData[tmpImage].src;
		
	}else{
	
		writeDebug('swapImage: Did not find in cache ' + imageFile);
		getObjectRef(imageName).src = imageFile;
		
	};
	
	
	
}



function masterLoop(){
	
	masterLoopTimerLength = 900;
	
	if(isFfw || isRew)
		masterLoopTimerLength = 300;
		
	loopTimerID = setTimeout("masterLoop();",masterLoopTimerLength);
	loopCount = loopCount + 1;
	
	
	if(allsystemsgo) {
		
		//We always want to poll the player and update the clock
		//writeDebug("blah1");
		checkPlayer();
	    position = getPlayerPosition();
		//alert(position);
	    
		if(!isFfw && !isRew){
	    	if (currentPlayer == "real" || currentPlayer == "qt") { nextposition = position / 1000; } else { nextposition = position; }
	  	}
		// Display the status or the clock
		
		//if(isPlaying() || isFfw || isRew))
		if((isPlaying() && !isBuffering()) || isFfw || isRew)
			displayTime(nextposition);
		else{
			text = reportState();
        	displayText(text);
		}
		
		if (isFfw || isRew){
        	if (isFfw) {
            	if(scanSpeed < maxScanSpeed)
					scanSpeed = scanSpeed + scanSpeedInc;
					
            	//audioSet(nextposition+scanSpeed);
				nextposition+=scanSpeed;
       		} else {
				if(scanSpeed < maxScanSpeed)
					scanSpeed = scanSpeed + scanSpeedInc;
           		 // Fast Rewind 30 seconds.
            	if (nextposition > scanSpeed) {
               		//audioSet(nextposition-scanSpeed);
					nextposition-=scanSpeed;
					
            	} else {
                	audioSet(.1);
					stopPressed();
					
					// stop.
					
            	}
        	}
    	} else {
			scanSpeed = 0;		
		}
		
		if(slideControl=="push")
			pushCheckSlide();
			
		if(slideControl=="sync")
			syncCheckSlide();
            
        if(captionTimes.length > 0 && captionValues.length > 0)
            syncCheckCaption();
            
        if(customTimes.length > 0 && customEvents.length > 0)
            syncCustomEvent();
			
		customInterfaceOnTick();
		customOnTick();
		
		/* if(currentCaption < captionTimes.length){
			if(captionTimes[currentCaption + 1] < nextposition){
				currentCaption +=1;
				writeDebug("Firing Caption at time: " + nextposition);
				writeToLayer("lyClosedCaption","<TABLE WIDTH=" + initLyClosedCaption[2] + "><TR><TD class=textCaption>" + captionValues[currentCaption] + "</TD></TR></TABLE>");
				
			}
		} */
		
		
		
		// If we are 
		
	}
	
	if(!(loopCount%(60*waitTime)) && trackFlag){
		
		getObjectRef('imTracker').src = trackURL + "?m=" + mediaID + "&u=" + mediaUserID + "&c="+loopCount + "&p=" + currentPlayer + "&i=" + uniqueID + "&r=" + randSessionID + "&l=1";
		writeDebug(trackURL + "?m=" + mediaID + "&u=" + mediaUserID + "&c="+loopCount + "&p=" + currentPlayer + "&i=" + uniqueID + "&r=" + randSessionID + "&l=1");
		writeDebug("Tracking User");
	}
	
	if(!(loopCount%10) && postTrackFlag && isPlaying() && !isBuffering() && !isFfw && !isRew){
		if(nextposition / (postWaitTime * 60)>=1){
		
	    	if(parseInt(nextposition / (postWaitTime * 60)) != currentZone){
				currentZone = parseInt(nextposition / (postWaitTime * 60));
				getObjectRef('imTracker').src = trackURL + "?m=" + mediaID + "&u=" + mediaUserID + "&c="+currentZone + "&p=" + currentPlayer + "&i=" + uniqueID + "&r=" + randSessionID + "&l=0";
				writeDebug(trackURL + "?m=" + mediaID + "&u=" + mediaUserID + "&c="+loopCount + "&p=" + currentPlayer + "&i=" + uniqueID + "&r=" + randSessionID + "&l=0");
				writeDebug("Post Tracking User... in zone: " + currentZone);
			}
		
		} else {
		
			//What to do?  Do we want to track users at time 0????
			// Setting now to track users at 60 seconds!
			
			if(nextposition > 60){
		
				if(0 != currentZone){
					currentZone = 0;
					getObjectRef('imTracker').src = trackURL + "?m=" + mediaID + "&u=" + mediaUserID + "&c="+currentZone + "&p=" + currentPlayer + "&i=" + uniqueID + "&r=" + randSessionID+ "&l=0";
					writeDebug(trackURL + "?m=" + mediaID + "&u=" + mediaUserID + "&c="+loopCount + "&p=" + currentPlayer + "&i=" + uniqueID + "&r=" + randSessionID+ "&l=0");
					writeDebug("Post Tracking User... in zone*: " + currentZone);
				}
			}
		
		
		
		}
	
	}
	
	
	if(!(loopCount%5))
		writeStatus();
		
	if(!(loopCount%6)){
		if((isPlaying() || isBuffering()) && buttonState != "play" && buttonState != "ffw" && buttonState !="rew"){
			buttonState = "play"
			setPlayerButtons();
		} else if(isStopped()  && buttonState != "stop"){
			buttonState = "stop";
			setPlayerButtons();		
		}
	}
}

function checkModKeys(evt) {
	
	var alt = (is.ns) ? evt.modifiers & Event.ALT_MASK : window.event.altKey;
	var ctrl = (is.ns) ? evt.modifiers & Event.CONTROL_MASK : window.event.ctrlKey;
	var shift = (is.ns) ? evt.modifiers & Event.SHIFT_MASK : window.event.shiftKey;

	
	if(ctrl){
		makeCenterPopup("debug.html","debug",300,200);
		debug=1;
	}
		
	if(shift){
		makeCenterPopup("remotedebug.cfm","remoteDebug",100,100);
		remoteDebug=1;
	}

	//writeDebug("Tried Key Checking");
	
	return false;

}

function syncCheckCaption(){

	var newCaptionFound = false;

	if(!playerAbility)
		return false;

	if(!isPlaying() && !isBuffering() && !isFfw && !isRew)
		return;

	while(captionTimes[currentCaption + 1] < nextposition && currentCaption < captionTimes.length){
		newCaptionFound = true;
     	currentCaption +=1;
   	}

	while(nextposition < captionTimes[currentCaption] && currentCaption > 1){
		newCaptionFound = true;
		currentCaption = currentCaption - 1;
	}
	
	if(newCaptionFound){
		writeToLayer("lyClosedCaption","<TABLE WIDTH=" + initLyClosedCaption[2] + "><TR><TD class=textCaption>" + captionValues[currentCaption] + "</TD></TR></TABLE>");
		writeDebug("Firing Caption at time: " + nextposition);
	}
        
}

function syncCustomEvent() {

	if(!playerAbility)
		return false;

    if(!isPlaying() && !isBuffering() && !isFfw && !isRew)
		return;

	if(currentCustomEvent < customTimes.length){
		if(customTimes[currentCustomEvent + 1] < nextposition){
			currentCustomEvent +=1;
			writeDebug("Firing Custom Event: " + customEvents[currentCustomEvent]);
			eval(customEvents[currentCustomEvent]);
			
		}
	}
	if(currentCustomEvent >0){
		if(customTimes[currentCustomEvent] > nextposition){
			writeDebug("Firing Custom Event: " + customEventsReverse[currentCustomEvent]);
			eval(customEventsReverse[currentCustomEvent]);
			currentCustomEvent -=1;
		}
	}

}