//
//  Class:
//      JModalDialog
//
//  Class used for displaying modal dialogs. (faked with divs and iframes)
//  
//  Since:      
//      13-01-2006
//  Changed:
//      --
//  Version:    
//      0.9
//  Creator:    
//      Data Access Europe (Judith Hilgerink)
//

//  PRIVATE
var JModalDialog_TabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME", "SELECT"); 
// MM: changed to array to keep track of all opened Modal Dialogs
var oJModalDialog_ActiveDragMasks = new Array();
var oJModalDialog_CurrentDialog = new Array();

//
//  Displays a modal dialog with the given html.
//
//  Params:
//      sHTML       String with the html content displayed in the dialog
//      sTitle      (optional) The text displayed in the title bar
//      sWidth      (optional) The width of the dialog in pixels
//      sHeight     (optional) The height of the dialog in pixels
//      sContainer  (default = null) Id of dom element container to be used 
//                  instead of generated
//  Returns:
//      Reference tot the JModalDialog object 
//   
function JModalDialog_DisplayHTML(sHTML, sTitle, sWidth, sHeight, sContainer){
    var oJModalDialog, oDialogDiv;
    
    //  Create dialog
    oJModalDialog = new JModalDialog(sWidth, sHeight);
    oJModalDialog.sTitle = sTitle;
    if(typeof(sContainer) != "undefined" && sContainer != null){
            oJModalDialog.sContainer = sContainer;
    }    
    oDialogDiv = oJModalDialog.customDialog();    
    
    // set content of dialog
    oDialogDiv.innerHTML = sHTML;
    
    //  Position the modal dialog
    oJModalDialog.positionDialog(true);
    
    return oJModalDialog;
}

//
//  Displays a modal dialog with the given dom element as content. The content 
//  is moved from its original dom position to the dialog and returned when 
//  the dialog is closed.
//
//  Params:
//      oContent    The dom element displayed in the dialog
//      sTitle      (optional) The text displayed in the title bar
//      sWidth      (optional) The width of the dialog in pixels
//      sHeight     (optional) The height of the dialog in pixels
//      sContainer  (default = null) Id of dom element container to be used 
//                  instead of generated
//  Returns:
//      Reference to the JModalDialog object
//
function JModalDialog_DisplayDOM(oContent, sTitle, sWidth, sHeight, sContainer){
    var oJModalDialog, oDialogDiv, oContentParent, oContentNext;
    
    //  Save the parent element from the content
    oContentParent = oContent.parentNode;
    oContentNext = oContent.nextSibling;
    
    //  Init custom dialog and get content div
    oJModalDialog = new JModalDialog(sWidth, sHeight);
    oJModalDialog.sTitle = sTitle;
    if(typeof(sContainer) != "undefined" && sContainer != null){
            oJModalDialog.sContainer = sContainer;
    }
    
    //  Attach to special internal onClose event and reset the content if possible
    oJModalDialog.onClose = function(){
            if(oContentParent != null){
                oContentParent.insertBefore(oContent, oContentNext);
            }
        }
    
    oDialogDiv = oJModalDialog.customDialog();    
    
    //  Add the content to the dialog
    oDialogDiv.appendChild(oContent);
    
    
    
    var aoInputs = oDialogDiv.getElementsByTagName("INPUT");
    for (var iCount = 0; iCount<aoInputs.length; iCount++){
        aoInputs[iCount].tabIndex = aoInputs[iCount].getAttribute("origtabindex");
    }

    var aoSelects = oDialogDiv.getElementsByTagName("SELECT");
    for (var iCount = 0; iCount<aoSelects.length; iCount++){
        aoSelects[iCount].tabIndex = aoSelects[iCount].getAttribute("origtabindex");
    }
    
    var aoTextAreas = oDialogDiv.getElementsByTagName("TEXTAREA");
    for (var iCount = 0; iCount<aoTextAreas.length; iCount++){
        aoTextAreas[iCount].tabIndex = aoTextAreas[iCount].getAttribute("origtabindex");
    }
    
    oDialogDiv.appendChild(oContent);
    
    //  Position the modal dialog
    oJModalDialog.positionDialog(true);
    
    return oJModalDialog;
}



