﻿(function ($) {
    $.fn.navigation = function (timeout) {
        var selector = $(this);
        var url = window.location.pathname.replace("/", "").replace(".aspx", "").toLowerCase();

        $.each(selector, function (i, item) {
            var parent = $(item).parent();

            if (!$(parent).hasClass("exclude")) {
                $(parent).prepend("<span>" + $(item).html() + "</span>").hover(function (event) {
                    $(parent).find("span").stop().animate({ marginTop: "-" + $(item).height() + "px" }, timeout);
                }, function () {
                    if (!$(item).hasClass("selected")) {
                        $(parent).find("span").stop().animate({ marginTop: "0px" }, timeout);
                    }
                });

                if ($(parent).attr("id") == url) {
                    $(this).addClass("selected");
                    $(this).prev("span").css("marginTop", -$(selector.selector).height() + "px");
                }
            }
        }).click(function () {
            if ($(this).hasClass("selected")) {
                return false;
            } else {                
                $(selector.selector + ".selected").prev().animate({ marginTop: "0px" }, 0);
                $(selector.selector + ".selected").removeAttr("class");
                $(this).addClass("selected");
            }
        });
    };
})(jQuery);
