﻿village = {}

village.navBar = function(instance) { 
    
    if(typeof(instance)=="String") { 
        this.instance = $(instance); 
    }
        
    this.instance = instance;
    this.idSeed = 0;  
}


village.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 = $(val).parent();

                var parentCoords =
                    subListParent.offset();

                var id = "sn-id-" + ++navBar.idSeed;

                subList.attr("id", id);
                subList.addClass("subnav-cont");

                subListParent.data("subnavid", id);

                subListParent.hover(

                    function() {

                        var el = $("#" + $(this).data("subnavid"));
                        var tId = $(this).data("timeoutid");

                        window.clearTimeout(tId);

                        var dropDownWidth = 130;

                        if (el.selector.match('2')) {
                            dropDownWidth = 160;
                        }

                        el.css({
                            top: subListParent.offset().top + (subListParent.height() + 8),
                            left: subListParent.offset().left,
                            width: dropDownWidth
                        });

                        el.slideDown();
                    },
                    function() {

                        var el = $(this);

                        el.data("timeoutid",
                            window.setTimeout(function() {
                                el.find("ul")
                                    .slideUp();
                            }, "500")
                        );
                    }
                );

                subList.hover(

                    function(e) {
                        e.stopPropagation();
                        var tId = $(this).parent().data("timeoutid");
                        window.clearTimeout(tId);
                    },
                    function() {

                    }
                );

            });
    }
}


village.init = function() {

    var mainNav = new village.navBar($("#main-nav ul"));
    mainNav.init();
}