﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("USC.Web");

USC.Web.AnchorExtender = function(element) {
    USC.Web.AnchorExtender.initializeBase(this, [element]);
    this._anchor = null;
}

USC.Web.AnchorExtender.prototype = {
    initialize: function() {
        USC.Web.AnchorExtender.callBaseMethod(this, 'initialize');

        var element = this.get_element();
        if (!element) throw Error.invalidOperation("The block is not exist!");

        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;
        }
        USC.Web.AnchorExtender.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;
    }
}

USC.Web.AnchorExtender.registerClass('USC.Web.AnchorExtender', Sys.UI.Control);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
