//
// Initialization script for Unreal Media Player 5.0a
// Compatible with Internet Explorer and Mozilla
//
// This code is Copyright 2007 by System Support Services
// for the exclusive use by Mare Stare, LLC. No portion of 
// this code may be used without the express written consent
// of System Support Services AND Mare Stare, LLC.
//
// This code has the fixes for Firefox to resize and to display errors.

var isIE  = window.ActiveXObject != null;
var isMoz = document.implementation && document.implementation.createDocument;
var details = navigator.userAgent;
var is9x = false;
var Player;

function EventHandler()
{
    // Default event handlers - can be overridden
    this.OnResize = function OnResize(width, height) {
    	if (isMoz) {
    		var o = document.getElementById(Player);
				o.width  = width;
				o.height = height; 
				if (width > 380) {
					document.getElementById("panel").style.width = (width + 20)+"px";
				} else {
					document.getElementById("panel").style.width = "400px";
				}
    	}
		}
    this.OnStart = function OnStart() { }
    this.OnPause = function OnPause(state) { }
    this.OnStop = function OnStop(toBeContinued) { }
    this.OnError = function OnError(errorType, errorID) {	
    	if (isMoz) {
    		if ((errorType == 1) && (errorID == 1004)) {
				p = document.getElementById(Player);
				p.Stop();
				p.UseHTTP();
				p.Play();

			} else {
   			p.Stop();
				// alert("There was a problem connecting to this Live Source."); 
  		}
    }
  } 
}

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)
        {
 						if (details.indexOf("9x") 						!=-1) is9x = true;
						else if (details.indexOf("Windows ME")!=-1) is9x = true;
						else if (details.indexOf("95")        !=-1) is9x = true;
						else if (details.indexOf("98")        !=-1) is9x = true;
						else if (details.indexOf("XP")        !=-1) is9x = false;
						else if (details.indexOf("NT")   	 		!=-1) is9x = false;        
						if (is9x) 
						{
            		objectHTML += " classid=\"clsid:CA11EB7C-1C85-4577-8A49-9E28EFB30184\" ";
								objectHTML += " codebase=\"http://www.umediaserver.net/bin/UMediaControl4.cab#version=4,0,0,0\">";
						}
						else 
						{
        				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='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 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();
	}
}

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 5.0" : "http://www.umediaserver.net/bin/UMediaPlayer5.xpi" };
  xpi = { "Unreal Media Player 5.0" : "http://www.csystems.co.il/umediaplayer/download/UMediaPlayer5.xpi" };
    InstallTrigger.install(xpi, installCallback);
}


function Play(player, alias)
{
	Player = player;
	try
	{
		var o = document.getElementById(Player);
		
		o.UseCustomLogo(loadImage);
		o.UseMediaServer(mediaServer);
		
		o.UseTCP();
		o.UseLiveAlias(alias);
		o.EnableFlowControl(true, true);
		o.UseCustomErrorHandler(true);

		InitPlayer(Player);
		o.Play();
		window.status = "Connecting...";
	}
	catch (e)
	{
		var code = "Play(\"" + Player + "\")";
  		setTimeout(code, 1000);
		window.status = "Player object wasn't created";
	}
}

//Specify which events we want to subscribe to in each player instance.
function InitPlayer(Player)
{
    var pobj = new PlayerObject(Player);
    pobj.object = document.getElementById(Player);
    pobj.initPlayer()
}


// Only for Mozilla browsers
function OnInstall()
{
	alert("Installation succeeded, the web page will now reload.");
	window.location.reload();
}


/* -- ONSTART EVENT HANDLER -- */

function OnStart()
{
	window.status = "Playing";
}

/* -- ONSTOP EVENT HANDLERS -- */

function OnStop(toBeContinued, player)
{
	window.status = "Stopped";
	if(!toBeContinued)
   	{
     	   var o = document.getElementById(player);
     	   // o.width  = 320;
     	   // o.height = 265;
   	}
}

function OnStop1(toBeContinued)
{
	OnStop(toBeContinued, "player1");
}
function OnStop2(toBeContinued)
{
	OnStop(toBeContinued, "player2");
}
function OnStop3(toBeContinued)
{
	OnStop(toBeContinued, "player3");
}
function OnStop4(toBeContinued)
{
	OnStop(toBeContinued, "player4");
}


/* -- ONPAUSE EVENT HANDLER -- */

function OnPause(state)
{
	if (state == 0)
		window.status = "Paused";
	else if (state == 1)
		window.status = "Buffering...";
	else if (state == 2)
		window.status = "Seeking...";
}


/* -- RESIZE EVENT HANDLERS -- */

function OnResize1(width, height)
{
	Player = "player1";
  var o = document.getElementById(Player);
	o.width  = width;
	o.height = height;
}
function OnResize2(width, height)
{
	Player = "player2";
  var p = document.getElementById(Player);
	p.width  = width;
	p.height = height;
}
function OnResize3(width, height)
{
	Player = "player3";
  var q = document.getElementById(Player);
	q.width  = width;
	q.height = height;
}
function OnResize4(width, height)
{
	Player = "player4";
  var r = document.getElementById(Player);
	r.width  = width;
	r.height = height;
}


/* -- ONERROR EVENT HANDLERS -- */

function OnError1(errorType, errorID)
{
	if ((errorType == 1) && (errorID == 1004)) {
		p = document.getElementById("player1");
		p.Stop();
		p.UseHTTP();
		p.Play();

	} else {
   	p.Stop();
		//alert("There was a problem connecting to this Live Source."); 
  }
}

function OnError2(errorType, errorID)
{
	if ((errorType == 1) && (errorID == 1004)) {
		q = document.getElementById("player2");
		q.Stop();
		q.UseHTTP();
		q.Play();
	} else {
   	alert("There was a problem connecting to this Live Source."); 
  }
}

function OnError3(errorType, errorID)
{
	if ((errorType == 1) && (errorID == 1004)) {
		r = document.getElementById("player3");
		r.Stop();
		r.UseHTTP();
		r.Play();
	} else {
   	alert("There was a problem connecting to this Live Source."); 
  }
}

function OnError4(errorType, errorID)
{
	if ((errorType == 1) && (errorID == 1004)) {
		s = document.getElementById("player4");
		s.Stop();
		s.UseHTTP();
		s.Play();
	} else {
   	alert("There was a problem connecting to this Live Source."); 
  }
}

