/* Copyright Stephen Myers 2006 */
/* http://www.stephenmyers.co.uk */
/* BETA Version in development */
/* Inspired by Prototype http://prototype.conio.net/ and lightbox http://www.huddletogether.com/projects/lightbox/ */

/*-------------------------------GLOBAL VARIABLES------------------------------------*/
var isIE = document.all ? true : false;
/*-----------------------------------------------------------------------------------------------*/


var ModalBox = {    
    yPos: 0,
    xPos: 0,
    maskObj: null,
    windowObj: null,

    // Called to display the ModalBox
    Open: function(windowId){
        this.windowObj = document.getElementById(windowId);
        this.maskObj = document.getElementById('screenMask');
        this.Activate();
    },
    
    // Called to hide the ModalBox
    Close: function(windowId){
        this.windowObj = document.getElementById(windowId);
        this.maskObj = document.getElementById('screenMask');
        this.Deactivate();
    },
    
    // Called to postion the ModalBox over an active element
    GhostElement: function(ghostElementId, editorId){
        var ghostElement = document.getElementById(ghostElementId);
        var box;	    
	    //alert('Left ' + ghostElement.offsetLeft + ' : Top ' + ghostElement.offsetTop);
	    
        if (document.getBoxObjectFor) { 
		    box = document.getBoxObjectFor(ghostElement); 
                    
		    this.windowObj.style.width = box.width + 'px';
	        this.windowObj.style.left = box.x + 'px';
	        this.windowObj.style.top = box.y + 'px';
	        
	        this.windowObj.style.marginTop = '0px';
	        this.windowObj.style.marginLeft = '0px';
	    } 
	    else if (ghostElement.getBoundingClientRect) { 
		    box = ghostElement.getBoundingClientRect(); 

		    this.windowObj.style.width = (box.right - box.left);
            this.windowObj.style.left = box.left;
            this.windowObj.style.top = box.top;

	        this.windowObj.style.marginTop = 0;
	        this.windowObj.style.marginLeft = 0;
	    } 
        
        // Set the size of the editor
        var editor = document.getElementById(editorId);
        editor.InitialHeight = -1;     
        editor.InitialWidth = -1;     
        
        var theIFrame = document.getElementById("RadEContentIframe" + editor.Id);
        if(theIFrame != null){
            theIFrame.style.height = (box.top - box.bottom) + "px";
            theIFrame.style.width = (box.right - box.left) + "px";
        }
        if (!isIE) editor.SetSize(box.width, box.height);//FireFox bug, TD does not decrease height  
    },

    // Sets the Element display property
    DisplayModalBox: function(display){
        
        this.maskObj.style.display = display;
        this.windowObj.style.display = display;
    
        if (isIE) { 
            var centerPoint = document.body.scrollWidth/2;
            var box = this.windowObj.getBoundingClientRect();
            this.windowObj.style.left = centerPoint-((box.right - box.left)/2);
			
			this.maskObj.style.height = document.body.scrollHeight;
			this.maskObj.style.width = document.body.scrollWidth;
	    } 
	    else{ 
            var centerPoint = window.innerWidth/2;
	        var box = document.getBoxObjectFor(this.windowObj); 
            this.windowObj.style.left = centerPoint-(box.width/2) + 'px';
	    }
	    
        if(display != 'none') this.LoadInfo();
    },
        
    // Turn everything on - mainly the IE fixes
    Activate: function(){
        if (isIE){
	        this.GetScroll();
	        this.PrepareIE('100%', 'hidden');
	        this.SetScroll(0,0);
	        this.HideSelects('hidden');
        }
        this.DisplayModalBox("block");
    },
    	
    // IE requires height to 100% and overflow hidden or else you can scroll down past the ModalBox
    PrepareIE: function(height, overflow){
        bod = document.getElementsByTagName('body')[0];
        bod.style.height = height;
        bod.style.overflow = overflow;

        htm = document.getElementsByTagName('html')[0];
        htm.style.height = height;
        htm.style.overflow = overflow; 
    },
    	
    // In IE, select elements hover on top of the ModalBox
    HideSelects: function(visibility){
        selects = document.getElementsByTagName('select');
        for(i = 0; i < selects.length; i++) {
	        selects[i].style.visibility = visibility; 
      }
    },
    	
    // Taken from ModalBox implementation found at http://www.huddletogether.com/projects/lightbox/
    GetScroll: function(){
        if (self.pageYOffset) {
	        this.yPos = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){
	        this.yPos = document.documentElement.scrollTop; 
        } else if (document.body) {
            this.yPos = document.body.scrollTop;
        }
    },
    	
    SetScroll: function(x, y){
        window.scrollTo(x, y); 
    },
    	
    // Method to perform any load behaviour when the ModalBox is shown
    LoadInfo: function() {
	    
    },
    	
    // Hides the ModalBox
    Deactivate: function(){
    		
        if (isIE){
	        this.SetScroll(0,this.yPos);
	        this.PrepareIE("100%", "auto");
	        this.HideSelects("visible");
        }
    	
        this.DisplayModalBox("none");
    },
    
    startDrag: function(e, id){
      var el;
      var x, y;

      // If an element id was given, find it. Otherwise use the element being
      // clicked on.

      if (id)
        dragObj.elNode = document.getElementById(id);
      else {
        if (isIE)
          dragObj.elNode = window.event.srcElement;
        else
          dragObj.elNode = event.target;

        // If this is a text node, use its parent element.

        if (dragObj.elNode.nodeType == 3)
          dragObj.elNode = dragObj.elNode.parentNode;
      }

      // Get cursor position with respect to the page.

      if (isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
          + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
          + document.body.scrollTop;
      }
      else {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
      }

      // Save starting positions of cursor and element.

      dragObj.cursorStartX = x;
      dragObj.cursorStartY = y;
      dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
      dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

      if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
      if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

      // Update element's z-index.

      dragObj.elNode.style.zIndex = ++dragObj.zIndex;

      // Capture mousemove and mouseup events on the page.

      if (isIE) {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup",   dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      else {
        document.addEventListener("mousemove", dragGo,   true);
        document.addEventListener("mouseup",   dragStop, true);
        event.preventDefault();
      }	
    },
    
    dragGo: function (event) {
      var x, y;

      // Get cursor position with respect to the page.

      if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
          + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
          + document.body.scrollTop;
      }
      if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
      }

      // Move drag element by the same amount the cursor has moved.

      dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
      dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

      if (browser.isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (browser.isNS)
        event.preventDefault();
    },
    
    stopDrag: function(e, id){
        // Stop capturing mousemove and mouseup events.

      if (browser.isIE) {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup",   dragStop);
      }
      if (browser.isNS) {
        document.removeEventListener("mousemove", dragGo,   true);
        document.removeEventListener("mouseup",   dragStop, true);
      }
    }
}
