(function($){
	
	/* Slide Down Thingy */
	/* Version: 1.0.2 */
	
	$.fn.slideDownThingy = function( options ){
	
		var conf = { speed 		: 500,
					 easing 	: 'swing',
					 align	 	: 'right',
					 width		: 'auto', 
					 height		: 'auto',
					 parentID	: 'wrapper',
					 hideText	: false,
					 openText	: 'Open',
					 closeText	: 'Close' }

		if ( options ) $.extend( conf, options );
		
		var el = this;
		
		var s = conf.speed;
		var e = conf.easing;
		
		var oT = conf.openText;
		var cT = conf.closeText;
		
		// Find out where to put the Slide Down Thingy
		var pL;
		var pR; 

		if ( conf.align == 'left' ) { pL = 0; pR = 'auto'; }
		if ( conf.align == 'right' ) { pL = 'auto'; pR = 0; }
		
		// Makes content as inline-block, therefore we're able to get exact width
		el.css({ 'display' : 'inline-block' });
		
		// Put everything at the end of the wrapper
		var fp = conf.parentID;

		$('#' + fp).css({ 'position' : 'relative', 'overflow' : 'hidden' }).append('<div id="sdtWrapper"><div id="sdtContainer"><div id="sdtButton" class="closed">' + oT + '</div></div></div>');
		
		var fw = $('#sdtWrapper');
		var fc = $('#sdtContainer');
		var fb = $('#sdtButton');
		
		// Move content to Slide Down Thingy
		el.prependTo(fc);
		
		// Find out the height and width of the content
		var w;
		var h;
		
		if ( conf.width == 'auto' ) { w = el.outerWidth(true); } else {	w = conf.width;	}
		if ( conf.height == 'auto' ) { h = el.outerHeight(true); } else { h = conf.height; }
		
		// Add the CSS to make it working
		fw.css({ 'position'	: 'absolute',
				 'top'		: -h,
				 'left'		: pL,
				 'right'	: pR,
				 'width'	: w,
				 'height'	: h });

		fc.css({ 'position' : 'relative',
				 'width'	: w,
				 'height'	: h });
		
		fb.css({ 'position'	: 'absolute',
				 'top'		: '100%',
				 'right'	: 0 });
		
		// Hide text if required
		if ( conf.hideText == true ) { fb.css({ 'text-indent' : '-9999px' }); }
		
		// Slide Up/Down functionality
		fb.live('click', function() {
		
			// Before we can run the function we need to make sure it's not animating
			if ( ! fw.is(':animated') ) {
			
				if ( parseInt( fw.css('top') ) != 0 ) {
					fw.animate({ 'top' : 0 }, { duration : s, easing : e, complete : 
						function() {
							fb.text(cT).removeClass('closed').addClass('opened');
						}
					});
				} else {
					fw.animate({ 'top' : -h }, { duration : s, easing : e, complete: 
						function() {
							fb.text(oT).removeClass('opened').addClass('closed');
						}
					});
				}
			}
		});
		
		return this;
	};
})( jQuery );
