/*
 * Compact Navigation
 * Copyright 2010 Martino Flynn
 * Author Mike Ruschak
 *
 * Version 1.0
 *
 * This plugin allows the navigation to collapse and expand.
 */

(function($) {
	$.fn.compactNavigation = function(options) {
		var version = "1.0";
	
		var defaults = {
			speed: 500
		};

		var options = $.extend(defaults, options);
		
		var self = this;
		
		self.init = function() {
			
			self.locHash = window.location.hash;
			
			if (self.locHash.length > 0) {
				$("li ul.main_link_content", self).not(self.locHash + " ul.main_link_content").hide();
			} else {
				$("li ul.main_link_content", self).hide();
			}
			
			self.setupNavigation();
		};
		
		self.setupNavigation = function() {
			$(".main_link", self).click(function(e) {
				var $this = $(this);
				var linkRel = $this.attr('rel');
				var $linkContent = $("#main_link_content_" + linkRel);
				var displayAttr = $linkContent.css('display');
				
				if (displayAttr == 'none') {
					$("li ul.main_link_content", self).not("#main_link_content_" + linkRel).slideUp();
					$linkContent.slideDown();
				}
				
				if (displayAttr == 'block') {	
					$linkContent.slideUp();
				}
				
				self.setHash($(this).attr('href'));
				
				e.preventDefault();
				
				return false;
			});
		};
		
		self.setHash = function ( hash ) {
			// Write hash
			if ( typeof window.location.hash !== 'undefined' ) {
				if ( window.location.hash !== hash ) {
					window.location.hash = hash;
				};
			} else if ( location.hash !== hash ) {
				location.hash = hash;
			};
			
			// Done
			return hash;
		};
	    
	    self.init();
	};
})(jQuery);
