$(document).ready(function() {

    $(".hasMenu").click(function(){
        return false;
    });

    (function ($) {

        var linkTimeout;
        var lastHoveredLeft;

        // TOP MENU HOVERS

        // on mouseover show menu
        $(".categoriesList li.hasMenu").mouseenter(function(){

            var listItem = $(this);

            // clear all timeouts
            $(".categoriesList li.hasMenu").each(function(){
                clearTimeout($(this).data("linkTimeout"));
            });

            // href value of hovered link - also id of div with menu content
            var href = listItem.find("a").eq(0).attr("href");

            // add borders
            listItem.removeClass("inactive");

            // check if there are opened menus
            var thereIsOpen = false;
            $("#productsNav .submenu").each(function(){
                if($(this).css("display") == "block") {
                    thereIsOpen = $(this).attr("id");
                }
            });

            // if theres any opened ones switch immediately, else animate
            if(thereIsOpen && thereIsOpen != $(href).attr("id")) {
                $("#productsNav .submenu").css("display", "none");
                $(href).css("display", "block");
            } else if ($(href).css("display") == "none" ) {
                $(href).animate({ height: "toggle" }, 500, "easeInOutQuint");
            }

        }).mouseleave(function(){

            var listItem = $(this);

            // cycle through links and check if there's one with class current
            var currentOne;
            $(".categoriesList li.hasMenu").each(function(){
                if($(this).hasClass("current")) {
                    currentOne = $(this); return false;
                } else {
                    currentOne = 0;
                }
            });

            // ako ima aktivni nakon mouseout  vrati na aktivni
            if(currentOne) {
                var leaveCurrentOpen = function(){ // function to close all others and open currentOne
                    var href = currentOne.find("a").eq(0).attr("href"); // href value of link leaving - also id of div
                    // close all others
                    $("#productsNav .submenu").each(function(){
                        $(this).css("display", "none");
                    });
                    $(href).css("display", "block");
                    currentOne.removeClass("inactive");
                };

                // set timeout for opening curentOne
                linkTimeout = setTimeout(leaveCurrentOpen, 500);
                currentOne.data("linkTimeout", linkTimeout);

                // restore borders
                listItem.addClass("inactive");
            } else {
                var someelement = lastHoveredLeft = this; // li leaving
                var href = $(this).find("a").eq(0).attr("href"); // href value of link leaving - also id of div

                // workaround ako link nije sa dropdownom nego direktni
                if(href.substr(0,1) == '#') {
                    if($(href).css("display") == "block" && $(href).attr("id") == $(someelement).find("a").attr("href").substr(1)) {
                        linkTimeout = setTimeout(function(){ $(href).animate({ height: "toggle" }, 500, "easeInOutQuint"); }, 500);
                        $(someelement).data("linkTimeout", linkTimeout);
                    }
                }

                // restore borders
                $(someelement).addClass("inactive");
            }
        });


        // DIV HOVERS

        // when mouseover div clear timeouts
        $("#productsNav .submenu").mouseenter(function(){
            // clear all timeouts
            $(".categoriesList li.hasMenu").each(function(){
                clearTimeout($(this).data("linkTimeout"));
            });
        }).mouseleave(function(){

            // cycle through links and check if there's one with class current
            var currentOne;
            $(".categoriesList li.hasMenu").each(function(){
                if($(this).hasClass("current")) {
                    currentOne = $(this); return false;
                } else {
                    currentOne = 0;
                }
            });

            if(currentOne) {
                var leaveCurrentOpen = function(){ // function to close all others and open currentOne
                    var href = currentOne.find("a").eq(0).attr("href"); // href value of link leaving - also id of div
                    // close all others
                    $("#productsNav .submenu").each(function(){
                        $(this).css("display", "none");
                    });
                    $(href).css("display", "block");
                    currentOne.removeClass("inactive");
                };

                // set timeout for opening curentOne
                linkTimeout = setTimeout(leaveCurrentOpen, 500);
                currentOne.data("linkTimeout", linkTimeout);
            } else {
                var someelement = this;
                var id = $(this).attr("id");
                var divsLi = lastHoveredLeft = $(".categoriesList li").find("a[href=#"+id+"]").parent();
                if($(someelement).css("display") == "block") {
                    linkTimeout = setTimeout(function(){ $(someelement).animate({ height: "toggle" }, 500, "easeInOutQuint"); }, 500);
                    divsLi.data("linkTimeout", linkTimeout);
                }
            }
        });
    })(jQuery)
});

