/*

Modified 09/03/2006

Rudi Yardley

- Solved Ticking Bug


*/


//	USED FOR DEBUGGING 
/*

debug = function(string){
	//try{
		document.getElementById("debug").innerHTML += string + "<br>"
	//}catch(e){};
}
//
*/


//	<EventBroadcaster.js>

/**
 * EventBroadcaster should work as a static object controlling events as they 
 * are called from the Flash VideoFrame. Listener heirarchy is separate here 
 * from G6 Publish interface (for now)
 */
EventBroadcaster = function()
{
	_listeners = [];
};

/**
 *	Broadcast some event
 *	Accepts an event tag which is the name of the function called to listeners.
 */
EventBroadcaster.prototype.broadcastEvent = function(eventTag)
{
    var argArray = [];

	for (var i = 1; i < arguments.length; ++i)
    {
        argArray.push(arguments[i]);
    }
	
	i = _listeners.length;
	while(i--)
	{
		var curListener = _listeners[i];
		if (typeof(curListener[eventTag]) == "function")
		{
			var functionToCall = curListener[eventTag];
			functionToCall.apply(curListener, argArray);
		}
	}
};

/**
 *	Add a listener
 */
EventBroadcaster.prototype.addListener = function(listener)
{
	_listeners.push(listener);
};

/**
 *	Remove a listener
 */
EventBroadcaster.prototype.removeListener = function(listener)
{
	i = _listeners.length;
	while(i--)
	{
		if(_listeners[i] == listener)
		{
			_listeners.splice(i, 1);
		}
	}
};


//	</EventBroadcaster.js>

//	<FJSEventBroadcaster.js>

/**
 *	window level function that passes the event to the event broadcaster (for flash use)
 */
FJSEventBroadcaster_broadcastEvent = function()
{
    var functionToCall = FJSEventBroadcaster.broadcastEvent;
    var argArray = [];
    for (var i = 0; i < arguments.length; ++i)
    {
        argArray.push(arguments[i]);
    }
	//debug("FJSEventBroadcaster_broadcastEvent:"+argArray[0]);
    functionToCall.apply(functionToCall, argArray);
}

FJSEventBroadcaster = new EventBroadcaster();

//	</FJSEventBroadcaster.js>



var DataBuffer = function()
{
	this.data = [];
}

DataBuffer.prototype.addData = function(name, value)
{
	var temp={};
	temp[name] = value;
	this.data.push(temp);
}

DataBuffer.prototype.hasContent = function()
{
	return this.data.length > 0;
}

DataBuffer.prototype.getItem = function(name)
{
	var output = null;
	var i = this.data.length;
	while(i--)
	{
		if (typeof(this.data[i][name]) != "undefined")
		{
			output = this.data[i][name];
		}
	}
	return output;
}

//	<Currentmedia.js>


var Fullscreen = function(videoFrame)
{
	//	Fullscreen object construction launches new window
	this._parentVideoFrame = videoFrame;
	videoref = this._parentVideoFrame;
	fullscreenref = this;
	
	FJSEventBroadcaster.addListener(this);
	this.launchWindow();
}

Fullscreen.prototype.launchWindow = function()
{

	//	Open a new JS Window with a copy of the flash player inside
	//	Pass all the same values to it.
	
	this._parentVideoFrame.pause();	
	if(typeof(this._fullscreenWindow) == 'undefined' || typeof(this._fullscreenWindow) == 'null')
	{
		this._fullscreenWindow =  window.open("","fsWindow","status=no,menubar=no,location=no,left=0,top=0,resizable=0,height=" + screen.availHeight + ",width=" + screen.availWidth);
		
		//	Write to the popup
		var d = this._fullscreenWindow.document;
		d.write("<html>");
		d.write("<head>");
		d.write("<scr"+"ipt>");
		
		d.write("reportHome = function(eventString)");
		d.write("{");
		d.write("	opener.FJSEventBroadcaster_broadcastEvent(eventString);");
		d.write("};");
		
		d.write("getOpenerFsRef = function()");
		d.write("{");
		d.write("	if (opener.fullscreenref)");
		d.write("	{");
		d.write("		return opener.fullscreenref;");
		d.write("	};");
		d.write("};");
		
		d.write("<\/scr"+"ipt>");
		d.write('<style>*{margin:0px;padding:0px;background-color:#000;}</style>');	
		d.write("</head>");
		d.write("<body onunload='reportHome(\"onFullscreenClose\");' onblur='reportHome(\"onFullscreenBlur\");'>");
		d.write("<iframe id='myiframe' src='/g6publish/common/flash/Components/VideoFrame/fs_window.htm' marginwidth=0 marginheight=0 scrolling=no hspace=0 width=100% height=100% frameborder=0></iframe>");
		d.write("</body>");
		d.write("</html>");	
		d.close();	
	}
}

