/*****************************************************************************
 *
 * Copyright © 2009 Simon Robb
 *
 * All rights reserved
 *
 * This is a dramatically simplified version of the original Carousel script!
 *
 ****************************************************************************/

(function ($) {
	$.fn.Carousel = function (params) {
		var defaults = {
			initialIndex: 0,			// The panel to start on by index
			autoCycle: true,			// Begin scrolling on init
			cycleInterval: 8000,		// How long between scrolls
			panelWidth: '1000',			// Width of a panel
			panelHeight: '100',			// Height of a panel
			direction: 'horizontal',	// Direction of scroll
			panelsVisible: 1,			// Number of panels visible at once
			length: 700					// Length of animation
        };
        
        var settings = {};
        settings = $.extend({}, defaults, params);
		
        // return this;
        return this.each(function (i) {
            var $$ = $(this);
			settings.element = $$;

		   	var car = new Carousel (settings);
		
			// Store the carousel for public interaction
			$$.data ('carousel', car);
		});
	};
	
	/**
	 * The class constructor
	 * @constructor
	 */
	window.Carousel = function (settings) {
		/**
		 * Class settings
		 */
		this._settings = settings;
		
		/**
		 * Member variables
		 */
		this._panels = this._settings.element.find ('li');
		this._index = 0;
		this._value = 0;
		this._count = this._panels.length;
		this._isHovering = false;
		
		// Ensure that overflow is hidden
		this._settings.element.css ('overflow', 'hidden');
		
		// Functionality
		var car = this;
		if (this._settings.nextButton) this._settings.nextButton.click (function () {car.next(); car.stop ()});
		if (this._settings.prevButton) this._settings.prevButton.click (function () {car.previous(); car.stop ()});

		// Keep track of whether the cursor is over the panels
		this._panels.hover (
			function () { car._isHovering = true; },
			function () { car._isHovering = false; }
		);
		
		// Begin cycling if necessary
		if (this._settings.autoCycle)
			this.play ();
	};
	
	Carousel.prototype = {
		next: function () {
			var car = this;
			var firstItem = $('li:first-child', this._panels.parent()).eq (0);

			var params = { 'marginTop': '-=' + this._settings.panelHeight + 'px' };

			firstItem
				.stop ()
				.animate (
					params,
					this._settings.length,
					function () {
						// By the time this callback occurs, the index will have been incremented
							firstItem
								.css ('marginTop', 0)
								.appendTo (car._panels.parent ());
						}
					);
		},
		
		previous: function () {
			
		},
		
		first: function () {
			
		},
		
		last: function () {
			
		},
		
		play: function () {
			var car = this;
			
			// Reset if necessary
			this.stop ();
				
			this._timer = setInterval (function () { if (!car._isHovering) car.next (); }, this._settings.cycleInterval);
		},
		
		stop: function () {
			if (this._timer)
				clearInterval (this._timer);
		},
		
		getIndex: function () {
			return this._index;
		},
		
		getValue: function () {
			return this._value;
		},
		
		recount: function () {
			this._count = $('li:visible', this._panels.parent ()).length;
			
			// If there are fewer items than the number being shown
			if (this._count < this._settings.panelsVisible)
				this._count = this._settings.panelsVisible;
				
			return this._count;
		}
	};
})(jQuery);

