﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="BadBug.debug.js" />

Type.registerNamespace( "BadBug.UI.Controls" );

$getStyleValue = function( element, name ){
    /// <summary>
    /// 获取指定控件指定运行时样式
    /// </summary>
    /// <param name="element" type="Object">要获取样式的控件</param>
    /// <param name="name" type="String">样式名称</param>
    /// <returns type="String"></returns>
    var v = element[ name ];
    if( v ) return v;
    if( element.currentStyle )
    {
        return element.currentStyle[ name ];
    }
    else if( document.defaultView && document.defaultView.getComputedStyle )
    {   
        var _s = document.defaultView.getComputedStyle( element, null );
        return _s && _s[ name ];
    }
    return null;
};

$getRect = function( element ){
    /// <summary>
    /// 获取指定控件的客户区信息
    /// </summary>
    /// <returns type="Sys.UI.Bounds"></returns>
    if( element == document.body || element == document )
        element = document.documentElement;
    return new Sys.UI.Bounds( element.offsetLeft, element.offsetTop, element.offsetWidth, element.offsetHeight );
};

$getRealRect = function( element, rect ){
    /// <summary>
    /// 获取指定控件的实际客户区信息
    /// </summary>
    /// <param name="element" type="Object">要设置的控件</param>
    /// <param name="rect" type="Sys.UI.Bounds">要参与计算的已有区域(可选)</param>
    /// <returns type="Sys.UI.Bounds"></returns>
    var _getInt = function( element, name ){
        var v =  $getStyleValue( element, name );
        if( v )
        {
            if( v == "auto" ) return 0;
            var _t = parseInt( v.replace( "px", "" ) );
            if( isNaN( _t ) ) return 0;
            return _t;
        }
        return 0;            
    };
    var rc = null;
    if( !rect || Object.getTypeName( rect ) != "Sys.UI.Bounds" ) rc = $getRect( element );
    else rc = new Sys.UI.Bounds( rect.x, rect.y, rect.width, rect.height );
    var v = _getInt( element, "marginTop" );
    if( v != 0 ) rc.y -= v;
    v = _getInt( element, "marginLeft" );
    if( v != 0 ) rc.x -= v;
    v = _getInt( element, "paddingTop" );
    if( v != 0 ) rc.height -= v;
    v = _getInt( element, "paddingRight" );
    if( v != 0 ) rc.width -= v;
    v = _getInt( element, "paddingBottom" );
    if( v != 0 ) rc.height -= v;
    v = _getInt( element, "paddingLeft" );
    if( v != 0 ) rc.width -= v;
    v = _getInt( element, "borderTopWidth" );
    if( v != 0 ) rc.height -= v;
    v = _getInt( element, "borderRightWidth" );
    if( v != 0 ) rc.width -= v;
    v = _getInt( element, "borderBottomWidth" );
    if( v != 0 ) rc.height -= v;
    v = _getInt( element, "borderLeftWidth" );
    if( v != 0 ) rc.width -= v;
    return rc;        
};

$getWindowVisbleRect = function(){
    /// <summary>
    /// 获取窗口可视区域
    /// </summary>
    /// <returns type="Sys.UI.Bounds"></returns>
    if( window.innerWidth )
    {
        var windowVRc = new Sys.UI.Bounds(
            document.documentElement.scrollLeft, 
            document.documentElement.scrollTop,
            window.innerWidth,
            window.innerHeight
        );
        if( document.documentElement.scrollWidth > document.documentElement.clientWidth )
        {
            windowVRc.height -= 18;
        }
        if( document.documentElement.scrollHeight > document.documentElement.clientHeight )
        {
            windowVRc.width -= 18;
        }
                 
        return $getRealRect( document.documentElement, windowVRc );
    }
    else
    {
        var windowVRc = new Sys.UI.Bounds(
            document.documentElement.scrollLeft, 
            document.documentElement.scrollTop,
            document.documentElement.clientWidth,
            document.documentElement.clientHeight
        );
        if( document.documentElement.scrollWidth > document.documentElement.clientWidth )
        {
            windowVRc.height -= 18;
        }
        if( document.documentElement.scrollHeight > document.documentElement.clientHeight )
        {
            windowVRc.width -= 18;
        }
        return $getRealRect( document.documentElement, windowVRc );
    }
};

$setRect = function(element, rect) {
    /// <summary>
    /// 指定指定控件的显示区域
    /// </summary>
    /// <param name="element" type="Object">要设置的控件</param>
    /// <param name="rect" type="Sys.UI.Bounds">显示区域</param>
    Sys.UI.DomElement.setLocation(element, rect.x, rect.y);
    element.style.width = rect.width.toString() + "px";
    element.style.height = rect.height.toString() + "px";
};

$setPos = function( element, x, y ){
    /// <summary>
    /// 指定指定控件的显示位置
    /// </summary>
    /// <param name="x" type="Number">x坐标(像素)</param>
    /// <param name="y" type="Number">y坐标(像素)</param>
    Sys.UI.DomElement.setLocation( element, x, y );
};

MessageBoxType = function() {
    /// <summary>
    /// 消息窗口类型
    /// </summary>
    /// <field name="abortRetryIgnore" type="Number" integer="true" static="true"></field>
    /// <field name="noButton" type="Number" integer="true" static="true"></field>
    /// <field name="ok" type="Number" integer="true" static="true"></field>
    /// <field name="okCancel" type="Number" integer="true" static="true"></field>
    /// <field name="retryCancel" type="Number" integer="true" static="true"></field>
    /// <field name="yesNo" type="Number" integer="true" static="true"></field>
    /// <field name="yesNoCancel" type="Number" integer="true" static="true"></field>
    if( arguments.length !== 0 ) throw Error.parameterCount();
    throw Error.notImplemented();
};

MessageBoxType.prototype = {
    abortRetryIgnore: 0,
    noButton: 1,
    ok: 2,
    okCancel: 3,
    retryCancel: 4,
    yesNo: 5,
    yesNoCancel: 6
};

MessageBoxType.registerEnum( "MessageBoxType" );

MessageBoxIcon = function() {
    /// <summary>
    /// 图标类型
    /// </summary>
    /// <field name="none" type="Number" integer="true" static="true"></field>
    /// <field name="warning" type="Number" integer="true" static="true"></field>
    /// <field name="information" type="Number" integer="true" static="true"></field>
    /// <field name="question" type="Number" integer="true" static="true"></field>
    /// <field name="correct" type="Number" integer="true" static="true"></field>
    /// <field name="error" type="Number" integer="true" static="true"></field>
    /// <field name="lock" type="Number" integer="true" static="true"></field>
    /// <field name="male" type="Number" integer="true" static="true"></field>
    /// <field name="female" type="Number" integer="true" static="true"></field>
    /// <field name="together" type="Number" integer="true" static="true"></field>
    /// <field name="good" type="Number" integer="true" static="true"></field>
    /// <field name="bad" type="Number" integer="true" static="true"></field>
    /// <field name="normalMessage" type="Number" integer="true" static="true"></field>
    /// <field name="importantMessage" type="Number" integer="true" static="true"></field>
    /// <field name="forbidden" type="Number" integer="true" static="true"></field>
    /// <field name="search" type="Number" integer="true" static="true"></field>
    if( arguments.length !== 0 ) throw Error.parameterCount();
    throw Error.notImplemented();
}

MessageBoxIcon.prototype = {
    none: 0,
    warning: 1,
    information: 2,
    question: 3,
    correct: 4,
    error: 5,
    lock: 6,
    male: 7,
    female: 8,
    together: 9,
    good: 10,
    bad: 11,
    normalMessage: 12,
    importantMessage: 13,
    forbidden: 14,
    search: 15
};

MessageBoxIcon.registerEnum( "MessageBoxIcon" );

BadBug.UI.Controls._iconNames = [
    "none",
    "warning",
    "information",
    "question",
    "correct",
    "error",
    "lock",
    "male",
    "female",
    "together",
    "good",
    "bad",
    "normalMessage",
    "importantMessage",
    "forbidden",
    "search"
];

WindowState = function(){
    /// <field name="hidden" type="Number" integer="true" static="true"></field>
    /// <field name="normal" type="Number" integer="true" static="true"></field>
    /// <field name="minimize" type="Number" integer="true" static="true"></field>
    /// <field name="maximize" type="Number" integer="true" static="true"></field>
    if (arguments.length !== 0) throw Error.parameterCount();
    throw Error.notImplemented();
};
WindowState.prototype = {
    hidden: 0,
    minimize: 1,
    normal: 2,
    maximize: 3
};

WindowState.registerEnum("WindowState");

ClientType = function(){
    /// <field name="elementRef" type="Number" integer="true" static="true"></field>
    /// <field name="elementCopy" type="Number" integer="true" static="true"></field>
    /// <field name="url" type="Number" integer="true" static="true"></field>
    /// <field name="html" type="Number" integer="true" static="true"></field>
};

ClientType.prototype = {
    elementRef: 0,
    elementCopy: 1,
    url: 2,
    html: 3
};

ClientType.registerEnum("ClientType");


