NewsTicker = Class.create({
	initialize: function (){

		this.FadeInDuration = 1500;
		this.FadeOutDuration = 1500;
		this.switchDelayDuration = 5000;

		this.ticker = $('newsticker');
		var self =  this;
		//this.ticker.observe('click', function(event){
		//	self.onClick(event);
		//});
		this.content = [
			{
				wrapper: $('newsticker_content_wrapper1'),
				title: $('newsticker_content_title1'),
				date: $('newsticker_content_date1')
			},
			{
				wrapper: $('newsticker_content_wrapper2'),
				title: $('newsticker_content_title2'),
				date: $('newsticker_content_date2')
			}
		];
		this.currentContent = 1;
		this.lastContent = 0;

		this.position = -1;
		this.loadData();

	},
	loadData: function(){
		this.data = eval($('newsticker_data').innerHTML);
		this.onDataLoaded();
	},
	onDataLoaded: function(){
		this.next();
	},
	next: function(){
		this.lastPosition = this.position++;
		if(this.position >= this.data.length){
			this.position = 0;
		}

		var t = this.lastContent;
		this.lastContent = this.currentContent;
		this.currentContent = t;

		this.content[this.currentContent].title.innerHTML = this.data[this.position].title;
		this.content[this.currentContent].date.innerHTML = this.data[this.position].date;
		this.fadeOut();
		this.fadeIn();
	},
	prev: function(){
		this.lastPosition = this.position--;
		if(this.position < 0){
			this.position = this.data.length-1;
		}

		var t = this.lastContent;
		this.lastContent = this.currentContent;
		this.currentContent = t;

		this.content[this.currentContent].title.innerHTML = this.data[this.position].title;
		this.content[this.currentContent].date.innerHTML = this.data[this.position].date;
		this.fadeOut();
		this.fadeIn();
	},
	fadeIn: function(id){
		var self = this;
		clearTimeout(self.toId);
		var fadeInAnim = new Animator({
			duration: this.FadeInDuration,
			onComplete: function (){
				self.toId = setTimeout(
					function(){
						if(self.data.length > 1)
							self.next();
					},
					self.switchDelayDuration
				);
			}
		});
		fadeInAnim.addSubject(
			new NumericalStyleSubject(
    			this.content[this.currentContent].wrapper,
    			'opacity',
    			0,
    			1
    		)
    	);
    	fadeInAnim.play();
	},
	fadeOut: function(id){
		var fadeOutAnim = new Animator({
			duration: this.FadeOutDuration
		});
		fadeOutAnim.addSubject(
			new NumericalStyleSubject(
    			this.content[this.lastContent].wrapper,
    			'opacity',
    			1,
    			0
    		)
    	);
    	fadeOutAnim.play();
	},
	onClick: function(event){
		document.location = 'news.php?id=' + this.data[this.position].id;
	}
});

news_ticker = new NewsTicker();



