/**
 * jquery.accordionmenu 1.0.
 * 
 * Copyright (c) 2011 Colin Mackie
 * http://www.colinmackie.com
 *
 * Licensed under GPLv3
 * http://www.opensource.org/licenses/gpl-3.0.html
 *
 * Version : 1.0
 * Released: 2011-05-31
 */

(function($) {

	var showItem = function($this, $item, $link)
	{
		var $div = $("div", $item);
		
		if (!$div.is(':visible'))
		{
			$('li.accordion-menu-active > div', $this).slideUp('normal',
				function()
				{
					$('li.accordion-menu-active', $this).removeClass('accordion-menu-active');
				}
			);
			$div.slideDown('normal',
				function()
				{
					$item.addClass('accordion-menu-active');
					if ($link)
					{
						var url = $link.attr("href");
						if (url && url.length != 0 && url != "#")
						{
							window.location.href = url;
						}
					}
				}
			);
		}
		else
		{
			if ($link)
			{
				var url = $link.attr("href");
				if (url && url.length != 0 && url != "#")
				{
					window.location.href = url;
				}
			}
		}
	}

	var methods =
	{
	
		// init function
		init : function(options)
		{
			var settings =
			{
				color: "#333333"
			};

			return this.each(function ()
			{
				var prop;
				
				if (options)
				{
					$.extend(settings, options);
				}
				
				var $this = $(this);
				var data = $this.data("accordionmenu");
				
				if (!data)
				{
					$(this).data("accordionmenu", {
						target : $this
					});
					data = $this.data("accordionmenu");
					
					// set settings
					if (settings.width)
					{
						$this.css("width", settings.width + "px");
					}
					if (settings.height)
					{
						$this.css("height", settings.height + "px");
					}
					var width = $this.width();
					var height = $this.height();

					// get the id				
					var id = $this.attr("id");
					if (!id)
					{
						id = "" + new Date().getTime();
						$this.attr("id", id);
					}

					// set the class and add the settings
					$this.addClass("accordion-menu");

					data.id = id;

					$('li > div', $this).hide();
					$('li.expand > div', $this).show();
					$('li.expand', $this).addClass('accordion-menu-active');

					$('li > h3 a', $this).bind({
						click: function(e)
						{
							e.stopImmediatePropagation();
							var $item = $(this).parentsUntil('li').parent();
							var $div = $item.find('div');
	
							showItem($this, $item, $(this));
							return false;
						}
					});

				}
			});
		},
		
		// show method
		show : function(index)
		{
			var $this = $(this);
			var data = $this.data("accordionmenu");
			
			var $item = $("li:nth-child(" + (index+1)+ ")", $this);
			showItem($this, $item);
			
			return $this;
		},
		
		isInit : function()
		{
			var $this = this;
			var data = $this.data("accordionmenu");
			return (data != null);
		},
		
		// destory method
		destroy : function() 
		{
			return this.each(function()
			{
				var $this = this;
				var data = $this.data("accordionmenu");
				data.tooltip.remove();
				$this.removeData("accordionmenu");
			});
		}
		
	};
  	
	// call the appropriate method
  $.fn.accordionmenu = function(method)
  {
		if (methods[method])
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if (typeof method === 'object' || ! method)
		{
			return methods.init.apply(this, arguments);
		}
		else
		{
			$.error('Method ' +  method + ' does not exist on jQuery.tooltip');
		}
  };
 
})(jQuery);

