/*
 * 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("");
 *
 */
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.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.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.updateText = "In order to view the Real Player webcast option, you need to update your Real Player. Please select the Windows Media option or alternatively download and install Real Player. Would you like to download Real Player now?";
	this.mpInfo = detectRealPlayer();
	if(this.mpInfo.installed)
	{	
	}
	else
	{
		var yes = confirm(this.updateText);
	    if(yes)
	    {
	        window.location.href = "http://forms.real.com/real/realone/intl/intl_realone.html?dc=817816815&type=rp10_en_uk&lang=en&loc=gb&src=ZG.uk.home.home.hd.def,ZG.uk.rp10_p_f.rp10_p_f.760.rp10_plus_free";
	    }
		else
	    {
	        try
	        {
	            window.location.href = selectPlayerUrl;
	        }
	        catch(ex)
	        {	        
	        }
	    }
	}
}
RealPlayer.prototype.createHtml = function()
{
	var html = "";
	if(isIE && this.width > 1)
	{
		html = '<object id="'+this.id+'" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"';
		html += ' width="'+this.width+'" height="'+this.height+'"';
		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="true" />';
		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 += ' </object>';
	}
	else
	{
		html += '<embed id="'+this.id+'" name="'+this.id+'" type="audio/x-pn-realaudio-plugin" width="'+this.width+'" height="'+this.height+'" 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 += ' 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>';
		}
		
		//JavaScript
		//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;	
}
mp_onGoToURL = function(url, target)
{
	//alert("Url:"+url);
	try
	{
		mp.scriptCommand(url, target);
	}
	catch(e)
	{
	}
}
RealPlayer.prototype.checkState = function()
{
	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(y > this.seekingPosition)
			{
				this.isSeeking = false;
				this.currentPosition = y;
				this.setPlayState(3);
				firePositionChange = true;
			}
		}
		else
		{
			this.currentPosition = y;
			this.setPlayState(3);
			firePositionChange = true;
		}
	}
	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.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)
{
	this.mp.SetVolume(level);
}
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)
	{
		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 = 2;
		this.width = 2;
	}
}
RealPlayer.prototype.setZoom = function(p)
{
	var w = this.width*(p/100.0);
	w = w>0?w:1;
	var h = this.height*(p/100.0);
	h = h>0?h:1;
	this.setSize(w, h);

	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")
	{
		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 && (mode == "audio" || mode == "invisible") && 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)
{
	var img = findObj("audioonly-image");
	if(img != null && url != null && url != "")
	{
		//alert(url);
		img.src = url;
		if(this.uiMode == "audio" || this.uiMode  == "invisible")
		{
			img.style.display = 'block';
		}
	}	
}
RealPlayer.prototype.write = function()
{
	//alert(this.mpInfo.installed);
	if(this.mpInfo.installed)
	{
		this.mp = findObj(this.id);
		if(this.mp != null)
		{
			this.destroy();
		}
		var s = this.createHtml();
		writeLayer(this.divId, null, s);
		this.mp = findObj(this.id);
		
		try
		{
		    var test = this.mp.GetVersionInfo();
		}
		catch(e)
		{
		    var yes = confirm(this.installError);
		    if(yes)
		    {
		        window.location.href = "http://forms.real.com/real/realone/intl/intl_realone.html?dc=817816815&type=rp10_en_uk&lang=en&loc=gb&src=ZG.uk.home.home.hd.def,ZG.uk.rp10_p_f.rp10_p_f.760.rp10_plus_free";
		    }
		    else
		    {
		        try
		        {
		            window.location.href = selectPlayerUrl;
		        }
		        catch(ex)
		        {	        
		        }
		    }
		}
	}
}