// 
//  jquery.cookaccordion.js
//  Originally written by Jan Jarfalk (http://www.unwrongest.com/projects/accordion/)
//  Updated by Aaron Vanderzwan (http://www.aaronvanderzwan.com/)
// 
//  This plugin relies on and must be loaded after jquery.cookie.js plugin 
//  (http://plugins.jquery.com/project/Cookie)

(function($) {
	$.fn.extend({
		accordion: function(options) {

			var defaults = {
				cookieName: 'accordiate',
				prefix: '<span></span>'
			};
			options = $.extend(defaults, options);

			function activate(a, b, c) {
				$(a)[(c || 'parent')]('li').toggleClass('active').siblings().removeClass('active').children('ul, div').slideUp(1000);
				$(a).siblings('ul, div')[(b || 'slideToggle')]((!b) ? 1000 : null);
			}

			function setCookie(a) {
				var e = $(a).html();
				e = e.replace(options.prefix, '');
				e = e.replace(options.prefix.toUpperCase(), '');  // FOR IE
				$.cookie(options.cookieName, e);
			}

			return this.each(function() {
				if ($(this).data('accordiated')) { return false; }
				$.each($(this).find('ul, li>div'), function() {
					$(this).data('accordiated', true);
					$(this).not('.show').hide();
				});
				$.each($(this).find('a'), function() {
					$(this).click(function(e) {
						activate(e.target);
						if ($(this).parent().children('ul').length > 0) {
							return false;
						} else {
							setCookie(this);
						}
					});
				});

				var d = false;

				if (d) {
					activate(d, 'toggle', 'parents');
					$(d).addClass('active').parent().addClass('active').parent().addClass('active').parent().addClass('active').children('ul,div').show();
				}
			});
		}
	});
})(jQuery);