comp_tab = {
	tabs: [],
	cur_tab: 0,
	default_tab:0,
	reset: function() {
		this.tabs = [];
		this.cur_tab = this.default_tab;
		//if (console) console.log('comp_tab reset');
	},
	showDefault: function() {
		if (this.tabs[this.cur_tab]) this.tabs[this.cur_tab].show(true);
	}
}

TabComponent = Class.create();
TabComponent.prototype = {
	
	initialize: function(target, action, options) {
		this.id = 'tab_'+target;
		this.self = $(this.id);
		
		this.target = target;
		this.action = action;
		this.tab_num = comp_tab.tabs.length;
		
		this.injectBehaviour();
		
		this.options = options || {};
		
		if (this.options.default_tab) {
			comp_tab.default_tab = this.options.default_tab;
			comp_tab.cur_tab = comp_tab.default_tab;
		}
		
		if (this.tab_num == comp_tab.cur_tab) {
			var me = this;
			page_init.scripts.tabs = function() {
				comp_tab.showDefault();
			};
		}
		
		// if (console) console.log('GLOBAL CUR: ' + comp_tab.cur_tab + ' tab_num: ' + this.tab_num);
		if (comp_tab.cur_tab == this.tab_num) {
			Element.addClassName(this.self, 'on');
		} else {
			if (!this.options.no_fade) {
				new Effect.Opacity(this.id, {from:0.5, to:0.5});
			}
		}
	},
	
	injectBehaviour: function() {
		this.self.onclick = this.tabClick.bindAsEventListener(this);
		this.self.onmouseover = this.tabOver.bindAsEventListener(this);
		this.self.onmouseout = this.tabOut.bindAsEventListener(this);
	},
	
	tabClick: function(e) {
		//if (console) console.log('CUR TAB: ' + comp_tab.cur_tab + ' act: ' + this.tab_num);
		if (comp_tab.cur_tab != this.tab_num) {
			if (comp_tab.tabs[comp_tab.cur_tab]) comp_tab.tabs[comp_tab.cur_tab].hide(this);
		}
		if (this.options.no_fade) {
			if (comp_tab.tabs[comp_tab.cur_tab]) Element.removeClassName(comp_tab.tabs[comp_tab.cur_tab].self, 'on');	
			Element.addClassName(this.self, 'on');	
		}
		this.self.style.cursor = '';
		
		comp_tab.cur_tab = this.tab_num;					   
		
		this.trigger();
	}, 
	
	trigger: function() {
		if (this.action) {
			// if (console) console.log('ACTION COMMAND: ' + this.action);
			var regExp = /^javascript:/;
			if (regExp.test(this.action)) {
				eval(this.action.replace('javascript:', ''));
			} else {
				if (this.action != '#') document.location = this.action;	
			}
		}	
	},
	
	tabOver: function(e) {
		this.self.style.cursor = 'pointer';				   
	},
	
	tabOut: function(e) {
		this.self.style.cursor = '';				   
	},
	
	show: function() {
		// if (console) console.log('Show: ' + this.id);
		
		Element.addClassName(this.self, 'on');
		
		comp_tab.cur_tab = this.tab_num;
		
		if (!this.options.no_fade) {
			var e = new Effect.Opacity(this.id, {duration:0.25, from:0.5, to:1.0});
		}

		if (this.action) {
			this.trigger();
		} else {
			//if (console) console.log('SHOW: Target: ' + this.target);
			switch(this.options.transition) {
				case 'blind':
					var effect = new Effect.BlindDown($(this.target), 
								   { duration: 0.75
								   });
					break;
				default:
					var effect = new Effect.Appear($(this.target), 
								   { duration: 0
								   });
					break;
			}
		}
		
	},
	
	hide: function(caller) {
		// if (console) console.log('Hide: ' + this.id);
		
		Element.removeClassName(this.self, 'on');
		
		if (!this.options.no_fade) {
			var e = new Effect.Opacity(this.id, {duration:0.25, from:1.0, to:0.5});
		}
		
		if (!this.action) {
			// if (console) console.log('HIDE Target: ' + this.target);
			switch(this.options.transition) {
				case 'blind':
					var effect = new Effect.BlindUp($(this.target), 
												   { duration: 0.75,
												   afterFinish: function() { caller.show(); }
												   });
					break;
				default:
					var effect = new Effect.Fade($(this.target), 
												   { duration: 0,
												   afterFinish: function() { caller.show(); }
												   });
					break;
			}

		}
					
		
	}
	
	
	
};