/*
* magic_panel.js
* Amiel Martin
* 2009-08-22
*
* a dryification of all the drop down panel stuff we are doing
*/

(function($){

	$.fn.magicPanel = function(options) {
		var opts = (typeof options === "undefined") ? $.fn.magicPanel.defaults : $.extend({}, $.fn.magicPanel.defaults, options);

		function show(selector) {
			$(document).bind('keydown.magicpanel', function(e) {
		        if (e.keyCode == 27) panel_close.apply($(selector)); // -> ESC
		        return true;
			});
			$(selector).stop().show().animate({ top: "-40px" }, { duration: 500, easing: 'easeOutBack', complete: opts.after_show });
		}

		function panel_close() {
			$(document).unbind('keydown.magicpanel');
			$(this).stop().animate({ top: "-160px" }, { duration: 500, easing: 'easeInBack', complete: opts.after_close });
			return false;
		}

		this.each(function() {
			var $this = $(this),
			close_link = $('<a class="close" href="#"></a>').click(function() {
				panel_close.apply($(this).parents('.panel'));
			});

			$this.append(close_link);

			

			if (opts.link) {
				$(options.link).click(function(){ show($this); return false; });
			} else {
				show($this);
			}
		});
	};

	$.fn.magicPanel.defaults = {
		after_show: function(){},
		after_close: function(){}
	};

})(jQuery);