Fullscreen.prototype.onFullscreenClose = function()
{
	this._parentVideoFrame.setClipIndex(this._clipIndex);
}

Fullscreen.prototype.onFullscreenBlur = function()
{
	//NN Bug fix 747
	if((this._fullscreenWindow != null) && typeof(this._fullscreenWindow) != 'undefined')
		this._fullscreenWindow.close();
	this._fullscreenWindow = null;
}

Fullscreen.prototype.setClipIndex = function(clipIndex)
{
	this._clipIndex = clipIndex;
}

/**
 *	Currentmedia Class
 *	Class that contains information about the currently playing clip
 */
var Currentmedia = function(videoFrame)
{
	this._videoFrame = videoFrame;
	this._currentMedia = {};
	FJSEventBroadcaster.addListener(this);
}

/**
 *	Event Handler 
 */
Currentmedia.prototype.onGetCurrentmedia = function(currentMedia, currentPlayList)
{
	if(typeof(currentMedia) == "object")
	{
		this._currentMedia = currentMedia;
	}
}

/**
 *	Returns the item in the _currentMedia collection referenced by the infoTag
 */
Currentmedia.prototype.getItemInfo = function(infoTag)
{
	//	HACK: 	NETSACAPE 
	//			(abstract is a reserved word in a netscape javascript object 
	//			property and cannot be successfully parsed as an object property)
	//			Translate abstract to description
	
	if (infoTag.toLowerCase()=="abstract")
	{
		infoTag = "description";
	}	
	
	//	END HACK
	
	var output = null;
	for(var elem in this._currentMedia)
	{
		if (elem.toLowerCase() == infoTag.toLowerCase())
		{
			output = this._currentMedia[elem];
		}
	}
	
	return output;
}

//	<Currentmedia.js>

//	<Controls.js>

/**
 *	Controls Class
 *	Class that works to organize all functions that control the video
 */
var Controls = function(videoFrame)
{
	this._videoFrame = videoFrame;
}

Controls.prototype.play = function()
{
	this._videoFrame.play();
}

Controls.prototype.pause = function()
{
	this._videoFrame.pause();
}

Controls.prototype.fastForward = function()
{
	this._videoFrame.fastForward();
}

Controls.prototype.fastReverse = function()
{
	this._videoFrame.fastReverse();
}

/**
 *	Returns the current position of the video
 */
Controls.prototype.getCurrentPosition = function()
{
	//return 
}

Controls.prototype.setCurrentPosition = function(position)
{
	this._videoFrame.setCurrentPosition(position);
}

//</Controls.js>

// <Settings.js>

/**
 *	Settings Class
 *	Class that works to organize all settings for the player.
 */
var Settings = function(videoFrame)
{
	this._videoFrame = videoFrame;
	this._volume = this._videoFrame.volume;
	this._isMuted = false;
}

Settings.prototype.mute = function()
{
	if(this._isMuted)
	{
		this._videoFrame.setVolume(this._volume);
		this._isMuted = false;
	}
	else
	{
		this._videoFrame.setVolume(0);
		this._isMuted = true;
	}
}

