﻿// Improve this
jQuery(function () {
    jQuery.noConflict();

    jQuery(document).ready(function () {
        jQuery(".wantsDefaultButtonNext").live("click", function () {
            initDefaultBehaviours();
        });
        jQuery(".wantsDefaultButtonPrev").live("click", function () {
            initDefaultBehaviours();
        });
    });

    initDefaultBehaviours = function () {
        jQuery(".wantsDefaultButtonPrev").each(function () {
            var me = this;
            jQuery(this).keypress(function (e) {
                if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                    e.preventDefault();
                    // Find first element with class isDefaultButton before this
                    var btn = jQuery(me).prevAll(".isDefaultButton:first");
                    if (btn.attr("tagName") == "A")
                        window.location = btn.attr("href");
                    else
                        btn.click();
                    return false;
                }
            });
        });

        jQuery(".wantsDefaultButtonNext").each(function () {
            var me = this;
            jQuery(this).keypress(function (e) {
                if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                    e.preventDefault();
                    // Find first element with class isDefaultButton after this
                    var btn = jQuery(me).nextAll(".isDefaultButton:first");
                    if (btn.attr("tagName") == "A")
                        window.location = btn.attr("href");
                    else
                        btn.click();
                    return false;
                }
            });
        });
    };
});
