/*
---
script: RegioAnimation_Carousel.js
description: The Scroller scrolls smoothly through the given Elements
copyright: Copyright (c) 2011 REGIOCAST
authors: [Marcel Domke]

requires: 
  core:1.3.0: 
  - Class.Extras
  - Element.Event
  - Fx.Scroll
  - Array
provides: [Carousel]
browsers: FF,IE
...
*/

var RegioAnimation_Carousel = new Class({
	
	Extends: RegioAnimation,

	options: {
		highlight_color: ['#ffffff','#D3E3F0']
	},
	
	scroll_element_counter: 0,

	initialize: function(element, options) {
		this.setOptions(options);
		
		this.btn_up = this.options.buttons.shift();
		this.btn_down = this.options.buttons.pop();
		this.btn_up.removeEvents();
		this.btn_down.removeEvents();

		this.scroll_container = element.getElement('.scroller_container');
		this.scroll_container_element = this.scroll_container.getElements('.slide');

		this.Fx_Scroll = new Fx.Scroll(this.scroll_container);

		this.btn_up.addEvents({
			'click': function (event) {
				
				if( this.btn_up.hasClass('inactive')) 
				{
					event.stop();
				} 
				else 
				{
					this._scroll('down');
					
					this.btn_down.removeClass('inactive');
					this.btn_up.removeClass('inactive');
					
					if( this.scroll_element_counter == 0 ) 
					{
						this.btn_up.addClass('inactive');
						this.btn_down.removeClass('inactive');
					}
				}
				
			}.bind(this)
		});
		
		this.btn_down.addEvents({
			'click': function (event) {
				
				if( this.btn_down.hasClass('inactive') ) 
				{
					event.stop();
				} 
				else 
				{
					this._scroll('up');
					
					this.btn_down.removeClass('inactive');
					this.btn_up.removeClass('inactive');
					
					if( this.scroll_element_counter == (this.scroll_container_element.length-1) ) 
					{
						this.btn_up.removeClass('inactive');
						this.btn_down.addClass('inactive');
					}
				}
				
			}.bind(this)
		});
	},
	
	_scroll: function ( direction ) {
		
		if( direction == 'up' ) 
		{
			this.scroll_element_counter++;
		} 
		else if( direction == 'down' ) 
		{
			this.scroll_element_counter--;
		}
		
		if( this.scroll_element_counter < 0 ) 
			this.scroll_element_counter = 0;
		if( this.scroll_element_counter >= (this.scroll_container_element.length-1) ) 
			this.scroll_element_counter = (this.scroll_container_element.length-1);

		this.Fx_Scroll.toElement(
			this.scroll_container_element[this.scroll_element_counter]
		);
		
		this.scroll_container_element[this.scroll_element_counter].highlight(
			this.options.highlight_color[0],this.options.highlight_color[1]	
		);
		
	}
	
});
