/** Klasse zur Darstellung des Rennmenues */
var RaceMenu = new Class({
	Implements: Options,

	options: { //default-Werte
		elMenuBase: null,
		elBtnMenuToggleAll: null,
		initState: null
	},

	elMenuBase: null,
	arrElCategoryAll: [],

	/** Initialisieren */
	initialize: function(options) {
		this.setOptions(options); //options Übernehmen

		if(this.options.elMenuBase != null) {
			this.arrElCategoryAll = this.options.elMenuBase.getElements('.leftbox_element');
			this.bindEvents();

			switch(this.options.initState) {
				case 'closed': this.hideCategoryAll(); break;
				case 'open': this.showCategoryAll(); break;
			}
		}
	},

	toggleCategory: function(elCategory) {
		if(elCategory.getNext().isDisplayed()) { this.hideCategory(elCategory); }
		else { this.showCategory(elCategory); }
	},

	showCategory: function(elCategory) {
		elCategory.getNext().show();
		elCategory.getElement('a').setStyle('background-position', '0 bottom');
	},

	hideCategory: function(elCategory) {
		elCategory.getNext().hide();
		elCategory.getElement('a').setStyle('background-position', '0 0');
	},

	toggleCategoryAll: function() {
		var hasHiddenCategories = false;
		outerLoop:
		for(var i=0; i<this.arrElCategoryAll.length; i++) {
			if(!this.arrElCategoryAll[i].getNext().isDisplayed()) {
				hasHiddenCategories=true;
				break outerLoop;
			}
		}
		if(hasHiddenCategories) { this.showCategoryAll(); }
		else { this.hideCategoryAll(); }
	},

	showCategoryAll: function() {
		this.arrElCategoryAll.each(function(el) {
			this.showCategory(el);
		}.bind(this));
	},

	hideCategoryAll: function() {
		this.arrElCategoryAll.each(function(el) {
			this.hideCategory(el);
		}.bind(this));
	},

	/** macht das Menü interaktiv bedienbar */
	bindEvents: function() {

		this.arrElCategoryAll.each(function(el) {
			el.addEvent('click', function(e) {
				this.toggleCategory(el);
			}.bind(this));
		}.bind(this));

		if(this.options.elBtnMenuToggleAll != null) {
			this.options.elBtnMenuToggleAll.addEvent('click', function(e) {
				this.toggleCategoryAll();
			}.bind(this));
		}
	}
});
