var buffering = false;
var isPlaying = false;
var volume = 75;
var slBuffer = false;

var originalSizeX=-1;
var originalSizeY=-1;
var originalScrollPositionX=-1;
var originalScrollPositionY=-1;
var originalPositionX;
var originalPositionY;
var minimizedState=0;
var isLive = false;
var curIndexId;
var bufferingStatus = 1;


function callLb(obj){
	debugThis("callLb "+obj['asFunction']);
	var objFlash = getObject("objLeaveBehind");
	if(objFlash){
		if(typeof(objFlash.callLb) == 'function')
			objFlash.callLb(obj);
	}
}

function callTitle(obj){
	debugThis("callTitle "+obj['asFunction']);
	var objFlash = getObject("titleObj");
	if(objFlash){
		if(typeof(objFlash.callTitle) == 'function')
			objFlash.callTitle(obj);
	}
}

function callPlayer(obj){
	debugThis("callPlayer "+obj['asFunction']);
	
	var objFlash = getObject("objFlashPlayer");
	if(objFlash){
		if(typeof(objFlash.callPlayer) == 'function')
			objFlash.callPlayer(obj);
	}
}

function callOverlay(obj){
	debugThis("callOverlay "+obj['asFunction']);
	var objFlash = getObject("objFlashPlayerTrans");
	if(objFlash){
		if(typeof(objFlash.callOverlay) == 'function')
			objFlash.callOverlay(obj);
	}
}

function callAllOverlay(obj){
	debugThis("callAllOverlay "+obj['asFunction']);
	var objFlash = getObject("objFlashPlayerAll");
	if(objFlash){
		if(typeof(objFlash.callAllOverlay) == 'function')
			objFlash.callAllOverlay(obj);
	}
}

function callHeader(obj){
	debugThis("callHeader "+obj['asFunction']);
	var objFlash = getObject("headerFl");
	if(objFlash){
		if(typeof(objFlash.callHeader) == 'function')
			objFlash.callHeader(obj);
	}
}

function callSkyscraperL(obj){
	debugThis("callSkyscraperL "+obj['asFunction']);
	var objFlash = getObject("skyscraperL");
	if(objFlash){
		if(typeof(objFlash.callSkyscraperL) == 'function')
			objFlash.callSkyscraperL(obj);
	}
}

function callSkyscraperR(obj){
	debugThis("callSkyscraperR "+obj['asFunction']);
	var objFlash = getObject("skyscraperR");
	if(objFlash){
		if(typeof(objFlash.callSkyscraperR) == 'function')
			objFlash.callSkyscraperR(obj);
	}
}

function callNav(obj){
	debugThis("callNav "+obj['asFunction']);
		
	var objFlash = getObject("movieObj");
	if(objFlash){
		debugThis("typeof "+typeof(objFlash.callNav));
		if(typeof(objFlash.callNav) == 'function')
			objFlash.callNav(obj);
	}else{
		debugThis("callNav not ready");
	}
}

function callCarousel(obj){
	debugThis("callCarousel "+obj['asFunction']);
	var objFlash = getObject("carouselObj");
	if(objFlash){
		if(typeof(objFlash.callCarousel) == 'function')
			objFlash.callCarousel(obj);
	}
}




function getObject(movieName) {
	var obj;
	if (document.all) {		
		//alert("ie");
		obj = window[movieName];
	} else if (navigator.userAgent.indexOf("Safari") != -1){			
		//alert("safari");
		obj = window[movieName + "Embed"];
	} else {			
		//alert("other");
		obj = document[movieName + "Embed"];
	}
	return obj;
}

//get wmp object
function getWmpObject(movieName){
	var wmp = document.getElementById(movieName);
	
	return wmp;
}


function getTextFromFlash(htmlData) {
	htmlData.evalScripts();
}


function load3rdParty(url){
	debugThis('load3rdParty '+url);
	if(url == undefined || url == "" || url.indexOf("http") == -1)
		return;

	try{
		var e = document.createElement("img");
		e.src = url + "randNum=" + zMSRandiad();
   	 	document.getElementById("trackingDiv").appendChild(e);
	} catch (e) {debugThis('load3rdParty error ');}
}


function zMSRandiad(){return Math.round(Math.abs(Math.sin((new Date()).getTime()))*1000000000)%10000000;}


/******************************************************** media_api.js *************************************************/