Settings.prototype.setVolume = function(vol)
{
	if (vol > 100)
	{
		vol = 100;
	}
	else if(vol < 0)
	{
		vol = 0;
	}
	this._volume = vol;
	this._videoFrame.setVolume(vol);
	this._isMuted = false;
}


Settings.prototype.getVolume = function(vol)
{
	return this._volume;
}

// </Settings.js>

//	<VideoFrame.js>
var vfFlashObject;
//vfFlashObject.SetReturnValue = function() {}

/**
 *	VideoFrame Class
 *	Class that acts as a wrapper to the Flash VideoFrame player.
 */
var VideoFrame = function()
{
	//	Set Defaults
	this.autoStart = true;
	this.volume = 100;
	this.url = "";
	this.flashObjectIsLoaded = false;
	this.buffer = new DataBuffer();
	this.name = "vfFlashObject";
	//	Create Controls Object
	this.currentPlaylist = null;
	this.controls = new Controls(this);
	this.settings = new Settings(this);	
	this.currentmedia = new Currentmedia(this);	
	this.network = new Object();					//	Purely used for holding buffering information
	this.universalId = new Date().getTime();
	this.isFullscreen = false;						// Default to false
	
	this.flashProxy = "";
	
	FJSEventBroadcaster.addListener(this);
	//NN Bug Fix 1261 - new attribute scale for flash players use only
	this.flashScale="";
}


/**
 *	Write the control to the document element doc or to the div with the id given
 */
VideoFrame.prototype.write = function(playerDivId)
{
	var so = new SWFObject(this.getSwfFile(), "vfFlashObjectMovie", "100%", "100%", "8", "#000000");
	if (this.flashScale !="")
	{
		so.addParam("scale", this.flashScale);
	}
	so.addParam("allowScriptAccess", "always");
	so.addParam("wmode", "transparent");
	so.addParam("quality", "high");
	so.addVariable("isInternetExplorer", ((navigator.appName.indexOf ("Microsoft") != -1) ? true : false));
	so.addVariable("lcId",this.universalId);
	//NN Bug fix 1180 -start
	//document.write(so.getSWFHTML());
	var iswritten=so.write(playerDivId);
	//NN Bug fix 1180 -end
	
}

/**
 *  Writes the value (varVal) of a variable (varName) to the _root of the Flash movie
 */
VideoFrame.prototype.setFlashVar = function(varName, varVal)
{
	if(typeof(varVal)!='undefined' && this.flashObjectIsLoaded)
	{
		this.flashProxy.setFlashVar(varName,varVal);
		//vfFlashObject.setFlashVar(varName,varVal);
	}
}

VideoFrame.prototype.getSwfFile = function()
{
	return '/g6publish/common/flash/Components/VideoFrame/VideoFrame.swf';
}

VideoFrame.prototype.play = function()
{
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.playmovie();
		//vfFlashObject.playmovie();
	}
}

VideoFrame.prototype.pause = function()
{
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.pause();
		//vfFlashObject.pause();
	}
}

VideoFrame.prototype.getClipIndex = function()
{
	var output = 0;
	if(this.currentPlayList != null)
	{
		if(typeof(this.currentPlayList.clipIndex) != "undefined")
		{
			output = this.currentPlayList.clipIndex;
		}
	}
	return output;
}

VideoFrame.prototype.setClipIndex = function(clipIndex)
{
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.setClipIndex(clipIndex);
		//vfFlashObject.setClipIndex(clipIndex);
	}
	else
	{
		this.buffer.addData("clipIndex", clipIndex);
	}
}

VideoFrame.prototype.onClipLoaded = function(playListObject)
{
	if(this.flashObjectIsLoaded)
	{
		this.currentPlaylist = playListObject;
	}
}

VideoFrame.prototype.fastForward = function()
{
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.fastForward();
		//vfFlashObject.fastForward();
	}
}

VideoFrame.prototype.fastReverse = function()
{
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.fastReverse();
		//vfFlashObject.fastReverse();
	}
}

