/*
 * Mootools Class for element Cross-fading.
 * 
 */
var ContentSlider = new Class({
	
	Implements: [Options],  
	 
	options: {
		elementTag: 'div',
		buttonTag: 'a',
		step: 1,
		duration: 750
	},
	
	initialize: function(container, btn_prev, btn_next, options){
		this.setOptions(options);	
		
		// get the container
		if ($type(container) == 'element')
			this.container = container;
		else
			this.container = $(String(container));
		
		// no container -- return	
		if (!this.container) 
			return false;
			
		// get the prev/next buttons
		if ($type(btn_prev) == 'element')
			this.btn_prev = btn_prev;
		else
			this.btn_prev = $(String(btn_prev));
			
		if ($type(btn_next) == 'element')
			this.btn_next = btn_next;
		else
			this.btn_next = $(String(btn_next));

		// get the messages
		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;
		}
		
		this.current_message = 0;
			
		this.highlightButton();
		
	},
	
	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: this.highlightButton.bind(this)
		});
			
	},
	
	initButtons: function() {
		this.btn_prev.addEvent('click', this.prevMessage.bindWithEvent(this));	
		this.btn_next.addEvent('click', this.nextMessage.bindWithEvent(this));			
		this.btn_prev.setStyle("opacity",0.3);
	},
	
	highlightButton: function() {
		if (this.current_message > 0) {
			if (this.btn_prev.getStyle("opacity") <= 0.5) {
				this.btn_prev.fade("in");
			}
		} else {
			if (this.btn_prev.getStyle("opacity") == 1) {
				this.btn_prev.fade(0.3);
			}
		}
		if ((this.current_message + this.options.step) < this.number_of_messages) {
			if (this.btn_next.getStyle("opacity") <= 0.5) {
				this.btn_next.fade("in");
			}		
		} else {
			if (this.btn_next.getStyle("opacity") == 1) {
				this.btn_next.fade(0.3);
			}
		}
	},		
	
	nextMessage: function(e) {
		e.preventDefault();
		if ((this.current_message + this.options.step) < this.number_of_messages) {
			this.current_message = this.current_message + this.options.step;
			this.scroller.start(this.positions[this.current_message].x, this.positions[this.current_message].y);
		}		
	},
	
	prevMessage: function(e) {
		e.preventDefault();
		if (this.current_message > 0) {
			this.current_message = this.current_message - this.options.step;
			this.scroller.start(this.positions[this.current_message].x, this.positions[this.current_message].y);
		}		
	}	
});