var buffering = false;

 //get the video time of current playback
function loadMovie(contentUrl, width, height, indexId){
	debugThis('loadMovie '+contentUrl);
	curIndexId = indexId;
	
	if(silverlight){
		videoPlayer.loadSL(contentUrl, width, height);
	}else{
		loadWmp(contentUrl, width, height);
	}
}


//load the video for optin
function loadMovieOptin(contentUrl, width, height, doPlay){
	debugThis('loadMovieOptin '+contentUrl);
	
	if(silverlight){
		videoPlayer.loadSLOptin(contentUrl, width, height, doPlay);
	}
}


function startPlayOptIn(status){
	debugThis('startPlayOptIn '+status);
	optinActive = status;
	overlay.showOptinBg(!status);
}

function stopSLOptin(){
	videoPlayer.stopSLOptin();
}


//get the video time of current playback
function getBitrate(){
	var bitRate;
	
	if(silverlight){
		bitRate = -1;
	}else{
		bitRate = getWmpBitrate();
	}
	
	return bitRate;
}

//get the download progress of the file (progressive content only)
function getDownloadProgress(){
	var dProgress;
	
	if(silverlight){
		dProgress = videoPlayer.getSlDownloadProgress();
	}else{
		dProgress = getWmpDownloadProgress();
	}
	
	return dProgress;
}


function setCurIndexId(val){
	debugThis('setCurIndexId '+val);
	curIndexId = val;
}


//get the video time of current playback
function getVideoTime(){
	//debugThis("getVideoTime");
	var videoTime = 0;
	
	if(silverlight){
		videoTime = videoPlayer.getVideoTimeSL();
	}else{
		videoTime = getVideoTimeWmp();
	}
	
	videoTime = (Math.round(videoTime*100))/100;
	
	debugThis("videoTime:"+videoTime);
	
	return videoTime;
}

//get the total time for current video
function getTotalTime(){
	var totalTime = 0;
	
	if(silverlight){
		totalTime = videoPlayer.getTotalTimeSL();
	}else{
		totalTime = getTotalTimeWmp();
	}
	
	totalTime = (Math.round(totalTime*100))/100;
	
	return totalTime;
}



//set the volume of current medium
function setVolume(vol, controls){
	debugThis("setVolume ");
	
	if(silverlight){
		videoPlayer.setVolumeSL(vol, controls);
	}else{
		setVolumeWmp(vol);
	}
}


//set the volume of current medium
function setMute(status){
	debugThis('setMute '+status);
	
	if(silverlight){
		videoPlayer.setMuteSL(status);
	}else{
		setMuteWmp(status);
	}
}

//set the volume of current medium
function setControlsMute(status){
	debugThis('setControlsMute '+status);
	
	if(silverlight)
		controls.setControlsMute(status);
}

//set the volume of current medium
function setControlsVolume(val){
	debugThis('setControlsVolume '+val);
	
	if(silverlight)
		controls.setControlsVolume(val);
}


//set controls for wmp
function sendControl(contolState, fromBufferCheck) {
	//alert('sendControl:'+contolState);
	debugThis('sendControl '+contolState);
	
	if(silverlight){
		videoPlayer.sendControlSL(contolState);
	}else{
		sendControlWmp(contolState, fromBufferCheck);
	}
}


//set playhead for wmp after dragging it
function setPlayPos(playPos) {
	
	if(silverlight){
		videoPlayer.setPlayPosSL(playPos);
	}else{
		setPlayPosWmp(playPos);
	}
}



function updateTime(curTime, totTime, dProgress, adPlaying){
	//debugThis('updateTime '+curTime+ ' '+totTime+' dProgress:'+dProgress);
	controls.updateTime(curTime, totTime, dProgress, adPlaying);
}

//enabled fullscreen button
function enableFullscreen(status){
	debugThis('enableFullscreen '+status);
	
	var objFlash = getObject("objFlashPlayer");
	if(typeof(objFlash.enableFullBtn) == 'function')
		objFlash.enableFullBtn(status);
		
	if(silverlight){
		controls.enableFullscreen(status);
	}
}


function setModePlay(status){
	debugThis('setModePlay '+status);
	
	if(silverlight){
		controls.setModePlay(status);
	}
}

function setControlsLive(status){
	debugThis('setControlsLive '+status);
	isLive = status;
	
	if(silverlight){
		controls.setControlsLive(status);
	}
}


