
function appManager() {
	
}
 
appManager.prototype.init = function () {  
	appManagerloaded = false;
 	appManager.flashLoaded = false;
	appManager.setEnv();
	hasTimeStarted = false;
	
	if (appManager.env.isFirefox){  
		Player = document.getElementById("Player");
		adFrame = document.getElementById("adFrame");
		bbFlash = document.getElementById("bbFlash");	 
	}
	Player.style.display = "block"; //firefox happy 
	Player.style.visibility = "hidden";
	Player.uiMode = "invisible"; 
	Player.windowlessVideo = (parseInt(Player.versionInfo) >= 10) ? true : false;
		
	if (appManager.env.isFirefox) {    
		window.addEventListener("unload",appManager.finish,false);
	} else {   
		window.attachEvent("onbeforeunload",appManager.finish)
	}
	
	appManager.playIntro = false;
	appManager.fullScreenOK = false;
	appManager.introHasStarted = false;
	appManager.failureTimer = null;  
	appManager.showVideoTimer = null;  
	appManager.hideVideoTimer = null;  
	appManager.adCount = 0; 
	appManager.adPlaying = false;  
	 
	playlist = false; 
	videoEvent = {};  
	videoEvent.content = {type:"",id:""}; 

	appManager.startFresh();
	configManager.getValues();
	configManager.init();
	appManager.processConfig();
	adManager.processConfig();
	
	if (adManager.maxTime>-1) {
		appManager.clock = {videoTime:0,adTime:0,offset:100,timer:false};
		appManager.clock.timer = setInterval(appManager.updateClock,appManager.clock.offset); 
	}
	
	appManager.loaded = true;
	if (appManager.flashLoaded) appManager.playNextElement();
}  

appManager.prototype.setEnv = function () {
	appManager.env = {isFirefox:(navigator.userAgent.toLowerCase().indexOf("firefox")>-1)};
	if (top.document.location.hostname=="") appManager.env.mode = "local";
	else if (top.document.location.hostname.indexOf("-d.mtvi.com")>-1) appManager.env.mode = "d";
	else if (top.document.location.hostname.indexOf("-q.mtvi.com")>-1) appManager.env.mode = "q";
	else appManager.env.mode = "live";
}

appManager.prototype.processConfig = function () { 
	if (!configManager.fullScreen.message) appManager.fullScreenOK = true;
	appManager.playIntro = makeBoolean(configManager.intro); 
	appManager.channelName = (typeof configManager.channelName=="undefined") ? "undefined" : configManager.channelName;
}

appManager.prototype.updateClock = function () { 
	try { //in case of unload
		if (Player.playState==3) {  
			var n = appManager.clock.offset/1000;
         /* look for live video type . if it is a live video, then it is def. NOT an ad  */
         try {
         if (Player.currentMedia.getItemInfo("vidType") == "live"){
            appManager.adPlaying = false;
	         setFlashVar("video.adPlay","false");
         }
         }catch(e){ } 

			if (appManager.adPlaying) {
				//appManager.clock.adTime += n; 
			} else {
				appManager.clock.videoTime += n; 
				adManager.evalMark(); 
				//if (playlist.items[playlist.index].adEvalMark>-1) adManager.evalMark(); 
			}
		}
	} catch(e) {
		//printStatus("e updateClock: "+e.description);
	}
}   

appManager.prototype.startFresh = function () {
	adManager.stopAdCountTimer();
	appManager.asxLock = false; 
	appManager.adPlaying = false;
	setFlashVar("video.adPlay","false");
	appManager.kickstartLoop = 0;   
}

appManager.prototype.findNextElement = function () {
	try {
		playlist.items[playlist.index].mediaEnded = true;
	} catch(e) {} 
	
	if (appManager.playIntro) { 
		//printStatus("findNextElement: intro ended");
		appManager.playIntro = false;
		appManager.playNextElement();  
	} 
	else if (appManager.adPlaying) { 
		//printStatus("findNextElement: ad ended");
		appManager.playNextElement();
	}
	else {  
		//printStatus("findNextElement: content ended");
		appManager.requestItem();
	}
}  
 
