﻿(function ($) {
    $.fn.extend({
        horizontalMenuNone: function (options) {
            return this.each(function () {
                $(this).find("a").each(function (i) {
                    $(this).attr('title', $(this).text());
                });
            });
        },
        horizontalMenu: function (options) {
            var defaults = {
                menutype: "horizontal",
                homepage: "home",
                animateOver: 300,
                animateOut: 100
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var o = options;
                $(this).find("ul").first().find("ul").parent().each(function (i) {
                    var $curobj = $(this)
                    var $subul = $(this).find('ul:eq(0)')
                    this.istopheader = $curobj.parents("ul").length == 1 ? true : false
                    if (options.menutype == "horizontal") {
                        $curobj.children("a:eq(0)").addClass(this.istopheader ? "menu-horizontal-arrow-top" : "menu-horizontal-arrow-sub");
                    } else {
                        $curobj.children("a:eq(0)").addClass("menu-horizontal-arrow-sub");
                    }
                    $curobj.hover(
						function (e) {
						    var $targetul = $(this).children("ul:eq(0)")
						    $targetul.slideDown(options.animateOver)
						},
						function (e) {
						    var $targetul = $(this).children("ul:eq(0)")
						    $targetul.slideUp(options.animateOut)
						}
					)
                    $curobj.click(function () {
                        $(this).children("ul:eq(0)").hide()
                    })
                })//headers
                $(this).find("ul").first().find("ul").css({ display: 'none', visibility: 'visible' })
            });
        },
        horizontalMenuTab: function (options) {
            var defaults = {
                menutype: "horizontal",
                homepage: "home",
                animateOver: 300,
                animateOut: 100
            };
            var options = $.extend(defaults, options);
            var show1 = null;
            var show2 = null;
            var saveShow1, saveShow2;
            var cnt = 0;
            var showSelected = function () {
                //$("#module-407").text($("#module-407").text() + "out;");125
                if (cnt == 0) {
                    show1.css("display", "none");
                    show2.css("display", "none");
                    saveShow1.css("display", "block");
                    saveShow2.css("display", "block");
                    show1 = saveShow1;
                    show2 = saveShow2;
                }
            }
            var hideSelected = function () {
                if (show1 != saveShow1) {
                    saveShow1.css("display", "none");
                    show1.css("display", "block");
                    saveShow2.css("display", "none");
                    show2.css("display", "block");
                } else {
                    if (show2 != saveShow2) {
                        saveShow2.css("display", "none");
                        show2.css("display", "block");
                    }
                }
            }
            return this.each(function () {
                var o = options;
                var fstMenu = window.location.pathname.split('/');
                if ((fstMenu[1] != null) && (fstMenu[1].length > 0)) {
                    var selected_li = $(this).find('a[href="/' + fstMenu[1] + '"]').parent();
                    selected_li.attr("class", "selected");
                    show1 = selected_li.children("ul:eq(0)");
                }
                else {
                    show1 = $(this).find("li:first").children("ul:eq(0)");
                }

                if ((fstMenu[2] != null) && (fstMenu[2].length > 0)) {
                    var selected_li = show1.find('a[href="/' + fstMenu[1] + '/' + fstMenu[2] + '"]').parent();
                    selected_li.attr("class", "selected");
                    show2 = selected_li.children("ul:eq(0)");

                } else {
                    show2 = show1.find("li:first").children("ul:eq(0)");
                }

                if ((fstMenu[3] != null) && (fstMenu[3].length > 0)) {
                    var selected_li = show2.find("a[href='/" + fstMenu[1] + "/" + fstMenu[2] + "/" + fstMenu[3] + "']").parent();
                    selected_li.attr("class", "selected").attr("class", "selected");
                }

                $(this).find("li").children("ul").css("display", "none");
                show1.css("display", "block");
                show2.css("display", "block");

                saveShow1 = show1;
                saveShow2 = show2;

                $(this).find("li").each(function (i) {
                    switch ($(this).parents('ul').length) {
                        case 1: $(this).find("a:first").hover(
						        function (e) {
						            cnt++;
						            if (show1 != null) { show1.css("display", "none"); }
						            if (show2 != null) { show2.css("display", "none"); }
						            show1 = $(this).parent().children("ul:eq(0)")
						            show1.css("display", "block");
						            show2 = show1.find("li").first().children("ul:eq(0)");
						            show2.css("display", "block");
						            hideSelected();
						        },
						        function (e) {
						            cnt--;
						            var $targetul = $(this).parent().children("ul:eq(0)")
						            if ($targetul.length != 0) {
						                show1 = $targetul;
						                show2 = $targetul.find("li").first().children("ul:eq(0)");
						            }
						        }
					        );
                            $(this).hover(function (e) { cnt++; }, function (e) { cnt--; setTimeout(showSelected, 200); });
                            $(this).parent().hover(function (e) { cnt++; }, function (e) { cnt--; setTimeout(showSelected, 200); });
                            break;
                        case 2: $(this).find("a:first").hover(
						        function (e) {
						            cnt++;
						            show2.css("display", "none");
						            show2 = $(this).parent().children("ul:eq(0)");
						            show2.css("display", "block");
						            hideSelected();
						        },
						        function (e) {
						            cnt--;
						            show2 = $(this).parent().children("ul:eq(0)");
						        }
					        );
                            $(this).hover(function (e) { cnt++; }, function (e) { cnt--; setTimeout(showSelected, 200); });
                            $(this).parent().hover(function (e) { cnt++; }, function (e) { cnt--; setTimeout(showSelected, 200); });
                            break;
                        default: $(this).hover(
						        function (e) {
						            cnt++;
						            var $targetul = $(this).children("ul:eq(0)");
						            $targetul.slideDown(options.animateOver);
						        },
						        function (e) {
						            cnt--;
						            var $targetul = $(this).children("ul:eq(0)");
						            $targetul.slideUp(options.animateOut);
						            setTimeout(showSelected, 200);
						        }
					        );
                            $(this).click(function () {
                                $(this).parent().children("ul:eq(0)").hide()
                            });
                            break;
                    };

                });
            });
        }
    });
})(jQuery);

