/*! 
 *  Developed by CJ Amodeo (cjamodeo@gmail.com)
 *  Page should be XHTML 1.0 Transitional
 *  getBgColor(), setBgColor(color) -default:#000000,
 *  getOpacity(), setOpacity(val) -default:0.6,
 *  show(id) -ID of obj to display as modal, hide() -hides modal
 */
var f9ModalDialog = (function() {
    var bgColor = "#000000";
    var bgOpacity = 0.6;
    var dialogID = null;
    var divWidth = 0;
    var divHeight = 0;
    var grayoutDivID = "f9ModPopDivGrayout";
    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;
    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;
            } 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;
            } else {
                x = (document.body.clientWidth-divWidth)/2;
            }
        } 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 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.top = getScrollY() + "px";
    }       
    function constructor() { } 
    constructor.getBgColor = function() { return bgColor; }
    constructor.setBgColor = function(color) { bgColor = color; }
    constructor.getOpacity = function() { return bgOpacity; }
    constructor.setOpacity = function(opacity) { bgOpacity = opacity; }
    constructor.show = function(divDialogID) {
        if (IE6 && !IE7) scrollTop = document.documentElement.scrollTop;
        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();
    } 
    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.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);
        document.body.appendChild(div);
    }
    constructor.eventScroll = function() {
        if (dialogID != null) {
            if (document.getElementById(dialogID).style.display == "inline") {
                if (IE6 && !IE7) document.documentElement.scrollTop = scrollTop;
            }
        }
    }
    constructor.eventResize = function() {
        if (dialogID != null) {
            if (document.getElementById(dialogID).style.display == "inline") {
                positionGray();
            }
        }
    }
    addWindowEvent("load", constructor.eventLoad);
    if (IE6 && !IE7) { addWindowEvent("scroll", constructor.eventScroll); }
    if (!IE) { window.captureEvents(Event.RESIZE); }
    addWindowEvent("resize", constructor.eventResize);
    return constructor;   
})();
