// -----------------------------------------------------------------------------
//  Copyright 2009 Red Planet Marketing GmbH. All rights reserved.
// -----------------------------------------------------------------------------

var _RPXExternal = function()
{            	            
    this._tableWindows = {};  	      
};

	function _RPXExternal$_initTableScreen(width, height)
	{
		this.TABLE_DEFAULT_HEIGHT = height;
    	this.TABLE_DEFAULT_WIDTH = width;
    	this.TABLE_ASPECT_RATIO = this.TABLE_DEFAULT_WIDTH / this.TABLE_DEFAULT_HEIGHT;
		this.TABLE_MIN_HEIGHT = 350;
    	this.TABLE_MIN_WIDTH = this.TABLE_MIN_HEIGHT * this.TABLE_ASPECT_RATIO;
    	this.SCREEN_ASPECT_RATIO = window.screen.width / window.screen.height;
    
    	this._windowOffsetHeight = -1;
    	this._windowOffsetWidth = -1;    	
    	this._windowCurrentHeight = this.TABLE_DEFAULT_WIDTH;
    	this._windowCurrentWidth = this.TABLE_DEFAULT_HEIGHT;
	}

    function _RPXExternal$_calculateWindowOffset()
    {
    	
    	if (window.outerHeight && window.outerWidth){
    		this._windowOffsetHeight = window.outerHeight - window.innerHeight;
            this._windowOffsetWidth = window.outerWidth - window.innerWidth;
    	}
    	else if (window.resizeTo)
        {
            window.resizeTo(this.TABLE_DEFAULT_WIDTH, this.TABLE_DEFAULT_HEIGHT);

            this._windowOffsetHeight = this.TABLE_DEFAULT_HEIGHT - window.document.documentElement.clientHeight;
            this._windowOffsetWidth = this.TABLE_DEFAULT_WIDTH - window.document.documentElement.clientWidth;
        }
    }

    function _RPXExternal$closeAllTableWindows()
    {
        for (var i in this._tableWindows)
        {
            this._tableWindows[i].close();
            
            this._tableWindows[i] = null;
            
            delete this._tableWindows[i];
        }
    }

    function _RPXExternal$closeWindow()
    {
        window.close();
    }

    function _RPXExternal$focusOpenerWindow()
    {
        window.opener.focus();
    }

    function _RPXExternal$focusWindow()
    {
        window.focus();
    }

    function _RPXExternal$processTableClose(tableID)
    {
        this._tableWindows[tableID] = null;

        delete this._tableWindows[tableID];
    }

    function _RPXExternal$onTableWindowClose(tableID)
    {
        window.opener.document.getElementById("lobbyApplication").closeTable(tableID);
    }

    function _RPXExternal$onTableWindowResize()
    {
        window.clearTimeout(this._resizeTimer);

        this._resizeTimer = window.setTimeout(function() { RPXExternal.restoreTableWindowAspectRatio(); }, 300);
    }

    function _RPXExternal$openTableWindow(tableID)
    {
        var tableWindow = this.openWindow("cardroom.php?tableID=" + tableID, "rpxTable" + tableID,
            "height=548,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no,width=790");

        if (tableWindow)
        {
            tableWindow.focus();

            this._tableWindows[tableID] = tableWindow;
        }
        else
            swfobject.getObjectById("lobbyApplication").closeTable(tableID);
    }
    
    function _RPXExternal$openTableHistoryWindow(ID, gameID)
    {
    	
    	var tableHistoryWindow = this.openWindow("tableHistory.htm?ID=" + ID + "&gameID=" + gameID, "rpxTableHistory" + gameID,
        	"height=574,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no,width=790");
    	
    }

    function _RPXExternal$openWindow(url, name, features)
    {
        var win = window.open(url, name, features);

        if (!win)
            window.alert("The window has been blocked by pop-up blocker.\nPlease enable pop-ups for this site and try again.");

        return win;
    }

    function _RPXExternal$minimizeTableWindow()
    {
        this.resizeWindow(576.600, 400);
    }

    function _RPXExternal$resizeWindow(width, height, target)
    {
        target = target || window.self;
    
        if (window.resizeTo)
        {
            if (this._windowOffsetWidth == -1)
                this._calculateWindowOffset();
            
            window.clearInterval(this._ieTimer);
            
            try
            {
                target.resizeTo(width + this._windowOffsetWidth, height + this._windowOffsetHeight);
            }
            catch (e)
            {
                this._ieTimer = window.setInterval(function () { RPXExternal.resizeWindow(width, height, target); }, 100);
            }
        }
        else
        {
            target.innerHeight = height;
            target.innerWidth = width;
        }
    }

    function _RPXExternal$restoreTableWindowAspectRatio()
    {
        var newHeight = window.innerHeight || window.document.documentElement.clientHeight;
        var newWidth = window.innerWidth || window.document.documentElement.clientWidth;

        if (Math.abs(this._windowCurrentHeight - newHeight) > Math.abs(this._windowCurrentWidth - newWidth))
            newWidth = Math.round(newHeight * this.TABLE_ASPECT_RATIO);
        else
            newHeight = Math.round(newWidth / this.TABLE_ASPECT_RATIO);

        if (this.SCREEN_ASPECT_RATIO > this.TABLE_ASPECT_RATIO && (newHeight > window.screen.height || newWidth > window.screen.width))
        {
            newHeight = window.screen.height;
            newWidth = Math.round(newHeight * this.TABLE_ASPECT_RATIO);
        }

        if (this.SCREEN_ASPECT_RATIO < this.TABLE_ASPECT_RATIO && (newHeight > window.screen.height || newWidth > window.screen.width))
        {
            newWidth = window.screen.width;
            newHeight = Math.round(newWidth / this.TABLE_ASPECT_RATIO);
        }

        this._windowCurrentHeight = newHeight = Math.max(newHeight, this.TABLE_MIN_HEIGHT);
        this._windowCurrentWidth = newWidth = Math.max(newWidth, this.TABLE_MIN_WIDTH);

		if(newWidth>40) this.resizeWindow(newWidth, newHeight);
    }

    function _RPXExternal$restoreTableWindow()
    {
        this.resizeWindow(this.TABLE_DEFAULT_WIDTH, this.TABLE_DEFAULT_HEIGHT);
    }

    function _RPXExternal$setWindowTitle(value)
    {
        window.document.title = value;
    }

    function _RPXExternal$tileTableWindows()
    {
        var index = 0;
        var windowCount = 0;
        
        for (var i in this._tableWindows)
            windowCount++;
        
        var columnCount = (this.SCREEN_ASPECT_RATIO > 1 ? Math.ceil : Math.floor)(Math.sqrt(windowCount));
        var windowWidth = window.screen.width / columnCount;
        var windowHeight = windowWidth / this.TABLE_ASPECT_RATIO;
        
        for (var i in this._tableWindows)
        {
            this.resizeWindow(windowWidth, windowHeight, this._tableWindows[i]);
            
            this._tableWindows[i].moveTo((index % columnCount != 0 ? windowWidth : 0) * (index % columnCount), Math.floor(index / columnCount) * windowHeight);

            index++;
        }
    }

