/*
 * SuperNav v1.0 - Mean left hand nav that AJAX's content if there is another level.
 *
 * Copyright (c) 2008 Redweb Limited
 *
 * Built by Gavin Cooper gavincooper@redweb.com
 *
 * @params
 *		speed		: Animation speed.
 *		ajax		: Bool. AJAX in content of next page.
 *		expandClass : Class for all links which can be expanded.
 *		collapseClass:Class for all links which can be collapsed.
 *
 */
(function($) {  
	$.fn.superNav = function(options) {  
		var defaults = {  
			'speed' 		: 400,
			'ajax'			: true,
			'expandClass'	: 'expand',
			'collapseClass'	: 'collapse'
		}, options = $.extend(defaults, options);  
		
		return this.each(function() {
			// Cache parent.
			var $super = $(this);
							
			// Hide all the embeded lists.
			$("li ul", this).hide();
			
			$("li a."+options.collapseClass, this).parent().find("ul").show();
			
			
			$("li:has(ul)", this).each(function () {
			 	$(">a[class!='"+options.collapseClass+"']:first-child", this).each(function () {
					if ($(this).hasClass('selected'))
						$(this).addClass(options.expandClass).addClass('selected-'+options.expandClass);
					else
						$(this).addClass(options.expandClass);
				});
			});
			
			// Find li with class="selected" and open all parents.
			$("ul:has(a.selected)", $super).show();
			// Make sure all parents have the collapseClass.
			$("li:has(a.selected)", $super).each(function () {
				//alert($("a:first", this).parent().not(".selected").find("a:first").html());
				$("a:first:not(.selected)", this).parent().find("a:first").removeClass(options.expandClass).addClass(options.collapseClass);
			});
			
			// Handle clicks.
			$("a", this).click(function () {
				// If this element is the last in the tree then send the user to this page.
				if ($(this).parent().children("ul").length <= 0) { return true;	}
						
				// Toggle the expand/collapse... We don't use toggle because above we preset some links open and this won't work with toggle.
				if ($(this).hasClass("expand")) {
					$(this).parent().parent().find("ul:visible")
						.prev()
						.each(function () {
							// Swap classes of collapse class with expand class.
							if ($(this).hasClass('selected-'+options.collapseClass)) {
								$(this)
									.removeClass('selected-'+options.collapseClass)
									.removeClass(options.collapseClass)
									.addClass('selected-'+options.expandClass)
									.addClass(options.expandClass);
							} else {
								$(this)
									.removeClass(options.collapseClass)
									.addClass(options.expandClass);
							}
						});
						
										
					// Look for open lists in this list and close them.
					$(this).parent().parent().find("ul:visible")
						.slideUp(options.speed);
					
					// Open embeded ul.
					$(this).next().slideDown(options.speed);
					// Swap classes of expand class with collapse class.
					if ($(this).hasClass('selected-'+options.expandClass)) {
						$(this)
							.removeClass('selected-'+options.expandClass)
							.removeClass(options.expandClass)
							.addClass('selected-'+options.collapseClass)
							.addClass(options.collapseClass);
					} else {
						$(this)
							.removeClass(options.expandClass)
							.addClass(options.collapseClass);
					}
				} else {
					// Slide up embeded ul.
					$(this).next().slideUp(options.speed);
					
					if ($(this).hasClass('selected-'+options.collapseClass)) {
						$(this)
							.removeClass('selected-'+options.collapseClass)
							.removeClass(options.collapseClass)
							.addClass('selected-'+options.expandClass)
							.addClass(options.expandClass);
					} else {
						$(this)
							.removeClass(options.collapseClass)
							.addClass(options.expandClass);
					}
				}
				
				return false;
			});
			
			// Slide do the child of a selected element if it has one and swap the classes.
			$("a.selected", this).each(function() {
				// Swap classes of expand class with collapse class.
				if ($(this).next("ul").length) {
					if ($(this).hasClass('selected-'+options.expandClass)) {
						$(this)
							.removeClass('selected-'+options.expandClass)
							.removeClass(options.expandClass)
							.addClass('selected-'+options.collapseClass)
							.addClass(options.collapseClass);
					} else {
						$(this)
							.removeClass(options.expandClass)
							.addClass(options.collapseClass);
					}
					
					$(this).next().slideDown();
				}
			});
			
		}); // End each loop.
	}
})(jQuery); 