var G_PopupTimeout;
var G_IsAnimating = false;
var G_PopupTimeoutAnimation;
var G_PopupX = 0;
var G_PopupY = 0;
var G_PopupWidth;
var G_PopupHeight;
var G_OpenPopup = '';
var G_Popups = new Array();

function TimeoutPopupOpen(P_DivID)
{
    var L_StepConstant = 5;
    
    var L_PopupWidth = (1 * $(P_DivID).style.width.replace('px', ''));
    var L_PopupHeight = (1 * $(P_DivID).style.height.replace('px', ''));
    var L_PopupLeft = (1 * $(P_DivID).style.left.replace('px', ''));
    
    var L_StepX = (G_PopupWidth / L_StepConstant);
    var L_StepY = (G_PopupHeight / L_StepConstant);
    
    G_IsAnimating = true;
    
    $(P_DivID + '_Content').style.display = 'none';
    $(P_DivID).style.display = 'block';
    
    L_PopupWidth = L_PopupWidth + L_StepX;
    L_PopupHeight = L_PopupHeight + L_StepY;
    L_PopupLeft = L_PopupLeft - (L_StepX / 2);
    
    if ( (L_PopupWidth >= G_PopupWidth) ||
         (L_PopupHeight > G_PopupHeight))
    {
        L_PopupWidth = G_PopupWidth;
        L_PopupHeight = G_PopupHeight;
        L_PopupLeft = G_PopupX;
        
        G_IsAnimating = false;
    }
    
    $(P_DivID).style.width = L_PopupWidth + 'px';
    $(P_DivID).style.height = L_PopupHeight + 'px';
    $(P_DivID).style.left = L_PopupLeft + 'px';
        
    if (G_IsAnimating == true)
    {
        G_PopupTimeoutAnimation = setTimeout('TimeoutPopupOpen(\'' + P_DivID + '\')', 35);
    }
    else
    {
        $(P_DivID + '_Content').style.display = 'block';
        
        clearTimeout(G_PopupTimeoutAnimation);
    }
}

function TimeoutPopupClose(P_DivID)
{
    clearTimeout(G_PopupTimeout);
    clearTimeout(G_PopupTimeoutAnimation);
    
    G_IsAnimating = false;
    
    $(P_DivID).style.display = 'none';
    
    G_OpenPopup = '';   
}

function OpenPopup(P_DivID)
{   
    clearTimeout(G_PopupTimeout);
    
    
    if (G_OpenPopup != P_DivID)
    {
        var L_PopupAdded = false;
        
        clearTimeout(G_PopupTimeoutAnimation);    
        
        G_IsAnimating = false;
        
        for (var I_Index = 0; I_Index < G_Popups.length; I_Index++)
        {
            if (('' + G_Popups[I_Index]) == ('' + P_DivID))
            {
                L_PopupAdded = true;
            }
            
            if (('' + G_Popups[I_Index]) != ('' + P_DivID))
            {
                $(G_Popups[I_Index]).style.display = 'none';
            }
        }
        
        if (L_PopupAdded == false)
        {
            G_Popups.push(P_DivID);
        }
    }
    
    if ( (G_IsAnimating == false) &&
         (G_OpenPopup != P_DivID))
    {     
        G_PopupX = (1 * $(P_DivID + '_PositionX').value);
        G_PopupY = (1 * $(P_DivID + '_PositionY').value);
        G_PopupWidth = (1 * $(P_DivID + '_Width').value);
        G_PopupHeight = (1 * $(P_DivID + '_Height').value);
        
        $(P_DivID).style.width = '0px';
        $(P_DivID).style.height = '0px';
        $(P_DivID).style.bottom = G_PopupY + 'px';
        $(P_DivID).style.left = (G_PopupX + (G_PopupWidth  / 2)) + 'px';
        
        $(P_DivID + '_Content').style.width = ((1 * $(P_DivID + '_Width').value) - 2) + 'px';
        $(P_DivID + '_Content').style.height = ((1 * $(P_DivID + '_Height').value) - 2) + 'px';
        
        TimeoutPopupOpen(P_DivID);
        
        G_OpenPopup = P_DivID;
    }
}

function ClosePopup(P_DivID)
{
    G_PopupTimeout = setTimeout('TimeoutPopupClose(\'' + P_DivID + '\')', 300);
}
