﻿var smartech = {}

smartech.navBar = function(instance) { 
    
    if(typeof(instance)=="String") { 
        this.instance = $(instance); 
    }
        
    this.instance = instance;
    this.idSeed = 0;  
}


smartech.navBar.prototype = {

    init: function() {
        //iterate to find additional uls inside of the structure. 

        var navBar = this;

        this.instance.find("ul")
            .each(function(i, val) {

                var subList = $(val);
                var subListParent = subList.parent();
                var menuContainer = subListParent.parent();
                var pageType = menuContainer.parent().parent().parent();

                var id = "sn-id-" + ++navBar.idSeed;

                subListParent.data("subnavid", id);
                subList.wrap('<div id="' + id + '" class="subnav-cont"></div>');

                var btmCap = $('<div class="btm-cap"></div>');
                $("#" + id).append(btmCap);

                subList.css("display", "block");
                btmCap.css("display", "block");

                var adjustedHeight = 10;
                if (pageType[0].className.indexOf('sub') >= 0) {
                    adjustedHeight = 8;
                }

                subListParent.hover(

                    function() {

                        var el = $("#" + $(this).data("subnavid"));
                        var tId = $(this).data("timeoutid");
                        window.clearTimeout(tId);

                        el.css({
                            top: subListParent.position().top + (subListParent.height() + adjustedHeight),
                            left: subListParent.position().left
                        });

                        el.slideDown();
                    },
                    function() {

                        var el = $(this);
                        el.data("timeoutid",
                            window.setTimeout(function() {
                                el.find("div.subnav-cont")
                                    .slideUp();
                            }, "500")
                        );
                    }
                );

                $("#" + id).hover(

                    function(e) {
                        e.stopPropagation();
                        var tId = $(this).parent().data("timeoutid");
                        window.clearTimeout(tId);

                    },
                    function() {

                    }
                );

            });
    }
}


smartech.init = function() {

    var mainNav = new smartech.navBar($("#mainNav"));
    mainNav.init();
}
