﻿/// <reference path="../../lib/jquery-1.4.2.min.js" />
/// <reference path="../sys/System.js" />
/// <reference path="../sys/System.Guid.js" />



Controls.NumericUpDown = function (id, obj) {
    ///#region private fields
    var _thisRef = this;
    var leftBtnId = null;
    var rightBtnId = null;

    obj = obj || {};
    var _defaultValue = obj.defaultValue || 0;
    ///#endregion

    ///#region private fields
    function _getSelf() {
        return $('#' + _thisRef.id);
    }

    function _getLeftButtonHtml() {
        leftBtnId = System.Guid.newGuid();
        return "<input type='button' class='nudleft' value='-' id='" + leftBtnId + "' />"
    }

    function _getRightButtonHtml() {
        rightBtnId = System.Guid.newGuid();
        return "<input type='button' class='nudright' value='+' id='" + rightBtnId + "' />"
    }

    function _getLeftButtonObj() {
        return $('#' + leftBtnId);
    }


    function _getRightButtonObj() {
        return $('#' + rightBtnId);
    }

    function _incriment() {
        if (!_thisRef.value()) _thisRef.value(_defaultValue);
        if (_thisRef.value() < _thisRef.max && _thisRef.max - _thisRef.value() >= _thisRef.step)
            _thisRef.value(_thisRef.value() + _thisRef.step);
        else _thisRef.value(_thisRef.max);
    }

    function _decriment() {
        if (!_thisRef.value()) _thisRef.value(_defaultValue);
        if (_thisRef.value() > _thisRef.min && _thisRef.value() - _thisRef.min > _thisRef.step) {
            if (_thisRef.usedouble)
                _thisRef.value(Math.round((_thisRef.value() - _thisRef.step) * 100) / 100);
            else
                _thisRef.value(_thisRef.value() - _thisRef.step);
        }
        else _thisRef.value(_thisRef.min);
    }

    function _appendEvents() {
        _getSelf().keydown(function (e) {
            if (e.keyCode == 40 || e.which == 40) {
                _decriment();
            }
            else if (e.keyCode == 38 || e.which == 38) {
                _incriment();
            }
            else if ((e.which > 57 && e.which < 96) || (e.which > 105 && e.which < 190)) {
                return false;
            }
            else if (e.which == 190 && !_thisRef.usedouble) {
                return false;
            }
            _getSelf().change();
        }).change(function () {
            if (!parseFloat($(this).val())) _thisRef.value(_defaultValue);
        });

        _getLeftButtonObj().mousedown(function () { _decriment(); _getSelf().change(); });
        _getRightButtonObj().mousedown(function () { _incriment(); _getSelf().change(); });

    }

    ///#endregion

    ///#region public fields
    this.id = id;
    this.step = obj.step || 1;
    this.min = obj.min || 0;
    this.max = obj.max || 2147483648;
    this.usedouble = obj.usedouble || false;
    this.usepercent = obj.usepercent || false;
    ///#endregion


    ///#region public methods
    this.init = function () {
        this.render();
    }

    this.value = function (val) {
        if (val) {
            _getSelf().attr('value', val);
        }
        else {
            if (_thisRef.usedouble) {
                return Math.round(parseFloat(_getSelf().attr('value')) * 100) / 100;
            }
            else
                return parseFloat(_getSelf().attr('value'));
        }
    }
    this.render = function () {
        $(_getLeftButtonHtml()).insertBefore(_getSelf());
        $(_getRightButtonHtml()).insertAfter(_getSelf());
        if (obj.defaultValue) _getSelf().attr('value', _defaultValue);
        _appendEvents();
    }
    ///#endregion

    ///#region events
    ///#endregion
    this.init();
}
