﻿jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
$(function() {
    $.fn.extend({
        /**
        *   Input Reset
        *       Remove the text onfocus
        *       If empty return the original text
        */
        inputReset: function() {
            return this.each(function() 
            {
                if($(this).val() == "")
                    $(this).val($(this).attr("alt"));
                    
                // Password element
                if ($(this).attr("type").toLowerCase() == "password") {
                    var $me = $(this);
                    var $clone = $("<input type='text' autocomplete='off' />").val($me.val());
                    $(this).after($clone).hide();

                    // Show the password field
                    $clone.focus(function() {
                        $me.val("").show().focus();
                        $(this).hide();
                    });

                    // Show the clone (text field)
                    $(this).blur(function() {
                        if ($(this).val() == "") {
                            $(this).hide();
                            $clone.show();
                        }
                    });
                }
                // Normal element
                else {
                    $(this).blur(function() {
                        if ($(this).val() == "") {
                            $(this).val($(this).attr("alt"));
                        }
                    });

                    $(this).focus(function() {
                        if ($(this).val() == $(this).attr("alt")) {
                            $(this).val("");
                        }
                    });
                }
                return this;
            });
        },
        /**
        *   Disables the enter key
        */
        disableEnter: function() {
            return this.each(function() {
                $(this).keydown(function(event) {
                    if (event.which == 13) {
                        return false;
                    }
                });
            });
        },
        /**
        *   Max Length
        *   Set the max value of a input or textarea
        */
        maxLength: function(options) {
            var defaults = {
                maxLength: 250,                 // The max value of the element
                textTarget: "",                // Target to write 0 / 250 to
                usingModuleInputReset: false   // If it is false, the onload text will be put as the current text length
                                               // and if you are using inputReset you don't want text like 
                                               // "enter your e-mail" to be count as the current text length    
            };

            var options = $.extend(defaults, options);

            return this.each(function() {
                $(this).keydown(checkMaxLength)
                        .keyup(updateText)
                        .keyup(checkMaxLengthUp)
                        .focus(checkMaxLength);

                // On document ready check all
                if (options.usingModuleInputReset) {
                    // If its not the default value
                    if ($(this).val() == this.defaultValue) {
                        setText(0);
                    }
                    else {
                        setText($(this).val().length)
                    }
                }
                else {
                    setText($(this).val().length)
                }
                return this;
            });

            // Checks the maxValue (keydown)
            function checkMaxLength(event) {
                /*
                    Keys:
                    8               = Backspace
                    37, 38, 39, 40  = Arrow keys (L,U,R,D)
                    46              = Delete
                */
                var ignore = [8,37,38,39,40,46];
                
                // To many chars
                if (($(this).val().length == options.maxLength) && $.inArray(event.which, ignore) == -1) // 8 = backspace
                {
                    return false;
                }
            }

            // Check the maxValue (keyup)
            // Can happen when someone pastes a text
            function checkMaxLengthUp() {
                if ($(this).val().length > options.maxLength) {
                    $(this).val($(this).val().substring(0, options.maxLength));
                }
            }

            // Update the text (keyup)
            function updateText() {
                // Write text
                if ($(options.textTarget).length > 0) {
                    setText($(this).val().length);
                }
            }
            // Set the text (keyup)
            function setText(number) {
                $(options.textTarget).html(number + " / " + options.maxLength);
            }
        }
    });
});

