﻿var PopWindow = function(parent,x,y,width,height)
{

    this.background = document.createElement("div");
    this.background.style.background = "url(images/disablebg.png)";
    this.background.style.width = "100%";
    this.background.style.height = "100%";
    this.background.style.position = "absolute";
    this.background.style.top = "0px";
    this.background.style.left = "0px";
    this.background.style.visibility = "hidden";
    this.background.style.zIndex = 1;
      
    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.style.background = "url(images/fondpush.png)";
    
    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 = "50px";
    this.titlePanel.style.paddingLeft = "30px";
    this.titlePanel.style.paddingRight = "50px";
    this.titlePanel.style.fontWeight = "bold";
    this.titlePanel.style.fontSize = "14px";
    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 - (120))+"px";
    this.handle.appendChild(this.contentPanel);
    this.footerPanel = document.createElement("div");
    this.footerPanel.style.lineHeight = "50px";
    this.footerPanel.style.fontWeight = "bold";
    this.footerPanel.style.paddingLeft = "50px";
    this.footerPanel.style.paddingRight = "50px";
    this.footerPanel.style.paddingBottom = "150px";
    this.footerPanel.style.paddingTop = "30px";
    
    this.footerPanel.style.textAlign = "right";
    this.handle.appendChild(this.footerPanel);
    /*var me = this;
    this.handle.onclick = function(){me.close(false);};*/
    
    var me = this;
    var body=document.getElementsByTagName("body")[0];
    
    body.addEventListener("keyup",function(ev)
    {   
    var e=ev?ev:window.event;
         if(me.visible)
                {
                    if(e.keyCode==13)
                    {
                        eval(me.okCallback+"()");
                    }
                    else if(e.keyCode==27)
                    {
                        eval(me.cancelCallback+"()");
                    }
                    else
                    {
                        //alert(e.keyCode);
                    }
                }
            },true);      
    
}

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.innerHTML ="";
    this.contentPanel.appendChild(divContent);
}

PopWindow.prototype.setContentText = function(contentText)
{
    if(this.visible)return;
    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/btok2.png\" alt=\"OK\" /></a> ";
    if(cancelFunctionName!=null)this.footerPanel.innerHTML += " &nbsp; <a href=\"javascript:;\" onclick=\""+cancelFunctionName+"();\"><img src=\"images/btcancel2.png\" alt=\"ANNULER\" /></a>";
}

PopWindow.prototype.show = function(x,y)
{
    this.animate(true);
}

PopWindow.prototype.close = function()
{
    this.animate(false);
}
PopWindow.prototype.timer = null;
PopWindow.prototype.animate = function(anim)
{
    this.visible = anim;
    this.background.
        style.visibility = anim?"visible":"hidden";
    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",""));        
        if(anim)
        {
            this.handle.style.top = (top-(speed/2))+"px";
            this.handle.style.height = (currentHeight + speed)+"px"; 
        }
        else
        {           
            this.handle.style.top = (top+(speed/2))+"px";
            this.handle.style.height = (currentHeight -speed)+"px";             
        }
        var me = this;
        if(this.timer!=null)clearInterval(this.timer);
        this.timer= setInterval(function(){me.animate(anim);},1);
    }
    else
    {
        
    }
}
