if (typeof Driebit == 'undefined') Driebit = {}

Driebit.tabs = new Class({
	initialize: function (el) {
		this._element    = el;
		this._tabHandles = el.getElements('.tabs li:not(.label)');
		this._tabs       = el.getElements('div.content');

		this._tabHandles.addEvent('mouseup', (function (e) {
			for (var i=0; i<this._tabHandles.length; i++) {
				if (this._tabHandles[i] == e.target) {
					this.showTab(i);
					break;
				}
			}
		}).bind(this));

		for (var i=0; i<this._tabHandles.length; i++) {
			if (this._tabHandles[i].hasClass('active')) {
				this.showTab(i);
				break;
			}
		}
	},

	showTab: function (index) {
		for (var i=0; i<this._tabs.length; i++) {
			if (i == index) {
				this._tabs[i].removeClass('hide');
				this._tabHandles[i].addClass('active');
			} else {
				this._tabs[i].addClass('hide');
				this._tabHandles[i].removeClass('active');
			}
		}
	}
});

window.addEvent('domready', function () {
	$$('.tabbed').each(function (el) {
		new Driebit.tabs(el);
	});
	
	$$('.dropdown_more li').addEvent('click', function (e) {
		if (this.getParent('li').get('class') == 'label dropdown'){
			this.getParent('li').getElement('span').set('text', this.get('text'));	
		}
	});
	
	$$('.tabs li.dropdown').addEvent('mouseover', function () {
    $$('ul.dropdown_more').removeClass('hide');
    $$('.dropdown_icon').setStyle('background-position', '-26px 0px');
  });
  $$('.tabs li.dropdown').addEvent('mouseout', function () {
    $$('ul.dropdown_more').addClass('hide');
    $$('.dropdown_icon').setStyle('background-position', '0px 0px');
  });
});
