window.addEvent('domready', function() {
	if ($('ondernemers_uitgelicht')) {
		new OndernemersUitgelicht($('ondernemers_uitgelicht'));
	}

	$$('.widget.tabbed').each(function(el) {
		new TabDiv(el);
	});
});



var OndernemersUitgelicht = new Class({
	element: null,
	contentElements: null,
	current: null,
	initialize: function(element) {
		this.element = element;
		this.contentElements = element.getElements('.ondernemers_uitgelicht_content');
		element.getElements('.button_previous').addEvent('click', function() {
			this.gotoContent(this.current - 1);
			return false;
		}.bind(this));
		element.getElements('.button_next').addEvent('click', function() {
			this.gotoContent(this.current + 1);
			return false;
		}.bind(this));
		this.gotoContent(0);
	},
	gotoContent: function(id) {
		var count = this.contentElements.length;
		if (id < 0) id = 0;
		if (id >= count) id = count-1;
		if (id != this.current) {
			this.contentElements.hide();
			this.contentElements[id].show();
			this.current = id;
			this.element.getElements('.current').set('html', id+1);
		}
	}
});

var TabDiv = new Class({
	element: null,
	tabs: null,
	tabButtons: null,
	current: null,
	initialize: function(element) {
		this.element = element;
		this.tabs = element.getElements('.tab');
		this.tabButtons = element.getElements('.tabbutton');
		
		this.tabButtons.each(function(el, index){
			el.addEvent('click', function() {
				this.showTab(index);
				return false;
			}.bind(this));
		}.bind(this));
		this.showTab(0);
	},
	showTab: function(id) {
		var count = this.tabs.length;
		if (id < 0) id = 0;
		if (id >= count) id = count-1;
		if (id != this.current) {
			this.tabs.hide();
			this.tabs[id].show();
			this.tabButtons.removeClass('active');
			this.tabButtons[id].addClass('active');
			this.current = id;
		}
	}
})
