/** GreyFrame 
 * http://programmer.huang-home.net/GreyFrame/
 * 版权所有 (c) 2007 Programmer Huang (programmer.huang(a)gmail) 基于BSD协议
 * Copyright (c) 2007 Programmer Huang (programmer.huang(a)gmail) Under BSD licenses
 * Licenses: http://programmer.huang-home.net/GreyFrame/#LICENSES
 * $ID$
 */

function GreyFrame(frameName, defaultWidth, defaultHeight)
{
    var me = this;
    var isInited = false;
    var userAgent = navigator.userAgent;
    var isOpera = userAgent.indexOf("Opera") > -1;
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera ;
    var isFF = userAgent.indexOf("Firefox") > -1 ;
    var isSafari = userAgent.indexOf("Safari") > -1 ;
    var ieVer = 0;
    if(isIE)
    {
        /MSIE (\d+\.\d+);/.test(userAgent);
        ieVer = parseFloat(RegExp["$1"]);
    }
    var w = -1;
    var h = -1;
    var x = null;
    var y = null;
    this.defaultWidth = defaultWidth ? defaultWidth : 400;
    this.defaultHeight = defaultHeight ? defaultHeight : 300;
    this.defaultX = GreyFrame.PositionCenter;
    this.defaultY = GreyFrame.PositionCenter;
    this.closed = true;
//    this.moveable = false;
//    this.resizeable = false;
//    enableButtonBar
//    bgStyle = Disable/Enable/CloseOnClick
    var title = GreyFrame.TitlePage;
    var iframeContent = '<html><head></head><body onunload="window.ownerGreyFrame._iframeWindowOnunloadHandle();"></body></html>';
    var bgIframeContent = '<html><head></head><body onunload="window.ownerGreyFrame._gbIframeWindowOnunloadHandle();" style="background-color: gray; "></body></html>';
    
    var thisDocumentUnload = false;
    var reshowTitle = function()
    {
        titleSpan.innerHTML = "&nbsp;";
        try
        {
            if(title == null)
                titleSpan.firstChild.nodeValue = iframe.contentWindow.document.title;
            else
                titleSpan.innerHTML = title;
        }
        catch(e)
        {
        }
    }
    
    var greyDiv = document.createElement('div');
    greyDiv.style.display = "none";
    
    var bgDiv = document.createElement('div');
    bgDiv.style.position = "absolute";
    //bgDiv.style.backgroundColor = "gray";
    bgDiv.style.top = "0px";
    bgDiv.style.left = "0px";
    bgDiv.style.right = "0px";
    bgDiv.style.bottom = "0px";
    
    var bgIframe = document.createElement('iframe');
    bgIframe.style.backgroundColor = "gray";
    bgIframe.style.width = "100%";
//    bgIframe.style.height = "1000px";
    bgIframe.style.filter = "alpha(opacity=50)";
    bgIframe.style.MozOpacity = 0.5;
    bgIframe.style.opacity = 0.5;
    bgIframe.scrolling = "no";
    bgIframe.src = "about:blank";
    bgDiv.appendChild(bgIframe);
    greyDiv.appendChild(bgDiv);
    
    var fgDiv = document.createElement("div");
    fgDiv.style.display = "none";
    fgDiv.style.position = "absolute";
    fgDiv.style.textAlign = "center";
    
    var titleDiv = document.createElement("div");
    titleDiv.style.backgroundColor = "#6AB4FD";
    titleDiv.style.color = "#ffffff";
    titleDiv.style.height = GreyFrame.TitleHeight + "px";
    titleDiv.style.fontSize = "14px";
    
    var closeSpan = document.createElement("span");
    closeSpan.style.backgroundColor = "#96CAFE";
    closeSpan.style.border = "1px solid #6AB4FD";
    closeSpan.style.cssFloat = "right";
    closeSpan.style.styleFloat = "right";
    closeSpan.style.cursor = (isIE && ieVer < 6) ? "hand" : "pointer";
    closeSpan.innerHTML = "关闭";
    closeSpan.onclick = function()
    {
        me.close();
    };
    var titleSpan = document.createElement("span");
    titleSpan.style.padding = "2px 2px";
    titleSpan.style.whiteSpace = "nowrap";
    titleSpan.innerHTML = "";
    titleDiv.appendChild(closeSpan);
    titleDiv.appendChild(titleSpan);
    
    var iframe = document.createElement(isIE ? '<iframe name="' + frameName + '">' : 'iframe');
    iframe.style.backgroundColor = "#ffffff";
    iframe.style.width = this.defaultWidth + "px";
    iframe.style.height = this.defaultHeight + "px";
    iframe.style.border = "1px solid #cccccc";
    iframe.setAttribute("name", frameName);
    iframe.name = frameName;
    iframe.src = "about:blank";
    
    fgDiv.appendChild(titleDiv);
    fgDiv.appendChild(iframe);
    greyDiv.appendChild(fgDiv);
    
    var resizeBackground = function()
    {
        var pageHeight = GreyFrame.getPage("Height");
        if(pageHeight < GreyFrame.getView("Height"))
            pageHeight = GreyFrame.getView("Height");
        bgIframe.style.height = pageHeight + "px";
    }
    
    var onWindowScroll = function(e)
    {
        if(me.closed)
            return ;
        me.showFrame(x, y, w, h);
    };
    GreyFrame.addEventHandle(window, 'scroll', onWindowScroll);
    
    var onWindowResize = function(e)
    {
        if(me.closed)
            return ;
        resizeBackground();
        me.showFrame(x, y, w, h);
    };
    GreyFrame.addEventHandle(window, 'resize', onWindowResize);
    
    me._iframeWindowOnunloadHandle = function()
    {
        if(thisDocumentUnload)
        {
            me._iframeWindowOnunloadHandle = null;
            return ;
        }
        
        x = me.defaultX;
        y = me.defaultY;
        titleSpan.innerHTML = GreyFrame.TitleLoading;
        me.showBackground();
        me.showFrame(x, y, w, h);
        me.onOpenBefor();
    };
    me._gbIframeWindowOnunloadHandle = function()
    {
        thisDocumentUnload = true;
        me._gbIframeWindowOnunloadHandle = null;
    };
    var iframeLoadHandleRuning = false;
    var onIframeLoad = function()
    {
        if(iframeLoadHandleRuning)
            return ;
        iframeLoadHandleRuning = true;
        
        var isOpen = false;
        try
        {
            if(iframe.contentWindow && iframe.contentWindow.location.href == "about:blank")
                isOpen = false;
            else
                isOpen = true;
        }
        catch(e)
        {
            isOpen = true;
        }
        
        if(isOpen)
        {
            try
            {
                iframe.contentWindow.ownerGreyFrame = me;
                iframe.contentWindow.close = function()
                {
                    me.close();
                }
            }
            catch(e)
            {
            }
            
            bgDiv.style.zIndex = ++GreyFrame._zIndex;
            fgDiv.style.zIndex = ++GreyFrame._zIndex;
            reshowTitle();
            me.closed = false;
            me.onOpenAfter();
        }
        else
        {
            if(isInited)
            {
                me.onClose();
            }
            
            iframe.contentWindow.document.open();
            iframe.contentWindow.document.write(iframeContent);
            iframe.contentWindow.document.close();
            iframe.contentWindow.ownerGreyFrame = me;
            w = -1;
            h = -1;
            
            isInited = true;
        }
        
        iframeLoadHandleRuning = false;
    }
    GreyFrame.addEventHandle(iframe, 'load', onIframeLoad);
    
    this.onOpenBefor = function()
    {
//        alert('onOpenBefor');
    }
    this.onOpenAfter = function()
    {
//        alert('onOpenAfter');
    }
    this.onClose = function()
    {
        this.hideFrame();
        this.hideBackground();
    }
    
    this.showBackground = function()
    {
        resizeBackground();
        greyDiv.style.display = "block";
    }
    this.hideBackground = function()
    {
        greyDiv.style.display = "none";
    }
    this.showFrame = function(left, top, width, heigth)
    {
        if(width < 0)
            width = this.defaultWidth;
        if(heigth < 0)
            heigth = this.defaultHeight;
        if(left == GreyFrame.PositionCenter)
            left = (GreyFrame.getPage("Width") - width)/2;
        if(top == GreyFrame.PositionCenter)
            top = (GreyFrame.getView("Height") - heigth)/2;
        fgDiv.style.top = (GreyFrame.getScroll("Top") + top) + "px";
        fgDiv.style.left = (GreyFrame.getScroll("Left") + left) + "px";
        fgDiv.style.display = "block";
        this.resize(width, heigth);
    }
    this.hideFrame = function()
    {
        fgDiv.style.display = "none";
        this.closed = true;
    }
    this.open = function(url, width, heigth)
    {
        if(width) w = width;
        if(heigth) h = heigth;
        iframe.src = url;
    }
    this.close = function()
    {
        iframe.src = "about:blank";
    }
    this.resize = function(width, heigth)
    {
        w = width;
        h = heigth;
        var titleWidth = w - GreyFrame.valueInt(titleDiv.style.borderLeftWidth) 
                            - GreyFrame.valueInt(titleDiv.style.borderRightWidth);
        var styleWidth = w  - GreyFrame.valueInt(iframe.style.borderLeftWidth) 
                            - GreyFrame.valueInt(iframe.style.borderRightWidth);
        var styleHeight = h - GreyFrame.valueInt(iframe.style.borderTopWidth) 
                            - GreyFrame.valueInt(iframe.style.borderBottomWidth);
        if(titleWidth < 0) 
            titleWidth = 0;
        if(styleWidth < 0) 
            styleWidth = 0;
        if(styleHeight < 0) 
            styleHeight = 0;
        titleDiv.style.width = titleWidth + "px";
        iframe.style.width = styleWidth + "px";
        iframe.style.height = styleHeight + "px";
    }
    this.setTitle = function(newTitle)
    {
        title = newTitle;
        if(! this.closed)
            reshowTitle();
    }
    this.moveTo = function(X, Y)
    {
        x = X;
        y = Y;
        this.showFrame(x, y, w, h);
    }
    this.getX = function()
    {
        if(this.closed)
            return null;
        return fgDiv.offsetLeft;
    }
    this.getY = function()
    {
        if(this.closed)
            return null;
        return fgDiv.offsetTop;
    }
    this.getWidth = function()
    {
        if(this.closed)
            return null;
        return iframe.offsetWidth;
    }
    this.getHeight = function()
    {
        if(this.closed)
            return null;
        return iframe.offsetHeight;
    }
//    enableCloseButton
//    onButtonClick(button:Ok/Cancel/Yes/No)
//    setBgOpacity()
//    setContent()
//    getContent()
//    setXXXClass()//Title/fg/bg
//    setXXXStyle()
    
    var initGreyFrame = function()
    {
        window.document.body.appendChild(greyDiv);
        bgIframe.contentWindow.document.open();
        bgIframe.contentWindow.document.write(bgIframeContent);
        bgIframe.contentWindow.document.close();
        bgIframe.contentWindow.ownerGreyFrame = me;
        
        iframe.src = "about:blank";
        
        GreyFrame.removeEventHandle(window, 'load', initGreyFrame);
        initGreyFrame = null;
    }
    if(window.loaded)
    {
        initGreyFrame();
        onIframeLoad();
    }
    else
    {
        GreyFrame.addEventHandle(window, 'load', initGreyFrame);
    }
    
    var destoryGreyFrame = function()
    {
        GreyFrame.removeEventHandle(iframe, 'load', onIframeLoad);
        GreyFrame.removeEventHandle(window, 'unload', destoryGreyFrame);
        
        me.getHeight = null;
        me.getWidth = null;
        me.getY = null;
        me.getX = null;
        me.moveTo = null;
        me.setTitle = null;
        me.resize = null;
        me.close = null;
        me.open = null;
        me.hideFrame = null;
        me.showFrame = null;
        me.hideBackground = null;
        me.showBackground = null;
        me.onClose = null;
        me.onOpenAfter = null;
        me.onOpenBefor = null;
//        me._gbIframeWindowOnunloadHandle = null;
//        me._iframeWindowOnunloadHandle = null;
        onWindowResize = null;
        onWindowScroll = null;
        resizeBackground = null;
        iframe = null;
        titleSpan = null;
        closeSpan.onclick = null;
        closeSpan = null;
        titleDiv = null;
        fgDiv = null;
        bgIframe = null;
        bgDiv = null;
        greyDiv = null;
        reshowTitle = null;
        
        onIframeLoad = null;
        destoryGreyFrame = null;
    }
    GreyFrame.addEventHandle(window, 'unload', destoryGreyFrame);
}
GreyFrame._zIndex = 60000;
GreyFrame.TitleHeight = 22;
GreyFrame.TitlePage = null;
GreyFrame.TitleLoading = "正在载入...";
GreyFrame.PositionCenter = null;
GreyFrame.addEventHandle = function(element, eventName, eventHandle)
{
    if(element.addEventListener)
        element.addEventListener(eventName, eventHandle, false);
    else
        element.attachEvent("on"+eventName, eventHandle);
}
GreyFrame.removeEventHandle = function(element, eventName, eventHandle)
{
    if(element.removeEventListener)
        element.removeEventListener(eventName, eventHandle, false);
    else
        element.detachEvent("on"+eventName, eventHandle);
}
GreyFrame.valueInt = function(str)
{
    var n = parseInt(str);
    if(isNaN(n))
        return 0;
    else
        return n;
}
GreyFrame.getView = function(v)
{
    if(document.compatMode == "CSS1Compat")
        return document.documentElement["client" + v];
    else//BackCompat
        return document.body["client" + v];
}
GreyFrame.getScroll = function(v)
{
    return Math.max(document.documentElement["scroll" + v], document.body["scroll" + v])
}
GreyFrame.getPage = function(v)
{
    if(v == 'Width')
    {
        if(window.innerWidth && window.scrollMaxX) // Firefox
            return window.innerWidth + window.scrollMaxX;
        else if(document.body.scrollWidth > document.body.offsetWidth) // all but Explorer Mac
            return document.body.scrollWidth;
        else    // works in Explorer 6 Strict, Mozilla (not FF) and Safari
            return document.body.offsetWidth + document.body.offsetLeft;
    }
    else
    {
        if (window.innerHeight && window.scrollMaxY) // Firefox
            return window.innerHeight + window.scrollMaxY;
        else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
            return document.body.scrollHeight;
        else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
            return document.body.offsetHeight + document.body.offsetTop;
    }
}

window.loaded = false;
GreyFrame.addEventHandle(window, "load", function(e){window.loaded = true});

function sf(obj,rid){
	if(obj.checked==true){
		window.document.getElementById(rid).disabled = false;
	}else{
		window.document.getElementById(rid).disabled = true;
	}
}


