var modalDiv = new Object();

// set up the vars first

modalDiv.isMozilla;
modalDiv.objDiv = null;
modalDiv.over = false;
modalDiv.href = "";
modalDiv.width = "100";
modalDiv.height = "50"
modalDiv.contentDiv;
modalDiv.defaultContent; // used to hold content whilst showing other stuff.
modalDiv.onSuccess; // contains expression to run if succussfully closed not cancelled.


modalDiv.hide = function(success) 
{
    // set the content back to what it was
	document.getElementById('modalDivContent').innerHTML = modalDiv.defaultContent;
	document.getElementById('modalDivTitle').innerHTML = "Information";
	document.getElementById('modalDivTable').style.display =  'none';
	document.getElementById('modalDivTable').style.visibility='hidden';
	document.getElementById('modalDivDimmer').style.visibility = 'hidden';
	
	// now run the expression if we have been asked to
	if(modalDiv.onSuccess && success){
	    console.debug("about to eval:" + modalDiv.onSuccess); 
	    eval(modalDiv.onSuccess);
	}

}

//
//
//
modalDiv.MouseDown = function(e){
    
    if (modalDiv.over)
    {
        if (modalDiv.isMozilla) {
            modalDiv.objDiv = document.getElementById('modalDivTable');
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            modalDiv.objDiv = document.getElementById('modalDivTable');
            modalDiv.objDiv = modalDiv.objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}

//
//
//
modalDiv.MouseMove = function(e) 
{
    
    if (modalDiv.objDiv) {
    
        if (modalDiv.isMozilla) {
            
            modalDiv.objDiv.style.top = (e.pageY-Y) + 'px';
            modalDiv.objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else 
        {
            alert("Sorry you can't move this box in Internet Explorer because of a browser bug.\nWe recommend the use of Firefox or Safari browsers.");
            modalDiv.objDiv = null;
            return;
            /*
            // disable for ie due to crashing bug
            modalDiv.objDiv.pixelLeft = event.clientX-X + document.body.scrollLeft;
            modalDiv.objDiv.pixelTop = event.clientY-Y + document.body.scrollTop;
            return false;
            */
        }
    }
}

modalDiv.MouseUp = function() 
{
    modalDiv.objDiv = null;
}

modalDiv.init = function()
{
    // check browser
    modalDiv.isMozilla = (document.all) ? 0 : 1;

    if (modalDiv.isMozilla) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }

    document.onmousedown = modalDiv.MouseDown;
    document.onmousemove = modalDiv.MouseMove;
    document.onmouseup = modalDiv.MouseUp;

}

modalDiv.show = function(href, title, width, height, onSuccess)
{
   // override the defaults if we have new values
   if(width) modalDiv.width = width;
   if(height) modalDiv.height = height;
   
   // set the current href
   modalDiv.href = href;
   
   // if we are closed 'successfully' then we might evaluate
   // the onSuccess expression
   modalDiv.onSuccess = onSuccess;
   
   // calculate the  positioning relative to the viewport
   var viewPortWidth = windowState.getWidth();
   var viewPortHeight = windowState.getHeight();
   var horizontalScroll = windowState.getScrollX();
   var verticalScroll = windowState.getScrollY();
   
   var leftP = Math.round(horizontalScroll+((viewPortWidth-modalDiv.width)/2));
   leftP = (leftP < 0)?0:leftP;
   var topP = Math.round(verticalScroll+((viewPortHeight-modalDiv.height)/3.33));
   topP = (topP < 0)?0:topP;
       
    // actually show it	        
   	
   	// make sure the dimmer covers the whole page area
   	document.getElementById('modalDivDimmer').style.visibility = "visible";
   	document.getElementById('modalDivDimmer').style.height = document.getElementById("page").scrollHeight + 'px';
   	
   	document.getElementById('modalDivTable').style.display =  'block';
    document.getElementById('modalDivContent').style.width =  modalDiv.width + 'px';
    document.getElementById('modalDivContent').style.height = modalDiv.height + 'px';
    document.getElementById('modalDivTable').style.left = leftP + 'px';
    document.getElementById('modalDivTable').style.top = topP + 'px';
    document.getElementById('modalDivTitle').innerHTML = title;
	document.getElementById('modalDivTable').style.visibility = "visible";
	
    modalDiv.load();
}

modalDiv.load = function() {
    
    // take a copy of the content for putting back 
    // when we close the div
    modalDiv.defaultContent = dojo.byId('modalDivContent').innerHTML;
    
    dojo.xhrGet( {
        url: modalDiv.href, 
        handleAs: "text",
        timeout: 30000,
        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { 
          var content = dojo.byId('modalDivContent')
          content.innerHTML = response;
          
          // if this contains a visible input element we should select it
          // fixme - needs work.
          // var firstVis = dojo.query("input", content).forEach("if (item.type != 'hidden'){ item.select(); break; }" );
          
          return response;
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) {
          console.error("HTTP status code: ", ioArgs.xhr.status); 
          modalDiv.content = ioArgs.xhr.status;
          return response;
          }
    });
 }

// call init when we load
modalDiv.init();

