﻿
var PopWindow = function(parent,x,y,width,height)
{
    this.background = document.createElement("div");
    this.background.className = "windowBackground";
    this.background.style.zIndex = 80;
    parent.appendChild(this.background);
    //document.getElementById("bodyWalkaround").style.zIndex = 1;
    this.handle = document.createElement("div");
    this.handle.style.left = x+"px";
    this.handle.style.top = (y+(height/2))+"px";
    this.handle.style.zIndex = 80;
    this.handle.style.width = width+"px";
    this.handle.style.height = 1+"px";
    this.handle.className = "window";
    this.handle.style.position = "absolute";
    //this.handle.style.zIndex = 15;
    this.handle.style.overflow = "hidden";
    this.realHeight = height;    
    parent.appendChild(this.handle);
    this.titlePanel = document.createElement("div");
    this.titlePanel.style.lineHeight = "70px";
    this.titlePanel.style.paddingLeft = "30px";
    this.titlePanel.style.paddingRight = "50px";
    this.titlePanel.style.fontWeight = "bold";
    this.titlePanel.style.fontSize = "14px";
    this.titlePanel.style.textAlign = "center";
    this.titlePanel.innerHTML = "Message";
    this.handle.appendChild(this.titlePanel);
    this.contentPanel = document.createElement("div");
    this.contentPanel.style.paddingLeft = "20px";
    this.contentPanel.style.paddingRight = "50px";
    this.contentPanel.style.height = (height - (80+50))+"px";
    this.contentPanel.style.overflow = "hidden";
    this.handle.appendChild(this.contentPanel);
    this.footerPanel = document.createElement("div");
    this.footerPanel.style.lineHeight = "50px";
    this.footerPanel.style.marginTop = "0px";
    this.footerPanel.style.fontWeight = "bold";
    this.footerPanel.style.paddingLeft = "50px";
    this.footerPanel.style.paddingRight = "50px";
    this.footerPanel.style.paddingBottom = "150px";
    this.footerPanel.style.textAlign = "right";
    this.handle.appendChild(this.footerPanel);
    
    var me = this;
    var body = document.getElementsByTagName("body")[0];
    body.addEventListener("keyup",function(ev)
        {
        
            var e = ev?ev:window.event;
            if(me.visible && me.animationEnded)
            {
                if(e.keyCode==13)
                {
                    if(me.okCallback!=null)
                        eval(me.okCallback+"()");
                        
                }
                else if(e.keyCode==27)
                {
                    if(me.cancelCallback!=null)
                       eval(me.cancelCallback+"()");
                }
                else
                {
                    //alert(e.keyCode);
                }
            }
        },true);
        
    this.handle.style.visibility = "hidden";
}

PopWindow.prototype.handle;
PopWindow.prototype.contentPanel;
PopWindow.prototype.titlePanel;
PopWindow.prototype.footerPanel;
PopWindow.prototype.parent;
PopWindow.prototype.realHeight;
PopWindow.prototype.visible = false;
PopWindow.prototype.okCallback;
PopWindow.prototype.cancelCallback;

PopWindow.prototype.setContent = function(divContent)
{
    if(this.visible)return;
    this.contentPanel.style.textAlign = "left";
    this.contentPanel.innerHTML ="";
    this.contentPanel.appendChild(divContent);
}

PopWindow.prototype.setContentText = function(contentText)
{
    if(this.visible)return;
    if(typeof(contentText)=="undefined")return;
    if(contentText.indexOf("div")<0 &&
        contentText.indexOf("table")<0)
    {
        this.contentPanel.style.textAlign = "center";
        var divStart = "<div id=\"msg_text\" style=\" position: absolute;left:50%; top: 45%;display: table-cell; vertical-align: middle;\">"+
          "<div style=\"font-size:13px; position: relative; top: -50%;left:-50%;\">";
        var divEnd = "</div></div>";
        this.contentPanel.innerHTML =divStart+
            contentText+divEnd;
    }
    else
    {
        this.contentPanel.style.textAlign = "left";
         this.contentPanel.innerHTML =contentText
    }
    //this.contentPanel.innerHTML = contentText;
}
PopWindow.prototype.setTitle = function(title)
{
    if(this.visible)return;
    this.titlePanel.innerHTML = title;
}
PopWindow.prototype.setCallbacks = function(okFunctionName,cancelFunctionName)
{
    //if(this.visible)return;
    this.footerPanel.innerHTML ="";
    this.okCallback = okFunctionName;
    this.cancelCallback = cancelFunctionName;
    if(okFunctionName!=null)this.footerPanel.innerHTML += 
        "<a href=\"javascript:;\"  onclick=\""+okFunctionName+
        "();\"><img src=\"images/blank.gif\" class=\"linkBtnImageMini\" id=\"btOK\" alt=\"OK\" /></a> ";
    if(cancelFunctionName!=null)this.footerPanel.innerHTML += 
        " &nbsp; <a href=\"javascript:;\" onclick=\""+cancelFunctionName+
        "();\"><img src=\"images/blank.gif\" class=\"linkBtnImageMini\" id=\"btCancel\" alt=\"ANNULER\" /></a>";
}

PopWindow.prototype.show = function(x,y)
{
    this.background.style.visibility ="visible";
    this.animate(true);
}

PopWindow.prototype.close = function()
{
this.background.style.visibility ="hidden";
    this.animate(false);
}
PopWindow.prototype.timer = null;
PopWindow.prototype.animationEnd = null;
PopWindow.prototype.animationEnded = true;
PopWindow.prototype.animate = function(anim)
{
    this.animationEnded = false;
    //alert(this.contentPanel.innerHTML);
    if(!anim && (this.contentPanel.innerHTML.indexOf("msg_text")>-1))
        {this.setContentText("");}
    this.visible = anim;
    if(anim)
        this.handle.
            style.visibility = "visible";
    var speed = 50;
    var currentHeight = parseInt(
        this.handle.style.height.replace("px",""));
    if((anim && currentHeight<this.realHeight) || 
        (!anim && currentHeight>2 ))
    {
        var top = parseInt(this.handle.style.top.replace("px",""));
        /*this.handle.style.top = (top+(anim?-(speed/2):(speed/2)))+"px";
        this.handle.style.height = (currentHeight + (anim?speed:-speed))+"px"; */
        if(anim)
        {
            this.handle.style.top = (top-(speed/2))+"px";
            this.handle.style.height = (currentHeight + speed)+"px"; 
        }
        else
        {
            //var minus_speed = (speed/2);
            this.handle.style.top = (top+(speed/2))+"px";
            this.handle.style.height = (currentHeight -speed)+"px"; 
            
        }
        var me = this;
        if(this.timer!=null)clearTimeout(this.timer);
        this.timer= setTimeout(function(){try{me.animate(anim);}catch(e){}},1);
    }
    else
    {
        if(!anim)
        {
            this.handle.style.visibility = "hidden";
        }
        this.animationEnded = true;
        if(this.animationEnd!=null)
        {
            this.animationEnd();
        }
    }
}