/*
|--------------------------------------------------------------------------
| Food TV > Front-End > Carousel
|--------------------------------------------------------------------------
|
| @package		Food TV
| @subpackage	Frontend
| @company		Scripps Interactive.
| @phone		(212) 741-0700 x112
| @fax			(212) 741-4700
| @author		Joey Avino
| @email		javino@gabriels.net
| @link			http://www.gabriels.net
| @copyright	2008 Scripps Interactive.
| @requires		mootools.1.2.js
|
*/

/*
|--------------------------------------------------------------------------
| Food TV > Front-End > Carousel > Core JavaScript
|--------------------------------------------------------------------------
|
| NOTE: This file is setup to work without requiring edits.
| All items can be called from the external file.
|
*/

	
	/**
	* Function to activate the carousel once it's been initialized
	*
	* @name 	New Carousel Class
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino
	* @email	javino@gabriels.net
	*/
	var Carousel = new Class({
		
		/**
		* Function to activate the carousel
		*
		* @name 	Display Carousel
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	javino@gabriels.net
		*/
		Implements: Options, options: {

			item_width: 500,
			duration: 500,
			transition: Fx.Transitions.Pow.easeOut,
			next_btn_id: 'next',
			prev_btn_id: 'prev',
			event: 'click'

		},

		initialize: function(carousel, options) {
			
			this.setOptions(options);
			this.carousel = $(carousel); 
			if (this.carousel) {
				this.next_btn = $(this.options.next_btn_id);	
				this.prev_btn = $(this.options.prev_btn_id);

				this.max_margin = this.carousel.getChildren('div').length * this.options.item_width - this.options.item_width;
				this.position = parseInt(this.carousel.getStyle('left'));

				this.fx_carousel = new Fx.Tween(carousel, {

					property: 'left',
					duration: this.options.duration,
					transition: this.options.transition

				});

				this.next_btn.addEvent(this.options.event, this.next_item.bind(this));
				this.prev_btn.addEvent(this.options.event, this.previous_item.bind(this));
			}

		},





		/**
		* Function ran when the next button is used.
		* 
		* @name 	Next Item
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	javino@gabriels.net
		*/
		next_item: function() {

			if(this.position == -this.max_margin) {

				this.fx_carousel.start(0);
				this.position = 0;

			}

			else {
			  
				var new_position = this.position - this.options.item_width;
				this.fx_carousel.start(new_position);

				this.position = new_position;

			}
		},





		/**
		* Function ran when the previous button is used.
		* 
		* @name 	Previous Item
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	javino@gabriels.net
		*/
		previous_item: function() {

			if(this.position == 0) {

				this.fx_carousel.start(-this.max_margin);
				this.position = -this.max_margin;

			}

			else { 

				var new_position = this.position + this.options.item_width;
				this.fx_carousel.start(new_position);
				this.position = new_position;

			}
		}
	});