/**
 * Tabs.js - Tab that content!
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

var Tabs = function(id, options) {
	var tabs,
		activeTab,
		hoverTimeOut = null,
		listener = options.listener ? options.listener : 'click',
		hideMode = options.hideMode ? options.hideMode : 'display',
		selectedCls = options.selectedCls ? options.selectedCls : 'selected',
		onTabChange = options.onTabChange ? options.onTabChange : null;
	
	return {
		initialize: function() {
			tabs = get(id).getElementsByTagName('li');
			this.initEvents();
		},
		initEvents: function() {
			var self = this;
			
			for(var i = 0; i < tabs.length; i++) {
				var tab = tabs[i];
				var tabLink = tab.getElementsByTagName('a')[0];
				tab.container = get(tabLink.href.split('#')[1]);
				
				if(WS.hasClass(tab, selectedCls)) {
					activeTab = tab;
				}
				else {
					hideMode == 'visibility' ? WS.addClass(tab.container, 'hidden') : WS.hide(tab.container);
				}
				
				if(listener == 'mouseover') {
					WS.Event.addEvent(tab, listener, function() {
						var that = this;
						hoverTimeOut = setTimeout(function() {
							self.showTabContent(that);
						}, 250);
					});
					
					WS.Event.addEvent(tab, 'mouseout', function() {
						clearTimeout(hoverTimeOut);
					});
				}
				else {
					WS.Event.addEvent(tab, listener, function(e) {
						WS.Event.stopEvent(e);
						self.showTabContent(this);
					});
				}
			}
		},
		showTabContent: function(el) {
			if(el == activeTab) {
				return;
			}
			
			hideMode == 'visibility' ? WS.addClass(activeTab.container, 'hidden') : WS.hide(activeTab.container);
			WS.removeClass(activeTab, selectedCls);
			hideMode == 'visibility' ? WS.removeClass(el.container, 'hidden') : WS.show(el.container);
			WS.addClass(el, selectedCls);
			activeTab = el;
			
			if(onTabChange) {
				onTabChange(el.container);
			}
		}
	}
};

