﻿var textMaxlength = "";
var editorTextLength = "";
var module = "";
var commandName = "";

CounterModule = function (element) {
    CounterModule.initializeBase(this, [element]);
}

CounterModule.prototype = {
    initialize: function () {
        var selfPointer = this;
        var editor = this.get_editor();
        CounterModule.callBaseMethod(this, 'initialize');
        this.attachEventHandler("onkeydown", function (e) { selfPointer.UpdateCounter(e); });
        this.attachEventHandler("onkeypressed", function (e) { selfPointer.UpdateCounter(e); });
        this.attachEventHandler("onkeyup", function (e) { selfPointer.UpdateCounter(e); });
        //For IE
        if ($.browser.msie) {
            this.attachEventHandler("onpropertychange", function (e) { selfPointer.UpdateCounter(e); });
            this.attachEventHandler("onpaste", function (e) { selfPointer.UpdateCounter(e); });
            this.attachEventHandler("onselectionchange", function (e) { selfPointer.UpdateCounter(e); });
        }
        //For FireFox
        else {
            this.attachEventHandler("oninput", function (e) { selfPointer.UpdateCounter(e); });
            this.attachEventHandler("DOMSubtreeModified", function (e) { selfPointer.UpdateCounter(e); });
        }
        var element = document.all ? editor.get_document().body : editor.get_document();
        var eventHandler = document.all ? "drop" : "dragover";
        var selElem = editor.getSelectedElement();
        $telerik.addExternalHandler(element, eventHandler, function (e) {
            $telerik.cancelRawEvent(e);
            if (!document.all) {
                e.dataTransfer.effectAllowed = "none";
                e.preventDefault();
            }
            return false;
        });
        this.UpdateCounter();
    },
    //A method that does the actual work - it is usually attached to the "selection changed" editor event
    UpdateCounter: function (e) {
        var editor = this.get_editor();
        textMaxlength = editor._rootElement.getAttribute('maxlength');
        editorTextLength = GetTextLength(editor.get_text());
        module = this;
        //If text exceed the max length or equal prevent typing
        if (editorTextLength >= textMaxlength) {
            //Call handleMaxLength function to prevent writing if the text reached the max limit
            handleMaxLength(e);
        }
        CounterLabel(editor);
    }
};

function OnClientCommandExecuting(editor, args) {
    if ($.browser.msie) {
        commandName = args.get_commandName();
        if (commandName == "Enter" && (editorTextLength >= textMaxlength)) {
            args.set_cancel(true);
        }
    }
}

function OnClientPaste(editor, args) {
    if ($.browser.msie) {
        if (commandName == "Paste") {
            module.UpdateCounter(editor);
        }
    }
}

function CounterLabel(editor) {
    var CounterText = module.get_attributes()["text"];
    var CounterEmptyText = module.get_attributes()["emptytext"];
    var MaxLengthValidatorID = module.get_attributes()["maxlengthvalidatorid"];
    var element = module.get_element();
    var count = editorTextLength;
    var remainingCount = textMaxlength - count;
    if (count == 0) {
        var text = CounterEmptyText;
    }
    else {
        var text = CounterText;
    }
    text = text.replace("[charCount]", remainingCount);
    text = text.replace("[MaxLength]", textMaxlength);
    element.innerHTML = text;
    //Call Max Length Validator
    var val = $('#' + MaxLengthValidatorID);
    ValidatorValidate(val[0]);
}

function handleMaxLength(event) {
    if (event) {
        var code;
        if ($.browser.msie) {
            code = event.keyCode;
        }
        //If FireFox, Safari
        else {
            code = event.which;
        }
        if (code != 8 && (code < 33 || code > 46)) {
            if ($.browser.msie) {
                if ($.browser.version != "9.0") {
                    event.returnValue = false;
                }
                else {
                    event.preventDefault();
                }
            }
            //If FireFox, Safari
            else {
                event.preventDefault();
            }
        }
    }
}
