/*
 * Awesome Tabs
 * Copyright 2010 Martino Flynn
 * Author Mike Ruschak
 *
 * Version 1.0
 *
 * This plugin allows cool tab boxes to fly up.
 */

(function($) {
	$.fn.awesomeTabs = function(options) {
		var version = "1.0";
	
		var defaults = {
			speed: 500
		};

		var options = $.extend(defaults, options);
		
		var self = this;
		
		self.init = function() {
			self.$main_content_height = parseInt($("#main_content").height());
			self.setupTabs();
		};
		
		self.setupTabs = function() {
			$(".tab", self).click(self.clickTab);
			self.tab_close_top = parseInt($(".tab", self).css('top'));
		};
		
		self.clickTab = function(e) {
			var $this = $(this);
			
			var tab_top_position = parseInt($this.css('top'));
			var tab_close = $(".tab_close", $this);
			var tab_placement = self.tab_close_top;
			
			if (tab_top_position == tab_placement) {
				tab_placement = self.$main_content_height - (parseInt($this.height()) + parseInt($this.css('padding-top')) + parseInt($this.css('padding-bottom')));
				$this.animate({top: tab_placement}, options.speed);
				
				tab_close.show();
				
				$this.unbind('click');
				
				$(".tab_tab", $this).click(function() {
					$this.animate({top: self.tab_close_top}, options.speed);
					tab_close.hide();
					$(".tab_tab", $this).unbind('click');
					$this.click(self.clickTab);
				});
			}
			
			e.preventDefault();
		};
	    
	    self.init();
	};
})(jQuery);