BadBug.UI.Controls.WindowEventArgs = function(){
    this._cancel = false;
    this._eventRef = null;
};

BadBug.UI.Controls.WindowEventArgs.prototype = {
    get_cancel: function(){
        /// <returns type="Boolean"></returns>
        return this._cancel;
    },
    set_cancel: function( value ){
        /// <param name="value" type="Boolean"></param>
        this._cancel = value;
    },
    get_eventRef: function(){
        /// <returns type="Sys.UI.DomEvent"></returns>
        return this._eventRef;
    }
};

//BadBug.UI.Controls.WindowEventArgs.registerClass( "BadBug.UI.Controls.WindowEventArgs", Sys.EventArgs );

BadBug.UI.Controls.Window = function(){
    /// <summary>
    /// 模拟窗口
    /// </summary>
    BadBug.UI.Controls.Window.initializeBase( this );
    
    this._windowRef = null;
    this._clientContainerRef = null;
    this._titleBarRef = null;
    this._captionRef = null;
    this._miniBtnRef = null;
    this._maxiBtnRef = null;
    this._closeBtnRef = null;
    this._shadowRef = null;
    this._movingWindowRef = null;
    this._modalBackgroundRef = null;
    this._baffleRef = null;
    
    
       
    //事件委托
    this._ncEventDelegate = null;
    this._clientEventDelegate = null;
    this._ncMoveWaiterDelege = null;
    this._animationTimerDelege = null;
    
    //可设置属性
    this._startRect = new Sys.UI.Bounds( 200, 100, 300, 150 );
    this._parentRef = null;
    this._client = null; 
    this._clientType = ClientType.elementRef;
    this._shadowEnabled = true;
    this._shadowOffsetX = 3;
    this._shadowOffsetY = 3;
    this._titleBarVisible = true;
    this._miniButtonVisible = false;
    this._maxiButtonVisible = false;
    this._closeButtonVisible = true;
    this._resizeEnabled = true;
    this._isMessageWindow = false;
    this._messageWindowType = MessageBoxType.ok;
    this._messageWindowDefaultButtonIndex = 0;
    this._messageWindowCallBack = null;
    this._animationEnabled = false;
    this._caption = "标题栏";
    this._userValue = null;
     
    this._oldClientParentRef = null;
    this._id = "";
    this._normalRect = null;
    this._isMoving = false;
    this._isActive = false;
    this._isAnimation = false;
    this._animationStartRect = null;
    this._animationEndRect = null;
    this._animationTimer = 0;
    this._animationStepCounter = 0;
    this._animationXPrecision = 0;
    this._animationYPrecision = 0;
    this._animationWidthPrecision = 0;
    this._animationHeightPrecision = 0;
    this._moverWaitingTimer = 0;
    this._beginWaitEvenArg = null;
    this._windowState = WindowState.hidden;
    this._movingBeginX = 0;
    this._movingBeginY = 0;   
    this._resizeState = 0; 
    this._resizeDirection = "";
    this._globalSelectDisabled = false;
    this._eventHandlerList = new Sys.EventHandlerList();
};

