var tabHandler = Class.create({
	tabContainer: null,
	tabHolder: null,
	tabLinks: null,
	tabContents: null,
	activeTab: null,
	tabHooks: [],
	
	initialize: function(tabContainer) {
		var that = this;
		if (!tabContainer.tabHandler) {
			this.tabContainer = tabContainer;
			this.tabContainer.tabHandler = this;
			
			var tabContents = this.tabContainer.select('.tabContent');
			this.tabContents = new Hash();
			tabContents.each(function(element) {
				if (element.id != null) {
					element.tabHandler = that;
					that.tabContents.set(element.id, element);
				}
			});
			
			var tabHolders = this.tabContainer.select('.tabHolder');
			if (tabHolders && tabHolders.length > 0) {
				this.tabHolder = tabHolders[0];
				this.tabLinks = this.tabHolder.select('a');
				this.tabLinks.each(function(link) {
					if (link.href && link.href.indexOf('#') != -1) {
						var targetName = link.href.substring(link.href.indexOf('#') + 1);
						if (targetName) {
							var contentElement = that.tabContents.get(targetName);
							if (contentElement) {
								contentElement.tabLink = link;
								contentElement.tabLink.tabHandler = that;
								contentElement.tabLink.tabName = targetName;
								contentElement.tabLink.onclick = that.tabLinkClick;
								if (!that.activeTab && link.hasClassName('active')) {
									that.activeTab = targetName;
								}
							} else {
								link.hide();
							}
						}
					}
				});
			}
			this.setTabs();
		}
	},
	
	setTabs: function() {
		var that = this;
		if (this.tabContents) {
			this.tabContents.keys().each(function(key) {
				if (!that.activeTab) {
					that.activeTab = key;
				}
				var element = that.tabContents.get(key);
				if (key == that.activeTab) {
					element.show();
					that.executeTabHooks(element);
					that.executeHooks(key, element);
					if (element.tabLink) {
						element.tabLink.addClassName('active');
					}
				} else {
					element.hide();
					if (element.tabLink) {
						element.tabLink.removeClassName('active');
					}
				}
			});
		}		 
	},
	
	setTab: function(tabName) {
		var tab = this.tabContents.get(tabName);
		if (tab) {
			this.activeTab = tabName;
			this.setTabs();
		}
	},
	
	tabLinkClick: function() {     
		var that = this.tabHandler;
		if (that) {
			that.activeTab = this.tabName;
			that.setTabs();
			return false;
		} else {
			return true;
		}
	},
	
	registerTabHook: function(element, hook) {
		if (element) {
			var id = element.id;
			var tabContent = this.tabContents.get(id);
			if (tabContent) {
				var tabHandlerHooks = tabContent.tabHandlerHooks;
				if (!tabHandlerHooks) {
					tabHandlerHooks = new Array();
					tabContent.tabHandlerHooks = tabHandlerHooks;
				}
				tabHandlerHooks.push(hook);
				if (this.activeTab && this.activeTab == id) {
					hook();
				}
			}
		}
	},
	
	executeTabHooks: function(element) {
		if (element && element.tabHandlerHooks) {
			element.tabHandlerHooks.each(function(hook) {
				hook();
			}.bind(this));
		}
	},
	
	registerHook: function(el, hook) {
		this.tabHooks.push({
			key: el.href.substring(el.href.indexOf('#') + 1), 
			fn:  hook
		});
	},
	
	executeHooks: function(key, element) {
		this.tabHooks.each(function(item) {
			if(item.key == key){
				item.fn();
			}
		}.bind(this));
	}
});

function tabHandlerInit() {
	var tabContainer = $$('.tabContainer').concat($$('.wndContent'));
	if (tabContainer) {
		tabContainer.each(function(tbCtnr) {
			var tb = new tabHandler(tbCtnr);
			var wt = tbCtnr.select('a[hook]');
			wt.each(function(el){
				if(el.readAttribute('hook')){
					tb.registerHook(el, webtrekkHooks[el.readAttribute('hook')]);
				}
			});
		});	
	}
}

/*
 * Adjusts the height of the iBox and thus avoid scrollbars when possible 
 */
function setIBoxResizeHandler() {
	var tablinks = $$('#ibox_content .tabHolder a');
	iBox.checkBoxHeight();
	tablinks.each( function(tablink) {
		tablink.observe('click', iBox.checkBoxHeight);
	});
}

if ('observe' in document) {
	document.observe('dom:loaded', function() {
		tabHandlerInit();
	});
}