//
// Initialization script for Unreal Media Player 4.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:CA11EB7C-1C85-4577-8A49-9E28EFB30184\" ";
			objectHTML += " codebase=\"http://www.umediaserver.net/bin/UMediaControl4.cab#version=4,0,0,0\">";
        }
        else if (isMoz)
        {
            objectHTML += " type=\"application/x-unrealmediastream\"> ";
            objectHTML += "<h3>The Unreal Media Player 4 plugin is required in order to view our site.<br><br>";
            objectHTML += "Please <a href='javascript:InstallPlayer()'>Click here</a> to install this plugin.<br><br>";
            objectHTML += "Please reload this page after the installation completes.</h3>";

        }
        else
        {
            objectHTML += "<h1>UMediaPlayer 4.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 true;
			}

			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();
	}
}

function installCallback(url, status)
{
	// Reload the current page if installed successfully
	if (status == 0)
	{
		if (typeof(OnInstall) == "function")
			OnInstall();
    }
	else if (status == 999)
		alert("Please restart the brower in order to continue");
	else
		alert("Unreal Media Player plugin failed to install");
}

function InstallPlayer()
{
    xpi = { "Unreal Media Player 4.0" : "http://www.umediaserver.net/bin/UMediaPlayer.xpi" };
//    xpi = { "Unreal Media Player 4.0" : "http://www.csystems.co.il/umediaplayer/download/umediaplayer.xpi" };
    InstallTrigger.install(xpi, installCallback);
}