//  Displays a modal dialog with the given html.
//
//  Params:
//      sURL        String with the url to file being displayed
//      sTitle      (optional) The text displayed in the title bar
//      sWidth      (optional) The width of the dialog in pixels
//      sHeight     (optional) The height of the dialog in pixels
//      sContainer  (default = null) Id of dom element container to be used 
//                  instead of generated
//  Returns:
//      Reference tot the JModalDialog object 
//   
function JModalDialog_DisplayURL(sURL, sTitle, sWidth, sHeight, sContainer){
    var oJModalDialog;
    
    //  Create dialog
    oJModalDialog = new JModalDialog(sWidth, sHeight);
    oJModalDialog.sTitle = sTitle;
    if(typeof(sContainer) != "undefined" && sContainer != null){
            oJModalDialog.sContainer = sContainer;
    }    
    oJModalDialog.showPage(sURL);    
    
    return oJModalDialog;
}

//  Queue with waiting alert messages so they won't show on top of each other
//  PRIVATE
var aJModalDialog_Alert_Queue = new Array();

//
//  Handles the onAfterClose event from the modal dialog. It displays the next 
//  waiting modal dialog.
//
//  PRIVATE
function JModalDialog_AlertClosed(){
    aJModalDialog_Alert_Queue.shift();

    if(aJModalDialog_Alert_Queue.length > 0){
        JModalDialog_AleryFromQueue();
    }
}

//
//  Displays the first alert message waiting in the queue in a modal dialog.
//
//  PRIVATE
function JModalDialog_AleryFromQueue(){
    //  Fetch from queue
    var oQueuedAlert = aJModalDialog_Alert_Queue[0];
    
    //  Create dialog
    var oJModalDialog = new JModalDialog();
    oJModalDialog.sContainer = oQueuedAlert.sContainer;
    oJModalDialog.sTitle = oQueuedAlert.sTitle;
    if(oQueuedAlert.sContainer != null){
        oJModalDialog.sContainer = oQueuedAlert.sContainer;
    }
    oJModalDialog.onAfterClose = JModalDialog_AlertClosed;
    
    if (oQueuedAlert.sText == "" || oQueuedAlert.sText.length < 15) oJModalDialog.iWidth = 300;
    
    //  Display dialog
    oJModalDialog.showMessage(oQueuedAlert.sText);
}

//
//  Displays the given message in a modal dialog. If there is already a message
//  shown the message is queued until the previous is closed.
//
//  Params:
//      sText       The text to be shown in the dialog
//      sTitle      (optional) The title of the dialog
//      sContainer  (optional) Id of existing container instead of generated
//
function JModalDialog_Alert(sText, sTitle, sContainer){
    var oQueuedAlert;
    
    //  Set default values if not given
    if(typeof(sTitle) == "undefined"){
        var sTitle = "Alert";
    }
    if(typeof(sContainer) == "undefined"){
        var sContainer = null;
    }
    
    //  Add to queue
    oQueuedAlert = { "sText" : sText, "sTitle" : sTitle, "sContainer" : sContainer }
    aJModalDialog_Alert_Queue.push(oQueuedAlert);
    
    //  If no others waiting display
    if(aJModalDialog_Alert_Queue.length == 1){
        JModalDialog_AleryFromQueue();
    }
}

//
//  Closes the current dialog after calling the onFinished function.
//
//  Params:
//      sValue  The return value
//
function JModalDialog_Return(sValue){
    var oDialog = JModalDialog_Find();
    
    if(oDialog != null){
        oDialog.returns(sValue);
    }    
}

//
//  Closes the current dialog.
//
function JModalDialog_Hide(){
    var oDialog = JModalDialog_Find();    

    if(oDialog != null){
        oDialog.hide();
    }
}

//
//  Finds the current dialog (can also be used from the dialog frame)
//
//  Returns:
//      The current dialog (null if not found)
//
function JModalDialog_Find(){
    var oResult = oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1];
    
    if(oResult == null){
        oResult = window.top.oJModalDialog_CurrentDialog;
    }
    
    return oResult;
}

