﻿var DFModalDialogszIndex = 3;
var DFLastOpenedModal = null;
var DFLastOpenedZIndex = 0;
var DFOpenedModals = new Array();

function DFModalDialogBox(contentObj,headerObj){
    this.content = new Controls(contentObj);
    this.header = new Controls(headerObj);
    this.id = this.content.Control().id;
    this.IframeId = "DFModalDialogboxIEFix";
    this.backgroundDivId = "DFModalDialogboxBackground";
    this.zIndex = 0;
    this.locked = false;
    this.isopened = false;
    this.isInitialized = false;
    this.contentHolder = function(){
        var obj = this.content.Control();
        if(obj!=null){
            if(this.parentModal!=null){
                this.parentModal.zIndex = DFModalDialogszIndex - 1;
            }
            if(this.zIndex==0){
                this.zIndex = DFModalDialogszIndex;
                DFModalDialogszIndex++;
            }
            this.content.setZIndex(this.zIndex);
        }
        return this.content;
    };
    this.IFrame = function(){
        var ifrm = null;
        if(Browser.isIE()==true){            
            ifrm = new Controls(this.IframeId);
            if(ifrm.Control()==null){
                var newifrm = document.createElement("iframe");
                newifrm.id = this.IframeId;
                newifrm.setAttribute("frameborder",0);
                newifrm.setAttribute("marginheight",0);
                newifrm.setAttribute("marginwidth",0);
                document.body.appendChild(newifrm);
                ifrm = new Controls(this.IframeId);
            }
            var frm = new Controls(ifrm.Control());
            frm.setPosition(0,0);            
            var width = Math.max(Window.InnerWidth(),document.body.offsetWidth + document.body.offsetLeft);
            var height = Math.max(Window.InnerHeight(),document.body.offsetHeight + document.body.offsetTop);
            frm.setDimension(width,height);
            frm.setTransparency(0);
            frm.setZIndex(0);
        }
        return ifrm;
    };
    this.BackgroundDiv = function(){
        var bgDiv = new Controls(this.backgroundDivId);
        if(bgDiv.Control()==null){
            var newbgDiv = document.createElement("div");
            newbgDiv.id = this.backgroundDivId;
            newbgDiv.style.border = "none";
            document.body.appendChild(newbgDiv);
            bgDiv = new Controls(this.backgroundDivId);
        }
        var bg = new Controls(bgDiv.Control());
        bg.setPosition(0,0);
        var margin = Math.max(10, (document.body.style.margin * 2));
        var width = Math.max(Window.InnerWidth(),document.body.offsetWidth + margin);
        var height = Math.max(Window.InnerHeight(),document.body.offsetHeight + margin);
        bg.setDimension(width,height);
        bg.setTransparency(this.backgroundTransparancy);
        bgDiv.Control().style.backgroundColor = "" + this.backgroundColor + "";
        bg.setZIndex(1);
        return bgDiv;
    };
    this.Diasbler = function(){
        var divId = this.backgroundDivId + "_" + this.contentHolder().Control().id;
        var bgDiv = new Controls(divId);
        if(bgDiv.Control()==null){
            var newbgDiv = document.createElement("div");
            newbgDiv.id = divId;
            newbgDiv.style.border = "none";
            newbgDiv.style.backgroundColor = "#CCCCCC";
            this.contentHolder().Control().appendChild(newbgDiv);
            bgDiv = new Controls(divId);
        }
        var bg = new Controls(bgDiv.Control());
        bg.setPosition(0,0);
        bg.setDimension(this.contentHolder().Width(),this.contentHolder().Height());
        bg.setTransparency(this.backgroundTransparancy);
        bg.setZIndex(1);
        return bgDiv;
    };
    this.Topper = function(){
        var topperId = this.IframeId + this.id;
        var topper = new Controls(topperId);
        return topper;
    };
    this.lock = function(){
        this.Diasbler().setVisible(true);
        this.locked = true;
    };
    this.unlock = function(){
        this.Diasbler().setVisible(false);
        this.locked = false;
    };
    if(this.draggable==true){
        this.header.Control().par = this;
        this.header.Control().onmousedown = function(e){
            if(!this.par.locked){                
                this.par.contentHolder().startDrag(e,true);
                this.par.focusWindow();
            }
        }
        this.header.Control().onmouseup = function(e){
            this.par.contentHolder().stopDrag("DFMDB" + this.par.id + ".setPosition();");
            this.par.focusWindow();
        }
    };
    this.contentHolder().Control().par = this;
    this.contentHolder().Control().onmousedown = function(){
        this.par.focusWindow();
    };
    this.focusWindow = function(){
        if(!this.locked){
            if(DFLastOpenedModal!=null){
                DFLastOpenedModal.contentHolder().setZIndex(DFLastOpenedZIndex);
            }
            DFLastOpenedModal = this;
            DFLastOpenedZIndex = this.zIndex;
            DFLastOpenedModal.contentHolder().setZIndex(DFModalDialogszIndex + 1);
        }
    };
    this.setPosition = function(x,y,z){
        x = x!=null ? (x=="null" ? null : x) : x;
        y = y!=null ? (x=="null" ? null : y) : y;
        z = z!=null ? (z=="null" ? null : z) : z;
        if(x!=null && y!=null){
            this.contentHolder().setPosition(x,y);
        }else{
            //x = this.contentHolder().Left();
            //y = this.contentHolder().Top();            
        }
        if(z!=null){
            this.contentHolder().setZIndex(z);
            this.zIndex = z;
        }else{
            //z = this.zIndex;
        }
        //this.infoObj.Control().value = this.isopened.toString() + ":::" + x + ":::" + y + ":::" + z ;*/
    };
    onLoadFunctions.add("DFMDB" + this.id + ".init()");
    //onAfterLoadFunctions.add("DFMDB" + this.id + ".initialize();");
    return this;
}
DFModalDialogBox.prototype = {
    backgroundColor : new String("#000000"),
    backgroundTransparancy : new Number(35),
    stickOnScreen : new Boolean(false),
    onBeforeShow : function(){},
    onAfterShow : function(){},
    onBeforeHide : function(){},
    onAfterHide : function(){},
    parentModal : null,
    draggable : new Boolean(true),
    init : function(){
        this.isInitialized = true;
        this.contentHolder().setVisible(false);
        if(DFOpenedModals.count()==0){
            if(this.IFrame()!=null){this.IFrame().setVisible(false);}
            this.BackgroundDiv().setVisible(false);
        }
    },
    initialize : function(){
        if(this.isInitialized == true){
            /*var informations = this.infoObj.Control().value.split(":::");
            var visible = informations[0];
            var x = informations[1];
            var y = informations[2];
            var z = informations[3];
            if(visible=="true"){
                if(this.parentModal!=null){
                    if(this.parentModal.isopened!=true){
                        this.parentModal.show();                        
                    }
                    this.parentModal.initialize();
                }
                this.show();
            }else{this.hide();}
            this.setPosition(x,y,z);*/
        }
    },
    show : function(){
        if(!this.isInitialized){return};        
        if(this.parentModal!=null){if(this.parentModal.isopened!=true){return;}else{this.parentModal.lock();};};
        RunOrExcecute(this.onBeforeShow);
        var x = (Window.InnerWidth() / 2);
        var y = (Window.InnerHeight() / 2);
        this.setPosition(x,y,0);
        if(this.IFrame()!=null){this.IFrame().setVisible(true);};        
        this.BackgroundDiv().setVisible(true);
        this.contentHolder().setVisible(true);
        if(this.Topper().Control()!=null){this.Topper().setDimension(this.contentHolder().Width(),this.contentHolder().Height())};
        if(this.parentModal==null){
            x = (Window.InnerWidth() / 2) - (this.contentHolder().Width() / 2) + Window.ScrollX();
            y = (Window.InnerHeight() / 2) - (this.contentHolder().Height() / 2) + Window.ScrollY();
        }else{
            x = this.parentModal.contentHolder().Left() + (this.parentModal.contentHolder().Width() / 2) - (this.contentHolder().Width()/2);
            y = this.parentModal.contentHolder().Top() + (this.parentModal.contentHolder().Height() / 2) - (this.contentHolder().Height()/2);
        }
        if(this.isopened!=true){
            DFOpenedModals.push(this);
            this.isopened = true;
        }        
        this.setPosition(x,y,0);
        this.focusWindow();        
        RunOrExcecute(this.onAfterShow);
    },
    hide : function(){
        if(DFOpenedModals.count()>0 && this.isopened == true){
            if(!this.isInitialized){return};
            if(this.locked==true){return false;}
            if(this.parentModal!=null){this.parentModal.unlock();}
            RunOrExcecute(this.onBeforeHide);
            this.contentHolder().setVisible(false);
            DFOpenedModals.pop();
            if(DFOpenedModals.count()==0){
                if(this.IFrame()!=null){this.IFrame().setVisible(false);}
                this.BackgroundDiv().setVisible(false);
            }
            this.isopened = false;
            this.setPosition();
            RunOrExcecute(this.onAfterHide);
        }
    }
}