appManager.prototype.makeVideoObject = function (obj,prev) { 
	try { 
		if (typeof configManager.asxGen!="undefined" && obj.url.indexOf(".asx")==-1) obj.url = configManager.asxGen+ "?vid=" + obj.id;		
		
		//live hack
		//if (obj.id == "100596")
		if (obj.id == "103156")
		{
			obj.url = "mms://a648.l829548138.c8295.g.lm.akamaistream.net/D/648/8295/v0001/reflector:48138"
		}		

		var isLocal = (configManager.useOptimizer) ? appManager.isLocalFile(obj.url) : false; 
		if (isLocal) obj.url = isLocal;
		obj.location = isLocal ? "local" : "stream";  
		if (obj.content.type=="") obj.content.type = "playlist"; 
		if (obj.content.id==0) obj.content.id = obj.content.type; 
		obj.previousEvent = prev ? prev : videoEvent;     
		obj.newContainer = (eval("obj."+adManager.resetSwitch)!=eval("obj.previousEvent."+adManager.resetSwitch));    
		obj.forceCoAd = adManager.ifForceCoAd(obj.content.id); 
		obj.takesAds = adManager.ifTakesAds(obj.content.id); 
		obj.takesLeadingAd = adManager.ifTakesLeadingAd(obj.takesAds, obj.type);  
		obj.forceLeadingAd = adManager.ifForceLeadingAd(obj.newContainer, obj.takesLeadingAd); 
		obj.takesTrailingAd = adManager.ifTakesTrailingAd(obj.takesAds, obj.type); 
		obj.forceTrailingAd = adManager.ifForceTrailingAd(obj.takesAds, obj.type);  
		return obj; 
	} catch(e) {
		printStatus("e makeVideoObject: "+e.description);
	}
} 

function playVideo() {   
	//printStatus("playVideo.args.length "+playVideo.arguments.length); 
	var previousVideoEvent = videoEvent;  

	var args = playVideo.arguments;	 	
	videoEvent = appManager.makeVideoObject(args[0],previousVideoEvent);
	
	preBuffEvent = appManager.makeVideoObject(args[1]);   
	if (videoEvent.takesTrailingAd) {
		videoEvent.takesTrailingAd = !adManager.ifKillTrailingAd(false); //check pBE
		//getDebugPlayVideo(); 
	}
	
	if (!isValidURL(videoEvent.url)) {
		printStatus("invalid skipped: "+playlist.items[playlist.index].url);
		appManager.handleInvalidVideo(videoEvent.id);
		return;
	} 
	
	if (appManager.loaded) {  
		if (!appManager.adPlaying) appManager.playNextElement(); 
	} else {
		appManager.flashLoaded = true;
	}
} 

appManager.prototype.isLocalFile = function (url) { 
	var fPath = url.substring((url.indexOf("!") + 1), url.indexOf("?"));
	var fParts = fPath.split("/");
	var fileName = fParts[fParts.length-1];
	if (getFileProp(fileName,"Status") == "Completed"){
		return getFileProp(fileName,"PathName");
	}
	else return false; 
} 