_RPXExternal.prototype = {
    _calculateWindowOffset: _RPXExternal$_calculateWindowOffset,
    closeAllTableWindows: _RPXExternal$closeAllTableWindows,
    closeWindow: _RPXExternal$closeWindow,
    focusOpenerWindow: _RPXExternal$focusOpenerWindow,
    focusWindow: _RPXExternal$focusWindow,
    minimizeTableWindow: _RPXExternal$minimizeTableWindow,
    processTableClose: _RPXExternal$processTableClose,
    onTableWindowClose: _RPXExternal$onTableWindowClose,
    onTableWindowResize: _RPXExternal$onTableWindowResize,
    openTableWindow: _RPXExternal$openTableWindow,
    openTableHistoryWindow: _RPXExternal$openTableHistoryWindow,
    openWindow: _RPXExternal$openWindow,
    resizeWindow: _RPXExternal$resizeWindow,
    restoreTableWindow: _RPXExternal$restoreTableWindow,
    restoreTableWindowAspectRatio: _RPXExternal$restoreTableWindowAspectRatio,
    setWindowTitle: _RPXExternal$setWindowTitle,
    tileTableWindows: _RPXExternal$tileTableWindows,
	 initTableScreen: _RPXExternal$_initTableScreen
};

var RPXExternal = new _RPXExternal();