function DFModalTrigger(){
    this.modal = arguments[0];
    this.compid = arguments[1];
    this.event = arguments[2];
    this.onBefore = arguments[3];
    this.onAfter = arguments[4];
    return this;
}

function DFModalDialogShowTriggers(){
    for(sti=0;sti<arguments.length;sti++){
        var stringToExcecute = "";
        stringToExcecute += "document.getElementById('" +  arguments[sti].compid +"')." + arguments[sti].event + "=function(){";
        stringToExcecute += arguments[sti].modal + ".onBeforeShow=" + arguments[sti].onBefore + ";";
        stringToExcecute += arguments[sti].modal + ".onAfterShow=" + arguments[sti].onAfter + ";";
        stringToExcecute += arguments[sti].modal + ".show();";
        stringToExcecute += "return false;";
        stringToExcecute += "};";
        eval(stringToExcecute);
    }
}

function DFModalDialogHideTriggers(){
    for(hti=0;hti<arguments.length;hti++){
        var stringToExcecute = "";
        stringToExcecute += "document.getElementById('" +  arguments[hti].compid +"')." + arguments[hti].event + "=function(){";
        stringToExcecute += arguments[hti].modal + ".onBeforeHide=" + arguments[hti].onBefore + ";";
        stringToExcecute += arguments[hti].modal + ".onAfterHide=" + arguments[hti].onAfter + ";";
        stringToExcecute += arguments[hti].modal + ".hide();";
        stringToExcecute += "return false;";
        stringToExcecute += "}";
        eval(stringToExcecute);
    }
}