appManager.prototype.playNextElement = function () { 
	appManager.startFresh(); 
	cued = false;	 
 			
	if (playlist) playlist.index++; 
	if (appManager.playIntro && appManager.introHasStarted) appManager.playIntro = false; //intro interrupt
	if (playlist) printStatus("playNextElement:  "+playlist.index+"/"+playlist.items.length); 
	if (playlist && adManager.maxTime>-1) printStatus("playNextElement clock: "+ parseInt(appManager.clock.videoTime)+"/"+ adManager.maxTime); 
	//else printStatus("playNextElement:  no playlist"); 
	
	adManager.startPlayNextElement();
		
	if (adManager.ifAdInsertion()) {    
		//printStatus("playNextEl: ad");
		appManager.adPlaying = true; 
		setFlashVar("video.adPlay","true");
		
		try { 
			if (!playlist) {}
			else if (playlist.items.length<=playlist.index) return;
			else if (playlist.items[playlist.index].isAd) { 
				if (playlist.items[playlist.index-1].isAsx && !playlist.items[playlist.index-1].mediaEnded) {
					printStatus ("<b>ASX interrupt w ad</b>: "+playlist.items[playlist.index].url); 
				}	
				else if (!playlist.items[playlist.index-1].mediaEnded) {//interrupt & play
					cued = true; 
					Player.controls.playItem(root_playlist.item(playlist.index)); 
					printStatus ("<b>interrupt w cued ad</b>: "+playlist.items[playlist.index].url);
					appManager.removeCuedItems(playlist.index+1); 
				}
				else { //playing already 
					cued = true; 
					play(); 
					printStatus ("<b>cued ad</b>: "+playlist.items[playlist.index].url);
				} 
			} 
		} catch(e) {
			printStatus("e playNextEl ad: "+e.description) 
		}
		
		if (cued) appManager.delayHideVideo(200);
		else {  
			appManager.hideVideo();  
			appManager.stopFailureTimer(); 
			appManager.createNewPlaylist(); 
			appManager.addToPlaylist(adManager.makeAdObj(true));
			playlist.items[playlist.items.length-1].isAd = true;  
			setTimeout(play,30); 
			printStatus("<b>+ ad</b>: "+playlist.items[playlist.items.length-1].url);
			
		}   
		if (playlist.index==playlist.items.length-1) {//no items after ad
			printStatus("^^^ cued=swapped");
			setTimeout(appManager.switchCuedItem,100);
		}
		
		appManager.clock.videoTime = 0; 
	} 
	else {      
		//printStatus("playNextEl: video");
		//getDebugCompareURLs(); 
		try {
			if (!playlist) {} 
			else if (playlist.items.length<=playlist.index) return;
			else if (stripAuthToken(playlist.items[playlist.index].url)==stripAuthToken(videoEvent.url)) {//cued 
				if (playlist.items[playlist.index-1].isAsx && !playlist.items[playlist.index-1].mediaEnded) {
					printStatus("<b>ASX interrupt</b>: " + playlist.items[playlist.index].url);  
				}	
				else if (!playlist.items[playlist.index-1].mediaEnded) {//interrupt & play  
					cued = true; 
					Player.controls.playItem(root_playlist.item(playlist.index)); 
					printStatus ("<b>interrupt w cued</b>: "+playlist.items[playlist.index].url); 
				}
				else { //playing already 
					cued = true; 
					play(); 
					printStatus("<b>cued</b>: "+videoEvent.url); 
				}
			}   
		} catch(e) {
			printStatus("e playNextEl video: "+e.description) 
		}
		
		try {
			if (cued) {
				appManager.delayHideVideo(200);
				if (videoEvent.takesTrailingAd) videoEvent.takesTrailingAd = !adManager.ifKillTrailingAd(false); //check pBE
				setTimeout(appManager.addPrebufferItem,100);
			}
			else { 
				appManager.hideVideo();  
				appManager.stopFailureTimer();	 
				appManager.createNewPlaylist();
				if (appManager.playIntro) {//so buff_url doesn't add twice
					if (!isValidURL(configManager.intro)) appManager.playIntro = false;
					else { 
						appManager.introHasStarted = true; 
						var introObj = {url:configManager.intro};
						appManager.addToPlaylist(introObj); 	
						playlist.items[playlist.items.length-1].id = "intro";
						setTimeout(play,20); 
						appManager.addToPlaylist(videoEvent);   
					}
				}
				if (!appManager.playIntro) {  
					appManager.addToPlaylist(videoEvent);	 
					setTimeout(play,30); 
					setTimeout(appManager.addPrebufferItem,100);
				} 
				printStatus ("<b>+ video</b>: "+videoEvent.url); 
			}  
		} catch(e) {
			printStatus("e2 playNextEl video2: "+e.description) 
		}
		//setFlashVar("video.videoProtocol",playlist.items[playlist.index].location);
		reporting_manager.reportPlayVideo(videoEvent, preBuffEvent);
		ReporterHBX.reportPlayVideo(videoEvent, preBuffEvent);
		if (!appManager.playIntro) adManager.startAdCountTimer();
	} 
	
	adManager.endPlayNextElement();
	setTimeout(debugCurrentItem,200);
}  