VideoFrame.prototype.setVolume = function(vol)
{	
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.setVolume(vol);
		//vfFlashObject.setVolume(vol);
	}
}

VideoFrame.prototype.setUrl = function(url)
{
	this.url = url;
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.setUrl(url);
		//vfFlashObject.setUrl(url);
	}
	else
	{
		this.buffer.addData("url",url);
	}
}

VideoFrame.prototype.setCurrentPosition = function(position)
{
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.setPosition(position);
		//vfFlashObject.setPosition(position);
	}
}

/**
 *	FlashObject_onSynchronize is broadcast from the flash object twice a second  
 *	data from the FlashObject is stored in the relavent collections.
 */
VideoFrame.prototype.FlashObject_onSynchronize = function(reportObject)
{
	if(this.flashObjectIsLoaded)
	{
		this.controls.currentPosition 	= reportObject.controls_currentPosition;
		this.currentmedia.duration 		= reportObject.currentMedia_duration;
		this.network.bufferingProgress 	= reportObject.network_bufferingProgress;
		this.playState 					= reportObject.playState;
	}
}

VideoFrame.prototype.onGetCurrentmedia = function(currentMedia, currentPlayList)
{
	if (typeof(currentPlayList) == "object")
	{
		this.currentPlayList = currentPlayList;
		if(this.isFullscreen)
		{
			try
			{
				//	Send the clipIndex to the parent screen which inturn sends it to the parent fullscreen object
				parent.getOpenerFsRef().setClipIndex(currentPlayList.clipIndex);
			}
			catch(e){}
		}
	}
}

VideoFrame.prototype.getUrl = function()
{
	var output = null;
	
	if(this.flashObjectIsLoaded)
	{
		output = this.url;
	}
	
	return output;
}

VideoFrame.prototype.showBuffering = function()
{
	if(this.flashObjectIsLoaded)
	{
		//this.flashProxy.call("showBuffering");
	}
}

VideoFrame.prototype.hideBuffering = function()
{
	if(this.flashObjectIsLoaded)
	{
		//this.flashProxy.call("hideBuffering");
	}
}

VideoFrame.prototype.setPreloaderUrl = function(url)
{
	this._preloaderUrl = url;
	if(this.flashObjectIsLoaded)
	{
		this.flashProxy.setPreloaderUrl(url);
		//vfFlashObject.setPreloaderUrl(url);
	}
	else
	{
		//store in a buffer to be sent to the file in the VideoFrame_onLoad handler
		this.buffer.addData("preloaderUrl",url);
	}
}

VideoFrame.prototype.getPreloaderUrl = function()
{
	var output = null;

	if (typeof(this._preloaderUrl) != "undefined")
	{
		var url = this._preloaderUrl;
		if(url.split("http://").length < 2)
		{
			var urlpath = window.location.href;
			urlpathArray = urlpath.split("/");
			urlpathArray.splice(urlpathArray.length-1, 1);
			urlpath = urlpathArray.join("/");
			url = urlpath + "/" + url;
		}
		output = url;
	}
	return output;
}

VideoFrame.prototype.onFlashObjectLoaded = function()
{
	//alert("I'm loaded");
	this.buffer.addData( "autoStart", this.autoStart.toString() );

	//debug("JS:onFlashObjectLoaded");
	this.flashObjectIsLoaded = true;
	vfFlashObject = document.getElementById("vfFlashObjectMovie");
	
	this.flashProxy = vfFlashObject;
	
	if(this.buffer.hasContent())
	{
		this.flashProxy.onGetInitData(this.buffer);
	}
}


VideoFrame.prototype.doFullscreen = function()
{
	this._fullscreen = new Fullscreen(this);
}

VideoFrame.prototype.VideoFrame_Seek = function(ratio)
{
	var sec = Math.round(ratio*this.currentmedia.duration);
	this.controls.setCurrentPosition(sec)
}

//	</VideoFrame.js>

//cdebug("VideoFramePackkage");
