var NEWSSET = new Class({
	initialize: function(news) {
		this.imageDir = './images/content/';
		this.contentWrapperID = 'contentWrapper';
		this.newsWrapperID = 'newsContent';
		this.controlPanelWrapper = null;
		this.controlPanelWrapperID = 'controlPanel';
		this.newsElement = null;
		this.newsID = 'news';
		this.news = new Array();
		this.newsArchive = 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 < news.length; i++) {
			if(news[i].date < this.archiveDate || news[i].archive) this.newsArchive.push(new NEWS(this.newsID, i, news[i].date, news[i].headline, news[i].text, news[i].image, true));
			else this.news.push(new NEWS(this.newsID, i, news[i].date, news[i].headline, news[i].text, news[i].image, false));
		}
	},
	
	showNewsSet: function() {
		//if(this.controlPanelWrapper == null) this.showControlPanel();
		if(this.newsElement == null) {
			this.newsElement = new Element('div', {'id':this.newsID});
			$(this.newsWrapperID.toString()).grab(this.newsElement);
		}
		this.showNews();
		this.showArchiveLink((this.news.length) * this.fxFadeInTime);
	},
	
	hideNewsSet: function() {
		var _self = this;
		new Fx.Morph(this.newsElement, {duration:300}).start({'opacity':0}).chain(function() {
			_self.newsElement.set('html', '');
			if(Browser.Engine.trident4) _self.newsElement.setStyles({'height':450, 'opacity':1});
			else _self.newsElement.setStyles({'height':'auto', 'opacity':1});
		});
	},
	
	showNews: function() {
		for(var i = 0; i < this.news.length; i++) {
			switch(this.controlPanelMode.toString()) {
				case 'pic':
					this.news[i].showAsPicture((i + 1) * this.fxFadeInTime);
					break;
				case 'text':
					this.news[i].showAsText((i + 1) * this.fxFadeInTime);
					break;
				default:
					this.news[i].showAsTextPicture((i + 1) * this.fxFadeInTime);
					break;
			}
		}
	},
	
	hideNews: function() {
		var x = 1;
		for(var i = this.news.length - 1; i >= 0; i--) this.news[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.hideNewsSet();
				(function() {
					_self.showNewsSet();
				}).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.newsWrapperID.toString())) $(this.newsWrapperID.toString()).grab(this.controlPanelWrapper);
		new Fx.Morph(this.controlPanelWrapper, {duration:300}).start({'opacity':1});
	},
	
	showArchiveLink: function(fxTime) {
		var _self = this;
		if(this.newsArchive.length > 0) {
			this.archiveLinkWrapper = new Element('div').setStyles({
				'opacity':0,
				'padding':'20px 0px 0px 0px'
			});
			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 News 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.newsElement.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 News zuklappen');
		for(var i = 0; i < this.newsArchive.length; i++) this.newsArchive[i].showAsTextPicture((i + 1) * this.fxFadeInTime);
		$(this.newsWrapperID.toString()).setStyle('height', 'auto').setStyle('height', $(this.newsWrapperID.toString()).getHeight());
	},
	
	hideArchive: function() {
		var _self = this;
		var x = 1;
		for(var i = this.newsArchive.length - 1; i >= 0; i--) this.newsArchive[i].hide((x++) * this.fxFadeOutTime);
		(function() {
			_self.archiveLinkImage.src = _self.imageDir + _self.imageSourcePlus;
			_self.archiveLinkText.set('html', '&nbsp;Archiv f&uuml;r News aufklappen');
			$(_self.newsWrapperID.toString()).setStyle('height', 'auto')
			var height = $(_self.newsWrapperID.toString()).getHeight();
			if(Browser.Engine.trident4 && height < 430) height = 430;
			$(_self.newsWrapperID.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;
	}
});