/*! 
*  Developed by CJ Amodeo (cjamodeo@gmail.com)
*  Page should be XHTML 1.0 Transitional
*  evokeModalDialog functions:
*  getBgColor(), setBgColor(color) -default:#ffffff,
*  getOpacity(), setOpacity(val) -default:0.6,
*  show(id, appendToNodeID) -ID of obj to display as modal -will remove the object and append it to the sepcified node, 
*  hide() -hides modal
*  get/setHideOnOutsideClick(bool) -setting to true will hide the dialog when the gray area is clicked
*  attachClickEvent/detachClickEvent -register/unregister a function that is called when the gray area is clicked
*/
var evokeModalDialog = (function() {
    var bgColor = "#ffffff";
    var bgOpacity = 0.6;
    var dialogID = null;
    var divWidth = 0;
    var divHeight = 0;
    var grayoutDivID = "evoModPopDivGrayout";
    var grayoutDivObj = null;
    var userAgent = window.navigator.userAgent.toLowerCase();
    var userAgentVersion = (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0, '0'])[1];
    var IE = /msie/.test(userAgent);
    var IE6 = (IE && (parseInt(userAgentVersion) >= 6)); //includes IE7
    var IE7 = (IE && (parseInt(userAgentVersion) >= 7));
    var scrollTop = 0;
    var scrollLeft = 0;
    var hideOnOutsideClick = true;
    function getGrayWidth() {
        var w = 0;
        if (IE) {
            if (IE6) {
                w = document.documentElement.clientWidth;
            } else {
                w = document.body.clientWidth;
            }
        } else {
            w = window.innerWidth;
        }
        return w;
    }
    function getGrayHeight() {
        var h = 0;
        if (IE) {
            if (IE6) {
                h = document.documentElement.clientHeight;
                //h = document.documentElement.scrollHeight;
            } else {
                h = document.body.clientHeight;
            }
        } else {
            h = window.innerHeight;
        }
        return h;
    }
    function getCenterX() {
        var x = 0;
        if (IE) {
            if (IE6) {
                x = (document.documentElement.clientWidth - divWidth) / 2 + getScrollX();
            } else {
                x = (document.body.clientWidth - divWidth) / 2 + getScrollX();
            }
        } else {
            x = (window.innerWidth - divWidth) / 2;
        }
        if (!IE) x -= 10;
        if (x < 0) x = 0;
        return x;
    }
    function getCenterY() {
        var y = 0;
        if (IE) {
            if (IE6) {
                y = ((document.documentElement.clientHeight - divHeight) / 2) + getScrollY();
            } else {
                y = ((document.body.clientHeight - divHeight) / 2) + getScrollY();
            }
        } else {
            y = (window.innerHeight - divHeight) / 2;
        }
        if (!IE) y -= 10;
        if (y < 0) y = 0;
        return y;
    }
    function getScrollX() {
        var x = 0;
        if (IE) {
            if (IE6 && !IE7) x = document.documentElement.scrollLeft;
            else if (IE7) x = 0;
            else x = document.body.scrollLeft;
        } else {
            x = 0;
        }
        return x;
    }
    function getScrollY() {
        var y = 0;
        if (IE) {
            if (IE6 && !IE7) y = document.documentElement.scrollTop;
            else if (IE7) y = 0;
            else y = document.body.scrollTop;
        } else {
            y = 0;
        }
        return y;
    }
    function getStyle(el, style) {
        if (!document.getElementById)
            return;
        var value = el.style[style];
        if (!value) {
            if (el.currentStyle)
                value = el.currentStyle[style];
            else if (document.defaultView)
                value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
        }
        return value;
    }
    function addWindowEvent(eventName, func) {
        if (IE) {
            window.attachEvent("on" + eventName, func);
        } else {
            window.addEventListener(eventName, func, false);
        }
    }
    function positionGray() {
        var div = document.getElementById(dialogID);
        if (div != null) {
            div.style.left = getCenterX() + "px";
            div.style.top = getCenterY() + "px";
        }
        grayoutDivObj.style.width = getGrayWidth() + "px";
        grayoutDivObj.style.height = getGrayHeight() + "px";
        grayoutDivObj.style.left = getScrollX() + "px";
        grayoutDivObj.style.top = getScrollY() + "px";
    }
    function constructor() { }
    constructor.getHideOnOutsideClick = function() { return hideOnOutsideClick; }
    constructor.setHideOnOutsideClick = function(flag) { hideOnOutsideClick = false; }
    constructor.getBgColor = function() { return bgColor; }
    constructor.setBgColor = function(color) { bgColor = color; if (document.getElementById(grayoutDivID) != null) document.getElementById(grayoutDivID).style.backgroundColor = bgColor; }
    constructor.getOpacity = function() { return bgOpacity; }
    constructor.setOpacity = function(opacity) { bgOpacity = opacity; }


    constructor.show = function(divDialogID, appendToNodeID) {

        if (appendToNodeID != undefined) {
            var previous = document.getElementById(appendToNodeID).getElementById(divDialogID);
            if (previous != null) {
                document.getElementById(appendToNodeID).removeChild(document.getElementById(appendToNodeID).getElementById(divDialogID));
            }
            var layer = document.getElementById(divDialogID);
            var layerParent = layer.parentNode;
            layerParent.removeChild(layer);
            document.getElementById(appendToNodeID).appendChild(layer);

        }

        if (IE6 && !IE7) {
            scrollTop = getScrollY();
            scrollLeft = getScrollX();
        }
        if (grayoutDivObj == null)
            grayoutDivObj = document.getElementById(grayoutDivID);
        dialogID = divDialogID;
        divWidth = parseInt(getStyle(document.getElementById(dialogID), "width"));
        divHeight = parseInt(getStyle(document.getElementById(dialogID), "height"));
        grayoutDivObj.style.display = "inline";
        var div = document.getElementById(dialogID);
        div.style.display = "inline";
        div.style.zIndex = 99;
        if (IE6 && !IE7) {
            div.style.position = "absolute";
            grayoutDivObj.style.position = "absolute";
            if (IE6 && !IE7) {
                var elems = document.getElementsByTagName("select");
                for (var i = 0; i < elems.length; i++) {
                    elems[i].style.visibility = "hidden";
                }
                elems = div.getElementsByTagName("select");
                for (var i = 0; i < elems.length; i++) {
                    elems[i].style.visibility = "visible";
                }
            }
        } else {
            div.style.position = "fixed";
            grayoutDivObj.style.position = "fixed";
        }
        positionGray();
    }
    
    // version to be able to pass in an x,y coord to pick the disaply location
    // slightly broken in IE, as the scroll event needs to be removed
   	constructor.showPos = function(divDialogID, x, y, appendToNodeID) {

		if (appendToNodeID != undefined) {
			var previous = document.getElementById(appendToNodeID).getElementById(divDialogID);
			if (previous != null) {
				document.getElementById(appendToNodeID).removeChild(document.getElementById(appendToNodeID).getElementById(divDialogID));
			}
			var layer = document.getElementById(divDialogID);
			var layerParent = layer.parentNode;
			layerParent.removeChild(layer);
			document.getElementById(appendToNodeID).appendChild(layer);
		}
		
		dialogID = divDialogID;
		
		var div = document.getElementById(dialogID);
		div.style.display = "inline";
		div.style.zIndex = 99;
		div.style.position = "absolute";
		var diagX = x;
		var diagY = y;
/*		if (IE6 && !IE7) {
			div.style.position = "absolute";
			var diagX = e.pageX;
			var diagY = e.pageY;
		} else {
			div.style.position = "absolute";
			var diagX = e.pageX;
			var diagY = e.pageY;
		} */
		
		div.style.left = diagX+80 + "px";
		div.style.top = diagY-60 + "px";
	}
    
    constructor.hide = function() {
        document.getElementById(dialogID).style.display = "none";
        grayoutDivObj.style.display = "none";
        grayoutDivObj.style.width = "0px"; //OSX Firefox Hack
        grayoutDivObj.style.height = "0px";
        if (IE6 && !IE7) {
            var elems = document.getElementsByTagName("select");
            for (var i = 0; i < elems.length; i++) {
                elems[i].style.visibility = "visible";
            }
        }
    }
    
    constructor.hidePos = function() {
        document.getElementById(dialogID).style.display = "none";
        //grayoutDivObj.style.display = "none";
        //grayoutDivObj.style.width = "0px"; //OSX Firefox Hack
        //grayoutDivObj.style.height = "0px";
        if (IE6 && !IE7) {
            var elems = document.getElementsByTagName("select");
            for (var i = 0; i < elems.length; i++) {
                elems[i].style.visibility = "visible";
            }
        }
    }
    
    constructor.outsideClick = function() {
        if (hideOnOutsideClick)
            constructor.hide();
            if (IE) {
                destroy_flash_element('flash_holder','flash_holder_container');
            }
    }

    constructor.attachClickEvent = function(func) {
        var obj = document.getElementById(grayoutDivID);
        if (obj.attachEvent) {
            obj.attachEvent("onclick", func);
        } else {
            obj.addEventListener("click", func, false);
        }
    }
    
    constructor.detachClickEvent = function(func) {
        var obj = document.getElementById(grayoutDivID);
        if (obj.detachEvent) {
            obj.detachEvent("onclick", func);
        } else {
            obj.removeEventListener("click", func, false);
        }
    }

    constructor.eventLoad = function() {
        var ieOp = parseFloat(bgOpacity).toFixed(2) * 100;
        var grayoutStyle = "position:fixed;left:0px;top:0px;width:0px;height:0px;display:none;background-color:" + bgColor + ";z-index:98;filter:alpha(opacity=" + ieOp + ");opacity:" + bgOpacity + ";";
        if (document.createStyleSheet) {
            var css = document.createStyleSheet();
            css.addRule("#" + grayoutDivID, grayoutStyle);
        } else if (userAgent.indexOf("opera") != -1) {
            var css = document.createElement("style");
            var strCss = "#" + grayoutDivID + " { " + grayoutStyle + " }";
            css.innerHTML = strCss;
            document.body.appendChild(css);
        } else {
            var head = document.getElementsByTagName("head")[0];
            var css = document.createElement("style");
            css.type = "text/css";
            css.media = "all";
            head.appendChild(css);
            var index = 0;
            if (document.styleSheets.length > 0) index = document.styleSheets.length - 1;
            css = document.styleSheets[index];
            var strCss = "#" + grayoutDivID + " { " + grayoutStyle + " }";
            css.insertRule(strCss, 0); //won't work for opera for some reason
        }
        var div = document.createElement("div");
        div.setAttribute("id", grayoutDivID);
        //if (!IE6)
        //div.setAttribute("onclick", "evokeModalDialog.outsideClick();");
        document.body.appendChild(div);

        var obj = document.getElementById(grayoutDivID);
        if (obj.attachEvent) {
            obj.attachEvent("onclick", function() { evokeModalDialog.outsideClick(); });
        } else {
            obj.addEventListener("click", function() { evokeModalDialog.outsideClick(); }, false);
        }

        //if (IE6 && !IE7) {
        //document.getElementById(grayoutDivID).attachEvent("onclick", function() { evokeModalDialog.outsideClick(); });
        //}
    }
    constructor.eventScroll = function() {
        if (dialogID != null) {
            if (document.getElementById(dialogID).style.display == "inline") {
                if (IE6 && !IE7) {

                    document.documentElement.scrollTop = scrollTop;
                    document.documentElement.scrollLeft = scrollLeft
                }
            }
        }
    }
    constructor.eventResize = function() {
        if (dialogID != null) {
            if (document.getElementById(dialogID).style.display == "inline") {
                positionGray();
            }
        }
    }
    // removed adding the initialization event from window.onload
    // so it can be referenced in common.js main_init function so 
    // we can control initialization order 
    //addWindowEvent("load", constructor.eventLoad);
    if (IE6 && !IE7) { addWindowEvent("scroll", constructor.eventScroll); }   // comment this line to remove functionality to keep modal window in place; now it scrolls with window
    if (!IE) { window.captureEvents(Event.RESIZE); }
    addWindowEvent("resize", constructor.eventResize);
    return constructor;
})();