var EVENTSET = new Class({
	initialize: function(events) {
		this.imageDir = './images/content/';
		this.contentWrapperID = 'contentWrapper';
		this.eventWrapperID = 'eventContent';
		this.controlPanelWrapper = null;
		this.controlPanelWrapperID = 'controlPanel';
		this.eventElement = null;
		this.eventID = 'event';
		this.events = new Array();
		this.eventsArchive = new Array();
		this.countMonthForArchive = 3;
		this.archiveDate = this.getArchiveDate();
		this.archiveOpened = false;
		this.archiveLinkWrapper = null;
		this.archiveLink = null;
		this.archiveLinkImage = null;
		this.archiveLinkText = null;
		
		this.imageSourcePictureOff = 'button-pic-off.gif';
		this.imageSourcePictureOn = 'button-pic-on.gif';
		this.imageSourcePictureOver = 'button-pic-over.gif';
		this.imageSourceTextOff = 'button-text-off.gif';
		this.imageSourceTextOn = 'button-text-on.gif';
		this.imageSourceTextOver = 'button-text-over.gif';
		this.imageSourceTextPictureOff = 'button-textpic-off.gif';
		this.imageSourceTextPictureOn = 'button-textpic-on.gif';
		this.imageSourceTextPictureOver = 'button-textpic-over.gif';
		this.imageSourceSearchOff = 'button-search-off.gif';
		this.imageSourceSearchOn = 'button-search-on.gif';
		this.imageSourceSearchOver = 'button-search-over.gif';
		this.imageSourceRSSOff = 'button-rss-off.gif';
		this.imageSourceRSSOn = 'button-rss-on.gif';
		this.imageSourceMinus = 'minus.gif';
		this.imageSourcePlus = 'plus.gif';
		this.controlPanelMode = 'textpic';
		this.buttonTextPic = null;
		this.buttonPicture = null;
		this.buttonText = null;
		
		this.fxFadeInTime = 500;
		this.fxFadeOutTime = 300;
		
		for(var i = 0; i < events.length; i++) {
			if(events[i].date < this.archiveDate || events[i].archive) this.eventsArchive.push(new EVENT(this.eventID, i, events[i].date, events[i].headline, events[i].text, events[i].image, true));
			else this.events.push(new EVENT(this.eventID, i, events[i].date, events[i].headline, events[i].text, events[i].image, false));
		}
	},
	
	showEventSet: function() {
		//if(this.controlPanelWrapper == null) this.showControlPanel();
		if(this.eventElement == null) {
			this.eventElement = new Element('div', {'id':this.eventID});
			$(this.eventWrapperID.toString()).grab(this.eventElement);
		}
		this.showEvents();
		this.showArchiveLink((this.events.length) * this.fxFadeInTime);
	},
	
	hideEventSet: function() {
		var _self = this;
		new Fx.Morph(this.eventElement, {duration:300}).start({'opacity':0}).chain(function() {
			_self.eventElement.set('html', '');
			if(Browser.Engine.trident4) _self.eventElement.setStyles({'height':450, 'opacity':1});
			else _self.eventElement.setStyles({'height':'auto', 'opacity':1});
		});
	},
	
	showEvents: function() {
		for(var i = 0; i < this.events.length; i++) {
			switch(this.controlPanelMode.toString()) {
				case 'pic':
					this.events[i].showAsPicture((i + 1) * this.fxFadeInTime);
					break;
				case 'text':
					this.events[i].showAsText((i + 1) * this.fxFadeInTime);
					break;
				default:
					this.events[i].showAsTextPicture((i + 1) * this.fxFadeInTime);
					break;
			}
		}
	},
	
	hideEvents: function() {
		var x = 1;
		for(var i = this.events.length - 1; i >= 0; i--) this.events[i].hide((x++) * this.fxFadeOutTime);
	},
	
	showControlPanel: function() {
		var _self = this;
		this.controlPanelWrapper = new Element('div', {'id':this.controlPanelWrapperID}).setStyle('opacity', 0);
		
		this.buttonTextPic = new Element('img', {'alt':'Text mit Bild', 'class':'controlPanelButton', 'src':this.imageDir + this.imageSourceTextPictureOn, 'title':'Text mit Bild'});
		this.buttonTextPic.addEvent('mouseover', function() {
			if(_self.controlPanelMode != 'textpic') this.src = _self.imageDir + _self.imageSourceTextPictureOver;
		});
		this.buttonTextPic.addEvent('mouseout', function() {
			if(_self.controlPanelMode != 'textpic') this.src = _self.imageDir + _self.imageSourceTextPictureOff;
		});
		this.buttonTextPic.addEvent('click', function() {
			if(_self.controlPanelMode != 'textpic') {
				_self.controlPanelMode = 'textpic';
				this.src = _self.imageDir + _self.imageSourceTextPictureOn;
				if(_self.buttonPicture != null) _self.buttonPicture.src = _self.imageDir + _self.imageSourcePictureOff;
				if(_self.buttonText != null) _self.buttonText.src = _self.imageDir + _self.imageSourceTextOff;
				
				_self.hideEventSet();
				(function() {
					_self.showEventSet();
				}).delay(400);
			}
		});
		
		this.buttonPicture = new Element('img', {'alt':'Bild mit \u00dcberschrift', 'class':'controlPanelButton', 'src':this.imageDir + this.imageSourcePictureOff, 'title':'Bild mit \u00dcberschrift'});
		this.buttonPicture.addEvent('mouseover', function() {
			if(_self.controlPanelMode != 'pic') this.src = _self.imageDir + _self.imageSourcePictureOver;
		});
		this.buttonPicture.addEvent('mouseout', function() {
			if(_self.controlPanelMode != 'pic') this.src = _self.imageDir + _self.imageSourcePictureOff;
		});
		this.buttonPicture.addEvent('click', function() {
			if(_self.controlPanelMode != 'pic') {
				_self.controlPanelMode = 'pic';
				if(_self.buttonTextPic != null) _self.buttonTextPic.src = _self.imageDir + _self.imageSourceTextPictureOff;
				this.src = _self.imageDir + _self.imageSourcePictureOn;
				if(_self.buttonText != null) _self.buttonText.src = _self.imageDir + _self.imageSourceTextOff;
			}
		});
		
		this.buttonText = new Element('img', {'alt':'Text ohne Bild', 'class':'controlPanelButton', 'src':this.imageDir + this.imageSourceTextOff, 'title':'Text ohne Bild'});
		this.buttonText.addEvent('mouseover', function() {
			if(_self.controlPanelMode != 'text') this.src = _self.imageDir + _self.imageSourceTextOver;
		});
		this.buttonText.addEvent('mouseout', function() {
			if(_self.controlPanelMode != 'text') this.src = _self.imageDir + _self.imageSourceTextOff;
		});
		this.buttonText.addEvent('click', function() {
			if(_self.controlPanelMode != 'text') {
				_self.controlPanelMode = 'text';
				if(_self.buttonTextPic != null) _self.buttonTextPic.src = _self.imageDir + _self.imageSourceTextPictureOff;
				if(_self.buttonPicture != null) _self.buttonPicture.src = _self.imageDir + _self.imageSourcePictureOff;
				this.src = _self.imageDir + _self.imageSourceTextOn;
			}
		});
		
		this.controlPanelWrapper.grab(this.buttonTextPic).grab(this.buttonPicture).grab(this.buttonText);
		if($(this.eventWrapperID.toString())) $(this.eventWrapperID.toString()).grab(this.controlPanelWrapper);
		new Fx.Morph(this.controlPanelWrapper, {duration:300}).start({'opacity':1});
	},
	
	showArchiveLink: function(fxTime) {
		var _self = this;
		if(this.eventsArchive.length > 0) {
			this.archiveLinkWrapper = new Element('div').setStyles({
				'opacity':0,
				'padding-top':20
			});
			this.archiveLink = new Element('span', {'class':'link-on'});
			this.archiveLinkImage = new Element('img', {'src':this.imageDir + this.imageSourcePlus}).setStyle('margin-bottom', '-2px');
			this.archiveLinkText = new Element('span').set('html', '&nbsp;Archiv f&uuml;r Events aufklappen');
			this.archiveLink.grab(this.archiveLinkImage).grab(this.archiveLinkText);
			
			this.archiveLink.addEvent('click', function() {
				if(_self.archiveOpened) {
					_self.archiveOpened = false;
					_self.hideArchive();
				} else {
					_self.archiveOpened = true;
					_self.showArchive();
				}
			});
			
			this.archiveLinkWrapper.grab(this.archiveLink);
			this.eventElement.grab(this.archiveLinkWrapper);
			new Fx.Morph(this.archiveLinkWrapper, {duration:fxTime}).start({'opacity':1});
		}
	},
	
	showArchive: function() {
		this.archiveLinkImage.src = this.imageDir + this.imageSourceMinus;
		this.archiveLinkText.set('html', '&nbsp;Archiv f&uuml;r Events zuklappen');
		for(var i = 0; i < this.eventsArchive.length; i++) this.eventsArchive[i].showAsTextPicture((i + 1) * this.fxFadeInTime);
		$(this.eventWrapperID.toString()).setStyle('height', 'auto').setStyle('height', $(this.eventWrapperID.toString()).getHeight());
	},
	
	hideArchive: function() {
		var _self = this;
		var x = 1;
		for(var i = this.eventsArchive.length - 1; i >= 0; i--) this.eventsArchive[i].hide((x++) * this.fxFadeOutTime);
		(function() {
			_self.archiveLinkImage.src = _self.imageDir + _self.imageSourcePlus;
			_self.archiveLinkText.set('html', '&nbsp;Archiv f&uuml;r Events aufklappen');
			$(_self.eventWrapperID.toString()).setStyle('height', 'auto')
			var height = $(_self.eventWrapperID.toString()).getHeight();
			if(Browser.Engine.trident4 && height < 430) height = 430;
			$(_self.eventWrapperID.toString()).setStyle('height', height);
		}).delay(x * this.fxFadeOutTime);
	},
	
	getArchiveDate: function() {
		var archive = new Date(new Date().getFullYear(), new Date().getMonth() - this.countMonthForArchive + 1, new Date().getDate());
		var archiveYear = archive.getFullYear();
		var archiveMonth = (archive.getMonth() < 10 ? '0' + archive.getMonth() : archive.getMonth());
		var archiveDay = (archive.getDate() < 10 ? '0' + archive.getDate() : archive.getDate());
		var archiveDate = archiveYear + '-' + archiveMonth + '-' + archiveDay;
		
		return archiveDate;
	}
});