var MultiStream = {};

MultiStream.Player = function( PlayerWidth, PlayerHeight )
{
	try {
	
		var test = new ActiveXObject("WMPlayer.OCX.7");
	
		var div = document.createElement("div");
		
		div.innerHTML =
		'<object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"'
		+'type="application/x-oleobject" width="'+PlayerWidth+'" height="'+PlayerHeight+'"'
		+' style="width:'+PlayerWidth+';height:'+PlayerHeight+'>'
		//+'<param name="fileName" value="'+media+'">'
		+'<param name="transparentatStart" value="true">'
		+'<param name="ShowControls" value="false">'
		+'<param name="windowlessvideo" value="1">'
		+'</ob'+'ject>'
		
		this.Element = div.getElementsByTagName("object")[0];
	
	} 
	catch ( Error )
	{
		var embed = document.createElement("embed");
		
		with ( embed )
		{
			type = "application/x-mplayer2";
			pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/download/default.asp";
			//src="http://streamingportal.multistream.co.uk/celticmediafestival/pres01/pres01.asx" ;
			width = PlayerWidth;
			height = PlayerHeight;
		}
		
		this.Element = embed;
		
	}
	
	if(typeof this.Element.Mute == "boolean") // Change this !!
	{
		this.controlSupported = true;
		
		var that = this;
		window.setInterval(function()
		{
			that._WMPListener();
			
		}, 10);
		// Hacky a bit, should use the IE PlayStateChange script event 
		
		this.addEventListener(this.States.Stopped, function()
		{
			that.fireEvents( that.States.Update );
		});

	} else {
	
		this.controlSupported = false;
	
	}
	
}
MultiStream.Player.prototype = {

	PlayerState : "temp",

	events : [],
	
	States : {
		Stopped : 0,
		Paused : 1,
		Playing : 2,
		Buffering : 3,
		StateChange : "state",
		Update : "update",
		Temp : "temp"
	},
	
	addEventListener : function ( state, func )
	{	
		if(!this.events[state])
		{	
			this.events[state] = [];
		}
		this.events[state].push(func)
	},
	
	removeEventListener : function ( state, func )
	{
		if(this.events[state])
		{
			for(var i = 0; i < this.events[state].length; i++)
			{
				if(this.events[state][i] == func)
				{
					// SPLICE BUT FAST
				}
			}
		}
	},
	
	fireEvents : function ( state )
	{
		if(this.events[state])
		{
			for(var i = 0; i < this.events[state].length; i++)
			{
				this.events[state][i]();
			}
		}
		
	},
	
	setMedia : function ( fileName )
	{
		if(this.Element.nodeName.toLowerCase() == "object")
		{
			this.Element.setAttribute("fileName",fileName)
			
		} else {
		
			this.Element.src = fileName;
			
		}

	},
	
	setSeek : function ( percent )
	{
		this.Play(); 
		this.Element.currentPosition = Math.round(percent / 100 * this.Element.SelectionEnd);
		
		if(this.PlayerState == this.States.Paused)
			this.Pause();
			
	},
	
	getSeek : function ()
	{
		if(this.Element.currentPosition && this.Element.SelectionEnd) {
			return Math.round(this.Element.currentPosition/this.Element.SelectionEnd*100);
		}
		else {
			return 0;
		}
		
	},
	
	Play : function ( fileName )
	{
		if( fileName )
			this.setMedia( fileName );
			
		this.Element.Play();
		
	},
	
	Stop : function ()
	{
		this.Element.Stop();
	},
	
	Pause : function ()
	{
		this.Element.Pause();
	},
	
	Fullscreen : function ()
	{
		this.Element.DisplaySize = 3;
	},
	
	_WMPListener : function ()
	{
		var oldState = this.PlayerState;
		this.PlayerState = this.Element.playState;
		
		if(oldState != this.PlayerState)
		{
			this.fireEvents ( this.States.StateChange );
			this.fireEvents ( this.PlayerState );
		}
		
		if(this.PlayerState == this.States.Playing)
			this.fireEvents( this.States.Update );

	}

}
