﻿var callSender = null;
var lastCommonPopupAction = null;
var imgAskSender=null;

function CommonPopupAction() {
    this.ArrowPosition = 0;
    this.OffsetX = 0;
    this.Height = 0;
    this.Width = 0;
    this.DisplayArrow=true;
}

CommonPopupAction.prototype.paPopup = function(caller, content, arrowPosition, offsetX, height)
{
    callMainPopup(caller, content, null, arrowPosition, offsetX, height, this.Width);
}

CommonPopupAction.prototype.paPopupWithId = function(caller, contentParentId, arrowPosition, offsetX, height)
{    
    imgAskSender=caller;
    var currentPopupAction = this;
    lastCommonPopupAction = function () {
        callMainPopup(caller, null, contentParentId, arrowPosition, offsetX, height,currentPopupAction.Width,currentPopupAction.DisplayArrow);
    }
    
    lastCommonPopupAction();
}

function callMainPopup(caller, content, contentParentId,arrowPosition, offsetX, height, width,displayArrow)
{
    callSender = caller;
    showPopupBox();
    
    var offsetNumber = 0;
    var arrowPos = $get("CommonPopupBox_Layer1");
    arrowPos.style.display="";
    if(caller)
    {
        var LinkLoc = GetLocationOfControl(caller);
        arrowPos.style.top = (LinkLoc.y + caller.offsetHeight) + "px";
        arrowPos.style.left = (LinkLoc.x + (caller.offsetWidth / 2.0) - 10) + "px";
    }
    else
    {
        if(typeof(height) == "undefined" || height == null)
            arrowPos.style.top = xScrollTop()+xClientHeight()/2+"px";
        else
            arrowPos.style.top = xScrollTop()+(xClientHeight()-height)/2+"px";
        arrowPos.style.left = (xClientWidth()-width)/2+"px";
    }
    if(typeof(arrowPosition) != "undefined" && arrowPosition != null)
    {
        offsetNumber = arrowPos.offsetLeft + arrowPosition;
        arrowPos.style.left = offsetNumber + "px";
    }
    
    var popupBodyPos = $get("CommonPopupOut");
    var popupLeftPos = arrowPos.offsetLeft - 65;
    popupLeftPos = popupLeftPos > 0 ? popupLeftPos : 5;
    popupBodyPos.style.top = (arrowPos.offsetTop + 3) + "px";
    popupBodyPos.style.left = popupLeftPos  + "px";
    if(typeof(offsetX) != "undefined" && offsetX != null)
    {
        offsetNumber = popupBodyPos.offsetLeft + offsetX;
        offsetNumber = offsetNumber > 0 ? offsetNumber : 5;
        popupBodyPos.style.left = offsetNumber + "px";
    }
    
    if( arrowPos.offsetLeft < popupBodyPos.offsetLeft || 
        arrowPos.offsetLeft > popupBodyPos.offsetLeft + popupBodyPos.clientWidth)
    {
        arrowPos.style.left = (popupBodyPos.offsetLeft + 10) + "px";   
    }
    
    if(typeof(height) != "undefined" && height != null)
    {
        height = height < 150 ? 150 : height;
        popupBodyPos.style.height = height + "px";
        $get("CommonPopupIn").style.height = height + "px";
    }
    
    var popupBodyContent = $get("PopupBody");
    var bufferContainer = $get("CachePopupContainer");
    var mainFrame1 = $get("MaskPopupFrame");
    if( width > 0 )
    {
        popupBodyContent.style.width = width + "px";
        popupBodyPos.style.width = width + "px";
        mainFrame1.style.width=width + 3 + "px";
    }
    else
    {
        popupBodyContent.style.width = 335 + "px";
        popupBodyPos.style.width = 335 + "px";
        mainFrame1.style.width= 338 + "px";
    }
            
    for(var i = 0; i < popupBodyContent.childNodes.length; i ++)
    {
        contentContainer = popupBodyContent.childNodes[i];
        if( typeof(contentContainer.style) != "undefined" && contentContainer.style != null )
            contentContainer.style.display = "none";
        
        bufferContainer.appendChild(contentContainer);
    }
    
    if(content != null)
    {
        popupBodyContent.innerHTML = content;
    }
    
    if(contentParentId != null)
    {
        contentContainer = $get(contentParentId);
        contentContainer.style.display = "";
        popupBodyContent.appendChild(contentContainer);
    }
    
    if( popupBodyContent.clientHeight > 150 )
    {
        var mainFrame = $get("MaskPopupFrame");
        mainFrame.style.height = (20 + (popupBodyContent.clientHeight - 150)) + "px";
    }
    else
    {
        var mainFrame = $get("MaskPopupFrame");
        mainFrame.style.height = 20 + "px";
    }   
    
    if(displayArrow)
        arrowPos.style.display="";
    else
        arrowPos.style.display="none";
}

function showPopupBox()
{
    var container = $get("CommonPopupContainer");
    if( container != null )
    {
        container.style.display = "none";
        container.style.display = "";
    }
}

function HidePopupPanel()
{
    var container = $get("CommonPopupContainer");
    if( container != null)
        container.style.display = "none";    
}

function GetLocationOfControl(ATarget)
{
    var target = ATarget;
    var pos = new CPos(target.offsetLeft, target.offsetTop);
    
    var target = target.offsetParent;
    while (target)
    {
        pos.x += target.offsetLeft;
        pos.y += target.offsetTop;
        
        target = target.offsetParent
    }
    
    return pos;
}

function CPos(x, y)
{
    this.x = x;
    this.y = y;
}

function $get(ClientID)
{
    return document.getElementById(ClientID);
}

function BodyClickHandler(e)
{
    e = window.event || e;
    var sender = e.srcElement || e.target;
    if( callSender != null && sender.id != callSender.id )
    { 
        var popupBodyContent = $get("PopupBody");
        if( popupBodyContent.innerHTML.indexOf(sender.id) <= 0)
        {
            HidePopupPanel();
        }
    }
}
document.onclick = BodyClickHandler;
window.onresize = function () {
    if(imgAskSender!=null)
    {
        //imgAskSender.click();
    }
    /*var container = $get("CommonPopupContainer");
    if( container != null && container.style.display != "none")
    {    
        if (typeof(lastCommonPopupAction) == "function")
            lastCommonPopupAction();
    }*/
}