﻿Type.registerNamespace("Mjlsh");

Mjlsh.FixedBlock = function(element) {
    Mjlsh.FixedBlock.initializeBase(this, [element]);
    this._anchor = null;
}

Mjlsh.FixedBlock.prototype = {
        initialize : function() {
        Mjlsh.FixedBlock.callBaseMethod(this, 'initialize');
        
        var element = this.get_element();
        if (!element) throw Error.invalidOperation("没有对应文字块！");
        
        this._repositionHandler = Function.createDelegate(this, this._reposition);
        
        $addHandler(window, 'resize', this._repositionHandler);
        
        this._reposition();
    },
    
    dispose : function() {
        if (this._repositionHandler) {
            $removeHandler(window, 'resize', this._repositionHandler);
            this._repositionHandler = null;
        }
        
        Mjlsh.FixedBlock.callBaseMethod(this, 'dispose');
    },

    _reposition : function(eventObject) {
        var element = this.get_element();
        if (!element) return;
        
        var clientWidth;
        var clientHeight;
        switch(Sys.Browser.agent) {
            case Sys.Browser.InternetExplorer:
                clientWidth = document.documentElement.clientWidth;
                clientHeight = document.documentElement.clientHeight;
                break;
            case Sys.Browser.Safari:
                clientWidth = window.innerWidth;
                clientHeight = window.innerHeight;
                break;
            case Sys.Browser.Opera:
                clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
                break;
            default:  // Sys.Browser.Firefox, etc.
                clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
                break;
        }
        
        if (this._anchor.indexOf("Right") != -1) {
            var newWidth = clientWidth - parseInt(element.style.left);
           if (newWidth < 1) {
                newWidth = 1;
            }
            element.style.width = newWidth + "px";
          }
          
          if (this._anchor.indexOf("Bottom") != -1) {
            var newHeight = clientHeight - parseInt(element.style.top);
            if (newHeight < 1) {
                newHeight = 1;
                }
                element.style.height = newHeight + "px";
           } 
    },
    
    get_Anchor : function() {
        return this._anchor;
    },
    set_Anchor : function(value) {
        this._anchor = value;
    }
}
Mjlsh.FixedBlock.registerClass('Mjlsh.FixedBlock', Sys.UI.Behavior);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();