TeaserRotator = Class.create();

TeaserRotator.prototype = {

	acquirePreviousGroup: function(e) {
		Event.stop(e);
		if (this.totalGroups > 1) {
			var newGroup = (this.group > 1) ? this.group-1 : this.totalGroups;
			this.getNewTeasers(newGroup);
		}
		return false;
	},
		
	acquireNextGroup: function(e) {
		Event.stop(e);
		if (this.totalGroups > 1) {
			var newGroup = (this.group < this.totalGroups) ? this.group+1 : 1;
			this.getNewTeasers(newGroup);
		}
		return false;
	},
	
	getNewTeasers: function(group) {	
		new Effect.Fade(this.teasersGroup, {afterFinish: function() {
			this.indicator.on();
			var ajax = new Ajax.Updater(
				this.teasersGroup,
				'/o2tv/'+this.language+'/home/index.awf',
				{method: 'get',
					asynchronous: true,
					parameters: 'group='+group,
					onComplete: function() {
					
						this.indicator.off();
						this.group = group;
						initAnchorizers();
						
						new Effect.Appear(this.teasersGroup, {queue:'end'});
						
					}.bind(this),
					onFailure: function() {alert('Error: problem with TeaserRotator : getNewTeasers');},
					requestHeaders: ['cache-control','no-cache','pragma','no-cache']
				}
			);
		}.bind(this)});
	},

	initialize: function(rotator, firstGroup, totalGroups, language) {
		this.rotator = rotator;
		this.totalGroups = totalGroups;
		this.language = language;
		this.teasersGroup = $('teasersGroup');
		this.group = firstGroup;
		this.indicator = new AjaxIndicatorClass($('ajax-indicator'));
		
		document.getElementsByClassName('next', this.rotator).each(function(nextIndicator) {
			Event.observe(nextIndicator, 'click', this.acquireNextGroup.bindAsEventListener(this));
		}.bind(this));
		document.getElementsByClassName('prev', this.rotator).each(function(prevIndicator) {
			Event.observe(prevIndicator, 'click', this.acquirePreviousGroup.bindAsEventListener(this));
		}.bind(this));
	}
}

