﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BOK");

BOK.barGraph = function(element) {
    BOK.barGraph.initializeBase(this, [element]);

    // Properties
    this._value = null;

    // Objects
    this._image = null;
    this._displayValue = null;
}

BOK.barGraph.prototype = {
    // Init
    initialize: function() {
        BOK.barGraph.callBaseMethod(this, 'initialize');
    },

    // Dispose
    dispose: function() {
        BOK.barGraph.callBaseMethod(this, 'dispose');
    },

    // Page Load
    pageLoad: function() {
        if (!this._value) {
            this._element.style.visibility = 'hidden';
        }
    },

    // Private Methods
    _setOutput: function() {
        if (!this._length) {
            this._length = this._value / this._maxValue;
            this._length = parseInt(this._length * parseInt(this._maxLength.replace('px', '')));
        }

        this._element.style.visibility = 'visible';

        for (var i=0; i<this._element.childNodes.length; i++) {
            if (this._element.childNodes[i].className == "value")
            {
                this._element.childNodes[i].innerHTML = String.localeFormat("{0:" + this._labelFormat + "}", this._value);
            }
        }

        if (this._presistLength == "false") {
            var n = this._length
            this._length = null;

            if (n < 0) {
                n = 0;
            }

            this._growBar(n + "px");
        }
        else {
            this._growBar(this._length);
        }
    },

    _growBar: function(barLength) {
        var currentLength = parseInt(this._element.style.width.replace('px', ''));

        if (isNaN(currentLength)) {
            currentLength = 0;
        }

        barLength = parseInt(barLength.replace('px', ''));

        if (currentLength <= barLength) {
            while (currentLength <= barLength) {
                currentLength = currentLength + 1;

                if (currentLength <= parseInt(this._maxLength.replace('px', ''))) {
                    this._element.style.width = currentLength + "px";
                }
            }
        }
        else if (currentLength >= barLength) {
            while (currentLength >= barLength) {
                currentLength = currentLength - 1;

                if (currentLength <= parseInt(this._maxLength.replace('px', ''))) {
                    if (currentLength > -1) {
                        this._element.style.width = currentLength + "px";
                    }
                    else {
                        this._element.style.width = "0px";
                    }
                }
            }
        }
    },

    get_value: function() {
        return this._value;
    },
    set_value: function(value) {
        if (value) {
            this._value = value;

            this._setOutput();
        }
    }
}

BOK.barGraph.createProperty("maxValue");
BOK.barGraph.createProperty("maxLength");
BOK.barGraph.createProperty("length");
BOK.barGraph.createProperty("presistLength");
BOK.barGraph.createProperty("labelFormat");

BOK.barGraph.registerClass('BOK.barGraph', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();