appManager.prototype.addPrebufferItem = function () {
	try {
		if (Player.playState!=3) play();
		if (isValidURL(preBuffEvent.url)) appManager.addToPlaylist(preBuffEvent);	
	} catch(e) {
		printStatus("e addPrebufferItem: "+e.description);
	}
}
 
appManager.prototype.switchCuedItem = function () {
	if (isValidURL(videoEvent.url)) appManager.addToPlaylist(videoEvent);	
}
  
appManager.prototype.requestItem = function () {
	//printStatus("request item");
	setFlashVar("video.playState", 8);
}

appManager.prototype.createNewPlaylist = function () {  
	stop();
	root_playlist = Player.newPlaylist("","");
	Player.currentPlaylist = root_playlist;  
	printStatus("createNewPlaylist");
	playlist = new Object();
	playlist.items = new Array(); 
	playlist.index = 0;   
} 

appManager.prototype.insertAd = function (wo) {  
	try { 
		appManager.addToPlaylist(adManager.makeAdObj(wo),playlist.index+1);   
		playlist.items[playlist.index+1].isAd = true;  
		//printStatus("....... attach ad");
	} catch(e) {
		printStatus("e insertAd: "+e.description);
	}
} 
 
appManager.prototype.addToPlaylist = function (obj,pos) {
	obj.tenSec = false;
	obj.mediaEnded = false;
	obj.isAd = false;
	obj.isAsx = isAsx(obj.url); 
	obj.isError = false;   
	try {   
		if (pos) {   
			playlist.items.splice(pos,0,obj);
			root_playlist.insertItem(pos,Player.newMedia(obj.url));
		}
		else {  
			playlist.items.push(obj); 
			root_playlist.appendItem(Player.newMedia(obj.url));
		}	 
		printStatus("....... addToPlaylist: "+obj.url); 
	} catch(e) {
		printStatus("e addToPlaylist: "+e.description);
	}  
} 

appManager.prototype.removeCuedItems = function (pos) { 
	try {
		playlist.items.splice(pos,playlist.items.length-pos);  
		for (var i=pos;i<=root_playlist.count;i++) {
			root_playlist.removeItem(root_playlist.item(i));
		} 
	} catch(e) {
		printStatus("e removeCued: "+e.description);
	}
}
 
appManager.prototype.finish = function () { 
	try { 
		ReporterHBX.reportEndVideo();
		configManager.finish();
		if (Player) {
			Player.controls.stop();
			Player.close();   
		}
		document.getElementById("holder").innerHTML = "";
	} catch(e) {}
} 
 
//-------------START Monitoring & Recovery
appManager.prototype.handleInvalidVideo = function (ivid) {
	appManager.stopFailureTimer();
	
	if (appManager.adPlaying && ivid) {//while ad is playing
		printStatus("handleInvalidVideo: "+ivid+", during ad");	
		appManager.requestItem();
		return;
	}
	
	if (!ivid) ivid = appManager.adPlaying ? "ad" : playlist.items[playlist.index].id;
	//printStatus("handleInvalidVideo: "+ivid+", "+appManager.handleInvalidVideo.caller);	
	
	if (ivid == "intro") {
		appManager.playIntro = false;
		appManager.playNextElement();
	} else if (ivid == "ad") { 
		appManager.adCount = 0; 
		setFlashVar("video.playState", 1001);//PLAYSTATE_FAILURE
		appManager.playNextElement();
	} else {  
		setFlashVar("video.playState", 1001);//PLAYSTATE_FAILURE
		setTimeout(appManager.requestItem, 3000);
	}
}  

appManager.prototype.startFailureTimer = function () {
	try {
		var t = eval("configManager.failureTime"+(appManager.adPlaying?"ad":"video")+"."+(appManager.kickstartLoop==0?"congestion":".kickstart"));  
		appManager.failureTimer = setTimeout('appManager.kickstartNextVideo()',t);
	} catch(e) {}
}

appManager.prototype.stopFailureTimer = function () {
	try {
		clearTimeout(appManager.failureTimer);
	} catch(e) {}
}