//
//  Constructor: Sets the default values and the given size.
//
function JModalDialog(iWidth, iHeight){
    if(iWidth == null || typeof(iWidth) == "undefined") var iWidth = 0;
    if(iHeight == null || typeof(iHeight) == "undefined") var iHeight = 0;
    
    //  Public Properties
    this.sTitle         = "New modal dialog";
    this.iWidth         = iWidth;
    this.iHeight        = iHeight;
    this.sContainer     = "";

    //  Private Properties
    this.iX             = -1;
    this.iY             = -1;
    this.iDragOffsetY   = 0;
    this.iDragOffsetX   = 0;
    
    this.oDivInner      = null;
    this.oDivMask       = null;
    this.oDivDragMask   = null;
    this.oDivContainer  = null;
    this.oDivTitlebar   = null;
    this.oDivTitle      = null;
    this.oDivControls   = null;
    this.oPopupFrame    = null;
    this.fOrigMouseDown = null;
    	    
    this.bDragging;
    
    this.iCount = oJModalDialog_CurrentDialog.length;
    oJModalDialog_CurrentDialog[this.iCount] = this;
        
}

//
//  Displays the dialog and loads an external page into it.
//
//  Params:
//      sUrl    Url of page to load 
//
JModalDialog.prototype.showPage = function(sUrl){
    this.disableTabIndexes();
    this.createDialog();
    
    this.oPopupFrame = document.createElement("iframe");
    this.oDivInner.appendChild(this.oPopupFrame);
    this.oPopupFrame.setAttribute("id","popupFrame" + this.iCount);
    this.oPopupFrame.setAttribute("name","popupFrame" + this.iCount);
    this.oPopupFrame.className = "popupFrame";
    this.oPopupFrame.setAttribute("scrolling","auto");
    this.oPopupFrame.setAttribute("frameborder","0");
    this.oPopupFrame.setAttribute("allowtransparency","true"); 
    this.oPopupFrame.src = sUrl;  

    this.positionDialog(true);
}

//
//  Displays a dialog with the given message and hides the focus.
//
//  Params:
//      sText The text shown in the dialog
//
JModalDialog.prototype.showMessage = function(sText){
    var oTextDiv, oFocusInput;
    
    this.disableTabIndexes();
    this.createDialog();
        
    //  Create and add text div
    oTextDiv = document.createElement("div");
    oTextDiv.setAttribute("id","alertText"); 
    browser.dom.setElementText(oTextDiv, sText);
    this.oDivInner.appendChild(oTextDiv);
    
    //  Create a focus holder
    oFocusInput = document.createElement("input")
    oFocusInput.setAttribute("id", "give_away_focus")
    oFocusInput.style.position = "absolute";
    oFocusInput.style.width = "0";
    oFocusInput.style.height = "0";    
    document.body.appendChild(oFocusInput);
    
    //  Timed function that sets the focus and removes the focus element
    var fSetFocus = function(){
        oFocusInput.focus();
        document.body.removeChild(oFocusInput); 
    }
    setTimeout(fSetFocus, 100);
    
    this.positionDialog(true);
}

//
//  Creates a dialog for custom usage, the inner div is returned so it van be 
//  filled for own needs.
//
JModalDialog.prototype.customDialog = function(){
    this.disableTabIndexes();
    this.createDialog();  
    
    return this.oDivInner;
}

//
//  Hides the dialog and calls the onFinished function with the result and the
//  loadobject.
//
//  Params:
//      oResult  The return value
//
JModalDialog.prototype.returns = function(oResult){
    this.hide();
    this.onFinished(this, oResult);
}

//
//  Deletes / hides the dialog
//
JModalDialog.prototype.hide = function(){
    //  Call the onBeforeClose event
    if(this.onBeforeClose(this)){
        //  Restore stuff
        if(this.fOrigMouseDown != null){
            document.onmousedown = this.fOrigMouseDown;
            this.fOrigMouseDown = null;
        }
    
        this.restoreTabIndexes();
    	
        // Display all select boxes
        browser.gui.displaySelectBoxes(this.oDivMask, this.oDivInner);

        document.body.removeChild(this.oDivMask);
        document.body.removeChild(this.oDivContainer);

    	// MM: Remove closed Modal Dialog from array
        oJModalDialog_CurrentDialog.splice((this.iCount),1);
        
        //  Call internal onClose used in JModalDialog_DisplayDOM
        if(typeof(this.onClose) == "function"){
            this.onClose();
        }
        
        //  Call the onAfterClose Event
        this.onAfterClose(this);
    }
}

