var GALATEA_FE = new Class({
	
	Implements: [Options],
	
	options : 
	{
		toolTipSelector : 'tool-tip',
		zebraTableSelectors : ['zebra-table']
	},
	
	initialize : function (options)
	{
		this.setOptions(options);
		this.initTips();
		this.activateXBC();
	},
	
	initTips: function (className)
	{

		var className = '.' + ((typeof(className) == 'string') ? className : this.options.toolTipSelector);

		var tips = $$(className).each(function(el){
			var tipContent	= el.title;
			var tipBulle	= tipContent.split('::');
			var tipTitle	= tipBulle[0];
			var tipText		= tipBulle[1];
			el.store('tip:title', tipTitle);
			el.store('tip:text', tipText);
		});
		
		var tips = new Tips($$(className), {
			maxTitleChars : 75,
			onShow: function(tip) {
				tip.fade('in');
			},
			onHide: function(tip) {
				tip.fade('out');
			}
		});
	},

	refreshTips: function(className)
	{
		System.initTips(className);
	},
	
	/* XBC : cross browser compatibility */
	activateXBC: function ()
	{
		// zebra tables using pseudo selectors
		this.options.zebraTableSelectors.each(function(selector){
			var oddSelector = 'table.' + selector + ' tr:nth-child(2n+1)';
			var evenSelector = 'table.' + selector + ' tr:nth-child(2n)';
			
			$$(oddSelector).each(function(el){
				el.addClass('odd');
			});
			$$(evenSelector).each(function(el){
				el.addClass('even');
			});
		});
	}
	
});

var EMMG_FE = new Class({
	
	Implements: [Options],
	
	options : 
	{
		navContainer : 'navigation',
		agendaContainer : 'agenda-items',
		agendaFilterContainer : 'agenda-filter',
		agendaCategoryFilter : 'agendaCategoryFilter',
		agendaArchivFilter : 'agendaArchivFilter',
		agendaSortFilter : 'agendaSortFilter',
		agendaItemSelector : '.agenda-item',
		toolTipSelector : 'tool-tip'
	},
	
	properties : 
	{
		agendaFilter : 
		{
			currentCategory : '',
			currentArchiv : 'agenda-archiv1',
			currentOrder : 0
		}
	},
	
	members : 
	{
		agendaItems : [],
		agendaSorter : null
	},
	
	initialize : function (options)
	{
		this.setOptions(options);
		this.initNavigation();
		
		var agendaFilter = $(this.options.agendaFilterContainer);
		if(agendaFilter)
		{
			this.initAgenda();
		}
	},
	
	initNavigation : function()
	{
		$(this.options.navContainer).getElements('ul.level_1 > li').each(function(el){
			el.setStyle('margin-top', (el.getChildren()[0].getStyle('margin-top').toInt() + 0) + 'px');
			el.getChildren()[0].setStyle('margin-top', '0px');
		});
	},
	
	initAgenda : function ()
	{
		// retrieve all the agenda items, clone them and store them for later sorting operations
		thisClass = this;
		$$('#' + this.options.agendaContainer + ' ' + this.options.agendaItemSelector).each(function(el){
			thisClass.members.agendaItems.push(el.clone(true, true));
		});
		
		// store default properties in Cookie or retrieve them if Cookie already exists
		if(Cookie.read('properties') == null)
		{
			Cookie.write('properties', JSON.encode(this.properties));
		} 
		else
		{
			this.properties = JSON.decode(Cookie.read('properties'));
		}
		
		// init the filter gui elements with the properties values
		var filter = false;
		var categoryFilter = $(this.options.agendaCategoryFilter);
		if(categoryFilter)
		{
			categoryFilter.value = this.properties.agendaFilter.currentCategory;
			categoryFilter.addEvent('change', this.filterAgendaItems.bind(this));
			filter = true;
		}
		
		var archivFilter = $(this.options.agendaArchivFilter);
		if(archivFilter)
		{
			archivFilter.value = this.properties.agendaFilter.currentArchiv;
			archivFilter.addEvent('change', this.filterAgendaItems.bind(this));
			filter = true;
		}
		
		var sortFilter = $(this.options.agendaSortFilter);
		if(sortFilter)
		{
			sortFilter.value = this.properties.agendaFilter.currentOrder;
			sortFilter.addEvent('change', this.sortAgendaItems.bind(this));
			// sort the items if the order is not the default one 
			if(this.properties.agendaFilter.currentOrder > 0)
			{
				this.sortAgendaItems();
				// as the sort already applies the filtering, no more filtering is required
				filter = false; 
			}
		}
		
		// filter the agenda items if required
		if(filter)
		{
			this.filterAgendaItems();
		}
	},
	
	filterAgendaItems : function ()
	{
		// hide the current displayed items 
		var selector = this.buildAgendaFilterSelector();
		$$(selector).each(function(el){
			el.set('styles', {display:'none'});
		});
		
		// update the current filter values as the user changed them
		this.properties.agendaFilter.currentCategory = $(this.options.agendaCategoryFilter).value;
		this.properties.agendaFilter.currentArchiv = $(this.options.agendaArchivFilter).value;
		
		// display the items according to the filter values
		selector = this.buildAgendaFilterSelector();
		
		$$(selector).each(function(el){
			el.set('styles', {display:'block'});
		});
		
		Cookie.write('properties', JSON.encode(this.properties));

	},
	
	buildAgendaFilterSelector : function ()
	{
		var selector = '';
		
		if(this.options.agendaContainer.length > 0)
		{
			selector += '#' + this.options.agendaContainer + ' ';
		}
		selector += this.options.agendaItemSelector;
		
		if(this.properties.agendaFilter.currentCategory.length > 0)
		{
			selector += '.' + this.properties.agendaFilter.currentCategory;
		}
		
		if(this.properties.agendaFilter.currentArchiv.length > 0)
		{
			selector += '.' + this.properties.agendaFilter.currentArchiv;
		}
		
		return selector;
	},
	
	sortAgendaItems : function ()
	{
		// get the agenda items container
		var container = $(this.options.agendaContainer);
		// remove all the elements
		container.getChildren().each(function(el){el.destroy();});
		// reverse the order of the stored items
		this.members.agendaItems.reverse();
		// clone the agenda items 
		var sortedItems = [];
		this.members.agendaItems.each(function(el){
			sortedItems.push(el.clone(true, true));
		});
		// insert the cloned and sorted items back in the container
		container.adopt(sortedItems);
		this.properties.agendaFilter.currentOrder = $(this.options.agendaSortFilter).value;
		// finally we have to filter the items 
		this.filterAgendaItems();
	}
	
});

window.addEvent('load', function() {
	var galateaSite = new GALATEA_FE({zebraTableSelectors:['zebra-tabelle-design-1', 'zebra-tabelle-design-2']});
	var emmgSite = new EMMG_FE();
});

