/*
 * Mootools Class for element Cross-fading.
 * 
 */
var PROMO = new Class({
	
	Implements: [Options],  
	 
	options: {
		elementTag: 'div.rail-promo',
		buttonTag: 'a',
		duration: 550,
		interval: 5000,
		pauseInterval: 13000
	},
	
	initialize: function(container, buttons, options){
		this.setOptions(options);	
		
		if ($type(container) == 'element')
			this.container = container;
		else
			this.container = $(document).getElement(container);
			
		if (!this.container) 
			return false;

		if ($type(buttons) == 'element')
			this.buttons = buttons;
		else
			this.buttons = $(document).getElements(buttons);

		this.messages = this.container.getElements(this.options.elementTag);
		this.number_of_messages = this.messages.length;
		
		if (this.number_of_messages < 2) {			
			return false;
		}
		
		this.initMessages();
		this.initButtons();
		
		if (location.hash.indexOf('#') == 0) {
			start_hash = location.hash.substr(1);
		} else {
			start_hash = location.hash;
		}
		
		if (this.hashes[start_hash])
			this.current_message = this.hashes[start_hash];			
		else
			this.current_message = 0;
			
		this.highlightButton();
				
		this.startLoop();
	},
	
	initMessages: function() {
		
		this.positions = new Array();
		this.hashes = new Array();
			
		this.messages.each(function(message, index) {
			
			this.hashes[message.id] = index;	
			this.positions[index] = message.getPosition(this.container);			
			
		}, this);
		
		this.scroller = new Fx.Scroll(this.container, {
			transition: Fx.Transitions.Expo.easeInOut,
			duration: this.options.duration,
			link: 'cancel',
			onStart: function() {
				this.highlightButton.delay(this.options.duration / 2, this);
			}.bind(this)
		});
			
	},
	
	initButtons: function() {
		this.buttons.each(function(button, index) {
			button.addEvent('click', this.skipToMessage.bindWithEvent(this, index));			
		}, this);		
	},
	
	highlightButton: function() {
		this.buttons.each(function(button, index) {
			button.removeClass('active');
		}, this);	
		this.buttons[this.current_message].addClass('active');
	},	
	
	startLoop: function () {
		if (this.timer) $clear(this.timer);
		this.timer = this.rotateMessage.periodical(this.options.interval, this);
	},
	
	pauseLoop: function() {
		if (this.timer) $clear(this.timer);
		this.timer = this.startLoop.delay(this.options.pauseInterval, this);
	},
	
	rotateMessage: function() {
		if (this.current_message < this.number_of_messages - 1) 
			this.current_message++;
		else
			this.current_message = 0;
		this.scroller.start(this.positions[this.current_message].x, this.positions[this.current_message].y);
	},
	
	skipToMessage: function(e, index) {
		e.preventDefault();
		this.pauseLoop();
		this.current_message = index;
		this.scroller.start(this.positions[index].x, this.positions[index].y);		
	}	
});


window.addEvent('domready', function() {
	new PROMO('#rail-carousel-outer', '#rail-carousel-nav a');
});