
(function(jQuery){
	jQuery.fn.accordian = function(options) {
		var defaults = {
			title: 'h3',
			disclosure: '',
			multipleOpen: true
		};
		var o = jQuery.extend(defaults, options);
		var $self = this;
		
		return this.children('li').each(function() {
			$container = jQuery(this);
			
			// Add in disclosure triangle, if any
			if (o.disclosure != '') {
				$dis = jQuery('<div class="disclosure"></div>').css({ 
					float: 'left', 
					marginRight: 5, 
					width: o.disclosure.width, 
					height: o.disclosure.height, 
					background: 'url('+o.disclosure.image+') no-repeat top left' 
				});
				if (o.disclosure.marginTop > 0) $dis.css('margin-top', o.disclosure.marginTop);
				
				$container.children(o.title).css('cursor', 'pointer').prepend($dis);
			}
			
			$container.children('.acc-content').hide();
			
			$container.children(o.title).click(function() {
				var curClick = this;
				
				if (! o.multipleOpen) {
					$self.children('li').each(function() {
						if ($(this).children(o.title).equals_object($(curClick))) return true;
						
						if (jQuery(this).children(o.title).data('state') == 'open') {
							jQuery(this).children(o.title).data('state', 'closed');
							
							jQuery(this).children(o.title).children('.disclosure').css('background-position', 'top');
							jQuery(this).children(o.title).parent().children('.acc-content').hide('fast');
						}
					});
				}

				if (jQuery(this).data('state') != 'open') {
					jQuery(this).data('state', 'open');
					
					jQuery(this).children('.disclosure').css('background-position', 'bottom');
					jQuery(this).parent().children('.acc-content').show('fast');
				} else {
					jQuery(this).data('state', 'closed');
					
					jQuery(this).children('.disclosure').css('background-position', 'top');
					jQuery(this).parent().children('.acc-content').hide('fast');
				}
			});
		});
	};
		
	jQuery.fn.equals_object = function(compareTo) { 
		if (!compareTo || !compareTo.length || this.length!=compareTo.length) { 
			return false; 
		}
		 
		for (var i=0; i<this.length; i++) { 
			if (this[i]!==compareTo[i]) { 
				return false; 
			} 
		} 
		
		return true; 
	}
})(jQuery);
