//
// Initialization script for Unreal Media Player 5.0
// Compatible with Internet Explorer and Mozilla
//
var isIE  = window.ActiveXObject != null;
var isMoz = document.implementation && document.implementation.createDocument;

function EventHandler()
{
    // Default event handlers - can be overridden
    this.OnResize = function OnResize(width, height) { }
    this.OnStart = function OnStart() { }
    this.OnPause = function OnPause(state) { }
    this.OnStop = function OnStop(toBeContinued) { }
    this.OnError = function OnError(errorType, errorID) { }
}

function PlayerObject(id)
{
    this.object = null;
    this.id = id;
    this.eventHandler = new EventHandler();

	this.getId = function getId()
	{
		return this.id;
	}

	this.getDivId = function getDivId()
	{
		return "div" + this.id;
	}

	this.createPlayer = function createPlayer(width, height) {
	    var divTag = document.getElementById(this.getDivId());
	    if (divTag == null) {
	        document.write("<div id=\"" + this.getDivId() + "\"></div>");
	        divTag = document.getElementById(this.getDivId());
	    }

	    var objectHTML = "<obj" + "ect id=\"" + this.getId() + "\" width=" + width + " height=" + height + " ";
	    if (isIE) {
	        objectHTML += " classid=\"clsid:26522409-8BBF-4c5b-A4D3-CF4B1D6F255B\" ";
	        objectHTML += " codebase=\"http://www.umediaserver.net/bin/UMediaControl5.cab#version=5,0,0,160\">";
	    }
	    else if (isMoz) {
	        objectHTML += " type=\"application/x-umediaplayer5\" > ";
	        objectHTML += "<h3>The Unreal Media Player 5 plugin is required in order to view our site.<br><br>";
	        objectHTML += "Please <a href='http://www.umediaserver.net/umediaserver/BrowsersPlugin.html' target='_new'>Click here</a> to download and install this plugin.<br><br>";
	        objectHTML += "Please reload this page after the installation completes.</h3>";

	    }
	    else {
	        objectHTML += "<h1>UMediaPlayer 5.0 is not supported in this browser.</h1>";
	    }

	    objectHTML += " </obj" + "ect>";
	    divTag.innerHTML = objectHTML;
	    this.object = document.getElementById(this.getId());
	}

	this.testObject = function testObject()
	{
		try
		{
			if (isIE)
			{
				var test = this.object.MediaName;
				return (test != null);
			}

			var err = this.object.Ping();
			if (err == 0)
				return true;

			if (err == 1)
				alert("Mozilla failed to call UMediaPlayer. Please reinstall the plugin.")
			else if (err == 2)
				alert("Failed to create UMediaPlayer control, please reinstall the plugin.")
		}
		catch (e) { }
		return false;
	}

    this.attachEvents = function attachEvents()
    {
        if (isIE)
        {
	    	this.object.attachEvent("onresize", this.eventHandler.OnResize);
    		this.object.attachEvent("onstart", this.eventHandler.OnStart);
    		this.object.attachEvent("onpause", this.eventHandler.OnPause);
    		this.object.attachEvent("onstop", this.eventHandler.OnStop);
    		this.object.attachEvent("onerror", this.eventHandler.OnError);
        }
        else if (isMoz)
        {
	    	this.object.AttachEvents(this.eventHandler);
        }
        else
            throw("Browser is not supported");
    }

	this.initPlayer = function initPlayer()
	{
		if (this.testObject())
				this.attachEvents();
	}
}



