/// <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.addEventListener = function(eventName, handler)
{
    //todo improve logic
    this.events[eventName.toString().toLowerCase()] = handler;
}
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);
    }
}
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);
            }
        }
    }
}