//
//  Creates the dialog objects (divs, iframes, etc..)
//
//  PRIVATE
JModalDialog.prototype.createDialog = function(){
    var bCustomContainer = false;
    var iAdjust = 0;
    
    if (typeof(scrollMaxY) != "undefined") if (scrollMaxY > 0) iAdjust = 16;
    
   //This will first create all the html DOM elements and after that set the height and width.
    var oDivMask = document.createElement("div");
    document.body.appendChild(oDivMask);
    oDivMask.setAttribute("id","popupMask" + this.iCount);
    oDivMask.className = "popupMask";
    oDivMask.style.zIndex=900+(1*this.iCount);
    oDivMask.innerHTML="&nbsp;";
    oDivMask.style.height = browser.gui.getViewportHeight() + "px";
    oDivMask.style.width = browser.gui.getViewportWidth() - iAdjust + "px";
    oDivMask.style.top = parseInt(document.documentElement.scrollTop, 10) + "px";
    oDivMask.style.left = parseInt(document.documentElement.scrollLeft, 10) + "px";

    if (this.sContainer != "") {
        if (document.getElementById(this.sContainer)){
            var oDivContainer = document.getElementById(this.sContainer).cloneNode(true);
            if (oDivContainer) bCustomContainer = true;
        }
    }

    if (!bCustomContainer) var oDivContainer = document.createElement("div");
    document.body.appendChild(oDivContainer);
    oDivContainer.setAttribute("id","popupContainer" + this.iCount);
    oDivContainer.className = "popupContainer";
    oDivContainer.style.zIndex=900+(2*this.iCount);
    
    if (bCustomContainer){
        this.scanCustomDialog(oDivContainer);
        
        var oDivTitlebar = document.getElementById("popupTitleBar" + this.iCount);        
        var oDivTitle = document.getElementById("popupTitle" + this.iCount);
        oDivTitle.innerHTML = this.sTitle;	        
        var oDivControls = document.getElementById("popupControls" + this.iCount);        
        var oAnchorClose = document.getElementById("a" + this.iCount);        
        var oImgClose = document.getElementById("img" + this.iCount);            
        var oDivInner = document.getElementById("popupInner" + this.iCount);
    }else{       
        var oDivTitlebar = document.createElement("div");
        oDivContainer.appendChild(oDivTitlebar);
        oDivTitlebar.setAttribute("id","popupTitleBar" + this.iCount);
        oDivTitlebar.className = "popupTitleBar";
        
        var oDivTitle = document.createElement("div");
        oDivTitlebar.appendChild(oDivTitle);
        oDivTitle.setAttribute("id","popupTitle" + this.iCount);
        oDivTitle.className = "popupTitle";    
        oDivTitle.innerHTML = this.sTitle;	
        
        var oDivControls = document.createElement("div");
        oDivTitlebar.appendChild(oDivControls);
        oDivControls.setAttribute("id","popupControls" + this.iCount);
        oDivControls.className = "popupControls";
        
        var oAnchorClose = document.createElement("a");
        oDivControls.appendChild(oAnchorClose);
        oAnchorClose.setAttribute("href","javascript: JModalDialog_Hide();");
        oAnchorClose.setAttribute("id","a" + this.iCount);
        
        var oImgClose = document.createElement("img");
        oAnchorClose.appendChild(oImgClose);
        oImgClose.setAttribute("src","Images/ModalDialog/Close.gif");
        oImgClose.setAttribute("id","img" + this.iCount);
            
        var oDivInner = document.createElement("div");
        oDivContainer.appendChild(oDivInner);
        oDivInner.setAttribute("id","popupInner" + this.iCount);
        oDivInner.className = "popupInner";
    }   
    
    if(this.iHeight > 0){
        oDivInner.style.height = this.iHeight + "px";
    }    
    if(this.iWidth > 0){
        oDivInner.style.width = this.iWidth + "px";
    }
    
    this.oDivMask = oDivMask;
    this.oDivInner = oDivInner;
    this.oDivTitlebar = oDivTitlebar
    this.oDivTitle = oDivTitle
    this.oDivControls = oDivControls
    this.oDivContainer = oDivContainer;
    
    //  Store origional mousedown handler and remove it if first dialog
    if (oJModalDialog_CurrentDialog.length == 1)
    {
        this.fOrigMouseDown = document.onmousedown;
        document.onmousedown = "";
    }
    
    browser.events.addGenericListener("resize", window, this.onWindow_Resize);
    browser.events.addGenericListener("scroll", window, this.onWindow_Scroll);
    //  Attach drag event
    if (oDivTitlebar) browser.events.addGenericListener("mousedown", oDivTitlebar, this.onTitle_MouseDown);
    //  Make sure close buttons won't couse drag
    if (oAnchorClose) browser.events.addGenericListener("mousedown", oAnchorClose, browser.events.stop);
    
    oDivContainer.style.display = "";
}