function setPlayState(status){
	debugThis('setPlayState '+status);
	
	if(silverlight){
		controls.setPlayState(status);
	}
}


//enabled fullscreen button
function setHitMc(status, adId, clickThroughObj){
	debugThis('setHitMc '+status+'clickThroughObj '+clickThroughObj);
	
	if(silverlight){
		videoPlayer.setHitMc(status, adId, clickThroughObj);
	}
}


function showBuffer(status){
	debugThis('showBuffer '+status);
	
	if(silverlight){
		videoPlayer.showBuffer(status);
	}
}


function enableControls(status, adPlaying){
	debugThis('enableControls '+status);
	
	if(silverlight){
		controls.enableControls(status, adPlaying);
	}
}



function enableDragging(status){
	debugThis('enableDragging '+status);
	if(silverlight){
		controls.enableDragging(status);
	}
}


function maximizeWmv(){
	debugThis('maximizeWmv ');
	if(silverlight){
		
	}else{
		maximizeWmp();
	}
}


function minimizeWmv(){
	debugThis('minimizeWmv ');
	if(silverlight){
		videoPlayer.minmizeSl();
	}else{
		minmizeWmp();
	}
}



function getFullscreenWmv(){
	debugThis('getFullscreenWmv ');
	var isFullscreen = false;
	
	if(silverlight){
		isFullscreen = videoPlayer.getFullscreenSl();
	}else{
		isFullscreen = getFullscreenWmp();
	}
	
	return isFullscreen;
}


function getBuffer(){
	debugThis("getBuffer ");
	var buffering;
	if(silverlight){
		buffering = videoPlayer.getBufferSl();
	}else{
		buffering = getBufferWmv();
	}
	debugThis("getBuffer "+buffering);
	return buffering;
}

function enableBandWidth(status){
	controls.enableBandWidth(status);
}

function maximizeFlv(){
	var objFlash = getObject("objFlashPlayer");
	objFlash.goFullScreen();
	debugThis(' max flv send off ');
}


function getBufferStatus(){
	debugThis('bufferDone  '+bufferDone);
	return bufferDone;
}

function loadOverlay(url, metaData, adId, tweenCase, showBg){
	overlay.loadOverlay(url, metaData, adId, tweenCase, showBg);
}

function clearOverlay(){
	overlay.clearOverlay();
}

function showOptIn(status){
	if(status){
		overlay.showOptIn();
	}else{
		overlay.hideOptIn();
	}
}



function getStatus(){
	return playStatus;
}

function setStatus(status){
	debugThis("setStatus "+status);
	playStatus = status;
}

function resetMediaPlayer(){
	debugThis('resetMediaPlayer');
	try{
		
		loadIndexId				= -1;
		loadContentUrl			= "";
		isError					= false;
		resetPlayer				= true;

		//silverlight
		if(silverlight){
			mediaOpen = false;
			videoPlayer.resetPlayer();
			controls.resetControls();
			overlay.clearOverlay();	
			setVisibility('silverlightPlayer', false);
		}else{
			setVisibility('videoPlayer', false);
		}

	} catch(e){}
	debugThis('resetMediaPlayer done');
}


function publish247Schedule(responseArray){
	debugThis("publish247Schedule "+responseArray.length)
	debugThis(responseArray);
	var obj = getObject("movieObj");
	obj.publish247ScheduleFlash(responseArray);
}



/**
 * http://www.hypergeneric.com/corpus/javascript-inner-viewport-resize/
 * IE is limited for getting the outer window size, so we need a special method
 * to determine it, see ResizeToInner
 */

