jQuery.noConflict();
$navigation = function(jEl) {
	var self = this;
	
    this.options = {
        event : arguments[1] && arguments[1].event ? arguments[1].event : 0,
        showDuration : 500,
        hideDuration : 300,
        defaultselectedSections : jEl.find("li.selected")
		
    };
	
    this.navigation = jEl;
    this.navigation.find("li").each(function() {
	
        var o = jQuery(this);
		
        if (o.find("> ul").length > 0) {
            o.addClass("HasChild");
        }

			
    });

    this.refresh = function(evt) {
			// hold default selected sections
    	var selectedSections = self.options.defaultselectedSections;
		for (i = 0; i < selectedSections.length; i++) {
            var selectedSection = jQuery(selectedSections[i]);
            selectedSection.addClass("Hilite");
        }

        var leaf = null;
        this.navigation.find("li.selected").each(function() {
            var o = jQuery(this);
            if (o.find("li.selected").length <= 0) {
                leaf = o;
            }
        });

        while (leaf != null && leaf.hasClass("selected")) {
            if (!leaf.hasClass("Hilite")) {
                leaf.find("> ul:first").hide(self.options.hideDuration);
            }
            leaf.removeClass("selected");
            leaf = leaf.parent().parent();
        }
    };
    
    jQuery(document).bind(self.options.event, function(evt) {
        if (self.navigation.find("li.selected").length > 0) {
            self.refresh(self.options.event);
        }

        self.navigation.find('li.Hilite').each(function() {
            jQuery(this).find("> ul:first").show(self.options.showDuration);
        });

        evt.stopPropagation();
    });
};

jQuery.fn.extend( {
    addNavigation : function() {
        var options = arguments[0];
        this.each(function() {
            new $navigation(jQuery(this), options);
        });
    }
});