appManager.prototype.kickstartNextVideo = function () {
	appManager.stopFailureTimer();
	appManager.hideVideo();
	
	if (Player.playState != 3) {
		appManager.kickstartLoop++;
		Player.fullscreen = false;
		if (appManager.kickstartLoop==1) {
			play();
			printStatus("kickstartNextVideo: congestion msg");
			setFlashVar("video.playState", 1000);//PLAYSTATE_CONGESTION 
			appManager.startFailureTimer(true);
		} else if (Player.currentMedia.getItemInfo("vidType") != "live") {
			printStatus("kickstartNextVideo: kick!");
			appManager.adCount = 0; 
			appManager.startFresh();
			if (appManager.adPlaying) { 
				appManager.playNextElement();
			}
			else {
				setFlashVar("video.playState", 1001);//PLAYSTATE_FAILURE
				setTimeout(appManager.requestItem,3000);
			}
		}
	}
} 
//-------------END Monitoring & Recovery

//-------------START Display 
appManager.prototype.showVideo = function () {
	clearTimeout(appManager.hideVideoTimer);
	Player.uiMode = "none";
	Player.style.visibility = "visible"; 
	setFlashVar('video.videoDisplay', 'visible');
}

appManager.prototype.delayShowVideo = function (n) {
	clearTimeout(appManager.showVideoTimer);
	clearTimeout(appManager.hideVideoTimer);
	appManager.showVideoTimer = setTimeout("appManager.showVideo()",n);
}

appManager.prototype.hideVideo = function () { 
	try {//in case of unload
		if (Player.playState==3) return; 
		clearTimeout(appManager.showVideoTimer);
		
		if (!Player.fullScreen) { 
			Player.style.visibility = "hidden"; 
			setFlashVar('video.videoDisplay','hidden');
		}
	} catch(e) {}
} 

appManager.prototype.delayHideVideo = function (n) {
	clearTimeout(appManager.showVideoTimer);
	clearTimeout(appManager.hideVideoTimer);
	if (Player.playState!=3) {
		appManager.hideVideoTimer = setTimeout("appManager.hideVideo()",n);
	}
}

function resize (x,y,w,h) { 
	try {
		Player.style.left = x;
		Player.style.top = y;
		Player.style.width = w;
		Player.style.height = h; 
	} catch(e) {}
}

function resizeWindow(size) {
	var new_width = new_height = 0;
	
	// Commented by Frank Conway to allow for resizing w/ inpage player
	//if (window.opener) {
		if (window.screen.width==800 && window.screen.height==600 ){ //abort normal resize for 800x600
			new_width = window.screen.availWidth;
			new_height = window.screen.availHeight;
			try {
				window.resizeTo( new_width, new_height );
				window.moveTo(0,0);			
			} catch(e) {
				window.resizeBy( window.screen.availWidth - new_width, window.screen.availHeight - new_height );
				window.moveBy(-window.screenLeft,-window.screenTop);			
			}
		}
		else {
			new_width = (size>-1) ? resizeDimensions[size][0] : 0;
			new_height = (size>-1) ? resizeDimensions[size][1] : 0;
			try { //move window if it will be off-screen with new dimensions	
				var move_x = (window.screenLeft + new_width > window.screen.availWidth) ? (window.screen.availWidth - new_width - window.screenLeft - 10) : 0;
				var move_y = (window.screenTop + new_height > window.screen.availHeight) ? (window.screen.availHeight - new_height - window.screenTop - 20) : 0; 
				window.moveBy( move_x, move_y ); 
				window.resizeBy( (new_width - current_width), (new_height - current_height) ); //resizeTo() includes chrome
			} catch(e) {} 
		}
	//}
	
	// Prevent scroll bars when resizing inpage
	document.getElementById("holder").style.width = new_width;
	document.getElementById("holder").style.height = new_height;
		
	if (window.opener) {
		current_width = new_width;
		current_height = new_height;
	} else {
		// Width and height now set by offestWidth & offsetHeight of centerTable for inpage player
		current_width = document.getElementById("centerTable").offsetWidth;
		current_height = document.getElementById("centerTable").offsetHeight;
	}

}