//
//  recursive function used for adding unique ids to custom dialog elements
//
//  PRIVATE
JModalDialog.prototype.scanCustomDialog = function(oModalElement){
    
    for (var iCount = 0; iCount<oModalElement.childNodes.length; iCount++){
        if (typeof(oModalElement.childNodes[iCount].id) != "undefined" && oModalElement.childNodes[iCount].id !="udefined") {
            oModalElement.childNodes[iCount].setAttribute("id",oModalElement.childNodes[iCount].id + this.iCount);
        }
        this.scanCustomDialog(oModalElement.childNodes[iCount]);
    }   
}

//
//  Sets the position of the dialog
//
JModalDialog.prototype.positionDialog = function(bSetWidth){
    var iContentWidth = this.iWidth, iContentHeight = this.iHeight, iTitleHeight = this.oDivTitle.offsetHeight, iTitleAdjustHeight, iFullWidth, iFullHeight, iScrollTop, iScrollLeft, iTitleHeight, iDialogWidth;
    var iAdjust = 0;
   
    if (typeof(scrollMaxY) != "undefined") if (scrollMaxY > 0) iAdjust = 16;
    
    //  Fetch sizes
    if(iContentHeight == null || isNaN(iContentHeight) || iContentHeight < 1){
        iContentHeight = this.oDivInner.offsetHeight;
    }
    iFullWidth = browser.gui.getViewportWidth();
    iFullHeight = browser.gui.getViewportHeight();
    iScrollLeft = parseInt(document.documentElement.scrollLeft, 10);
    iScrollTop = parseInt(document.documentElement.scrollTop, 10);
    iContentHeight += parseInt(this.oDivTitlebar.offsetHeight, 10);
    
    //  The width of the container needs to be static and should be set the first time
    if(bSetWidth){
        //  If no width set calculate the optimal content width according to the content or the tile
        if(iContentWidth == null || isNaN(iContentWidth) || iContentWidth < 1){
            iTitleWidth = this.oDivTitle.offsetWidth + this.oDivControls.offsetWidth;
            iContentWidth = this.oDivInner.offsetWidth;
            
            if(iTitleWidth > iContentWidth){
                iContentWidth = iTitleWidth;
            }
            
            //  Always take extra pixels for borders and stuff
            iContentWidth += 12;
        }
        
        //  Check if not to wide
        if(iDialogWidth > (iFullWidth - iAdjust)){
            iDialogWidth = iFullWidth - iAdjust;
        }        
        
        //  Set width
        this.oDivContainer.style.width = iContentWidth + "px";
        
        //  If needed make space for multiline title
        if (this.oDivTitle.offsetHeight > iTitleHeight){
            this.oDivTitle.style.width = (this.oDivTitlebar.offsetWidth - (this.oDivControls.offsetWidth||20) - 8) + "px";
            iContentHeight += parseInt(this.oDivTitle.offsetHeight-iTitleHeight, 10);
            this.oDivContainer.style.top = ((this.iY - (iContentHeight / 2)) + iScrollTop) + "px";
            iTitleAdjustHeight = parseInt(this.oDivTitlebar.offsetHeight, 10) + parseInt(this.oDivTitle.offsetHeight-iTitleHeight, 10);
            this.oDivTitlebar.style.height = iTitleAdjustHeight-8 + "px";
        }
        
        // for IE <= 6
        browser.gui.hideSelectBoxes(this.oDivMask, this.oDivInner);
    }
    
    //  Fetch content width
    if(iContentWidth == null || isNaN(iContentWidth) || iContentWidth < 1){
        iContentWidth  = this.oDivInner.offsetWidth;
    }    
    
    //  Set mask size
    this.oDivMask.style.height = iFullHeight + "px";
    this.oDivMask.style.width = iFullWidth - iAdjust + "px";
    this.oDivMask.style.top = iScrollTop + "px";
    this.oDivMask.style.left = iScrollLeft + "px";

    //  Calculate if dialog is not offscreen
    if(this.iX < (iContentWidth / 2) || this.iX > (iFullWidth - (iContentWidth / 2))){
        this.iX = (iFullWidth / 2);
    }
    if(this.iY < (iContentHeight / 2) || this.iY > (iFullHeight - (iContentHeight / 2))){
        this.iY = (iFullHeight / 2);
    }
    
    //  Set position and size
    this.oDivContainer.style.top = ((this.iY - (iContentHeight / 2)) + iScrollTop) + "px";
    this.oDivContainer.style.left = ((this.iX - (iContentWidth / 2)) + iScrollLeft) + "px";   
}