BadBug.UI.Controls.Window.prototype = {
    initialize: function() {
        BadBug.UI.Controls.Window.callBaseMethod(this, "initialize");

        //if( !this._parentRef ) this._parentRef = document.body;
        this._parentRef = document.body;

        var tempClientValue = null;

        if (!this._loadFromCache()) {
            this._createNew();
        }
        if (this._clientType == ClientType.elementRef) {
            this._oldClientParentRef = this._client.parentNode;
            if (this._oldClientParentRef) this._oldClientParentRef.removeChild(this._client);
            Sys.UI.DomElement.removeCssClass(this._client, "ext-template");
        } else if (this._clientType == ClientType.elementCopy) {
            this._client = this._client.cloneNode(true);
            Sys.UI.DomElement.removeCssClass(this._client, "ext-template");
        } else if (this._clientType == ClientType.url) {
            var iframe = document.createElement("iframe");
            iframe.frameBorder = 0;
            iframe.name = this._id + ".ClientFrame_" + (BadBug.UI.Controls.Window.__WindowCounter++).toString();
            iframe.style.width = "100%";
            iframe.scrolling = "auto";
            //iframe.src = this._client;
            tempClientValue = this._client;
            this._client = iframe;
        } else if (this._clientType == ClientType.html) {
            this._clientContainerRef.innerHTML = this._client;
            this._client = null;
        }

        this._clientContainerRef.style.height = "0px";
        if (this._client) {
            this._clientContainerRef.appendChild(this._client);
        }

        this._windowRef.style.display = "none";
        this._windowRef.style.position = "absolute";
        this._windowState = WindowState.hidden;
        this._modalBackgroundRef.style.display = "none";

        this._ncEventDelegate = Function.createDelegate(this, this.__ncEventHandler);
        this._clientEventDelegate = Function.createDelegate(this, this.__clientEventHandler);
        this._ncMoveWaiterDelege = Function.createDelegate(this, this.__ncMoveWaiterHandler);
        this._animationTimerDelege = Function.createDelegate(this, this._animationTimerHandler);

        var container = this._getContainer();
        container.appendChild(this._windowRef);
        container.appendChild(this._shadowRef);
        container.appendChild(this._movingWindowRef);
        container.appendChild(this._modalBackgroundRef);
        container.appendChild(this._baffleRef);

        this._miniBtnRef.className = "MiniBtn";
        this._maxiBtnRef.className = "MaxiBtn";
        this._closeBtnRef.className = "CloseBtn";

        //var _wvr = $getWindowVisbleRect();
        //this.moveTo(Math.ceil((_wvr.width - this._startRect.width) / 2), Math.ceil((_wvr.height - this._startRect.height) / 2));


        $addHandler(this._titleBarRef, "mousedown", this._ncEventDelegate);
        $addHandler(this._titleBarRef, "mouseup", this._ncEventDelegate);

        $addHandler(this._titleBarRef, "mouseover", this._ncEventDelegate);
        $addHandler(this._titleBarRef, "mouseout", this._ncEventDelegate);
        $addHandler(this._titleBarRef, "click", this._ncEventDelegate);
        $addHandler(this._titleBarRef, "dblclick", this._ncEventDelegate);
        $addHandler(this._titleBarRef, "mousemove", this._ncEventDelegate);

        $addHandler(this._movingWindowRef, "mousemove", this._ncEventDelegate);
        $addHandler(this._movingWindowRef, "mouseout", this._ncEventDelegate);
        $addHandler(this._movingWindowRef, "mouseup", this._ncEventDelegate);

        $addHandler(this._windowRef, "mousemove", this._ncEventDelegate);
        $addHandler(this._windowRef, "mousedown", this._ncEventDelegate);

        if (this._isMessageWindow) {
            $addHandler(this._windowRef, "click", this._ncEventDelegate);
        }

        if (this._clientType == ClientType.url && tempClientValue) {
            this._client.src = tempClientValue;
        }

        this._drawTitleBar();
    },
    dispose: function() {
        if (this._windowRef) this.close();
        BadBug.UI.Controls.Window.callBaseMethod(this, "dispose");
    },
    get_id: function() {
        /// <summary>
        /// 获取窗口的id
        /// </summary>
        /// <returns type="String"></returns>
        return this._id;
    },
    get_startRect: function() {
        /// <summary>
        /// 获取窗口创建时的显示区域
        /// </summary>
        /// <returns type="Sys.UI.Bounds"></returns>
        return this._startRect;
    },
    set_startRect: function(value) {
        /// <summary>
        /// 设置窗口创建时的显示区域(仅用于初始化)
        /// </summary>
        /// <param name="value" type="Sys.UI.Bounds"></param>
        if (this.get_isInitialized()) throw Error.invalidOperation();
        this._startRect = value;
    },
    get_client: function() {
        /// <summary>
        /// 获取窗口用户区元素引用
        /// </summary>
        /// <returns type="Object"></returns>
        return this._client;
    },
    set_client: function(value) {
        /// <summary>
        /// 设置窗口用户区元素模板引用或者路径(仅用于初始化)
        /// </summary>
        /// <param name="value"></param>
        if (this.get_isInitialized()) throw Error.invalidOperation();
        this._client = value;
    },
    get_clientType: function() {
        /// <summary>
        /// 获取窗口用户区元素引用
        /// </summary>
        /// <returns type="ClientType"></returns>
        return this._clientType;
    },
    set_clientType: function(value) {
        /// <summary>
        /// 设置窗口用户区元素模板(仅用于初始化)
        /// </summary>
        /// <param name="value" type="ClientType"></param>
        this._clientType = value;
    },
    get_parent: function() {
        /// <summary>
        /// 获取窗口容器
        /// </summary>
        /// <returns type="Object"></returns>
        return this._parentRef;
    },
    set_parent: function(value) {
        /// <summary>
        /// 设置窗口容器(仅用于初始化)
        /// </summary>
        /// <param name="value" type="Object"></param>
        throw Error.notImplemented("will be suppoted later.");
        //if( this.get_isInitialized() ) throw Error.invalidOperation();
        //this._parentRef = value;
    },
    get_shadowEnabled: function() {
        /// <summary>
        /// 获取窗口是否显示阴影
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._shadowEnabled;
    },
    set_shadowEnabled: function(value) {
        /// <summary>
        /// 设置窗口是否显示阴影(仅用于初始化)
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        this._shadowEnabled = Boolean.parse(value.toString());
    },
    get_shadowOffsetX: function() {
        /// <summary>
        /// 获取窗口阴影的水平偏移
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._shadowOffsetX;
    },
    set_shadowOffsetX: function(value) {
        /// <summary>
        /// 设置窗口阴影的水平偏移(仅用于初始化)
        /// </summary>
        /// <param name="value" type="Number"></param>
        this._shadowOffsetX = parseInt(value);
    },
    get_shadowOffsetY: function() {
        /// <summary>
        /// 获取窗口阴影的竖直偏移
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._shadowOffsetY;
    },
    set_shadowOffsetY: function(value) {
        /// <summary>
        /// 设置窗口阴影的竖直偏移(仅用于初始化)
        /// </summary>
        /// <param name="value" type="Number"></param>
        this._shadowOffsetY = parseInt(value);
    },
    get_titleBarVisible: function() {
        /// <summary>
        /// 获取标题栏可视状态
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._titleBarVisible;
    },
    set_titleBarVisible: function(value) {
        /// <summary>
        /// 设置标题栏可视状态
        /// </summary>
        /// <param name="value" type="Boolean"></param>    
        this._titleBarVisible = Boolean.parse(value.toString());
        if (this.get_isInitialized()) this._drawTitleBar();
    },
    get_miniButtonVisible: function() {
        /// <summary>
        /// 获取标题栏是否显示最小化按钮
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._miniButtonVisible;
    },
    set_miniButtonVisible: function(value) {
        /// <summary>
        /// 设置标题栏是否显示最小化按钮
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        this._miniButtonVisible = Boolean.parse(value.toString());
        if (this.get_isInitialized()) this._drawTitleBar();
    },
    get_maxiButtonVisible: function() {
        /// <summary>
        /// 获取标题栏是否显示最大化按钮
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._maxiButtonVisible;
    },
    set_maxiButtonVisible: function(value) {
        /// <summary>
        /// 设置标题栏是否显示最大化按钮
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        this._maxiButtonVisible = Boolean.parse(value.toString());
        if (this.get_isInitialized()) this._drawTitleBar();
    },
    get_closeButtonVisible: function() {
        /// <summary>
        /// 获取标题栏是否显示关闭按钮
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._closeButtonVisible;
    },
    set_closeButtonVisible: function(value) {
        /// <summary>
        /// 设置标题栏是否显示关闭按钮
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        this._closeButtonVisible = Boolean.parse(value.toString());
        if (this.get_isInitialized()) this._drawTitleBar();
    },
    get_resizeEnabled: function() {
        /// <summary>
        /// 获取窗口是否允许调整大小
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._resizeEnabled;
    },
    set_resizeEnabled: function(value) {
        /// <summary>
        /// 设置窗口是否允许调整大小
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        this._resizeEnabled = Boolean.parse(value.toString());
    },
    get_isMessageWindow: function() {
        /// <summary>
        /// 获取窗口是否消息窗口
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._isMessageWindow;
    },
    set_isMessageWindow: function(value) {
        /// <summary>
        /// 设置窗口是否消息窗口(仅用于初始化)
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        if (this.get_isInitialized()) throw Error.invalidOperation();
        this._isMessageWindow = Boolean.parse(value.toString());
    },
    get_messageWindowType: function() {
        /// <summary>
        /// 获取消息窗口类型
        /// </summary>
        /// <returns type="MessageBoxType"></returns>
        return this._isMessageWindow;
    },
    set_messageWindowType: function(value) {
        /// <summary>
        /// 设置消息窗口类型(仅用于初始化)
        /// </summary>
        /// <param name="value" type="MessageBoxType"></param>
        if (this.get_isInitialized()) throw Error.invalidOperation();
        this._messageWindowType = value;
    },
    get_messageWindowCallBack: function() {
        /// <summary>
        /// 获取消息窗口的用户回调函数
        /// </summary>
        return this._messageWindowCallBack;
    },
    set_messageWindowCallBack: function(value) {
        /// <summary>
        /// 设置消息窗口的用户回调函数
        /// </summary>
        if (value != null && typeof (value) != "function") throw Error.argumentType("value", value, Function);
        this._messageWindowCallBack = value;
    },
    get_messageWindowDefaultButtonIndex: function() {
        /// <summary>
        /// 获取消息窗口默认按钮索引
        /// </summary>
        /// <returns type="Number"></returns>
        return this._isMessageWindow;
    },
    set_messageWindowDefaultButtonIndex: function(value) {
        /// <summary>
        /// 设置消息窗口默认按钮索引
        /// </summary>
        /// <param name="value" type="Number"></param>
        if (typeof value != "number" || value < 0) value = 0;
        if (this.get_isInitialized()) {
            //if( !this._isMessageWindow ) throw Error.invalidOperation();
            var btn = this._client.getElementsByTagName("input")[value];
            if (btn != null) btn.focus();
        }
        this._messageWindowDefaultButtonIndex = value;

    },
    get_messageWindowText: function() {
        /// <summary>
        /// 获取消息窗口显示的文本
        /// </summary>
        /// <returns type="String"></returns>
        if (!this._isMessageWindow) throw Error.invalidOperation();
        var txtSpan = $get("__messageBoxTextSpan", this._client);
        if (txtSpan) return txtSpan.innerHTML;
        return "";
    },
    set_messageWindowText: function(value) {
        /// <summary>
        /// 设置消息窗口显示的文本
        /// </summary>
        /// <param name="value" type="String">要显示在消息窗口的新文本</param>
        if (!this._isMessageWindow) throw Error.invalidOperation();
        var txtSpan = $get("__messageBoxTextSpan", this._client);
        if (txtSpan) txtSpan.innerHTML = value;
    },
    get_animationEnabled: function() {
        /// <summary>
        /// 获取窗口是否启用动画显示
        /// </summary>
        /// <returns type="Boolean"></returns>
        return this._animationEnabled;
    },
    set_animationEnabled: function(vlaue) {
        /// <summary>
        /// 设置窗口是否启用动画显示
        /// </summary>
        /// <param name="value" type="Boolean"></param>
        this._animationEnabled = vlaue == true;
    },
    get_caption: function() {
        /// <summary>
        /// 获取窗口标题文本
        /// </summary>
        /// <returns type="Number"></returns>
        return this._caption;
    },
    set_caption: function(value) {
        /// <summary>
        /// 设置窗口标题文本
        /// </summary>
        /// <param name="value" type="String"></param>
        this._caption = value;
        if (this.get_isInitialized()) this._captionRef.innerHTML = value;
    },
    get_userValue: function() {
        /// <summary>
        /// 获取用户保存在窗口中的数据对象
        /// </summary>
        /// <returns type="Object"></returns>    
        return this._userValue;
    },
    set_userValue: function(value) {
        /// <summary>
        /// 设置窗口标题文本
        /// </summary>
        /// <param name="value" type="String"></param>
        this._userValue = value;
    },
    get_windowState: function() {
        /// <summary>
        /// 获取窗口的显示状态
        /// </summary>
        /// <returns type="WindowState"></returns>
        return this._windowState;
    },
    add_show: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.addHandler("show", handler);
    },
    remove_show: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.removeHandler("show", handler);
    },
    add_resize: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.addHandler("resize", handler);
    },
    remove_resize: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.removeHandler("resize", handler);
    },
    add_minimize: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.addHandler("minimize", handler);
    },
    remove_minimize: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.removeHandler("minimize", handler);
    },
    add_maximize: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.addHandler("maximize", handler);
    },
    remove_maximize: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.removeHandler("maximize", handler);
    },
    add_close: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.addHandler("close", handler);
    },
    remove_close: function(handler) {
        /// <param name="handler" type="Function"></param>
        this._eventHandlerList.removeHandler("close", handler);
    },
    elableMessageWindowButton: function(index, enabled) {
        /// <summary>
        /// 启用/禁用消息窗口的指定索引的按钮
        /// </summary>
        /// <param name="index" type="Number">按钮索引</param>
        /// <param name="enabled" type="Boolean">启用状态</param>
        if (!this._isMessageWindow) throw Error.invalidOperation();
        var btn = this._client.getElementsByName("cmdbtn")[index];
        if (btn == null) throw new Error.argumentOutOfRange("index", index);
        btn.disabled = !enabled;
    },
    show: function(state) {
        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="state" type="WindowState"></param>
        if (typeof (state) != "number" || state < WindowState.hidden || state > WindowState.maximize) state = WindowState.normal;
        this._windowState = state;
        this._baffleRef.style.display = "block";
        this._shadowRef.style.display = this._shadowEnabled ? "block" : "none";
        this._windowRef.style.display = "inline";

        if (this._startRect) {
            this.setRect(this._startRect);
            this._startRect = null;
        }

        this._windowState = state;
        switch (state) {
            case WindowState.hidden:
                this.hide();
                break;
            case WindowState.normal:
                break;
            case WindowState.minimize:
                this.minimize();
                break;
            case WindowState.maximize:
                this.maximize();
                break;
        }
        this.focus();
        if (this._isMessageWindow) {
            this.autoSize();
            this.moveToCenter();
            this.set_messageWindowDefaultButtonIndex(this._messageWindowDefaultButtonIndex);
        }
        var handler = this._eventHandlerList.getHandler("show");
        if (handler) handler(this, Sys.EventArgs.Empty);
        var pt = Sys.UI.DomElement.getLocation(this._windowRef);
    },
    showModal: function() {
        /// <summary>
        /// 以模态方式显示窗口
        /// </summary>
        var rect = $getWindowVisbleRect();
        $setRect(this._modalBackgroundRef, rect);
        this._modalBackgroundRef.style.display = "block";
        this.show();
    },
    minimize: function() {
        /// <summary>
        /// 最小化窗口
        /// </summary>
        if (this._windowState > WindowState.minimize) {
            if (this._windowState == WindowState.normal) this._normalRect = $getRect(this._windowRef);
            this._windowState = WindowState.minimize;
            var newRect = $getRealRect(this._windowRef);
            var miniWindowContainer = this._getMinimizeContainer();
            var miniContainerRect = $getRealRect(miniWindowContainer);
            newRect.width = 170;
            newRect.height = this._titleBarRef.offsetHeight;
            newRect.x = 0;
            newRect.y = miniContainerRect.y;
            var lastChild = miniWindowContainer.lastChild;
            if (lastChild != null) {
                var rcLastChild = $getRect(lastChild);
                newRect.x = rcLastChild.x + rcLastChild.width;
                newRect.y += rcLastChild.y;
            }
            this.disableShadow();
            this._drawTitleBar();
            this._clientContainerRef.style.display = "none";
            this.setRect(newRect);
            if (!this._animationEnabled) {
                this._exchangeContainer();
                this.blur();
            }
            var handler = this._eventHandlerList.getHandler("minimize");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    maximize: function() {
        /// <summary>
        /// 最大化窗口
        /// </summary>
        //if( this._windowState < WindowState.maximize )
        {
            if (this._windowState == WindowState.normal) this._normalRect = $getRect(this._windowRef);
            this._windowState = WindowState.maximize;
            this.disableShadow();
            var newRect = $getWindowVisbleRect(); //$getRect( this._parentRef );
            newRect = $getRealRect(this._windowRef, newRect);
            newRect.x = 0;
            newRect.y = 0;
            newRect.width -= 3;
            newRect.height -= 3;
            if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version == 6) {
                newRect.width -= 2;
                newRect.height -= 2;
            }
            this._drawTitleBar();
            this._exchangeContainer();
            this._clientContainerRef.style.display = "block";
            this.setRect(newRect);
            var handler = this._eventHandlerList.getHandler("maximize");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    restore: function() {
        /// <summary>
        /// 还原窗口为常规大小
        /// </summary>
        if (this._windowState == WindowState.minimize || this._windowState == WindowState.maximize) {
            this._windowState = WindowState.normal;
            var newRect = this._normalRect;
            this.enableShadow();
            this._drawTitleBar();
            this._exchangeContainer();
            this.setRect(newRect);
            this._clientContainerRef.style.display = "block";
            var handler = this._eventHandlerList.getHandler("restore");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    _exchangeContainer: function() {
        /// <summary>
        /// 交换窗口容器
        /// </summary>
        if (this._windowState == WindowState.minimize) {
            var container = this._getContainer();
            if ($get(this._id, container)) {
                var miniContainer = this._getMinimizeContainer();
                container.removeChild(this._baffleRef);
                miniContainer.appendChild(this._baffleRef);
                container.removeChild(this._windowRef);
                this._windowRef.style.display = "inline";
                this.moveTo(0, 0);
                this._windowRef.style.position = "relative";
                miniContainer.appendChild(this._windowRef);
                BadBug.UI.Controls.WindowManager.updateWindowContainer(container);
                BadBug.UI.Controls.WindowManager.updateMinimizeContainer(miniContainer);
            }
        }
        else {
            var miniContainer = this._getMinimizeContainer();
            if ($get(this._id, miniContainer)) {
                var container = this._getContainer();
                var fromRect = $getRealRect(this._windowRef);
                var containerRect = $getRealRect(miniContainer);
                fromRect.x += containerRect.x;
                fromRect.y += containerRect.y;
                miniContainer.removeChild(this._baffleRef);
                container.appendChild(this._baffleRef);
                miniContainer.removeChild(this._windowRef);
                this._windowRef.style.display = "block";
                this._windowRef.style.position = "absolute";
                container.appendChild(this._windowRef);
                this.moveTo(fromRect.x, fromRect.y);
                BadBug.UI.Controls.WindowManager.updateMinimizeContainer(miniContainer);
                BadBug.UI.Controls.WindowManager.updateWindowContainer(container);
            }
        }
    },
    disableShadow: function() {
        /// <summary>
        /// 禁用阴影
        /// </summary>
        this._shadowRef.style.display = "none";
    },
    enableShadow: function() {
        /// <summary>
        /// 启用阴影
        /// </summary>
        this._shadowRef.style.display = this._shadowEnabled ? "block" : "none";
    },
    focus: function() {
        /// <summary>
        /// 激活窗口
        /// </summary>
        if (!this._isActive) {
            this._isActive = true;
            var value = 100;
            if (typeof (this._titleBarRef.style.filter) == "string")
                this._titleBarRef.style.filter = "Alpha(opacity=" + value.toString() + ")";
            else this._titleBarRef.style.MozOpacity = value / 100;
            this._windowRef.style.zIndex = 600;
            this._shadowRef.style.zIndex = 599;
            this._baffleRef.style.zIndex = 598;
            this._modalBackgroundRef.style.zIndex = 597;
            this._movingWindowRef.style.zIndex = 601;
            BadBug.UI.Controls.WindowManager.set_activeWindow(this);
            var handler = this._eventHandlerList.getHandler("focus");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    active: function() {
        /// <summary>
        /// 激活窗口
        /// </summary>
        this.focus();
    },
    blur: function() {
        /// <summary>
        /// 使窗口处于非激活状态
        /// </summary>
        if (this._isActive) {
            this._isActive = false;
            var value = 45;
            if (typeof (this._titleBarRef.style.filter) == "string")
                this._titleBarRef.style.filter = "Alpha(opacity=" + value.toString() + ")";
            else this._titleBarRef.style.MozOpacity = value / 100;
            this._windowRef.style.zIndex = 500;
            this._shadowRef.style.zIndex = 499;
            this._baffleRef.style.zIndex = 498;
            this._modalBackgroundRef.style.zIndex = 497;
            this._movingWindowRef.style.zIndex = 501;
            BadBug.UI.Controls.WindowManager.activeNextWindow(this);
            var handler = this._eventHandlerList.getHandler("blur");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    deActive: function() {
        /// <summary>
        /// 使窗口处于非激活状态
        /// </summary>
        this.blur();
    },
    hide: function() {
        /// <summary>
        /// 隐藏窗口
        /// </summary>
        this._windowState = WindowState.hidden;
        this._exchangeContainer();
        this._windowRef.style.display = "none";
        this._shadowRef.style.display = "none";
        this._baffleRef.style.display = "none";
        var handler = this._eventHandlerList.getHandler("hide");
        if (handler) handler(this, Sys.EventArgs.Empty);
    },
    setOpacity: function(value) {
        /// <summary>
        /// 设置窗口的透明度(取值范围0到100)
        /// </summary>
        /// <param name="value" type="Number"></param>
        if (typeof (this._windowRef.style.filter) == "string")
            this._windowRef.style.filter = "Alpha(opacity=" + value.toString() + ")";
        else this._windowRef.style.MozOpacity = value / 100;
    },
    updateBaffle: function() {
        var rc = $getRealRect(this._windowRef);
        $setRect(this._baffleRef, rc);
    },
    moveTo: function(x, y) {
        /// <summary>
        /// 移动窗口到指定坐标
        /// </summary>
        /// <param name="x" type="Number">指定X坐标</param>
        /// <param name="y" type="Number">指定Y坐标</param>
        Sys.UI.DomElement.setLocation(this._windowRef, x, y);
        Sys.UI.DomElement.setLocation(this._baffleRef, x - 1, y - 1);
        Sys.UI.DomElement.setLocation(this._shadowRef, x + this._shadowOffsetX, y + this._shadowOffsetY);
        var handler = this._eventHandlerList.getHandler("move");
        if (handler) handler(this, Sys.EventArgs.Empty);
    },
    moveToCenter: function() {
        /// <summary>
        /// 移动窗口到容器中央
        /// </summary>
        var parentRect = Sys.UI.DomElement.getBounds(document.body);
        var rect = Sys.UI.DomElement.getBounds(this._windowRef);
        if (this._shadowEnabled) {
            rect.width += this._shadowOffsetX;
            rect.height += this._shadowOffsetY;
        }
        var x = Math.ceil((parentRect.width - rect.width) / 2);
        var y = Math.ceil((parentRect.height - rect.height) / 2);
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        this.moveTo(x, y);
    },
    resize: function(width, height) {
        /// <summary>
        /// 调整窗口宽度和高度
        /// </summary>
        /// <param name="width" type="Number">指定宽度</param>
        /// <param name="height" type="Number">指定高度</param>
        var rc = new Sys.UI.Bounds(0, 0, width, height);
        //$getRealRect(this._windowRef, rc);
        if (!this._isMessageWindow) {
            var windowRealRc = Sys.UI.DomElement.getBounds(this._windowRef);
            if (this._titleBarVisible) {
                var titleRc = Sys.UI.DomElement.getBounds(this._titleBarRef);
                rc.height -= titleRc.height;
            }
            this._client.style.height = rc.height.toString() + "px";
            this._clientContainerRef.style.height = rc.height.toString() + "px";
        }
        this._windowRef.style.width = width.toString() + "px";
        this._windowRef.style.height = height.toString() + "px";
        this._baffleRef.style.width = (width + 2).toString() + "px";
        this._baffleRef.style.height = (height + 2).toString() + "px";
        this._shadowRef.style.width = this._windowRef.style.width;
        this._shadowRef.style.height = this._windowRef.style.height;
        var handler = this._eventHandlerList.getHandler("resize");
        if (handler) handler(this, Sys.EventArgs.Empty);
    },
    autoSize: function() {
        var clientRect = Sys.UI.DomElement.getBounds(this._client);
        var windowRect = Sys.UI.DomElement.getBounds(this._windowRef);
        var titleBarRect = Sys.UI.DomElement.getBounds(this._titleBarRef);
        windowRect.width = clientRect.width;
        windowRect.height = titleBarRect.height + clientRect.height;
        this.resize(windowRect.width, windowRect.height);
    },
    setRect: function(newRect) {
        /// <summary>
        /// 设置窗口显示区域
        /// </summary>
        /// <param name="newRect" type="Sys.UI.Bounds">指定新的显示区域</param>
        if (this._animationEnabled && this.get_isInitialized()) {
            this.doAnimation(newRect);
        }
        else {
            this.moveTo(newRect.x, newRect.y);
            this.resize(newRect.width, newRect.height);
        }
    },
    close: function() {
        /// <summary>
        /// 关闭窗口
        /// </summary>
        if (this._windowRef) {
            var handler = this._eventHandlerList.getHandler("close");
            if (handler) handler(this, Sys.EventArgs.Empty);

            BadBug.UI.Controls.WindowManager.removeWindow(this);

            $removeHandler(this._titleBarRef, "mousedown", this._ncEventDelegate);
            $removeHandler(this._titleBarRef, "mouseup", this._ncEventDelegate);

            $removeHandler(this._titleBarRef, "mouseover", this._ncEventDelegate);
            $removeHandler(this._titleBarRef, "mouseout", this._ncEventDelegate);
            $removeHandler(this._titleBarRef, "click", this._ncEventDelegate);
            $removeHandler(this._titleBarRef, "dblclick", this._ncEventDelegate);
            $removeHandler(this._titleBarRef, "mousemove", this._ncEventDelegate);

            $removeHandler(this._movingWindowRef, "mousemove", this._ncEventDelegate);
            $removeHandler(this._movingWindowRef, "mouseout", this._ncEventDelegate);
            $removeHandler(this._movingWindowRef, "mouseup", this._ncEventDelegate);

            $removeHandler(this._windowRef, "mousemove", this._ncEventDelegate);
            $removeHandler(this._windowRef, "mousedown", this._ncEventDelegate);

            if (this._isMessageWindow) {
                $removeHandler(this._windowRef, "click", this._ncEventDelegate);
            }

            this._clientContainerRef.removeChild(this._client);

            if (this._clientType == ClientType.elementRef) {
                Sys.UI.DomElement.addCssClass(this._client, "ext-template");
                if (this._oldClientParentRef) this._oldClientParentRef.appendChild(this._client);
            } else if (this._clientType == ClientType.elementCopy) {
                Sys.UI.DomElement.addCssClass(this._client, "ext-template");
            } else if (this._clientType == ClientType.url) {

            }

            this.hide();
            this._exchangeContainer();

            var container = this._getContainer();

            container.removeChild(this._windowRef);
            container.removeChild(this._shadowRef);
            container.removeChild(this._movingWindowRef);
            container.removeChild(this._modalBackgroundRef);
            container.removeChild(this._baffleRef);

            BadBug.UI.Controls.Window.__WindowCache.push({
                windowRef: this._windowRef,
                clientContainerRef: this._clientContainerRef,
                shadowRef: this._shadowRef,
                movingWindowRef: this._movingWindowRef,
                modalBackgroundRef: this._modalBackgroundRef,
                baffleRef: this._baffleRef
            });
            this._windowRef = null;
            this._clientContainerRef = null;
            this._shadowRef = null;
            this._movingWindowRef = null;
            this._modalBackgroundRef = null;
            this._titleBarRef = null;
            this._captionRef = null;
            this._miniBtnRef = null;
            this._maxiBtnRef = null;
            this._closeBtnRef = null;

            this._clearAnimationTimer();
            this._endWaitForMove();

            this._ncEventDelegate = null;
            this._clientEventDelegate = null;
            this._ncMoveWaiterDelege = null;
            this._animationTimerDelege = null;
            this._userValue = null;
        }
    },
    _drawTitleBar: function() {
        switch (this._windowState) {
            case WindowState.hidden:
                break;
            case WindowState.minimize:
                this._miniBtnRef.className = "NormalBtn";
                this._maxiBtnRef.className = "MaxiBtn";
                break;
            case WindowState.normal:
                this._miniBtnRef.className = "MiniBtn";
                this._maxiBtnRef.className = "MaxiBtn";
                break;
            case WindowState.maximize:
                this._miniBtnRef.className = "MiniBtn";
                this._maxiBtnRef.className = "NormalBtn";
                break;
        }
        this._titleBarRef.style.display = this._titleBarVisible ? "block" : "none";
        this._miniBtnRef.style.display = this._miniButtonVisible ? "block" : "none";
        this._maxiBtnRef.style.display = this._maxiButtonVisible ? "block" : "none";
        this._closeBtnRef.style.display = this._closeButtonVisible ? "block" : "none";
        this._captionRef.innerHTML = this._caption;
    },
    _loadFromCache: function(cacheObj) {
        var cache = BadBug.UI.Controls.Window.__WindowCache;
        if (cache.length > 0) {
            var cacheObj = cache.pop();
            this._windowRef = cacheObj.windowRef;
            this._clientContainerRef = cacheObj.clientContainerRef;
            this._shadowRef = cacheObj.shadowRef;
            this._movingWindowRef = cacheObj.movingWindowRef;
            this._modalBackgroundRef = cacheObj.modalBackgroundRef;
            this._baffleRef = cacheObj.baffleRef;
            this._id = this._windowRef.id;
            var titleBar = this._windowRef.childNodes[0];
            if (titleBar && titleBar.id && titleBar.id == this._id + ".TitleBar") {
                this._titleBarRef = titleBar;
                this._captionRef = titleBar.childNodes[3];
                this._miniBtnRef = titleBar.childNodes[2];
                this._maxiBtnRef = titleBar.childNodes[1];
                this._closeBtnRef = titleBar.childNodes[0];
            }
            else {
                throw "数据被破坏";
            }
            return true;
        }
        return false;
    },
    _createNew: function() {
        var windowId = "BadBug.UI.Controls.Window_" + (BadBug.UI.Controls.Window.__WindowCounter++).toString();
        this._id = windowId;
        var maxiBtn = document.createElement("div");
        maxiBtn.className = "MaxiBtn";
        maxiBtn.id = windowId + ".TitleBar.MaxiBtn";
        var miniBtn = document.createElement("div");
        miniBtn.className = "MiniBtn";
        miniBtn.id = windowId + ".TitleBar.MiniBtn";
        var closeBtn = document.createElement("div");
        closeBtn.className = "CloseBtn";
        closeBtn.id = windowId + ".TitleBar.CloseBtn";
        var caption = document.createElement("span");
        caption.id = windowId + ".TitleBar.Caption";
        var titleBar = document.createElement("div");
        titleBar.className = "TitleBar";
        titleBar.id = windowId + ".TitleBar";
        titleBar.appendChild(closeBtn);
        titleBar.appendChild(maxiBtn);
        titleBar.appendChild(miniBtn);
        titleBar.appendChild(caption);
        var clientContainerRef = document.createElement("div");
        clientContainerRef.className = "ClientContainer";
        clientContainerRef.id = windowId + ".ClientContainer";
        var windowRef = document.createElement("div");
        windowRef.className = "Window";
        windowRef.id = windowId;
        windowRef.style.display = "none";
        windowRef.appendChild(titleBar);
        windowRef.appendChild(clientContainerRef);
        var shadowRef = document.createElement("div");
        shadowRef.className = "WindowShadow";
        shadowRef.style.display = "none";
        var movingWindow = document.createElement("div");
        movingWindow.className = "MovingWindow";
        movingWindow.style.display = "none";
        var modalBackground = document.createElement("div");
        modalBackground.className = "ModalBackground";
        modalBackground.style.display = "none";

        var baffleRef = document.createElement("iframe");
        baffleRef.className = "WindowBaffle";


        this._windowRef = windowRef;
        this._clientContainerRef = clientContainerRef;
        this._shadowRef = shadowRef;
        this._movingWindowRef = movingWindow;
        this._modalBackgroundRef = modalBackground;
        this._baffleRef = baffleRef;
        this._titleBarRef = titleBar;
        this._captionRef = caption;
        this._miniBtnRef = miniBtn;
        this._maxiBtnRef = maxiBtn;
        this._closeBtnRef = closeBtn;
    },
    _disableGlobalSelect: function() {
        /// <summary>
        /// 禁用全局选择文本
        /// </summary>
        if (!this._globalSelectDisabled) {
            this._globalSelectDisabled = true;
            $addHandler(document, "selectstart", this._ncEventDelegate);
        }
    },
    _enableGlobalSelect: function() {
        /// <summary>
        /// 启用全局选择文本
        /// </summary>
        if (this._globalSelectDisabled) {
            this._globalSelectDisabled = false;
            $removeHandler(document, "selectstart", this._ncEventDelegate);
        }
    },
    _beginMove: function(e) {
        /// <summary>
        /// 开始移动窗口
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">触发此操作的事件</param>
        if (!this._isMoving) {
            this._isMoving = true;
            this._movingWindowRef.style.left = this._windowRef.style.left;
            this._movingWindowRef.style.top = this._windowRef.style.top;
            this._movingWindowRef.style.width = this._windowRef.clientWidth.toString() + "px";
            this._movingWindowRef.style.height = this._windowRef.clientHeight.toString() + "px";
            this._movingWindowRef.style.cursor = "move";
            this._movingWindowRef.style.display = "block";
            this._movingBeginX = e.clientX;
            this._movingBeginY = e.clientY;
            $addHandler(document, "mousemove", this._ncEventDelegate);
            $addHandler(document, "mouseup", this._ncEventDelegate);
            this._disableGlobalSelect();
            var handler = this._eventHandlerList.getHandler("beginMove");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    _Move: function(e) {
        /// <summary>
        /// 移动临时窗口
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">触发此操作的事件</param>
        if (this._isMoving) {
            Sys.UI.DomElement.setLocation(this._movingWindowRef, e.clientX - this._movingBeginX + this._windowRef.offsetLeft, e.clientY - this._movingBeginY + this._windowRef.offsetTop);
        }
    },
    _endMove: function(e) {
        /// <summary>
        /// 结束移动窗口
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">触发此操作的事件</param>
        if (this._isMoving) {
            this._isMoving = false;
            var targetRC = Sys.UI.DomElement.getBounds(this._movingWindowRef);
            this._movingWindowRef.style.display = "none";
            $removeHandler(document, "mousemove", this._ncEventDelegate);
            $removeHandler(document, "mouseup", this._ncEventDelegate);
            this.setRect(targetRC);
            this.focus();
            this._enableGlobalSelect();
            var handler = this._eventHandlerList.getHandler("endMove");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    _beginResize: function(e) {
        /// <summary>
        /// 开始改变窗口大小
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">触发此操作的事件</param>
        if (this._resizeState == 1) {
            this._resizeState = 2;
            var pt = Sys.UI.DomElement.getLocation(this._windowRef);
            Sys.UI.DomElement.setLocation(this._movingWindowRef, pt.x, pt.y);
            this._movingWindowRef.style.width = this._windowRef.clientWidth.toString() + "px";
            this._movingWindowRef.style.height = this._windowRef.clientHeight.toString() + "px";
            this._movingWindowRef.style.cursor = this._resizeDirection + "-resize"; ;
            this._movingWindowRef.style.display = "block";
            this._movingBeginX = e.clientX;
            this._movingBeginY = e.clientY;
            $addHandler(document, "mousemove", this._ncEventDelegate);
            $addHandler(document, "mouseup", this._ncEventDelegate);
            this._disableGlobalSelect();
            var handler = this._eventHandlerList.getHandler("beginResize");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    _Resize: function(e) {
        /// <summary>
        /// 改变临时窗口大小
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">触发此操作的事件</param>
        if (this._resizeState == 2) {
            var offsetX = e.clientX - this._movingBeginX;
            var offsetY = e.clientY - this._movingBeginY;
            var minHeight = this._titleBarRef.clientHeight;
            var minWidth = 70;
            var rcWindow = Sys.UI.DomElement.getBounds(this._windowRef);
            var rcMovingWindow = Sys.UI.DomElement.getBounds(this._movingWindowRef);
            if (this._resizeDirection.indexOf("n") != -1) {
                if (rcWindow.height - offsetY > minHeight) {
                    Sys.UI.DomElement.setLocation(this._movingWindowRef, rc.x + offsetX, rcMovingWindow.y);
                    this._movingWindowRef.style.height = (rcWindow.height - offsetY) + "px";
                }
            }
            else if (this._resizeDirection.indexOf("s") != -1) {
                if (rcWindow.height + offsetY > minHeight) {
                    this._movingWindowRef.style.height = (rcWindow.height + offsetY) + "px";
                }
            }
            if (this._resizeDirection.indexOf("w") != -1) {
                if (rcWindow.width - offsetX > minWidth) {
                    Sys.UI.DomElement.setLocation(this._movingWindowRef, rcWindow.x + offsetX, rcMovingWindow.y);
                    this._movingWindowRef.style.width = (rcWindow.width - offsetX) + "px";
                }
            }
            else if (this._resizeDirection.indexOf("e") != -1) {
                if (rcWindow.width + offsetX > minWidth) {
                    this._movingWindowRef.style.width = (this._windowRef.clientWidth + offsetX) + "px";
                }
            }
        }
    },
    _endResize: function(e) {
        /// <summary>
        /// 结束改变窗口大小
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">触发此操作的事件</param>
        if (this._resizeState == 2) {
            this._resizeState = 1;
            var targetRC = Sys.UI.DomElement.getBounds(this._movingWindowRef);
            this.setRect(targetRC);
            this._movingWindowRef.style.display = "none";
            $removeHandler(document, "mousemove", this._ncEventDelegate);
            $removeHandler(document, "mouseup", this._ncEventDelegate);
            this.focus();
            this._enableGlobalSelect();
            var handler = this._eventHandlerList.getHandler("endResize");
            if (handler) handler(this, Sys.EventArgs.Empty);
        }
    },
    _beginWaitForMove: function() {
        this._endWaitForMove();
        this._moverWaitingTimer = setTimeout(this._ncMoveWaiterDelege, 500);
    },
    _endWaitForMove: function() {
        if (this._moverWaitingTimer != 0) {
            clearTimeout(this._moverWaitingTimer);
            this._moverWaitingTimer = 0;
        }
    },
    doAnimation: function(targetRc) {
        /// <summary>
        /// 以动画方式移动窗口以及改变窗口大小
        /// </summary>
        /// <param name="targetRc" type="Sys.UI.Bounds">窗口将显示的目标区域</param>
        this._clearAnimationTimer();
        var startRc = $getRealRect(this._windowRef);
        var totalStep = 9;
        var minPresision = 5;
        this._animationXPrecision = 0;
        this._animationYPrecision = 0;
        this._animationWidthPrecision = 0;
        this._animationHeightPrecision = 0;
        while (totalStep > 2
        && Math.abs(this._animationXPrecision) < minPresision
        && Math.abs(this._animationYPrecision) < minPresision
        && Math.abs(this._animationWidthPrecision) < minPresision
        && Math.abs(this._animationHeightPrecision) < minPresision) {
            totalStep--;
            this._animationXPrecision = (targetRc.x - startRc.x) / totalStep;
            this._animationYPrecision = (targetRc.y - startRc.y) / totalStep;
            this._animationWidthPrecision = (targetRc.width - startRc.width) / totalStep;
            this._animationHeightPrecision = (targetRc.height - startRc.height) / totalStep;
        }
        this._animationStepCounter = 0;
        this._animationStartRect = startRc;
        this._animationEndRect = targetRc;
        this._beginAnimationTimer();
    },
    _beginAnimationTimer: function() {
        this._clearAnimationTimer();
        this._animationTimer = setTimeout(this._animationTimerDelege, 20);
    },
    _clearAnimationTimer: function() {
        if (this._animationTimer != 0) {
            clearTimeout(this._animationTimer);
            this._animationTimer = 0;
        }
    },
    _onAnimationComplete: function() {
        if (this._windowState == WindowState.minimize) {
            var handler = this._eventHandlerList.getHandler("animationComplete");
            if (handler) handler(this, Sys.EventArgs.Empty);
            this.blur();
            this._exchangeContainer();
        }
    },
    _getContainer: function() {
        return BadBug.UI.Controls.WindowManager.getWindowContainer(this._parentRef);
    },
    _getMinimizeContainer: function() {
        return BadBug.UI.Controls.WindowManager.getMinimizeContainer(this._parentRef);
    },
    __ncEventHandler: function(e) {
        /// <summary>
        /// 非客户区事件处理函数
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">事件</param>
        switch (e.type) {
            case "click":
                if (e.target.id == this._closeBtnRef.id) {
                    if (this._isMessageWindow) {
                        var result = "";
                        if (this._messageWindowType == MessageBoxType.ok) {
                            result = "确定";
                        }
                        else if (this._messageWindowType == MessageBoxType.yesNo) {
                            result = "否";
                        }
                        else if (this._messageWindowType == MessageBoxType.abortRetryIgnore) {
                            result = "忽略";
                        }
                        else {
                            result = "取消";
                        }
                        if (typeof (this._messageWindowCallBack) == "function") {
                            if (!this._messageWindowCallBack(this, result, this._userValue)) break;
                        }
                    }
                    this.close();
                }
                else if (e.target.id == this._miniBtnRef.id) {
                    if (this._windowState == WindowState.minimize) {
                        e.target.className = "MiniBtn";
                        this.restore();
                    }
                    else {
                        this.minimize();
                        e.target.className = "NormalBtn";
                    }
                }
                else if (e.target.id == this._maxiBtnRef.id) {
                    if (this._windowState == WindowState.minimize || this._windowState == WindowState.normal) {
                        this.maximize();
                        e.target.className = "NormalBtn";
                    }
                    else {
                        this.restore();
                        e.target.className = "MaxiBtn";
                    }
                }
                else if (this._isMessageWindow) {
                    var ctl = e.target;
                    if (ctl.tagName.toLowerCase() == "input") {
                        if (typeof (this._messageWindowCallBack) == "function") {
                            if (!this._messageWindowCallBack(this, ctl.value, this._userValue)) break;
                        }
                        this.close();
                    }
                }
                break;
            case "dblclick":
                if (this._maxiButtonVisible) {
                    if (this._windowState == WindowState.normal) {
                        this.maximize();
                    }
                    else if (this._windowState == WindowState.maximize) {
                        this.restore();
                    }
                }
                break;
            case "mouseover":
                var className = e.target.className;
                if (className.endsWith("Btn")) {
                    e.target.className = className + "_Over";
                }
                break;
            case "mouseout":
                this._endWaitForMove();
                var className = e.target.className;
                if (className.endsWith("_Down")) {
                    className = className.substr(0, className.length - 5);
                    e.target.className = className;
                }
                if (className.endsWith("_Over")) {
                    e.target.className = className.substr(0, className.length - 5);
                }
                break;
            case "mousedown":
                if (!this._isActive) this.focus();
                var className = e.target.className;
                if (className.endsWith("_Over")) {
                    e.target.className = className + "_Down";
                }
                else if (this._resizeState == 1) {
                    this._beginResize(e);
                }
                else if (this._windowState == WindowState.normal && (className == "TitleBar" || e.target == this._captionRef)) {
                    this._beginWaitEvenArg = e;
                    this._beginWaitForMove();
                }
                break;
            case "mouseup":
                //this._enableGlobalSelect();
                this._endWaitForMove();
                if (this._resizeState == 2) {
                    this._endResize(e);
                }
                else if (this._isMoving) {
                    this._endMove(e);
                }
                else {
                    var className = e.target.className;
                    if (className.endsWith("_Down")) {
                        e.target.className = className.substr(0, className.length - 5);
                    }
                }
                if ((className == "TitleBar" || e.target == this._captionRef) && document.selection) {
                    document.selection.empty();
                }
                break;
            case "mousemove":
                if (this._moverWaitingTimer != 0) {
                    this._endWaitForMove();
                    this._beginMove(e);
                }
                else if (this._resizeState == 2) {
                    this._Resize(e);
                }
                else if (this._isMoving) {
                    this._Move(e);
                }
                else if (this._windowState == WindowState.normal
                && this._resizeEnabled
                && (e.target == this._titleBarRef || e.target == this._windowRef)) {
                    var offsetX = e.offsetX; // - this._windowRef.offsetLeft;
                    var offsetY = e.offsetY; // - this._windowRef.offsetTop + this._windowRef.ownerDocument.documentElement.scrollTop;
                    var resizeDirection = "";
                    if (offsetY < 5) {
                        resizeDirection += "n";
                    }
                    else if (this._windowRef.clientHeight - offsetY < 5) {
                        resizeDirection += "s";
                    }
                    if (offsetX < 5) {
                        resizeDirection += "w";
                    }
                    else if (this._windowRef.clientWidth - offsetX < 5) {
                        resizeDirection += "e";
                    }
                    if (resizeDirection != "") {
                        this._windowRef.style.cursor = resizeDirection + "-resize";
                        this._resizeState = 1;
                    }
                    else {
                        this._windowRef.style.cursor = "default";
                        this._resizeState = 0;
                    }
                    this._resizeDirection = resizeDirection;
                }
                else {
                    if (this._resizeState == 1) {
                        this._windowRef.style.cursor = "default";
                        this._resizeState = 0;
                    }
                }
                break;
            case "selectstart":
                e.preventDefault();
                break;
        }
    },
    __clientEventHandler: function(e) {
        /// <summary>
        /// 客户区事件处理函数
        /// </summary>
        /// <param name="e" type="Sys.UI.DomEvent">事件</param>
    },
    __ncMoveWaiterHandler: function() {
        this._moverWaitingTimer = 0;
        this._beginMove(this._beginWaitEvenArg);
    },
    _animationTimerHandler: function() {
        this._animationTimer = 0;
        var rcTarget = this._animationEndRect;
        var rcFrom = this._animationStartRect;
        var rc = $getRealRect(this._windowRef);
        this._animationStepCounter++;

        if (this._animationXPrecision != 0) {
            if (this._animationXPrecision > 0) rc.x = rcFrom.x + Math.ceil(this._animationXPrecision * this._animationStepCounter);
            else rc.x = rcFrom.x + Math.floor(this._animationXPrecision * this._animationStepCounter);
        }
        if (this._animationYPrecision != 0) {
            if (this._animationYPrecision > 0) rc.y = rcFrom.y + Math.ceil(this._animationYPrecision * this._animationStepCounter);
            else rc.y = rcFrom.y + Math.floor(this._animationYPrecision * this._animationStepCounter);
        }
        if (this._animationWidthPrecision != 0) {
            if (this._animationWidthPrecision > 0) rc.width = rcFrom.width + Math.ceil(this._animationWidthPrecision * this._animationStepCounter);
            else rc.width = rcFrom.width + Math.floor(this._animationWidthPrecision * this._animationStepCounter);
        }
        if (this._animationHeightPrecision != 0) {
            if (this._animationXPrecision > 0) rc.height = rcFrom.height + Math.ceil(this._animationHeightPrecision * this._animationStepCounter);
            else rc.height = rcFrom.height + Math.floor(this._animationHeightPrecision * this._animationStepCounter);
        }
        if ((rcTarget.x - rcFrom.x) * (rc.x - rcTarget.x) >= 0
        && (rcTarget.y - rcFrom.y) * (rc.y - rcTarget.y) >= 0
        && (rcTarget.width - rcFrom.width) * (rc.width - rcTarget.width) >= 0
        && (rcTarget.height - rcFrom.height) * (rc.height - rcTarget.height) >= 0) {
            //animation completely
            this.moveTo(rcTarget.x, rcTarget.y);
            this.resize(rcTarget.width, rcTarget.height);
            this._onAnimationComplete();
        }
        else {
            this.moveTo(rc.x, rc.y);
            this.resize(rc.width, rc.height);
            this._beginAnimationTimer();
        }
    }
};

BadBug.UI.Controls.Window.__WindowCounter = 0;
BadBug.UI.Controls.Window.__WindowCache = [];

BadBug.UI.Controls.Window.registerClass( "BadBug.UI.Controls.Window", Sys.Component );

BadBug.UI.Controls._WindowManager = function(){
    BadBug.UI.Controls._WindowManager.initializeBase( this );
    this._windowList = $create( BadBug.DataStructures.LinkedList );
    this._windowContainers = $create( BadBug.DataStructures.LinkedList );
    this._minimizeContainers = $create( BadBug.DataStructures.LinkedList );
    this._globalEventDelegate = null;
};

BadBug.UI.Controls._WindowManager.prototype = {
    initialize: function(){
        BadBug.UI.Controls._WindowManager.callBaseMethod( this, "initialize" );
        this._globalEventDelegate = Function.createDelegate( this, this._globalEventHandler );
        $addHandler( window, "resize", this._globalEventDelegate );
        $addHandler( window, "scroll", this._globalEventDelegate );
    },
    dispose: function(){
        BadBug.UI.Controls._WindowManager.callBaseMethod( this, "dispose" );
        if( this._globalEventDelegate )
        {
            $removeHandler( window, "resize", this._globalEventDelegate );
            $removeHandler( window, "scroll", this._globalEventDelegate );
            this._globalEventDelegate = null;
        }        
    },
    findWindowById: function( id ){
        /// <summary>
        /// 通过id查找窗口引用
        /// </summary>
        /// <param name="id" type="String">要查找的窗口id</param>
        /// <returns type="BadBug.UI.Controls.Window"></returns>
        var node = this._windowList.getFirstNode();
        while( node != null )
        {
            var windowRef = node.value;
            if( windowRef.get_id() == id )
            {
                return windowRef;
            }
            node = node.next;
        }
        return null;
    },
    get_activeWindow: function(){
        /// <summary>
        /// 获取当前的活动窗口
        /// </summary>
        /// <returns type="BadBug.UI.Controls.Window"></returns>
        var node = this._windowList.getFirstNode();
        if( node != null ) return node.value;
        return null;
    },
    set_activeWindow: function( windowRef ){
        /// <summary>
        /// 将指定窗口设定为活动窗口
        /// </summary>
        /// <param name="windowRef" type="BadBug.UI.Controls.Window">指定窗口的引用</param>
        var node = this._windowList.find( windowRef );
        var firstNode = this._windowList.getFirstNode();
        if( node == null )
        {
            node = this._windowList.addFirst( windowRef );
        }
        else
        {
            this._windowList.removeNode( node );
            this._windowList.addNodeFirst( node );
        }
        if( firstNode != null && firstNode.value != windowRef )
        {
            firstNode.value.blur();
        }
    },
    activeNextWindow: function( windowRef ){
        /// <summary>
        /// 激活窗口队列中的下一个窗口
        /// </summary>
        /// <param name="windowRef" type="BadBug.UI.Controls.Window">指定窗口的引用</param>
        var node = this._windowList.find( windowRef );
        if( node == null ) return;
        var firstNode = this._windowList.getFirstNode();
        if( node == firstNode && this._windowList.getNodeCount() > 1 )
        {
            this._windowList.removeFirst();
            firstNode = this._windowList.getFirstNode();
            this._windowList.addNodeAfter( firstNode, node );
            var node = firstNode;
            while( node != null )
            {
                if( node.value.get_windowState() == WindowState.normal )
                {   
                    node.value.focus();
                    break;
                }
                node = node.next;
            }
            //if( node == null ) firstNode.value.focus();
            
        }
    },
    removeWindow: function( windowRef ){
        /// <summary>
        /// 从窗口管理器中删除指定窗口
        /// </summary>
        /// <param name="windowRef" type="BadBug.UI.Controls.Window">指定窗口的引用</param>
        if( !this._windowList ) return;
        var node = this._windowList.find( windowRef );
        if( node == null ) return;
        var firstNode = this._windowList.getFirstNode();
        this._windowList.removeNode( node );
        if( node == firstNode )
        {
            firstNode = this._windowList.getFirstNode();
            if( firstNode != null )
            {
                firstNode.value.focus();
            }
        }
    },
    updateWindows: function(){
        /// <summary>
        /// 重置所有窗口位置
        /// </summary>
        var node = this._windowContainers.getFirstNode();
        while( node != null )
        {
            this.updateWindowContainer( node.value );
            node = node.next;
        }
        node = this._minimizeContainers.getFirstNode();
        while( node != null )
        {
            this.updateMinimizeContainer( node.value );
            node = node.next;
        }        
        node = this._windowList.getFirstNode();
        while( node != null )
        {
            var windowRef = node.value;
            if( windowRef.get_windowState() == WindowState.maximize )
            {
                //windowRef.minimize();
                windowRef.maximize();
            }
            node = node.next;
        }
    },
    updateWindowContainer: function( containerRef ){
        var rect = $getWindowVisbleRect( containerRef.parentNode );
        $setPos( containerRef, rect.x, rect.y );
    },
    updateMinimizeContainer: function( containerRef ){
        var _h = containerRef.offsetHeight;
        var rect = $getWindowVisbleRect( containerRef.parentNode );
        rect.y = rect.height + rect.y - _h - 2;
        $setPos( containerRef, rect.x, rect.y );
        var node = this._windowList.getFirstNode();
        while( node != null )
        {
            if( node.value.get_windowState() == WindowState.minimize )
            {   
                node.value.updateBaffle();
            }
            node = node.next;
        }
    },
    getWindowContainer: function( parentRef ){
        /// <summary>
        /// 获取窗口容器
        /// </summary>
        /// <returns type="Object">窗口容器的引用</returns>
        if( this._windowContainers == null ) throw Error.invalidOperation();
        var id = "BadBug.UI.Controls.Window.WindowContainer";
        var container = $get( id, parentRef );
        if( container == null )
        {
            container = document.createElement( "div" );
            container.id = id;
            container.className = "WindowContainer";
            parentRef.appendChild( container );
            this.updateWindowContainer( container );
            this._windowContainers.addLast( container );
        }
        return container;
    },
    getMinimizeContainer: function( parentRef ){
        if( this._minimizeContainers == null ) throw Error.invalidOperation();
        var id = "BadBug.UI.Controls.Window.MinimizeContainer";
        var container = $get( id, parentRef );
        if( container == null )
        {
            container = document.createElement( "div" );
            container.id = id;
            container.className = "MinimizeContainer";
            parentRef.appendChild( container );
            this.updateMinimizeContainer( container );
            this._minimizeContainers.addLast( container );
        }
        return container;
    },
    _globalEventHandler: function( e ){
        switch( e.type )
        {
            case "resize":
            case "scroll":
                this.updateWindows();
                break;
        }
    }
}

BadBug.UI.Controls._WindowManager.registerClass( "BadBug.UI.Controls._WindowManager", Sys.Component );

BadBug.UI.Controls.WindowManager = $create( BadBug.UI.Controls._WindowManager );


BadBug.UI.Controls.Window.createMessageBox = function( caption, text, windowType, icon, defaultButtonIndex, callBack, context, width ){
    /// <summary>
    /// 创建一个消息窗口
    /// </summary>
    /// <param name="caption" type="String">窗口显示的标题</param>
    /// <param name="text" type="String">窗口显示的文本</param>
    /// <param name="windowType" type="MessageBoxType">窗口显示的按钮样式</param>
    /// <param name="icon" type="MessageBoxIcon">显示图标</param>
    /// <param name="defaultButtonIndex" type="Number">默认选中从左到右第defaultButtonIndex个按钮</param>
    /// <param name="callBack" type="Function">当有按钮点击时调用的函数</param>
    /// <param name="context" type="Object">将要传递给callBack的上下文对象</param>
    /// <param name="width" type="Number">设定消息窗口宽度</param>
    /// <returns type="BadBug.UI.Controls.Window"></returns>
   
    if( typeof( width ) != "number" || width < 0 )
    {
        width = 250;
    }   
    var btnArea = document.createElement( "div" );
    btnArea.className = "ButtonArea";
    if( windowType != MessageBoxType.noButton )
    {
        if( windowType == MessageBoxType.abortRetryIgnore )
        {
            var btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "中止";
            btnArea.appendChild( btn );
            btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "重试";
            btnArea.appendChild( btn );
            btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "忽略";
            btnArea.appendChild( btn );
        }
        else if( windowType <= MessageBoxType.okCancel )
        {
            var btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "确定";
            btnArea.appendChild( btn );
        }
        else if( windowType == MessageBoxType.retryCancel )
        {
            var btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "重试";
            btnArea.appendChild( btn );
        }
        else
        {
            var btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "是";
            btnArea.appendChild( btn );
            btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "否";
            btnArea.appendChild( btn );
        }
        if( windowType == MessageBoxType.okCancel
        || windowType == MessageBoxType.retryCancel
        || windowType == MessageBoxType.yesNoCancel )
        {
            var btn = document.createElement( "input" );
            btn.type = "button";
            btn.name = "cmdbtn";
            btn.value = "取消";
            btnArea.appendChild( btn );
        }    
    }
    var icoDiv = document.createElement( "div" );
    if( icon == MessageBoxIcon.none )
    {
        icoDiv.style.display = "none";
    }
    else
    {
        icoDiv.className = BadBug.UI.Controls._iconNames[ icon ];
    }
    
    var txtSpan = document.createElement( "span" );
    txtSpan.id = "__messageBoxTextSpan";
    txtSpan.innerHTML = text;
    var txtArea = document.createElement( "div" );
    txtArea.className = "TextArea";
    txtArea.appendChild( icoDiv );
    txtArea.appendChild( txtSpan );
    
    var msgElement = document.createElement( "div" );
    msgElement.className = "MessageBox";
    msgElement.appendChild( txtArea );
    msgElement.appendChild( btnArea );    

    var messageBox = $create( BadBug.UI.Controls.Window, {
        client: msgElement,
        clientType: ClientType.elementCopy,
        startRect: new Sys.UI.Bounds( 0, 0, width, 200 ),
        caption:  caption,
        resizeEnabled: false,
        animationEnabled: false,
        isMessageWindow: true,
        messageWindowType: windowType,
        messageWindowCallBack: callBack,
        messageWindowDefaultButtonIndex: defaultButtonIndex,
        userValue: context        
    } );
    messageBox.showModal();
    return messageBox;
};

var $messageBox = BadBug.UI.Controls.Window.createMessageBox;

if ( typeof(Sys) !== "undefined" ) Sys.Application.notifyScriptLoaded();

