/// <reference path="CuEnhancedPlayer.js" />
var _cuPlayerList = new Object();
function CastUpPlayer(container, pageID, componentID, width, height)
{
    this.PageID = this.getPageID(container, pageID);
    this.ComponentID = componentID;
    this.Communicator;
    this.events = new Object();
    _cuPlayerList[pageID] = this;
}
CastUpPlayer.prototype.getPageID = function(container, pageID)
{
    var retVal = this.getNewPageID();
    if (pageID != null)
    {
        retVal = pageID;
    }
    return retVal;
}
CastUpPlayer.prototype.addPlayer = function(frameContainerID, playerSource)
{
    var frameContainer = document.getElementById(frameContainerID);
    var playerUrl = playerSource.toString().indexOf("?") > 0 ? playerSource + "&pageid=" + this.PageID + "&componentid=" + this.ComponentID : playerSource + "?pageid=" + this.PageID + "&componentid=" + this.ComponentID;
    if (frameContainer != null)
    {
        frameContainer.src = playerUrl;
    }
}
CastUpPlayer.prototype.getNewPageID = function()
{
    return Math.ceil(Math.random() * 10000);
}
CastUpPlayer.prototype.setCommunicator = function(communicatorID)
{
    //todo improve logic
    this.Communicator = document.getElementById(communicatorID);
    if(this.Communicator!=null)
    {
	    if (this.events["onload"] != null && typeof(this.events["onload"])=="function" )
	    {
	        this.events["onload"]();
	    }
    }
}
CastUpPlayer.prototype.setSetMediaPlayerReady = function(communicatorID)
{
    //todo improve logic
    this.Communicator = document.getElementById(communicatorID);
    if (this.Communicator != null)
    {
        if (this.events["onmediaplayerready"] != null && typeof (this.events["onmediaplayerready"]) == "function")
        {
            this.events["onmediaplayerready"]();
        }
    }
}
CastUpPlayer.prototype.handleGlobalEvent = function(communicatorID, eventName, data)
{
    //todo improve logic
    this.Communicator = document.getElementById(communicatorID);
    if (this.Communicator != null)
    {
        eventName = eventName.toString().toLowerCase();
        if (this.events[eventName] != null && typeof (this.events[eventName]) == "function")
        {
            this.events[eventName](data);
        }
    }
}
CastUpPlayer.prototype.addEventListener = function(eventName, handler)
{
    //todo improve logic
    this.events[eventName.toString().toLowerCase()] = handler;
    if (eventName.toString().toLowerCase() != "onmediaplayerready")//backword compatability
    {
        if (this.Communicator != null)
        {
            this.Communicator.AddEventListener(eventName);
        }
    }
}

CastUpPlayer.prototype.removeEventListener = function(eventName, handler)
{
    this.events[eventName] = null;
}
CastUpPlayer.prototype.search = function(searchName, searchParams)
{
    if (this.Communicator != null)
    {
        this.Communicator.SendSearchEvent(searchName, searchParams);
    }
}
CastUpPlayer.prototype.sendEvent = function(eventName, eventParams) 
{
    if (this.Communicator != null) 
    {
        this.Communicator.SendCustomEvent(eventName, eventParams);
    }
}
CastUpPlayer.prototype.getPlayerCurrentPlaybackState = function()
{
    if (this.Communicator != null)
    {
        return this.Communicator.GetPlayerCurrentPlaybackState();
    }
}
function _cuPlayerRegesterCommunicator(id, pageID)
{
    if (_cuPlayerList != null)
    {
        for (var playerID in _cuPlayerList)
        {
            var player = _cuPlayerList[playerID];
            if (player!=null && player.PageID == pageID)
            {
                player.setCommunicator(id);
            }
        }
    }
}
function _cuMediaPlayerReady(id, pageID)
{
    if (_cuPlayerList != null)
    {
        for (var playerID in _cuPlayerList)
        {
            var player = _cuPlayerList[playerID];
            if (player != null && player.PageID == pageID)
            {
                player.setSetMediaPlayerReady(id);
            }
        }
    }
}
function _cuGlobalEventHandler(id, pageID,eventName,data)
{
    if (_cuPlayerList != null)
    {
        for (var playerID in _cuPlayerList)
        {
            var player = _cuPlayerList[playerID];
            if (player != null && player.PageID == pageID)
            {
                player.handleGlobalEvent(id, eventName,data);
            }
        }
    }
}