// 
//  Disables the tabindex of the background elements.
//
//  TODO: Find a way to do it under firefox
//
//  PRIVATE
JModalDialog.prototype.disableTabIndexes = function()
{
    if(oJModalDialog_CurrentDialog.length == 1){    
        var i = 0;
        for (var j = 0; j < JModalDialog_TabbableTags.length; j++)
        {
            var tagElements = document.getElementsByTagName(JModalDialog_TabbableTags[j]);
            for (var k = 0 ; k < tagElements.length; k++)
            {
                tagElements[k].setAttribute("origTabIndex", tagElements[k].tabIndex);
                tagElements[k].tabIndex="-1";            
                i++;
            }
        }
    }
}


//
//  Restores the tabindex of the background elemtns
//
//  TODO: Find a way to do it under firefox
//
//  PRIVATE
JModalDialog.prototype.restoreTabIndexes = function()
{
    if(oJModalDialog_CurrentDialog.length == 1){
        var i = 0;
        for (var j = 0; j < JModalDialog_TabbableTags.length; j++)
        {
            var tagElements = document.getElementsByTagName(JModalDialog_TabbableTags[j]);
            for (var k = 0 ; k < tagElements.length; k++)
            {
                tagElements[k].tabIndex = tagElements[k].getAttribute("origTabIndex");
                i++;
            }
        }
    }
}

//
//  Starts the dragging by creating the overlaying div and attaching the event 
//  listeners. Also saves the mouse offset from the upper left corner of the 
//  dialog.
//
//  Params:
//      mouseX  Current horizontal mouse position
//      mouseY  Current vertical mouse position
//
//  PRIVATE
JModalDialog.prototype.startDrag = function(mouseX, mouseY){
    if (!this.bDragging){
        this.bDragging = true;
        
        //  Create overlaying div
        this.oDivDragMask = document.createElement("div");
        document.body.appendChild(this.oDivDragMask);
        // MM: unique id
        this.oDivDragMask.setAttribute("id","popupDragMask" + this.iCount);
        // MM: add class for style settings
        this.oDivDragMask.setAttribute("class","popupDragMask");
        this.oDivDragMask.setAttribute("className","popupDragMask");
        this.oDivDragMask.innerHTML="&nbsp;";
        this.oDivDragMask.style.display = "block";
        
        //  Get & Set overlaying div size & location
        var fullHeight = browser.gui.getViewportHeight();
        var fullWidth = browser.gui.getViewportWidth();
        var scTop = parseInt(document.documentElement.scrollTop,10);
        var scLeft = parseInt(document.documentElement.scrollLeft,10);
        
        this.oDivDragMask.style.height = fullHeight + "px";
        this.oDivDragMask.style.width = fullWidth + "px";
        this.oDivDragMask.style.top = scTop + "px";
        this.oDivDragMask.style.left = scLeft + "px";
        
        //  Get drag offset
        this.iDragOffsetY = mouseY - browser.gui.getAbsoluteOffsetTop(this.oDivContainer);
        this.iDragOffsetX = mouseX - browser.gui.getAbsoluteOffsetLeft(this.oDivContainer);
        
        //  Add eventlisteners
        browser.events.addGenericListener("mouseup", this.oDivDragMask, this.onDragMask_MouseUpOut);
        browser.events.addGenericListener("mouseout", this.oDivDragMask, this.onDragMask_MouseUpOut);
        browser.events.addGenericListener("mousemove", this.oDivDragMask, this.onDragMask_MouseMove);
        
        oJModalDialog_ActiveDragMasks.push(this.oDivDragMask);
    }
}

