/**
	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,
		//docontrols: true,
		autoplay: false,
		fx_duration: 250,
		transition_interval: 5000
    },

    initialize: function(options) {
		this.setOptions(options);
		
		this.interval = null;
		this.container = $(this.options.container);
		//this.controls = $("newsControls");
		this.headlines = this.container.getElements("li");
		this.current_headline = 0;
		
		/*if ($chk(this.controls) && this.options.docontrols) {
			// quick and dirty with innerHTML :)
			this.controls.innerHTML = "<li><a href=\"javascript:" + this.options.id + ".previous()\" id=\"prevStory\" title=\"Previous Story\">Previous Story</a></li>";
			this.controls.innerHTML += "<li><a href=\"javascript:" + this.options.id + ".play()\" id=\"play\" title=\"Play\">Play</a><a href=\"javascript:" + this.options.id + ".pause()\" id=\"pause\" title=\"Pause\">Pause</a></li>";
			this.controls.innerHTML += "<li><a href=\"javascript:" + this.options.id + ".next()\" id=\"nextStory\" title=\"Next Story\">Previous Story</a></li>";
			
			this.play_link = $("play");
			this.pause_link = $("pause");
		}*/
		
		if (this.options.autoplay === true) { this.play(); }
		//var s = new Fx.Style(this.headlines[0], "opacity", { duration:this.options.fx_duration }).start(0, 1);
		this.fx = new Fx.Tween(this.headlines[0], { property: "opacity", duration: this.options.fx_duration }).start(0, 1);
	},
	
	transition: function(from, to) {
		//var f = new Fx.Style(from, "opacity", { duration:this.options.fx_duration, onComplete:function(elem) { elem.style.display = "none"; } });
		//var t = new Fx.Style(to, "opacity", { duration:this.options.fx_duration });
		
		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);
