/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Robocluster");

Robocluster.AutoHeight = function() {

    this._dynamicElements = null;
    this._patternElement = null;
    
    this._heightChangedHandler = null;
    Robocluster.AutoHeight.initializeBase(this);
}

Robocluster.AutoHeight.prototype = {

    //
    // Properties
    //

    get_DynamicElements: function () {
        return this._dynamicElements;
    },

    set_DynamicElements: function (value) {
        this._dynamicElements = value;
    },

    get_PatternElement: function () {
        return this._patternElement;
    },
    set_PatternElement: function (value) {
        this._patternElement = value;
    },

    //
    // Event Handlers
    //

    UpdateHeight: function () {
        // Set height to dynamic element based on pettern element height
        var minHeight = Number.parseInvariant(this._patternElement.minHeight.toString()) - this._patternElement.startTopPadding;
        var patternHeight = this._patternElement.element.offsetHeight - this._patternElement.startTopPadding;

        if (minHeight > patternHeight) {
            patternHeight = minHeight;
            this._patternElement.element.style.height = minHeight + "px";
            
        }

        for (var i = 0; i < this._dynamicElements.length; i++) {
            if (this._dynamicElements[i].element && this._dynamicElements[i].element.offsetHeight < patternHeight) {
                this._dynamicElements[i].element.style.height = patternHeight - 27 + "px";
            }

            if (this._dynamicElements[i].element && this._dynamicElements[i].element.offsetHeight > patternHeight) {
                //this._patternElement.element.style.height = this._dynamicElements[i].element.offsetHeight + "px";
            }
        }
    },

    UpdateWidth: function () {
        // Set width to dynamic element based on pettern element width
        var countAutoWidthElments = 0;
        for (var i = 0; i < this._dynamicElements.length; i++) {
            if (this._dynamicElements[i].autoWidth == true) {
                countAutoWidthElments++;
            }
        }

        if (countAutoWidthElments > 0) {
            var elementWidth = ((window.innerWidth || document.documentElement.clientWidth) - this._patternElement.element.offsetWidth - this.GetScrollerWidth()) / countAutoWidthElments;

            for (var i = 0; i < this._dynamicElements.length; i++) {
                this._dynamicElements[i].element.style.width = elementWidth + "px";
                if (i == this._dynamicElements.length - 1 && document.all) {
                    this._dynamicElements[i].element.style.width = elementWidth + 1 + "px"
                }
            }
        }
    },


    //
    // Event Delegate
    //
    _onHeightChanged: function () {

        this.UpdateHeight();
        this.UpdateWidth();
    },

    initialize: function () {
        Robocluster.AutoHeight.callBaseMethod(this, 'initialize');

        this._heightChangedHandler = Function.createDelegate(this, this._onHeightChanged);

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._heightChangedHandler);
        setTimeout(this._heightChangedHandler, 0);
        //$addHandler(window, 'resize', this._heightChangedHandler);
    },

    dispose: function () {
        delete this._heightChangedHandler;

        Robocluster.AutoHeight.callBaseMethod(this, 'dispose');
    },

    //
    // Utils
    //
    GetScrollerWidth: function () {
        var scr = null;
        var inn = null;
        var wNoScroll = 0;
        var wScroll = 0;

        // Outer scrolling div
        scr = document.createElement('div');
        scr.style.position = 'absolute';
        scr.style.top = '-1000px';
        scr.style.left = '-1000px';
        scr.style.width = '100px';
        scr.style.height = '50px';
        // Start with no scrollbar
        scr.style.overflow = 'hidden';

        // Inner content div
        inn = document.createElement('div');
        inn.style.width = '100%';
        inn.style.height = '200px';

        // Put the inner div in the scrolling div
        scr.appendChild(inn);
        // Append the scrolling div to the doc
        document.body.appendChild(scr);

        // Width of the inner div sans scrollbar
        wNoScroll = inn.offsetWidth;
        // Add the scrollbar
        scr.style.overflow = 'auto';
        // Width of the inner div width scrollbar
        wScroll = inn.offsetWidth;
        // Remove the scrolling div from the doc
        document.body.removeChild(
            document.body.lastChild);

        // Pixel width of the scroller
        return (wNoScroll - wScroll);
    }

}
Robocluster.AutoHeight.registerClass('Robocluster.AutoHeight', Sys.Component);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


