/**
 *	v.1.0.2
*/
;(function($) {
		
	$.fn.fadeToggle = function(speed, easing, callback) {
	   return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	$.fn.opacityToggle = function(speed, easing, callback) {
		return ( this.css('opacity') == 0 ) ? 
			this.animate({opacity:100}, speed, easing, callback) : 
				this.animate({opacity:0}, speed, easing, callback);
	}; 
	
	
		
		
	$.fn.expandor = function(config) { 
		var opts = $.extend({
			expandor_class: '.expandor',
			expandee_class: '.expandee',
			grouped_class: '.expandor-grouped'
				
		}, config);
		
		$(opts.expandee_class).hide().css({visibility:'hidden'});

		/**
		*	Toggle the expandor on / off
		*	The passed object should:
		*	- Have .expandee as children
		*	- Have .expandee's as siblings
		*	- or have the .expandee class
		*/
		$.fn.expandor.toggle = function(that) {

				
				var expandee = $(that).siblings('.expandee').first();
				
				if ( $(that).hasClass('.expandee-wrapper') ) {
					expandee = $(that).find('.expandee');
				}
				
				if ( $(that).hasClass(opts.expandee_class) ) {
					expandee = $(that);
				}
				
				
				if ( $(expandee).size() == 0 ) {
					expandee = $(that).find('.expandee');
				}
				if ( $(expandee).size() == 0 ) {
					expandee = $(that).parent().find('.expandee');
				}

				//Wrap the expandee if it's not already done so, to control the height animation
				var wrapper = ( $(expandee).parents('.expandee-wrapper').size() > 0 ) ? $(expandee).parents('.expandee-wrapper') : $(expandee).wrap('<div/>').parent().addClass('expandee-wrapper');

				
				//Is element currently visible
				var visible = ( $(expandee).css('visibility') == 'hidden' ) ? false: true;

				//Outer height (padding only, margins don't animate properly
				var height = (visible == false) ? $(expandee).outerHeight(true) : 0;

					
				if ( visible == false) {
					//Show the content
					$(wrapper).css({'height':0}).animate({'height':height}, 400, 'easeOutExpo');
					
					$(expandee).show('slide', { direction: 'right' } , 800 ).css('visibility','visible');
				} else {
					//Hide
					$(expandee).hide('slide', { direction: 'up' } , 400, function() {
							$(expandee).css('visibility','hidden');
					});
					$(wrapper)
						.animate({'height':0}, 400, 'easeInQuad', function() {
								$(wrapper).css({height:'auto'});
					});
				}
			
		}
		
		/**
		*	Bind to the expandor elements
		*/
		$(this).css({ cursor:'pointer' }).bind('click', function() {
			$.fn.expandor.toggle(this);
			return false;
		});


		/**
		*	Toggle grouped items, by rel attribute
		*/
		$(opts.grouped_class).bind('click', function() {
			var rel = $(this).attr('rel');
			
			var $items = $(this).parent().find('[rel="'+rel+'"]').has('.expandor');
			
			if ( $(this).hasClass('expandor-group-expanded') ) {
				$items = $items.find(':visible');
				$(this).removeClass('expandor-group-expanded')
			} else {
				$items = $items.find(':hidden;');
				$(this).addClass('expandor-group-expanded')
			}
			
			
			$items.each(function() {
				$.fn.expandor.toggle(this);
			}); 
			return false;
		}); 
		
		
		return this.each(function() {
			
				
		}); 

	}
})(jQuery); 

