PixelManager = new PixelManager();

function PixelManager()
{
    this.pObjects = [];

    this.deploy = function (host, port)
    {

        var pObject = this.getInstance(host, port);

        if ( pObject == undefined ){

            pObject = new Pixel(host, port);
            this.pObjects.push(pObject);

        }

        return pObject;
    };

    
    this.getInstance = function(host, port)
    {
        for (var $i = 0; $i < this.pObjects.length; $i++){
            var pObject = this.pObjects[$i];
            if (pObject.host == host && pObject.port == port){
                return pObject
            }
        }

        return undefined;
    };


}

function PixelManagerOnConnect (host, port)
{
    PixelManager.getInstance(host, port).onConnect()
}

function PixelManagerOnDisconnect(host, port)
{
    PixelManager.getInstance(host, port).onDisconnect();
}

function PixelManagerOnReceive(host, port, message)
{
    PixelManager.getInstance(host, port).onReceive(message);
}



function Pixel(host, port)
{
    this.host = host;
    this.port = port;
    this.id   = 'PixelObject@' + this.host + ':' + this.port;

    return this;
}



Pixel.prototype.connect = function ()
{
    if (typeof this.onConnect       != 'function'){ throw "onConnect not specified" }
    if (typeof this.onReceive       != 'function'){ throw "onRecieve not specified" }
    if (typeof this.onDisconnect    != 'function'){ throw "onDisconnect not specified" }

    var flashvars = {};
    flashvars.host = this.host;
    flashvars.port = this.port;

	var params = {};
	params.menu = "false";
    params.allowScriptAccess = "sameDomain";
    params.play = 'true';
    params.loop = 'false';

	var attributes = {};
	attributes.align = "middle";

	if ( $('#' + this.id).length == 0 ){
		$('#PixelObjects').append('<div id="' + this.id + '"/>');
	} else {
		$('#' + this.id).empty();
	}

	swfobject.embedSWF("/flash/SocketTransport.swf", this.id, "0", "0", "9.0.0", null, flashvars, params, attributes);
}

Pixel.prototype.sendMessage = function (message)
{
    swfobject.getObjectById(this.id).sendMessage(message);
}
     

