// ------------------ SUBSTITUTE FOR IE showModalDialog method ---------------------
// uses normal popup window with a div that prevents clicks on main window
// clicking on the main window returns focus to the dialog window
// Note: this differs from the modalDialog in that it will call the callback function when it is complete 
//		rather than waiting for a return value

// setup in calling page:
	// var SE_AP_ModalDialog = new SE_ModalDialog('SE_AP_ModalDialog', callbackfunctionobject);

// call to the dialog
	// SE_AP_ModalDialog.show(url, width, height, windowName)

// dialog returns value:
		// if (window.opener && window.opener.SE_AP_ModalDialog)
		//		window.opener.SE_AP_ModalDialog.handleReturn(somevalue);

// a callback function that will use the return value e.g.:
	// function myCallBackFunction()
		//{
		//	nextmethod(SE_AP_ModalDialog.returnValue);
		//}
	//

	function SE_ModalDialog(objectName, callBackFunction) {
	  
		this.objectName = objectName;
		this.popupWindow = null;
		this.returnValue = '';
		this.callbackFunction = callBackFunction;
		this.done = false;
		this.watch = null;
		this.dialogArguments = null;
		this.dialogStyleElement = null;
		this.cssText = "";
		
		this.removeWatch = _SE_ModalDialog_RemoveWatch;
		this.show = _SE_ModalDialog_Show;
		this.handleReturn = _SEModalDialog_HandleReturn;
		this.handlingReturn = false;
		this.toggleCover = _SE_ModalDialog_ToggleCover;
	}
	   
    function SE_ModalDialog_Watch(oModalDialog)
	{
	    if (oModalDialog.popupWindow && !oModalDialog.popupWindow.closed)
		{
		    try
		    {
		        if (oModalDialog.callingObjectName 
		        && !oModalDialog.popupWindow.callingObjectName) 
		            oModalDialog.popupWindow.callingObjectName = oModalDialog.callingObjectName;
		        if (typeof(oModalDialog.popupWindow.callingDialogName) == 'undefined' 
		            || !oModalDialog.popupWindow.callingDialogName ) 
		            oModalDialog.popupWindow.callingDialogName = oModalDialog.callingDialogName;
		     }
		     catch(e)
		     {
		        oModalDialog.removeWatch();
		        return;
		     }
		}
		
		// this handles cancels or window closes that don't call the handleReturn method
		if (oModalDialog.popupWindow && !oModalDialog.popupWindow.closed && !oModalDialog.done)
			oModalDialog.watch = setTimeout("SE_ModalDialog_Watch(" + oModalDialog.objectName + ")", 100);
		else 
		{
			var needCallback = (oModalDialog && !oModalDialog.done);
			oModalDialog.removeWatch();
			if (needCallback && oModalDialog.callbackFunction) 
				oModalDialog.callbackFunction();
		}
	}
	
	function _SE_ModalDialog_RemoveWatch()
	{
		// clean up the dialog and all of its parts (except the returnvalue which is needed by the callbackfuction
		this.done = true;
		if (this.popupWindow && !this.popupWindow.closed)
		{
		    var myPopup = this.popupWindow;
		    setTimeout(function () { try{myPopup.close();} catch(e){} }, 10);
			this.popupWindow = null;
		}
		if (this.watch)
			clearTimeout(this.watch);
		this.toggleCover(false);
	}
	        
	function _SE_ModalDialog_Show(url, width, height, windowName, params)
	{
		// open the window an start watching for it to close
		this.removeWatch();
		this.popupWindow = SE_MD_OpenWin(url, width, height, windowName, params);  
		
		this.popupWindow.focus(); 
		this.toggleCover(true);
		this.done = false;
		if (this.dialogStyleElement)
		    SE_ModalDialog_SetStyle(this);
		SE_ModalDialog_Watch(this);
	}
	
	var SE_ModalDialog_SetStyle_Count = 0;
	function SE_ModalDialog_SetStyle(myDialog)
	{
		SE_ModalDialog_SetStyle_Count++;
		var hasWindow = false;
		try
		{
		    hasWindow = myDialog && myDialog.popupWindow && myDialog.popupWindow.document && myDialog.popupWindow.document.styleSheets[myDialog.dialogStyleElement];
		}
		catch (exc)
		{
		}
		if (hasWindow)
			myDialog.popupWindow.document.styleSheets[myDialog.dialogStyleElement].cssText = myDialog.cssText;
	    else if (SE_ModalDialog_SetStyle_Count < 10 && myDialog)
	        setTimeout('SE_ModalDialog_SetStyle(' + myDialog.objectName + ')', 300);
	}


	function _SE_ModalDialog_ToggleCover(addCover)
	{
		// hide or show a full window transparent div that prevents the focus from leaving the dialog
		if (addCover)
		{
			this.coverDiv = document.createElement("div");
			this.coverDiv.id = "coverDiv";			
			this.coverDiv.className = "modalcover";
			this.coverDiv.onclick = function () {SE_ModalDialog_FocusOnPopup(modalPopup);}
			this.coverDiv.popupWindow = this.popupWindow;
			this.coverDiv.style.width = (document.body.scrollWidth ?  document.body.scrollWidth :  document.body.offsetWidth) + "px" ;
			document.body.appendChild(this.coverDiv);

			if ($("body").height() <= 2048)
			    this.coverDiv.style.height = $("body").height() + "px";
			else {
			
			    this.coverDiv.style.height = "2048px";
			    this.coverDivBottom = document.createElement("div");
			    this.coverDivBottom.id = "coverDivBottom";
			    this.coverDivBottom.className = "modalcover";
			    this.coverDivBottom.onclick = function() { SE_ModalDialog_FocusOnPopup(modalPopup); }
			    this.coverDivBottom.popupWindow = this.popupWindow;
			    this.coverDivBottom.style.width = this.coverDiv.style.width;
			    this.coverDivBottom.style.top = "2048px";
			    this.coverDivBottom.style.height = ($("body").height() - 2048) + "px";
			    document.body.appendChild(this.coverDivBottom);
			}
			modalPopup = this;
		}
		else if(this.coverDiv && typeof(this.coverDiv) != 'undefined')
		{
			this.coverDiv.parentNode.removeChild(this.coverDiv);
			this.coverDiv = null;
			if (this.coverDivBottom) {
			    this.coverDivBottom.parentNode.removeChild(this.coverDivBottom);
			    this.coverDivBottom = null;
			}
		}		
	}
	
	// static functions
	
	function _SEModalDialog_HandleReturn(returnValue)
	{
		// we are done - execute the callback
		this.returnValue = returnValue;
		this.removeWatch();
		this.handlingReturn = true;
		this.callbackFunction();
		this.handlingReturn = false;
	}
	
	function SE_ModalDialog_FocusOnPopup(modalPopup)
	{
		// someone clicked on the main window - go back to the dialog
		if (modalPopup.popupWindow && !modalPopup.popupWindow.closed)
			modalPopup.popupWindow.focus()
		else
			modalPopup.toggleCover(false);
	}
	
	function SE_MD_OpenWin(url, width, height, name, params)
	{
		var sh = screen.availHeight;
		var sw = screen.availWidth;
		var options = "";
		if(!width) width = 525;
		if(!height) height = 657;
		if (!name) name = "_blank";
		if (params)
			options =  params + ",";
		else
			options =  'dependent=yes,fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no,';
		options += 'width=' + width + ',height=' + height + ',top=' + (sh-height)/2 + ',left=' + (sw-width)/2;
		var win = window.open(url, name, options);
		win.focus();	
		return win;	
	}
	
	
    function SE_Modal_GetDivModalCover()
    {
        var divModalCover = document.getElementById("MLX_DivModalCover");
        if (!divModalCover)
        {
            divModalCover = document.createElement("DIV");
            divModalCover.id = "MLX_DivModalCover";
            divModalCover.style.backgroundColor = "Gray";
	        divModalCover.style.filter = "alpha(opacity=70)";
	        divModalCover.style.opacity = "0.7";
	        divModalCover.style.position = "absolute";
	        divModalCover.style.top = 0;
	        divModalCover.style.left = 0;
	        divModalCover.style.display = "none";
	        document.body.appendChild(divModalCover);
        }
        return divModalCover;
    }

    function SE_Modal_ToggleDivModalCover(display)
    {
        var divModalCover = document.getElementById("MLX_DivModalCover");
        if (!divModalCover) 
            divModalCover = SE_Modal_GetDivModalCover();
        
        if (display != "none")
        {
            divModalCover.style.display = "block";
            divModalCover.style.height = document.body.scrollHeight;
            divModalCover.style.width = document.body.scrollWidth;
            divModalCover.style.zIndex = 10;
        }
        else
            divModalCover.style.display = "none";
    }



    /* Greybox Redux
    * Required: http://jquery.com/
    * Written by: John Resig
    * Based on code by: 4mir Salihefendic (http://amix.dk)
    * License: LGPL (read more in LGPL.txt)
    */

    var GB_DONE = false;
    var GB_HEIGHT = 400;
    var GB_WIDTH = 400;

    function GB_show(caption, url, height, width) {
        GB_HEIGHT = height || 400;
        GB_WIDTH = width || 400;
        if (!GB_DONE) {
            $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img src='close.gif' alt='Close window'/></div>");
            $("#GB_window img").click(GB_hide);
            $("#GB_overlay").click(GB_hide);
            $(window).resize(GB_position);
            GB_DONE = true;
        }

        $("#GB_frame").remove();
        $("#GB_window").append("<iframe id='GB_frame' src='" + url + "'></iframe>");

        $("#GB_caption").html(caption);
        $("#GB_overlay").show();
        GB_position();

        if (GB_ANIMATION)
            $("#GB_window").slideDown("slow");
        else
            $("#GB_window").show();
    }

    function GB_hide() {
        $("#GB_window,#GB_overlay").hide();
    }

    function GB_position() {
        var de = document.documentElement;
        var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        $("#GB_window").css({ width: GB_WIDTH + "px", height: GB_HEIGHT + "px",
            left: ((w - GB_WIDTH) / 2) + "px"
        });
        $("#GB_frame").css("height", GB_HEIGHT - 32 + "px");
    }
   
