var newsflashClass = Class.create({

	initialize : function(id, intervall, newsflashJSON) {
		this.id = id;
		this.newsflashWrap = 'wrapNewsflash' + id;
		this.newsflashGo = 'go';
		this.newsflashInterval = intervall;
		this.newsflashTimer = false;
		this.newsflashNext = 0;
		this.newsflashJSON = newsflashJSON;
		this.newsflashAjaxPath = SERVERNAME + 'template/ct_newsflash/ajax.php';
		eval("this.NFjsonVar = " + newsflashJSON);
		this.buildNewsflashPointer();
		this.newsflash();
	},
	newsflash : function() {
		if(this.newsflashTimer)
			clearTimeout(this.newsflashTimer);

		if((this.newsflashNext + 1) > this.NFjsonVar.length)
			this.newsflashNext = 0;

		if(this.newsflashGo == 'go') {
			var nextID = this.NFjsonVar[this.newsflashNext].newsflashID;
			
			
			var opt = {
				method : 'post',
				parameters : {
					NTID : nextID
				},
				onSuccess : this.updateBox(this.id, this.newsflashWrap, this.newsflashNext)
			}
			new Ajax.Request(this.newsflashAjaxPath, opt);
		}
		
		if(this.NFjsonVar.length > 1)
			this.startTimeout();
		this.newsflashNext++;
	},
	updateBox : function(id, wrapper, nextflash) {
		
		if($$('#'+wrapper+' .newsflashBox').length == 1) {

			return function(response) {
				var activBox = $$('#'+wrapper+' div.newsflashBox')[0];
				activBox.setStyle({
					zIndex : 9
				});
				var div = new Element('div', {
					'class' : 'newsflashBox'
				}).setStyle({
					zIndex : 1
				});

				var nextbox = $(wrapper).appendChild(div);
				nextbox.update(response.responseText);

				new Effect.Opacity(activBox, {
					from : 1,
					to : 0,
					duration : 1.5,
					afterFinish : function() {
						activBox.remove();
					}
				});
				$$('#nfpID' + id + ' li').each(function(s) {
					s.removeClassName('active');
				});
				$$('#nfpID'+id+' li')[nextflash].className = 'active';
			}
		}
	},
	startTimeout : function() {
		var self = this;
		this.newsflashTimer = setTimeout(function() {
			self.newsflash();
		}, this.newsflashInterval);
	},
	buildNewsflashPointer : function() {
		var objBody = $('wrapNewsflashPointer' + this.id);
		var ul = new Element('ul', {
			'class' : 'newsflashPointer',
			id : 'nfpID' + this.id
		});
		for( i = 1; i <= this.NFjsonVar.length; i++) {
			ul.appendChild(Builder.node('li')).observe('click', this.newsflashSetActive.bind(this));
		}
		objBody.appendChild(ul);
	},
	newsflashSetActive : function(event) {
		this.clickObj = Event.element(event);
		this.newsflashNext = this.clickObj.previousSiblings().size();
		this.newsflash();
	},
	NFGoStop : function(m, d) {
		this.newsflashGo = m;
		if(m == 'stop') {
		} else {
		}
	}
});

