/*
 * DynamicTableSections
 * Copyright (c) 2010 Peter Kröner, <http://www.peterkroener.de>, MIT License
 */
var DynamicTableSections = new Class({
	Implements: Options,
	options: {
		startClosed: true,
		except: [],
		head: 'thead',
		body: 'tbody'
	},
	headers: [],
	sections: [],

	// Init
	initialize: function(table, options){
		this.setOptions(options);
		this.setElements($$(table));
		this.setEvents();
		this.setStart();
	},

	// Get the headers and the sections
	setElements: function(table){
		this.headers = table.getElements(this.options.head)[0];
		this.sections = table.getElements(this.options.body)[0];
	},

	// Set the click events for the table heads
	setEvents: function(){
		var that = this;
		this.headers.each(function(header, index){
			var i = index;
			header.addEvent('click', function(){
				that.toggle(i);
			});
			header.addClass('toggleable');
		});
	},

	// Bring the table to the state described in the options
	setStart: function(){
		var that = this;
		if(this.options.startClosed){
			this.headers.each(function(header, index){
				that.close(index);
			});
		}
		this.options.except.each(function(index){
			that.toggle(index);
		});
	},

	// Close a section
	close: function(index){
		this.headers[index].addClass('closed');
		this.headers[index].removeClass('opened');
		this.sections[index].addClass('closed');
		this.sections[index].removeClass('opened');
		this.sections[index].setStyles({
			display: 'none'
		});
	},

	// Close all
	closeAll: function(){
		for(var i = 0; i < this.headers.length; i++){
			this.close(i);
		}
	},

	// Open a section
	open: function(index){
		this.headers[index].addClass('opened');
		this.headers[index].removeClass('closed');
		this.sections[index].addClass('opened');
		this.sections[index].removeClass('closed');
		this.sections[index].setStyles({
			display: 'table-row-group'
		});
	},

	// Close all
	openAll: function(){
		for(var i = 0; i < this.headers.length; i++){
			this.open(i);
		}
	},

	// Toggle a section
	toggle: function(index){
		if(this.headers[index].hasClass('closed')){
			this.open(index);
		}
		else{
			this.close(index);
		}
	},

	// Toggle all
	toggleAll: function(){
		for(var i = 0; i < this.headers.length; i++){
			this.toggle(i);
		}
	}

});


// Execute the code on page load
window.addEvent('domready', function(){
	// Forget about IE < 8
	if(!(Browser.Engine.trident && Browser.Engine.version < 6)){
		var table = $$('table.data')[0];
		var buttons = $$('a.tableswitch');
		var section_count = table.getElements('thead:not(.foo)').length;
		var ct = new DynamicTableSections(table, {
			startClosed: true,
			except: [0, --section_count],
			head: 'thead:not(.foo)'
		});
		// Collapse/show all
		var state = 'closed';
		buttons.addEvent('click', function(e){
			e.stop();
			if(state == 'closed'){
				ct.openAll();
				state = 'opened';
				buttons.set('text', 'Alles zuklappen');
			}
			else{
				ct.closeAll();
				state = 'closed';
				buttons.set('text', 'Alles aufklappen');
			}
		});
	}
});

