/**
 * @license
 * jQuery Tools 1.2.5 Slideshow - Extend it.
 *
 * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
 *
 * http://flowplayer.org/tools/tabs/slideshow.html
 *
 * Since: September 2009
 * Date:    Wed Sep 22 06:02:10 2010 +0000
 */
(function($) {

	var tool;

	tool = $.tools.tabs.slideshow = {

		conf: {
			next: '.forward',
			prev: '.backward',
			disabledClass: 'disabled',
			autoplay: false,
			autopause: true,
			interval: 3000,
			clickable: true,
			api: false
		}
	};

	function Slideshow(root, conf) {

		var self = this,
			 fire = root.add(this),
			 tabs = root.data("tabs"),
			 timer,
			 stopped = true;


		// next / prev buttons
		function find(query) {
			var el = $(query);
			return el.length < 2 ? el : root.parent().find(query);
		}

		var nextButton = find(conf.next).click(function() {
			tabs.next();
		});

		var prevButton = find(conf.prev).click(function() {
			tabs.prev();
		});


		// extend the Tabs API with slideshow methods
		$.extend(self, {

			// return tabs API
			getTabs: function() {
				return tabs;
			},

			getConf: function() {
				return conf;
			},

			play: function() {

				// do not start additional timer if already exists
				if (timer) { return self; }

				// onBeforePlay
				var e = $.Event("onBeforePlay");
				fire.trigger(e);
				if (e.isDefaultPrevented()) { return self; }


				// construct new timer
				timer = setInterval(tabs.next, conf.interval);
				stopped = false;

				// onPlay
				fire.trigger("onPlay");

				return self;
			},

			pause: function() {

				if (!timer) { return self; }

				// onBeforePause
				var e = $.Event("onBeforePause");
				fire.trigger(e);
				if (e.isDefaultPrevented()) { return self; }

				timer = clearInterval(timer);

				// onPause
				fire.trigger("onPause");

				return self;
			},

			// when stopped - mouseover won't restart
			stop: function() {
				self.pause();
				stopped = true;
			}

		});

		// callbacks
		$.each("onBeforePlay,onPlay,onBeforePause,onPause".split(","), function(i, name) {

			// configuration
			if ($.isFunction(conf[name]))  {
				$(self).bind(name, conf[name]);
			}

			// API methods
			self[name] = function(fn) {
				return $(self).bind(name, fn);
			};
		});


		/* when mouse enters, slideshow stops */
		if (conf.autopause) {
			tabs.getTabs().add(nextButton).add(prevButton).add(tabs.getPanes()).hover(self.pause, function() {
				if (!stopped) { self.play(); }
			});
		}

		if (conf.autoplay) {
			self.play();
		}

		if (conf.clickable) {
			tabs.getPanes().click(function()  {
				tabs.next();
			});
		}

		// manage disabling of next/prev buttons
		if (!tabs.getConf().rotate) {

			var disabled = conf.disabledClass;

			if (!tabs.getIndex()) {
				prevButton.addClass(disabled);
			}

			tabs.onBeforeClick(function(e, i)  {
				prevButton.toggleClass(disabled, !i);
				nextButton.toggleClass(disabled, i == tabs.getTabs().length -1);
			});
		}
	}

	// jQuery plugin implementation
	$.fn.slideshow = function(conf) {

		// return existing instance
		var el = this.data("slideshow");
		if (el) { return el; }

		conf = $.extend({}, tool.conf, conf);

		this.each(function() {
			el = new Slideshow($(this), conf);
			$(this).data("slideshow", el);
		});

		return conf.api ? el : this;
	};

})(jQuery);


