//
//  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)
//

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_CurrentDialog = new Array();

var gsMouseDown = null;

//
//  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){
        if(oJModalDialog_CurrentDialog.length == 1 && gsMouseDown)
        {
            document.onmousedown = gsMouseDown;
            gsMouseDown=null;
        }
        oDialog.hide();
    }
}

//
//  Finds the current dialog (can also be used from the dialog frame)
//
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){
    this.iCount = oJModalDialog_CurrentDialog.length;
    	
    this.iWidth         = iWidth;
    this.iHeight        = iHeight;

    this.iX             = -1;
    this.iY             = -1;
    this.iDragOffsetY   = 0;
    this.iDragOffsetX   = 0;
    
    this.aTabIndexes    = new Array();
    this.oDivInner      = null;
    this.oDivMask       = null;
    this.oDivDragMask   = null;
    this.oDivContainer  = null;
    this.oDivTitlebar   = null;
    this.oPopupFrame    = null;
    	    
    // check to see if this is IE version 6 or lower. hide select boxes if so
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);    
    this.bHideSelects   = (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1);
    this.sTitle         = "New modal dialog";
    this.oLoadObject    =  null;
    
    oJModalDialog_CurrentDialog[this.iCount-1] = this;
}

//
//  Displays the dialog and loads an external page into it.
//
//  Params:
//      sUrl    Url of page to load 
//
JModalDialog.prototype.showPage = function(sUrl){
		// MM: not really Multi Moldal stuff but set mousedown script for body to nothing
		// MM: and store it so it can be restored later
		if (oJModalDialog_CurrentDialog.length == 1)
		{
	      gsMouseDown = document.onmousedown;
		    document.onmousedown = "";
		}
    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 given message
//
//  TODO: Fill out!
//
JModalDialog.prototype.showMessage = function(sText){

}

JModalDialog.prototype.customDialog = function(){
    this.disableTabIndexes();
    this.createDialog();  
    
    return this.oDivInner;
}

//
//  Should be overloaded with function that handles the result
//
//  Params:
//      oLoadObject Object that created the dialog (null if not set)
//      result      The return value
//
JModalDialog.prototype.onFinished = function(oLoadObject, result){
    
}

//
//  Hides the dialog and calls the onFinished function with the result and the
//  loadobject.
//
//  Params:
//      result  The return value
//
JModalDialog.prototype.returns = function(result){
    this.hide();
    this.onFinished(this.oLoadObject, result);
}

//
//  Deletes / hides the dialog
//
JModalDialog.prototype.hide = function(){
    this.restoreTabIndexes();
	
    // Display all select boxes
    if (this.bHideSelects == true)
    {
        browser.gui.displaySelectBoxes(this.oDivMask);
    }
   
    document.body.removeChild(this.oDivMask);
    document.body.removeChild(this.oDivContainer);

		// MM: Remove closed Modal Dialog from array
    oJModalDialog_CurrentDialog.splice((this.iCount - 1),1);
}

//
//  Creates the dialog objects (divs, iframes, etc..)
//
JModalDialog.prototype.createDialog = function(){
   //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=200+(1*this.iCount);
    oDivMask.innerHTML="&nbsp;";
    oDivMask.style.height = browser.gui.getViewportHeight() + "px";
    oDivMask.style.width = browser.gui.getViewportWidth() + "px";
    oDivMask.style.top = parseInt(document.documentElement.scrollTop, 10) + "px";
    oDivMask.style.left = parseInt(document.documentElement.scrollLeft, 10) + "px";

    var oDivContainer = document.createElement("div");
    document.body.appendChild(oDivContainer);
    oDivContainer.setAttribute("id","popupContainer" + this.iCount);
    oDivContainer.className = "popupContainer";
    oDivContainer.style.zIndex=200+(2*this.iCount);
    
    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.oDivContainer = oDivContainer;
    
    // for IE
    if (this.bHideSelects){
        browser.gui.hideSelectBoxes(oDivMask);
    }
    
    browser.events.addGenericListener("resize", window, this.onWindow_Resize);
    browser.events.addGenericListener("scroll", window, this.onWindow_Scroll);
    //  Attach drag event
    browser.events.addGenericListener("mousedown", oDivTitlebar, this.onTitle_MouseDown);
    //  Make sure close buttons won't couse drag
    browser.events.addGenericListener("mousedown", oAnchorClose, browser.events.stop);
}

//
//  Locates/centers the dialog
//
JModalDialog.prototype.positionDialog = function(bSetWidth){
    var iContentWidth = this.iWidth, iContentHeight = this.iHeight, iFullWidth, iFullHeight, iScrollTop, iScrollLeft, iTitleHeight;
    
    //  Fetch content size
    if(iContentWidth == null || isNaN(iContentWidth) || iContentWidth < 1){
        iContentWidth  = this.oDivInner.offsetWidth;
    }
    if(iContentHeight == null || isNaN(iContentHeight) || iContentHeight < 1){
        iContentHeight = this.oDivInner.offsetHeight;
    }
    
    //  Fetch other sizes
    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);
    
    //  Set mask size
    this.oDivMask.style.height = iFullHeight + "px";
    this.oDivMask.style.width = iFullWidth + "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";
    if(bSetWidth){
        this.oDivContainer.style.width = this.oDivInner.clientWidth + "px";
    }
}

// 
//  Disables the tabindex of the background elements.
//
//  TODO: Find a way to do it under firefox
//
JModalDialog.prototype.disableTabIndexes = function()
{
    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++)
        {
            this.aTabIndexes[i] = tagElements[k].tabIndex;
            tagElements[k].tabIndex="-1";
            i++;
        }
    }
}


//
//  Restores the tabindex of the background elemtns
//
//  TODO: Find a way to do it under firefox
//
JModalDialog.prototype.restoreTabIndexes = function()
{
    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 = this.aTabIndexes[i];
//            tagElements[k].tabEnabled = true;
            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
//
JModalDialog.prototype.startDrag = function(mouseX, mouseY){
    //  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);
}

//
//  Stops the dragging by deattaching the event listeners and removing the 
//  overlaying div.
//
JModalDialog.prototype.stopDrag = function(){
    //  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;
}

//
//  Drags the dialog using the given mouse position.
//
//  Params:
//      mouseX  Current horizontal mouse position
//      mouseY  Current vertical mouse position
//
JModalDialog.prototype.drag = function(mouseX, mouseY){
    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
//
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
//
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.
//
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.
//
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.
//
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);
    }
}