﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("TUI.TipsCommunity.Templates.WebControls");

TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender = function(element)
{
    TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender.initializeBase(this, [element]);
    
    this._characterCountDisplayElement = null;
    this._characterCountMax = null;
}

TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender.prototype =
{
    initialize: function()
    {
        TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender.callBaseMethod(this, 'initialize');
        $addHandlers(this.get_element(), { "keyup" : this.updateCharacterCountCurrent }, this);
    },
    dispose: function()
    {
        $clearHandlers(this.get_element());
        TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender.callBaseMethod(this, 'dispose');
    },
    get_characterCountDisplayElement : function()
    {
        return this._characterCountDisplayElement;
    },
    set_characterCountDisplayElement : function(value)
    {
        this._characterCountDisplayElement = value;
        this.updateCharacterCountCurrent();
    },
    get_characterCountMax : function()
    {
        return this._characterCountMax;
    },
    set_characterCountMax : function(value)
    {
        this._characterCountMax = value;
        this.updateCharacterCountCurrent();
    },
    updateCharacterCountCurrent : function()
    {
        /*
         * Don't bother trying to update anything unless there 
         * is an element available displaying the result
         */
        if (this.get_characterCountDisplayElement() && this.get_characterCountMax() && this.get_element())
        {
            var
                currentCount = this.get_element().value.length,
                charactersLeft = this.get_characterCountMax() - currentCount,
                characterCountDisplayElement = $(this.get_characterCountDisplayElement());
            characterCountDisplayElement.html(charactersLeft == 0 ? "0" : charactersLeft);
            if (charactersLeft < 0)
            {
                characterCountDisplayElement.addClass("character-overflow");
            }
            else
            {
                characterCountDisplayElement.removeClass("character-overflow");
            }
        }
    }
}
TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender.registerClass('TUI.TipsCommunity.Templates.WebControls.CharacterCountExtender', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

