/*---CPopup.js----*/

var CPopup = new _CPopup();

function _CPopup()
{
    this._windowName = "PopupPoza";
    this._popup = null;
    this.WIDTH = 500;
    this.HEIGHT = 300;
    this.open = _CPopup_open;
    this.close = _CPopup_close;
    this.getDimension = _CPopup_getDimension;
    this.getInstance = _CPopup_getInstance;
    this.closeAllPopups = _CPopup_closeAllPopups;
    return this;
}

function _CPopup_getInstance()
{
    return new _CPopup();
}

function _CPopup_open(url, windowProps, windowName)
{
    CPopupManager.closePopup(windowName);
    var locstr = '';
    var width = this.WIDTH;
    var height = this.HEIGHT;

    if( windowName )
    {
        this._windowName = windowName;
    }
    if( !windowProps )
    {
        windowProps = "width=" + width + ",height=" + height;
    }

    var dimension = this.getDimension(windowProps);
    if( dimension[0] != null )
    {
        width = dimension[0];
    }
    if( dimension[1] != null )
    {
        height = dimension[1];
    }

    var lx = window.screen.width/2 - width/2;
    var ly = window.screen.height/2 - height/2;
    if(navigator.userAgent.indexOf("AOL") != -1)
    {
        ly = ly - 108 ;
    }
    locstr = ",left=" + lx + ",screenX=" + lx + ",top=" + ly + ",screenY=" + ly;

    windowProps = windowProps + locstr;
    this._popup = window.open(url, this._windowName, windowProps );
    CPopupManager.addPopup(this._windowName, this._popup);
    try{ // prevent a javascript error during focus a empty window (Issue ID: INT0000337)
      this._popup.focus();
    } catch(err){   }
}

function _CPopup_close()
{
    try { // prevent a javascript error which appears for accessing this._popup properties if this popup is in the process of closing itself (Issue ID: SNF0001597)
        if( this._popup != null && ! this._popup.closed )
        {
            this._popup.close();
            CPopupManager.removePopup(this._windowName, this._popup);
        }
    }
    catch(err){ }
}

function _CPopup_closeAllPopups()
{
    CPopupManager.closeAllPopups();
}

function _CPopup_getDimension(windowProps)
{
    var dimension = new Array(2);

    if( !windowProps )
    {
        return dimension ;
    }

    var props = windowProps.split(",");
    for( var i=0; i<props.length; i++ )
    {
        var _props = props[i].split("=");
        if( _props.length == 2 )
        {
            if(_props[0] == "width")
            {
                dimension[0] = _props[1] ;
            }
            if(_props[0] == "height")
            {
                dimension[1] = _props[1] ;
            }
        }
    }

    return dimension;
}

var CPopupManager = new _CPopupManager() ;

function _CPopupManager()
{
    this._popupVector = null;
    this._popupTable = null;
    this.getAllPopups = _CPopupManager_getAllPopups;
    this.closeAllPopups =  _CPopupManager_closeAllPopups;
    this.closePopup = _CPopupManager_closePopup;
    this.addPopup = _CPopupManager_addPopup;
    this.removePopup = _CPopupManager_removePopup;
    this.indexOf = _CPopupManager_indexOf;
}

function _CPopupManager_getAllPopups()
{
    if( this._popupVector == null )
    {
        this._popupVector = new Array();
    }
    if( this._popupTable == null )
    {
        this._popupTable = new Array();
    }
    return this._popupVector;
}

function _CPopupManager_closeAllPopups()
{
    var popups = this.getAllPopups();
    for(var i=0; i<popups.length; i++)
    {
        var popup = popups[i];
        if( popup != null && popup.closed == false)
        {
            this.removePopup(popup._windowName, i);
            popup.close();
        }
    }
}

function _CPopupManager_addPopup(popupName, popup)
{
    if( this._popupVector == null )
    {
        this._popupVector = new Array();
    }
    this._popupVector[this._popupVector.length] = popup;
    if( this._popupTable == null )
    {
        this._popupTable = new Array();
    }
    this._popupTable[popupName] = popup;
}


function _CPopupManager_removePopup(popupName, index)
{
    if( this._popupVector != null && this._popupVector.length > 0 )
    {
        //Removing an element at index <index>
        if( typeof(this._popupVector.splice) == "function" )
        {
            this._popupVector.splice(index, 1); //remove one element
        }
        else
        {   //remove if value is found in this array
            for(var i=index; i<this._popupVector.length; i++)
            {
               this._popupVector[i] = this._popupVector[i+1];
            }
            this._popupVector.length = this._popupVector.length - 1;
        }
    }
    if( this._popupTable != null )
    {
        this._popupTable[popupName] = null;
    }
}

function _CPopupManager_closePopup(popupName)
{
    if( this._popupTable != null && this._popupTable.length > 0 )
    {
        var popup = this._popupTable[popupName];
        if( popup != null && popup.closed == false )
        {
            this.removePopup(popupName, this.indexOf(popup));
            popup.close();
        }
    }
}

function _CPopupManager_indexOf(object)
{
    for(var i=0; i<this._popupVector.length; i++)
    {
        if(this._popupVector[i] == object)
        {
            return i;
        }
    }
    return -1;
}

    