function setDimensions() {
	if (!window.opener && document.getElementById("centerTable")) {
		current_width = document.getElementById("centerTable").offsetWidth;
		current_height = document.getElementById("centerTable").offsetHeight;
	}
}

if (window.attachEvent) {
	window.attachEvent("onresize", setDimensions);
}
//-------------END Display 


//-------------START WMP Player & Network controls
function play() { try {Player.controls.play();} catch(e) {}}
function pause(){ try {Player.controls.pause();} catch(e) {}}
function stop(){ try {Player.controls.stop();} catch(e) {}}
function setVolume(val) { try {Player.settings.volume = val;} catch(e) {}}
 
function setFullscreen(){
	if (Player.playState == 3) { 
		if (appManager.fullScreenOK) {
			Player.fullscreen = true; 
		}
		else {
			//if (confirm(configManager.fullScreen.message)) {
				appManager.fullScreenOK = true;
				Player.fullscreen = true;
				 
			//}
		}
		REPORTING.makeCall("fullscreen");
	}
}

function setPosition(val) { 
	val = (val<2000) ? 2000:val;
	try {Player.controls.currentPosition = (val/1000);} catch(e) {}
}
 
function getPosition() { 
	var pos = Player.controls.currentPosition;
	if ((Player.style.display=="none"||Player.style.visibility=="hidden") && pos > 0) appManager.showVideo();
	setFlashVar("video.position", pos); 
	ReporterHBX.reportProgressVideo();
} 
//-------------END WMP Player & Network controls 


//-------------START WMP Events
function onPlaystateEvent(NewState) { 	
	appManager.stopFailureTimer();
	
	switch (NewState) {
		case 10: // Ready		
			appManager.delayHideVideo();
		case 0: // Undefined
		case 7: // Waiting
		case 9: // Transitioning
		case 11: // Reconnecting 
			appManager.startFailureTimer();
			break;
	}

	if (NewState == 3) { // playing
		try {
			setFlashVar("video.position", Player.controls.currentPosition);
			setFlashVar("video.duration", Player.currentMedia.duration);
			setFlashVar("video.bitRate", Player.network.bitRate);
			setFlashVar("video.maxBitRate", Player.network.maxBitRate);	
			appManager.delayShowVideo( 200 );	
		} catch(e) {}
		
		// added for live stream start time. 
		try {  
			//printStatus("useStartTime : "+useStartTime+", hasTimeStarted: "+hasTimeStarted);
			if (useStartTime && !hasTimeStarted){ 
				hasTimeStarted = true;
				Player.controls.currentPosition = GetStartTime(liveOffsetDate);
			}
			else {
				hasTimeStarted = false;
			}
		}
		catch(e) {
			printStatus("e live stream: "+e.description);
		}
	}
	
	if (NewState==8 && Player.currentPlaylist.isIdentical(root_playlist)) { //media ended
		//printStatus("MediaEnded");
		ReporterHBX.videoState8();
		appManager.findNextElement();
	} else if (NewState==8) {  
		//printStatus("asxLock");
		appManager.asxLock = true;
		ReporterHBX.videoState8(); 
	}
	
	if (NewState!=8) setFlashVar("video.playState", NewState);
	

}

function onOpenstateEvent(NewState){
	if (NewState==7) {//PlaylistChanged
		if (appManager.asxLock && Player.currentPlaylist.isIdentical(root_playlist)) {
			//printStatus("PlaylistChanged");
			if (appManager.loaded) appManager.findNextElement();
		}
	}
} 
 
function onBufferingEvent(Start) {
	if (Start) { 
		if ( !(Player.controls.currentPosition > 0) ) {
			adManager.stopAdCountTimer();
			appManager.hideVideo();
		}
	} 
	else { 
		if (Player.controls.currentPosition==0 && !appManager.adPlaying) {
			adManager.startAdCountTimer(); 
		}
		appManager.delayShowVideo( 50 );
	}
}

