﻿// JScript File
//requires:
//           1. common.js

//usage 
// var md = new modDiv('myDiv', true, 'myLink', 'click', 'btnCancel', 'click', 'btnSearch', 'click', 'doSubmit', []); 

function modDiv(divId, centerInWindow, callElementID, callElementEventName, cancelElementId, cancelEventName, okElementID, okElementEvent, okElementMethod, okElementMethodParams) {
    this.divId = divId;
    this.div = $$(divId);
    this.isIE = _isIE;
    this.div.style.display = 'none';
    
    this.caller = $$(callElementID);
    this.callerEvtName = callElementEventName;
    this.okElement = $$(okElementID);
    this.okElementEvent = okElementEvent;
    this.okElementMethod = okElementMethod;
    this.okElementMethodParams = okElementMethodParams;
    this.cancelElement = $$(cancelElementId);
    this.cancelEventName = cancelEventName;
    this.init();
 }    
    modDiv.prototype.init = function modDiv$init() {
        //this.getDims();
        if (this.caller != null && this.callerEvtName != null) {
            assignEventListener(this.caller, this.callerEvtName, this.show)
            this.caller.modDiv = this;
        }
        if (this.cancelElement != null && this.cancelEventName != null) {
            assignEventListener(this.cancelElement, this.cancelEventName, this.hide);
            this.cancelElement.modDiv = this;
        }
        if (this.okElement != null && okElementEvent != null) {
            assignEventListener(this.okElement, this.okElementEvent, this.confirm);
            this.okElement.modDiv = this;
        }
    }
    
    modDiv.prototype.confirm = function modDiv$confirm(e) {
        var me = (typeof(e.srcElement) != 'undefined' ? e.srcElement.modDiv : (e.modDiv ? e.modDiv : this.modDiv));
        me.hide();
        me.okElementMethod(me.okElementMethodParams);
    }
    
    
    modDiv.prototype.getDims = function modDiv$getDims(e) {
        this.elemToShow = new Array();
        this.bPos = getPosition(document.body);
        this.bPos.height = window.document.body.clientHeight;
        var bckDiv = document.createElement("div");
        bckDiv.className = "modalPopupTransparent";
        
        setPositionPx(bckDiv, 0, 0, window.document.body.clientWidth, window.document.body.clientHeight );
        //bckDiv.style.display = 'none';
        bckDiv.style.zIndex = 2000;
        document.body.appendChild(bckDiv);
        this.elemToShow.push(bckDiv);

        this.div.style.zIndex = 3000;
        this.div.style.display.position = 'absolute';
        this.div.style.display = 'block';
        this.div.style.display = '';
        var divSize = getPosition(this.div);
        setPositionPx(this.div, (this.bPos.width - divSize.width) / 2, (this.bPos.height - divSize.height) / 2);
        
      
        
        if (this.isIE) {
            var ifr = document.createElement("iframe");
            ifr.style.position = 'absolute';
            ifr.style.display = 'none';
            ifr.className = "modalPopupTransparent";
            ifr.style.zIndex = '1000';
            setPositionPx(ifr, (this.bPos.width - divSize.width) / 2 , (this.bPos.height - divSize.height) / 2 , divSize.width , divSize.height  );
            document.body.appendChild(ifr);
            this.elemToShow.push(ifr);
        }
        
    }
    
    modDiv.prototype.show = function modDiv$show(e) {
    
        var me = (typeof(e.srcElement) != 'undefined' ? e.srcElement.modDiv : (e.modDiv ? e.modDiv : this.modDiv));
        me.getDims();
        for (var i = 0; i < me.elemToShow.length; i++ ) {
            me.elemToShow[i].style.display = '';
        }
        me.div.style.display = '';
        return false;
    }
    modDiv.prototype.hide = function modDiv$hide(e) {
        var me = (typeof(e.srcElement) != 'undefined' ? e.srcElement.modDiv : (e.modDiv ? e.modDiv : this.modDiv));
        for (var i = 0; i < me.elemToShow.length; i++ ) {
            me.elemToShow[i].style.display = '';
            document.body.removeChild(me.elemToShow[i]);
        }
        me.div.style.display = 'none';
        return false;
    }
    




