
// 小古怪JS库
/*
  Xiaogug.Dialog 对话框应用
  Xiaogug.Dialog.Loading 显示页面加载中，当页面加载完成后，则关闭此对话框
  Xiaogug.Dialog.ShowMessage(msgTitle,msgText,handler,isShowCancel) 显示消息对话框
    msgTitle: 消息标题，如果为空，则显示“系统消息”
    msgText: 消息内容，需要显示的消息文本内容，可以为HTML
    handler: 确定处理函数，点确定后，要执行的JS函数
    isShowCancel: 是否显示取消按钮 true显示 false不显示
  Xiaogug.Dialog.Close 关闭对话框
  
  Xiaogug.PopupBox 弹出窗口，像MSN/QQ的弹出消息窗口
    Xiaogug.PopupBox.Height 设置弹出窗口的高度，默认为像互素，值不需要加单位，用数值赋值
    Xiaogug.PopupBox.Width 设置弹出窗口的宽度，默认为像互素，值不需要加单位，用数值赋值
    Xiaogug.PopupBox.Left 设置弹出窗口的距离主窗体的左边距，默认为像互素，值不需要加单位，用数值赋值
    Xiaogug.PopupBox.Right 设置弹出窗口的距离主窗体的右边距，默认为像互素，值不需要加单位，用数值赋值
    Xiaogug.PopupBox.Top 设置弹出窗口的距离主窗体的上边距，默认为像互素，值不需要加单位，用数值赋值
    Xiaogug.PopupBox.Bottom 设置弹出窗口的距离主窗体的下边距，默认为像互素，值不需要加单位，用数值赋值
    Xiaogug.PopupBox.PopupURL 设置弹出窗口包含的内容页地址
    Xiaogug.PopupBox.Direction 设置弹出窗口的弹出方式（方向），赋值可用数值或字符
      "topleft2right":0 窗口在左上角，从左往右弹出
      "topleftdown":1 窗口在左上角，从上往下弹出
      "topright2left":2 窗口在右上角，从右往左弹出
      "toprightdown":3 窗口在右上角，从上往下弹出
      "bottomleft2right":4 窗口在左下角，从左往右弹出
      "bottomleftup":5 窗口在左下角，从下往上弹出
      "bottomright2left":6 窗口在右下角，从右往左弹出
      "bottomrightup":7 窗口在右下角，从下往上弹出
    Xiaogug.PopupBox.Popup() 弹出窗口

  Xiaogug.SetFocus(control) 设置使控件获得焦点
    control 控件名称/控件对象
*/
              

var Xiaogug = {
    SetFocus: function(element){
        element = $(element);
        if(element == null){return;}
        element.focus();
    },
    Void: function(){},
    ApplicationUrl: "http://www.kids-growth.com//"
}

