/**
	changelog 04.08.09: updated to mootools 1.2; removed pause and play controls; added container option
*/
var CE_Newsticker_Controller = new Class({
    options: {
		id: null,
		container: null,
		autoplay: false,
		fx_duration: 250,
		transition_interval: 5000
    },

    initialize: function(options) {
		this.setOptions(options);
		
		this.interval = null;
		this.container = $(this.options.container);
		this.headlines = this.container.getElements("li");
		this.current_headline = 0;

		if (this.options.autoplay === true) { this.play(); }
	},
	
	transition: function(from, to) {
		var f = new Fx.Tween(from, { property: "opacity", duration: this.options.fx_duration, onComplete:function(elem) { elem.style.display = "none"; } });
		var t = new Fx.Tween(to, { property: "opacity", duration: this.options.fx_duration });
		
		f.start(1, 0).chain(
			function() {
				$(to).setStyles({ opacity: 0, display:"block" });
				t.start(0, 1);
			}
		);
	},
	
	do_headline: function(which) {
		if (this.current_headline !== which) {
			this.transition(this.headlines[this.current_headline], this.headlines[which]);
			this.current_headline = which;
		}
	},

	next: function() {
		if ($defined(this.interval)) { // reset the timer
			this.clear_interval();
			this.set_interval() 
		}
		
		if (this.current_headline == (this.headlines.length - 1)) {
			this.do_headline(0);
		} else {
			this.do_headline(this.current_headline + 1);
		}
	},
	
	previous: function() {
		if ($defined(this.interval)) { // reset the timer
			this.clear_interval();
			this.set_interval() 
		}
		
		if (this.current_headline == 0) {
			this.do_headline(this.headlines.length - 1);
		} else {
			this.do_headline(this.current_headline - 1);
		}
	},
	
	play: function() {
		if (this.paused === true) { this.next(); this.paused = false; }
		this.set_interval();
		if (this.options.docontrols) {
			this.transition(this.play_link, this.pause_link);
		}
	},
	
	pause: function() {
		this.clear_interval();
		if (this.options.docontrols) {
			this.transition(this.pause_link, this.play_link);
		}
		this.paused = true;
	},
	
	set_interval: function () {
		this.interval = setInterval(this.options.id + ".next()", this.options.transition_interval);
	},
	
	clear_interval: function() {
		clearInterval(this.interval);
		this.interval = null;
	}
});

CE_Newsticker_Controller.implement(new Options);