function onCurrentItemEvent() {
	try { //error-handling
		 if (appManager.adPlaying && Player.currentMedia.sourceURL == playlist.items[playlist.index+1].url) {
			printStatus("resynch: ad ending");
			appManager.playNextElement();
		}
	} catch(e) {}
 
	var coAdLink;
	try { //in case of unload
		var e = false; 
		var type = Player.currentMedia.getItemInfo("vidType").toLowerCase();
		var coAd = Player.currentMedia.getItemInfo("coAd");
		var expired = Player.currentMedia.getItemInfo("vidExpired"); 
		coAdLink = Player.currentMedia.getItemInfo("coAdLink");
		adManager.setCoAdLink(coAdLink); // null is a valid value.
	} catch(e) {
		var e = true;
	}
	
	if (!e) {
		setFlashVar("video.videoLive", type);  
		if (coAd) adManager.setCoAd(coAd);  
		if (expired) setFlashVar("video.videoExpired", expired);
	}
	
	
	// Jump into a Live Stream.
	try {
		var vidTime = Player.currentMedia.getItemInfo("vidTime").toLowerCase();
		if (vidTime != "" ){
			liveOffsetDate = vidTime;
			useStartTime = true;
			hasTimeStarted = false;
		}
		else{
			useStartTime = false;
		}
	}
	catch(e) {}

}
  
function onPositionEvent(oldPosition, newPosition) {
	adManager.clearAdEvalMark();
}  

/**function onErrorEvent() { 
	printStatus("ERROR: "+Player.error.item(Player.error.errorCount-1).errorDescription);
	if (!playlist.items[playlist.index].isError) {
		playlist.items[playlist.index].isError = true;
		appManager.handleInvalidVideo();
	}
}**/

function onMediaErrorEvent() { 
	try {
		if (!playlist.items[playlist.index].isError) {
			playlist.items[playlist.index].isError = true;
			appManager.handleInvalidVideo();
		}
		printStatus("MediaERROR: "+ Player.currentMedia.error.errorDescription +", "+ Player.currentMedia.sourceURL); 
	} catch(e) {}
}

function onMouseDownEvent(nButton,nShiftState,fX,fY){
	try {
		if (Player.fullscreen == false){
			// When an Ad is Playing.
			if (appManager.adPlaying){
				// If the coAdLive is in the ASX
				if (adManager.getCoAdLink()){
					// Open the Url in a new window.
					window.open(adManager.getCoAdLink());
				}
				else {
					setFullscreen();
				}
			}
			else{
				setFullscreen();
			}
		}
		else Player.fullscreen == false;
	} catch(e) {}
}
//-------------END WMP Events



/***************************************************************/
// Utils 

gElapsedTimeInSeconds = 0;
gClipLengthInSeconds = 0;

function GetStartTime(d){
	try{
		now = new Date(d);
		hours = now.getHours();
		minutes = now.getMinutes();     
		seconds = now.getSeconds();
		
		gElapsedTimeInSeconds = GetElapsedTime( hours, minutes, seconds );
		gClipLengthInSeconds = Player.currentMedia.duration;
		var d = GetCurrentPositionInSeconds( gElapsedTimeInSeconds, gClipLengthInSeconds );
		return d;				
	}
	catch (e) { }
}

function GetElapsedTime( _hours, _minutes, _seconds ) {
	return _seconds + ( _minutes * 60 ) + ( _hours * (60 * 60) );
}
	
function GetCurrentPositionInSeconds( gElapsedTimeInSeconds, gClipLengthInSeconds ){
	var elapsedTimeInMiliSeconds = 1000 * gElapsedTimeInSeconds;
	var clipLengthInMiliSeconds = 1000 * gClipLengthInSeconds;
	var currentNumberOfPasses = elapsedTimeInMiliSeconds / clipLengthInMiliSeconds
	var newNumberOfPasses = Math.floor( currentNumberOfPasses ) +1;
	var timePlayedInMiliSeconds = clipLengthInMiliSeconds * newNumberOfPasses;
	var timeRemainingInMiliSecondsForThisPass = clipLengthInMiliSeconds - (timePlayedInMiliSeconds - elapsedTimeInMiliSeconds);
	return Math.round( timeRemainingInMiliSecondsForThisPass/1000 );   
}