Xiaogug.Dialog = {
    init: function(){
        this.RenderBackground();
        this.RenderLoading();
        this.RenderMessage();
        this.initControls();
        this.setBackground();
        this.AdjustPosition();
    },
    
    RenderBackground: function(){
        document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\""+Xiaogug.ApplicationUrl+"CSS/XiaogugDialog.css\" />");
        document.write("<div id=\"Xgg_DialogBackgroundDiv\" style=\"z-index:9990;visibility:visible;position:absolute;alpha(opacity=20);-moz-opacity:0.2;background-color:#000000;\">");
        document.write("<iframe id=\"Xgg_DialogBackgroundIframe\" name=\"Xgg_DialogBackgroundIframe\" height=\"0px\" width=\"0px\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
        document.write("</div>");
    },
    
    RenderLoading: function(){
        document.write("<div id=\"Xgg_LoadingDialogDiv\" style=\"z-index:9991;position:absolute;visibility:visible;\" onselectstart=\"return false\">");
        
        document.write("<div class=\"XGGDialog_Main\">");
        document.write("<div class=\"Header\">");
        document.write("<div class=\"LeftHand\"></div><div class=\"RightHand\"></div>");
        document.write("<a href=\"javascript:Xiaogug.Void();\" onclick=\"Xiaogug.Dialog.Close();\" title=\"关闭\"></a>");
        document.write("</div>");
        document.write("<div class=\"Footer\">");
        document.write("<div class=\"LeftHand\"></div><div class=\"RightHand\"></div>");
        document.write("<div class=\"LoadingImg\"></div><span>页面加载中，请稍候......</span>");
        document.write("</div>");
        document.write("</div>");
        
        document.write("</div>");
        
    },
    
    RenderMessage: function(){
        document.write("<div id=\"Xgg_MessageDialogDiv\" style=\"z-index:9992;position:absolute;visibility:hidden;\" onselectstart=\"return false\">");

        document.write("<div class=\"XGGDialog_Main\">");
        document.write("<div class=\"Header\">");
        document.write("<div class=\"LeftHand\"></div><div class=\"RightHand\"></div>");
        document.write("<b></b><span id=\"Xgg_DialogTitle\">&nbsp;</span>");
        document.write("<a href=\"javascript:Xiaogug.Void();\" onclick=\"Xiaogug.Dialog.Close();\" title=\"关闭\"></a>");
        document.write("</div>");
        document.write("<div class=\"Item\">");
        document.write("<div class=\"LeftHand\"></div><div class=\"RightHand\"></div>");
        document.write("<b></b><span id=\"Xgg_DialogText\">&nbsp;</span>");
        document.write("</div>");
        document.write("<div class=\"Footer\">");
        document.write("<div class=\"LeftHand\"></div><div class=\"RightHand\"></div>");
        document.write("<input id=\"XggDialog_btnCancel\" type=\"button\" value=\"取 消\" onclick=\"Xiaogug.Dialog.Close();\" style=\"display:none;\" />");
        document.write("<input id=\"XggDialog_btnOK\" type=\"button\" value=\"确 定\" />");
        document.write("</div>");
        document.write("</div>");
        
        document.write("</div>");
    },
    
    ShowMessage: function(msgTitle,msgText,handler,isShowCancel){
        if(msgTitle == ""){msgTitle = "系统消息";}
        $("Xgg_DialogTitle").innerHTML = msgTitle;
        $("Xgg_DialogText").innerHTML = msgText;

        if(isShowCancel){
            this.Cancel_Button.style.display = "inline";
        }
        else{
            this.Cancel_Button.style.display = "none";
        }
        this.BackgroundContainer.style.visibility = "visible";
        this.BackgroundFrame.style.visibility = "visible";
        this.MessageDialog.style.visibility = "visible";        
        this.AdjustPosition(this.MessageDialog);
        this.AppendEvents(handler);
        this.setBackground();
    },
    
    AppendEvents: function(handler){
        if(handler == null){
            handler = "Xiaogug.Void()";
        }
        handler += ";Xiaogug.Dialog.Close();";
        //附加事件
        
        this.OK_Click = Function(handler);
        Event.observe("XggDialog_btnOK", "click", this.OK_Click);
        this.Ok_Button.focus();
    },
    
    initControls: function(){
        this.BackgroundContainer = $("Xgg_DialogBackgroundDiv");
        this.BackgroundFrame = $("Xgg_DialogBackgroundIframe");
        this.LoadingDialog = $("Xgg_LoadingDialogDiv");
        this.MessageDialog = $("Xgg_MessageDialogDiv");
        this.Ok_Button = $("XggDialog_btnOK");
        this.Cancel_Button = $("XggDialog_btnCancel");
    },
    
    setBackground: function(){
        this.BackgroundContainer.style.display = "block";
        this.BackgroundContainer.style.filter = "alpha(opacity=40)";
        this.BackgroundContainer.style.left = "0px";
        this.BackgroundContainer.style.top = "0px";
        this.BackgroundContainer.style.margin = "0px";
        //this.BackgroundContainer.style.width = (document.body.clientWidth||window.innerWidth)+"px";
        //this.BackgroundContainer.style.height = Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight)+"px";
        
        this.BackgroundFrame.style.width = (document.body.clientWidth||window.innerWidth)+"px";
        this.BackgroundFrame.style.height = Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight)+"px";
    },
    
    AdjustPosition: function(dialogContainer){
        if(dialogContainer == null){dialogContainer = this.LoadingDialog;}
        
        var clientWidth = (document.body.clientWidth||window.innerWidth);    
        if(clientWidth >380){
            dialogContainer.style.left = ((clientWidth - 380)/2) + "px";
        }
        else{
            dialogContainer.style.left = "0px";
        }
        var clientHeight = (Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight)-4);
        var clientScrollTop;
        if (window.innerHeight){
          clientScrollTop = window.pageYOffset
        }
        else if(document.documentElement && document.documentElement.scrollTop){
          clientScrollTop = document.documentElement.scrollTop
        }
        else if (document.body){
          clientScrollTop = document.body.scrollTop;
        }
        if ((clientHeight-clientScrollTop)>300){
            dialogContainer.style.top = (clientScrollTop+100)+"px";
        }
        else{
            dialogContainer.style.top = (clientHeight - clientScrollTop - 174)/2 + "px";
        }
    },
    
    fGoto: function(){},
    
    Close: function(){
        if(this.BackgroundContainer == null){return;}
        
        this.BackgroundContainer.style.visibility = "hidden";
        this.LoadingDialog.style.visibility = "hidden";
        this.BackgroundFrame.style.visibility = "hidden";
        this.MessageDialog.style.visibility = "hidden";
        
         Event.stopObserving("XggDialog_btnOK", "click");
         this.OK_Click = null;
    },
    
    BackgroundContainer: null,
    BackgroundFrame: null,
    LoadingDialog: null,
    MessageDialog: null,
    
    OK_Click: null,
    Ok_Button: null,
    Cancel_Button: null
}

Xiaogug.Dialog.FinishLoading = function(){
    Xiaogug.Dialog.Close();
}

Xiaogug.Dialog.init();
if(Prototype.Browser.IE || Prototype.Browser.Opera){window.attachEvent("onload",Xiaogug.Dialog.FinishLoading);}
else{window.addEventListener("load",Xiaogug.Dialog.FinishLoading,false);}