/*
 * RealPlayer
 * copyright 2005 Axisto Media Ltd. All rights reserved.
 * by Andreas Rimbe, Axisto Media Ltd (andreas.rimbe@axisto.com, http://www.axisto.com/)
 *
 * v1.5 - 06-05-2005
 *
 * Creates a real player object
 *
 * Usage: function(parentDiv, id, width, height, version)
 *
 *	rp = new RealPlayer("divWhereCreateMP", width, height);
 *	rp.setUrl("");
 *
 */
function mp_onGoToURL(url, target)
{
	//alert("Url:"+url);
	try
	{
		if(mp != null)
		{
		    mp.scriptCommand(url, target);
		}
	}
	catch(e)
	{
	}
}
 
RealPlayer = function(divId, id, width, height)
{
	this.divId = divId ? divId : "video";
	this.id = id ? id : "mp";
	
	this._preRollTime = 3;
	this.hasUrlTimer = false;
	this.hasPositionTimer = false;
	this.isReady = true;
	
	this.bufferTimer = null;
	this.audioOnlyImageId = "audioonly";
	this.autoStart = true;
	this.currentPosition = 0;
	this.checkScriptCommandSupportTimer = null;
	this.scriptCommandReceived = false;
	this.bufferProgress = 0;
	this.eventTimer = null;
	this.eventUpdateInterval = 500;
	this.isSeeking = false;
	this.seekingPosition = 0;
	this.playState = 0;
	this.uiMode = "none";
	this.useCallbacks = true;
	this.width = width ? width : 320;
	this.height = height ? height : 240;
	this.videoWidth = this.width;
	this.videoHeight = this.height;
	this.origHeight = this.height;
	this.installError = "There is a problem with your Real Player plugin. To fix the problem you need to re-install your Real Player. Would you like to re-install it now?";
	this.mpInfo = detectRealPlayer();
	if(!this.mpInfo.installed)
	{
	    //force embedding of object, let browser take care of it
	    this.mpInfo.installed = true;
		this.mpInfo.scriptable = true;
		//this.mpInfo.type = "ActiveX";
	}
}
RealPlayer.prototype.createHtml = function()
{
	var html = "";
	var w = this.width;
    var h = this.height;
	if(this.uiMode == "invisible")
    {
        w = 2;
        h = 2;
    }
	if(isIE && this.width > 1)
	{
		html = '<object id="'+this.id+'" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"';
		html += ' width="'+w+'" height="'+h+'"';
		html += ' codebase="http://www.real.com/products/player/index.html" VIEWASTEXT>';
		html += ' <param name="AUTOGOTOURL" value="FALSE" />';
		html += ' <param name="AutoStart" value="'+this.autoStart.toString()+'" />';
		html += ' <param name="CENTER" value="false" />';
		if(this.uiMode == "full")
		{
			html += ' <param name="CONTROLS" value="ImageWindow,StatusBar,ControlPanel" />';
		}
		else if(this.uiMode == "audio")
		{
			html += '<param name="CONTROLS" value="StatusBar,ControlPanel" />';
		}
		else
		{
			html += '<param name="CONTROLS" value="ImageWindow" />';
		}
		if(this.url != null)
		{
			html += '<param name="SRC" value="'+this.url+'" />';
		}
		html += '<param name="enablejavascript" value="true" />';
		html += '<param name="scriptcallbacks" value="OnGotoURL" />';
		
		html += ' </object>';
	}
	else
	{
		html += '<embed id="'+this.id+'" name="'+this.id+'" type="audio/x-pn-realaudio-plugin" width="'+w+'" height="'+h+'" console="one' +this.id + '"';
		if(this.uiMode == "full")
		{
			html += ' controls="ImageWindow,StatusBar,ControlPanel"'; 
		}
		else if(this.uiMode == "audio")
		{
			html += ' controls="StatusBar,ControlPanel"';
		}
		else
		{
			html += ' controls="ImageWindow"';
		}
		
		//if(!this.autoStart)
		html += ' autostart="' + this.autoStart.toString() + '"';
		if(this.url != null)
		{
			html += ' src="' + this.url + '"';
		}
		html += ' autogotourl="false"';
		html += ' enablejavascript="true"';
		html += ' scriptcallbacks="OnGotoURL"';
		html += '></embed>';
	}
	//callbacks
	//JavaScript
	
	if(this.useCallbacks)
	{
		//VBScript
		if(isIE)
		{
			/*
			html += '\n<script language="vbscript">';
			html += '\n\tSub mp_OnBuffering(ByVal lFlags, ByVal lPercentage)';
			html += '\n\t\tcall player_event("setBuffering", lFlags, lPercentage)';
			html += '\n\tEnd Sub';
			html += '\n<script language="vbscript">';
			html += '\n\tSub mp_OnPlayStateChange(ByVal lNewState)';
			html += '\n\t\tcall player_event("setPlayState", lNewState)';
			html += '\n\tEnd Sub';
			*/
			
			/*
			html += '<script language="vbscript">';
			html += '\n';
			html += 'Sub mp_OnGotoURL(ByVal url, ByVal target)';
		    html += '\n\t';
			html += 'call player_event("scriptCommand", url, target)';
			html += '\n';
			html += 'End Sub';
			html += '\n';
			html += '</script>';
			*/
			
			/*
		    html += '\n<script language="javascript" for="'+this.id+'" event="OnBuffering(flags, percent_complete)">';
		    html += '\n\tplayer_event("setBuffering", flags, percent_complete);';
		    html += '\n</script>';
		    html += '\n<script language="javascript" for="'+this.id+'" event="OnPlayStateChange(state)">';
		    html += '\n\talert("playstatechnage");';
		    html += '\n\tplayer_event("setPlayState", state);';
		    html += '\n</script>';
		    */
		    
		    /*
		    html += '\n<script language="javascript" for="'+this.id+'" event="OnGotoURL(url, target)">';
		    html += '\n\tplayer_event("scriptCommand", url, target);';
		    html += '\n</script>';
		    */
		}
	}
	//alert(html);
	return html;	
}
RealPlayer.prototype.checkState = function()
{
	//alert("Check state");
	var y = 0;
	var firePositionChange = false;
	//get position
	try
	{
	    y = this.getPosition();
	}
	catch(e)
	{}
	
	if(y != null && y >= 0 && y != this.currentPosition)
	{
		if(this.forcePosition != null)
		{
			this.forcePosition = null;
		}
		if(this.isSeeking)
		{
			if(Math.abs(y-this.currentPosition) < 2)
			{
				this.isSeeking = false;
				this.currentPosition = y;
				this.setPlayState(3);
				firePositionChange = true;
			}
		}
		else
		{
			this.currentPosition = y;
			this.setPlayState(3);
			firePositionChange = true;
		}
		
		if(isLive)
		{
		    if(this.checkScriptCommandSupportTimer == undefined)
		    {
		        //this.checkScriptCommandSupportTimer = setTimeout(this.id+".checkScriptCommandSupport()", 10000);
		    }
		}
	}
	else if(y == this.currentPosition)
	{
		this.setPlayState(4);
	}
	
	if(this.forcePlayState != null && this.forcePlayState != this.playState)
	{
		//alert('force '+this.forcePlayState);
		if(this.forcePlayState == 1)
		{
			this.stop();
		}
		else if(this.forcePlayState == 2)
		{	
			this.pause();
		}
		else if(this.forcePlayState == 3)
		{
			this.play();
		}
	}
	else
	{
		this.forcePlayState = null;
	}
	
	this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
	
	if(firePositionChange)
	{
	    try
		{
		    this.positionChange(this.currentPosition);
		}
		catch(e)
		{
		}
	}
}
RealPlayer.prototype.checkScriptCommandSupport = function()
{
    if(!this.scriptCommandReceived)
    {
        var msg = "There is a problem with your Real Player installation. Would you like to download Real Player?";
	    var yes = confirm(msg);
	    if(yes)
	    {
	        window.location.href = "http://uk.real.com/home/";
	    }
    }
}
RealPlayer.prototype.destroy = function()
{
	if(this.eventTimer != null)
	{
		clearTimeout(this.eventTimer);
		this.eventTimer = null;
	}
	this.mp = findObj(this.id);
	if(isIE && this.mp != null)
	{
		//this.stop();
		var p = findObj(this.divId);
		try
		{
			p.removeChild(this.mp);
		}
		catch(e)
		{
		}
		this.mp = null;
	}
	else
	{
		this.mp = null;
	}
}
RealPlayer.prototype.getDuration = function()
{
	return (this.mp.GetLength()/1000);
};
RealPlayer.prototype.setPlayState = function(s)
{
	//alert("real player playstate "+s);
	var state = 0;
	try
	{
		switch(s)
		{
			case 0:
			{
				state = 1;
				break;
			}
			case 1:
			{
				state = 9;
				break;
			}
			case 2:
			{
				state = 6;
				break;
			}
			case 3:
			{
				state = 3;
				break;
			}
			case 4:
			{
				state = 2;
				break;
			}
			case 5:
			{
				state = 7;
				break;
			}
			case 6:
			{
				state = 10;
				break;
			}
			default:
			{
				//alert("real player playstate "+s);
				break;
			}
		}
	}
	catch(e)
	{
	}
	if(state != null && (state == 6 || state != this.playState))
	{
		this.playState = state;
		if(this.playStateChange != null)
		{
			this.playStateChange(this.playState);
		}
	}
};
RealPlayer.prototype.getPosition = function()
{
	return (this.mp.GetPosition()/1000);
};
RealPlayer.prototype.setPosition = function(time)
{
	if( Math.abs(time*1000-this.mp.GetPosition()) > 1000)
	{
		this.isSeeking = true;
		this.seekingPosition = time;
		this.currentPosition = time;
		this.mp.SetPosition(time*1000);
	}
};
RealPlayer.prototype.getVolume = function()
{
	return this.mp.GetVolume();
}
RealPlayer.prototype.setVolume = function(level)
{
	try
	{
	    this.mp.SetVolume(level);
	}
	catch(ex)
	{
	}
}
RealPlayer.prototype.getErrorDescription = function()
{
	//return "";
}
RealPlayer.prototype.stop = function()
{
	try
	{
		this.mp.DoStop();
		this.forcePlayState = 1;
	}
	catch(ex)
	{
	
	}
}
RealPlayer.prototype.pause = function()
{
	try
	{
		this.mp.DoPause();
		this.forcePlayState = 2;
	}
	catch(ex)
	{
	
	}
}
RealPlayer.prototype.play = function()
{
    if(this.playState != 3)
    {
        try 
        {
            this.mp.DoPlay();
        }
        catch (e) 
        {
            try 
            {
                this.mp.Play();
            }
            catch (e) 
            {
            }
        }
    }
    else
    {
        this.pause();
    }
    
    if(this.useCallbacks && this.eventTimer == null)
    {
	    this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
    }	
}
RealPlayer.prototype.setBuffering = function(started, p)
{
	//alert("RP Buffering Update "+percentage);
	if(p >= 0 && p <= 100)
	{
		this.bufferingProgress = p;
	}
	else
	{
		this.bufferingProgress = 0;
	}
}
RealPlayer.prototype.error = function(errorMsg)
{
	alert("Real Player: " + errorMsg);
}
RealPlayer.prototype.setUrl = function(url)
{
	this.url = url;
	this.sendPlayStateEvents = true;
	if(this.mp != null)
	{
		try
		{
		    this.mp.SetAutoGoToUrl(false);
		}
		catch(e)
		{
		    try
		    {
		        this.mp.SetAutoGoToURL(false);
		    }
		    catch(e2)
		    {
		        
		    }
		}
		
		this.mp.AutoGoToUrl = false;
		
		try
		{
		    this.mp.SetSource(this.url);
		}
		catch(e)
		{
		}
		
		if(this.useCallbacks && this.eventTimer == null)
		{
			this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
		}
	}
}
RealPlayer.prototype.scriptCommand = function(type, data)
{
	//alert("RP: ScriptCommand " + type + " " + data);
	if(this.scriptChange != null)
	{
	    var data2 = type;
		if(type.lastIndexOf("/broadcast/") > 0)
		{
			data2 = type.substring(type.lastIndexOf("/broadcast/")+11)
		}
		var script = data2.split(":");
		if(script.length > 1)
		{
		    this.scriptChange(script[0], script[1]);
		}
	}
}
RealPlayer.prototype.setSize = function(w, h)
{
	if(this.mp != null && w > 0 && h > 0)
	{
	    this.width = w;
	    this.height = h;
		if(document.all && !document.getElementById)
		{
			document.all[this.id].style.pixelWidth = w;
			document.all[this.id].style.pixelHeight = h;
		}
		else
		{
			document.getElementById(this.id).style.width = w+"px";
			document.getElementById(this.id).style.height = h+"px";
		}
	}
	else
	{
		this.height = 1;
		this.width = 1;
	}
}
RealPlayer.prototype.setZoom = function(p)
{
	var w = this.videoWidth*(p/100.0);
	w = w>0?w:1;
	var h = this.videoHeight*(p/100.0);
	h = h>0?h:1;
	
	var stretchToFit = false;
	if(p > 100)
	{
	    stretchToFit = true;
	}
	
	try
	{
	    this.mp.SetCenter(stretchToFit);
	}
	catch(ex){}
	
	if(this.uiMode != "invisible")
	{
	    this.setSize(w, h);
	}
	else
	{
	    if(this.mp != null)
		{
		    this.mp.style.visibility = 'hidden';
		}
	}

	var playerDiv = document.getElementById(this.divId);
	if(w <= 2 && h <= 2 && playerDiv != null || p <= 10)
	{
	    playerDiv.style.visibility = "hidden";
	}
	else if(playerDiv != null)
	{
	    playerDiv.style.visibility = "visible";
	}
	
	//hide audioonly image
	var img = findObj(this.audioOnlyImageId);
	if(p == 0 && img != null)
	{
		img.style.display = 'none';
	}
	else if(this.uiMode == "audio" || this.uiMode == "invisible" || showAudioImage)
	{
		img.style.display = 'block';
	}
}
RealPlayer.prototype.setUIMode = function(mode)
{
	if(this.uiMode != mode)
	{
		this.uiMode = mode;
		var img = findObj(this.audioOnlyImageId);
		switch(mode)
		{
			case "audio":
			{
				this.height = 70;
				break;
			}
			case "none":
			{
				this.height = this.origHeight;
				break;
			}
			case "full":
			{
				this.height = this.origHeight+70;
				break;
			}
			case "invisible":
			{
				this.setZoom(0);
				break;
			}
		}
		if(this.mp != null)
		{
			this.setSize(this.width, this.height);
		}
		if(img != null && (this.uiMode == "audio" || this.uiMode == "invisible" || showAudioImage) && img.src != "")
		{
			img.style.display = 'block';
			img.style.visibility = 'visible';
		}
		else if(img != null)
		{
			img.style.display = 'none';
			img.style.visibility = 'hidden';
		}
		//audio - show image and controls
		//none - video window only
		//full - with controls
		//invisible
	}
}
RealPlayer.prototype.setImage = function(url, alt)
{
	var img = findObj("audioonly-image");
	if(img != null && url != null && url != "")
	{
		//alert(url);
		img.src = url;
		if(alt != null && alt != "")
		{
		    img.setAttribute('alt', "Image of " + alt);
		    img.setAttribute('title', alt);
		}
		if(this.uiMode == "audio" || this.uiMode  == "invisible")
		{
			img.style.display = 'block';
		}
	}	
}
RealPlayer.prototype.write = function()
{
	this.mp = $(this.id);
	if(this.mp != null)
	{
		this.destroy();
	}
	var s = this.createHtml();
	writeLayer(this.divId, null, s);
	
	this.mp = $(this.id);
	//Event.observe(this.id, 'GotoURL', mp_onGoToURL, true);
	
	if(this.useCallbacks && this.eventTimer == null)
	{
		this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
	}
}

document.write('<script language="javascript" for="mp" event="OnGotoURL(url, target)" >player_event("scriptCommand", url, target);</script>');