﻿/*
* jQuery AssignEnterKey Plug-in *
* Summary This plug-in will attach a click/blur event to all input elements
inside a parent container, the events will add and remove a class to the
parent container that is used to associate the enter key with the a
button or submit button.

* * Notes Using the options, you can target a different controls for both
triggering the focus and the target to be executed.
* * Example $("FIELDSET").AssignEnterKey();
* * Developed by Zachary.Hunter@gmail.com * Date 2009-05-29 23:05 

*/
(function ($) {

    $.fn.AssignEnterKey = function (options) {
        var defaults = { triggerExp: ":input", targetExp: "a, :submit,:button" };
        var options = $.extend(defaults, options);
        $(document).keypress(function (e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                e.preventDefault();
                var target = $(".EnterKeyAssigned").find(options.targetExp);
                if (target.is("a")) {
                    target.click(function () {
                        var href = $(this).attr("href");
                        if (href.substr(0, 11) == "javascript:") {
                            new Function(href.substr(11)).call(this);
                        } else { window.location = href; }
                        return false;
                    });

                }
                target.click();
                return false;
            }
        }
       );
        return this.each(function () {
            var obj = $(this);
            obj.find(options.triggerExp).click(function () { obj.addClass("EnterKeyAssigned"); }).blur(function () { obj.removeClass("EnterKeyAssigned"); });
        }
      );
    };

})(jQuery);