function GetInnerSize () {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

function getScrollPosition () {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [x,y];
}

// http://4umi.com/web/javascript/window.htm
function windif() {  
	var difx, dify, w = GetInnerSize();
	var winx = w[0], winy = w[1];  
	window.resizeTo( winx, winy );  
	w = GetInnerSize();  
	difx = winx - w[0]; 
	dify = winy - w[1];  
	window.resizeTo( winx + difx, winy + dify );  
	return [difx, dify]; 
}


/**
 * http://www.hypergeneric.com/corpus/javascript-inner-viewport-resize/
 * IE is limited for getting the outer window size
 * this function is a modified version of the URL above
 * the trick is, to use the maximum space available, determine the inner size
 * and then calculate
 */

function ResizeToInner (w, h, x, y) {
	

	diff = windif();
	window.resizeTo(w+diff[0], h+diff[1]);
	//window.moveTo(x,y);
	
	return diff;
}


				

/*
 * Watch this movie full blown up.
 */
function minimizeWindow(){
	if(minimize == 0){
		
		var bName = navigator.appName;
		var ver;
		var sizeX = 506;
		var sizeY = 359; //423; //377;
		var scrollToX = 519;
		var scrollToY = 115;//135;
	
		if (bName == "Microsoft Internet Explorer") {
			if (navigator.appVersion.indexOf("MSIE 7") != -1) ver = "ie7";
			else if (navigator.appVersion.indexOf("MSIE 6") != -1) ver = "ie6";	
		}
	
		if (ver == "ie7") {
			sizeX = 491;
			sizeY = 343; //405;//360;
			scrollToY = 115;//139;
		}
		else if (ver == "ie6") {
			//sizeX = 517;
			//sizeY = 360
			sizeX = 489;
			sizeY = 343; //405;//360;
			scrollToX = 520;
			scrollToY = 115;//137;
		}
		//alert (navigator.appVersion);
	
		try{
			if (minimizedState == 0) {
	
				originalPositionX = window.screenLeft || window.screenX;
				originalPositionY = window.screenTop || window.screenY;
				
				var inner=GetInnerSize();
				originalSizeX=inner[0];
				originalSizeY=inner[1];
				
				var scroll = getScrollPosition();
				originalScrollPositionX = scroll[0];
				originalScrollPositionY = scroll[1];		
				
				// double it - to take scrollbars into account (only IE)
				ResizeToInner(sizeX, sizeY);
				var diff = ResizeToInner(sizeX, sizeY);
				
				window.scrollTo(scrollToX, scrollToY);
				minimizedState=1;
			}
			else {
				ResizeToInner(originalSizeX, originalSizeY);
				minimizedState=0;
				window.scrollTo(originalScrollPositionX, originalScrollPositionY);
				window.moveTo(originalPositionX, originalPositionY);						
	
				var nowPositionX = window.screenLeft || window.screenX;
				var nowPositionY = window.screenTop || window.screenY;
				
				// to correct for IE positioning errors
				window.moveBy(originalPositionX - nowPositionX, originalPositionY - nowPositionY);
				
				ResizeToInner(originalSizeX, originalSizeY);
							
			}	
		}
		catch(e){}
		
	}else{
		
		window.open("http://www.bigpondtv.com", "bigpondTv");
		
	}
}


function maximizeWindow(){
		//try{
			var objFlash = getObject("objFlashPlayer");
			var mediaTypeNew = objFlash.getMediaType();
			
			debugThis('maximizeWindow '+mediaTypeNew)
			
			var isBillboard = objFlash.billboardShown();
			
			
			if(mediaTypeNew == 'FLV' || isBillboard == true){
				maximizeFlv();
			}else{
				maximizeWmv();
			}		
		//}
		//catch(e){}
}

function maximizeWmv(){
	document.objMediaPlayer.fullScreen=true;
}

function maximizeFlv(){
	var objFlash = getObject("objFlashPlayer");
	objFlash.goFullScreen();
}


//function that formats the time for SL set time pos
function dateFormat(originalSeconds, totalTime){
	//originalSeconds  = 20000
	var daysDifference = Math.floor(originalSeconds/60/60/24);
	originalSeconds = originalSeconds - daysDifference*60*60*24;
	var hoursDifference = Math.floor(originalSeconds/60/60);
	originalSeconds = originalSeconds - hoursDifference*60*60;
	var minutesDifference = Math.floor(originalSeconds/60);
	originalSeconds = originalSeconds - minutesDifference*60;
	var secondsDifference = Math.floor(originalSeconds);
	
	//create the extra 0's
	if(hoursDifference < 10){
		hoursDifference = "0" +hoursDifference ;
	}
	if(minutesDifference<10){
		minutesDifference = "0" + minutesDifference  ;
	}
	if(secondsDifference<10){
		secondsDifference = "0" +secondsDifference ;
	}
	
	var timeStr;
	if(totalTime > 3599){
		timeStr = hoursDifference + ":"+minutesDifference + ":"+ secondsDifference;
	}else{
		timeStr = minutesDifference + ":"+ secondsDifference;
	}
	return timeStr;
}