//
//  Stops the dragging by deattaching the event listeners and removing the 
//  overlaying div.
//
//  PRIVATE
JModalDialog.prototype.stopDrag = function(){

    for(var iPos in oJModalDialog_ActiveDragMasks){
        this.oDivDragMask = oJModalDialog_ActiveDragMasks[iPos];
        //  Remove event listeners
        browser.events.removeGenericListener("mouseup", this.oDivDragMask, this.onDragMask_MouseUpOut);
        browser.events.removeGenericListener("mouseout", this.oDivDragMask, this.onDragMask_MouseUpOut);
        browser.events.removeGenericListener("mousemove", this.oDivDragMask, this.onDragMask_MouseMove);
        //  Remove overlaying div
        document.body.removeChild(this.oDivDragMask);
        this.oDivDragMask = null;
        
    }
    oJModalDialog_ActiveDragMasks.length = 0;
    this.bDragging = false;
}

//
//  Drags the dialog using the given mouse position.
//
//  Params:
//      mouseX  Current horizontal mouse position
//      mouseY  Current vertical mouse position
//
//  PRIVATE
JModalDialog.prototype.drag = function(mouseX, mouseY){
    if (this.bDragging){
        var scTop = parseInt(document.documentElement.scrollTop,10);
        var scLeft = parseInt(document.documentElement.scrollLeft,10);
        var newX = (mouseX - this.iDragOffsetX);
        var newY = (mouseY - this.iDragOffsetY);
        
        if(newX > scLeft && newX < (scLeft + browser.gui.getViewportWidth() - this.oDivContainer.offsetWidth)){ 
            this.oDivContainer.style.left = newX + "px";
            this.iX = (newX - scLeft);
        }else{
            this.iDragOffsetX = mouseX - browser.gui.getAbsoluteOffsetLeft(this.oDivContainer);
        }
        if(newY > scTop && newY < (scTop + browser.gui.getViewportHeight() - this.oDivContainer.offsetHeight)){
            this.oDivContainer.style.top = newY + "px";
            this.iY = (newY - scTop);
        }else{
            this.iDragOffsetY = mouseY - browser.gui.getAbsoluteOffsetTop(this.oDivContainer);
        }   
    }
}

//
//  Fetches the onscroll event, relocates the frame
//
//  PRIVATE
JModalDialog.prototype.onWindow_Scroll = function(e){
    if(oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1] != null){
    	  // MM: get last Modal Dialog in araray (the one currently active)
        oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1].positionDialog();
    }
}

//
//  Fetches the onresize event, relocates the frame
//
//  PRIVATE
JModalDialog.prototype.onWindow_Resize = function(e){
    if(oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1] != null){
    	  // MM: get last Modal Dialog in araray (the one currently active)
        oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1].positionDialog();
    }
}

//
//  Fetches the mousedown on the titlebar, starts draging.
//
//  PRIVATE
JModalDialog.prototype.onTitle_MouseDown = function(e){
    if(oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1] != null){
        var x = browser.events.getMouseX(e);
        var y = browser.events.getMouseY(e);
        // MM: get last Modal Dialog in araray (the one currently active)
        oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1].startDrag(x, y);
    }
}

//
//  Fetches the onmouseup or onmouseout event from the overlaying div, stops
//  dragging.
//
//  PRIVATE
JModalDialog.prototype.onDragMask_MouseUpOut = function(e){
	  if(oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1] != null){
	  	  // MM: get last Modal Dialog in araray (the one currently active)
        oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1].stopDrag();
    }
}

//
//  Fetches onmousemove from dragmask, drags the dialog.
//
//  PRIVATE
JModalDialog.prototype.onDragMask_MouseMove = function(e){
    if(oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1] != null){
        var x = browser.events.getMouseX(e);
        var y = browser.events.getMouseY(e);
        // MM: get last Modal Dialog in araray (the one currently active)
        oJModalDialog_CurrentDialog[oJModalDialog_CurrentDialog.length-1].drag(x, y);
    }
}



//  - - - - - - - USER EVENTS - - - - - - - -

//
//  Should be overloaded with function that handles the result
//
//  Params:
//      oJModalDialog   Reference to the dialog object
//      oResult         The return value
//
JModalDialog.prototype.onFinished = function(oJModalDialog, oResult){
    
}

//
//  Is called before the dialog is closed
//
//  Params:
//      oJModalDialog   Reference to the dialog object
//  Returns:
//      If false the dialog won't be closed
//
JModalDialog.prototype.onBeforeClose = function(oJModalDialog){
    return true;
}

//
//  Is called after the dialog is closed
//
//  Params:
//      oJModalDialog   Reference to the dialog object
//
JModalDialog.prototype.onAfterClose = function(oJModalDialog){
    
}