(function ($) {

    var undefined;

    $.fn.blength = function () {

        for (el in this[0]) {
            return true;
        }

        return false;
    };

    $.reReplace = function (re, original, reenplazo) {

        var reex = new RegExp(re, "g");

        return String(original).replace(reex, reenplazo);
    }

    $.fn.isContinue = function (selector) {

        if ($(this).is(selector))
            return this;

        return null;
    }

    $.fn.wait = function (time, type) {

        time = time || 1000;
        type = type || "fx";

        return this.queue(type, function () {
            var self = this;
            setTimeout(function () {
                $(self).dequeue();
            }, time);
        });
    };

    $.fn.qRunItIf = function (ex, fn, args) {

        var self = this;

        return $(self).queue(function () {

            args = args || [];

            if (ex)
                fn.apply(self, args);

            $(self).dequeue();

        });

    };

    $.fn.qRunIt = function (fn, args) {

        return $(this).qRunItIf(true, fn, args);

    };


    $.fn.runItContext = function (fn, args) {

        try {
            args = args || [];
            fn.apply(this, args);
        }
        catch (ex) { }

        return this;
    };

    $.fn.runIf = function (expre, fn, args, async, delay) {

        if (expre) {

            var self = this;

            async = async || false;
            delay = delay || 10;
            args = args || [];

            var interal = function () {
                fn.apply(self, args);
            };

            if (async)
                setTimeout(interal, delay);
            else
                interal();
        }

        return this;
    };

    $_keysIgnore = { 8: 1 };
    
    $.fn.__onlyThisCharacter = function (validCharacter) {

        $(this).bind("keypress.onlyNumbers", function (ev) {

            var k = ev.charCode || ev.keyCode || ev.which;

            if ($_keysIgnore[k])
                return true;
            else if (ev.ctrlKey || ev.altKey || ev.metaKey)  //Ignore
                return true;
            else if (validCharacter[k] != undefined)
                return true;

            return false;
        });

        return this;
    };

    $.fn.onlyNumbers = function (otherChar) {

        var validCharacter = [], base = 48;

        if (otherChar != undefined) {
            if (typeof otherChar == 'boolean') { // bool 
                if (otherChar)
                    base = 40;
            }
            else if (typeof otherChar == 'string') {

                $.each(otherChar.split(''), function (i, el) {
                    validCharacter[el.charCodeAt(0)] = true;
                });
            }
        }

        for (var ind = base; ind < 58; ind++)
            validCharacter[ind] = true;

        return $(this).__onlyThisCharacter(validCharacter);
    };

    $.fn.tagName = function () {
        if (this == null || this.length == 0)
            return "";
        return this.get(0).tagName;
    };

    $.locationSearch = function () {

        var resultado = [];

        try {
            var search = window.location.search;

            if (search == null || search == '')
                return [];

            search = search.replace(/\?/, '').split('&');

            for (var i in search)
                resultado[search[i].split('=')[0]] = search[i].split('=')[1];
        }
        catch (ex) {
        }

        return resultado;
    };

    $.fn._textCounterReferenc = [];
    $.fn.textCounter = function (options) {

        var defaults = {
            maxlimit: 100,
            description: null,
            enter: true
        };

        var options = $.extend({}, defaults, options);

        if (options.description != null) {
            if (typeof options.description == 'string')
                options.description = $('#' + options.description);
        }

        var fevent = function (ev) {

            var value = $(this).val(),
                k = ev.charCode || ev.keyCode || ev.which,
                incremente = 1;

            if (k == 8)
                incremente = -1;

            if (options.enter == false && k == 13)
                return false;

            if (ev.ctrlKey || ev.altKey || ev.metaKey)  //Ignore
                return true;

            if ((value.length + incremente) > options.maxlimit)
                return false;

            return true;
        };

        var fcheck = function (el) {

            var value = $(this).val();

            if (value.length > options.maxlimit) {
                $(this).val(value.substr(0, options.maxlimit));
            }

            return $(this).val();
        };

        var fcounter = function (ev) {
            var value = fcheck.call(this);
            $(options.description).text(options.maxlimit - value.length);
        };

        $(this).each(function (i, el) {
            if ($(this).is(':input')) {

                $(this).attr("tcounterref", $.fn._textCounterReferenc.length + 1);
                $.fn._textCounterReferenc[$(this).attr("tcounterref")] = {
                    update: function () {
                        $(this).trigger('keyup');
                    }
                };

                $(this).unbind('keypress.textCounter').bind('keypress.textCounter', fevent);
                $(this).unbind('keyup.textCounter').bind('keyup.textCounter', fcounter);
                $(this).unbind('change.textCounter').bind('change.textCounter', fcounter);
            }
        });

        return this;
    };

    $.fn.counterUpdate = function () {
        return $(this).each(function (i, el) {
            if ($(this).is(':input')) {
                var ind = $(this).attr("tcounterref");
                if ($.fn._textCounterReferenc[ind] != undefined)
                    $.fn._textCounterReferenc[ind].update.call(this);
            }
        });
    };

    var attrOverwrite = $.fn.attr;
    $.fn.attr = function (attributeName, value) {

        value = value || "";

        if ($.browser.msie && $.browser.version == "6.0" && value == "selected") {
            this[0].setAttribute(attributeName, value);
            return $(this);
        }
        else {
            return attrOverwrite.apply(this, arguments);
        }

    };

    $.fn.trim = function () {

        var formElems = /textarea|input|select/i,
            tagName = $(this).tagName().toLowerCase(),
            texto = "";

        if (formElems.test(tagName)) {
            texto = $(this).val();
        }
        else {
            texto = $(this).text();
        }

        return String(texto).trim();
    };

    $.validarMail = function (email) {
	      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	      return filter.test(email);
	  };


})(jQuery);
