/**
* Slider plugin.
*
* Usage:
*	$('...').kslider({..options..});
*
* @package Cosc
*/
(function($) {

	/**
	* @var bool Flag to test when the global, one-time init has taken place
	*/
	var GLOBAL_INIT_DONE = false;

	var TIMERS = {};

	/**
	* Plugin methods
	*/
	var methods = {

		/**
		* Initialise this instance.
		*
		* @param object Options
		* @return void
		*/
		init: function(options) {

			// Single, global init
			if(!GLOBAL_INIT_DONE) {

				// Page navigation handlers
				

				// Flag, so we never run this lot again
				GLOBAL_INIT_DONE = true;
			}

			// Options
			var settings = {
				height: null,
				autoStart: true,
				interval: 5000
			};
			if(options) {
				$.extend(settings, options);
			}

			// Instance init
			return this.each(function(i, el) {

				// Get options
				var $this = $(this);
				var data = {
					options: $.extend(true, {}, settings),
					id: "kslider" + Math.round(Math.random() * 99999999999)
				};
				TIMERS[data.id] = null;

				// Meta
				data.meta = {
					current: 0,
					size: $this.children().size()
				};
				var children = $this.children();

				// UI:
				// - Make $this relative
				// - Get height of tallest child if height is unspecified
				// - Wrap all children in a slide panel
				var height = data.options.height;
				if (height === null) {
					height = 0;
					children.each(function () {
						height = Math.max(height, $(this).outerHeight());
					});
				}

				data.width = $this.width();
				$this.css({
					height: height,
					position: 'relative',
					overflow: 'hidden',
					clear: 'both'
				}).hover(function () {
					$this.addClass('kslider-paused');
				}, function () {
					$this.removeClass('kslider-paused');
					clearTimeout(TIMERS[data.id]);
					$this.kslider('gotoNextSlide');
				});

				children.css({opacity:0}).wrapAll($('<div class="kslider-wrap"></div>').css({
					position: 'absolute',
					height: height,
					width: 9000,
					top: 0,
					left: 0
				})).first().css({opacity:1});

				children.css({
					position: 'absolute',
					display: 'block',
					width: data.width,
					height: height,
					top: 0,
					left: 0
				});

				// Store data in instance
				$.data(this, 'kslider', data);

				// Handlers
				//this.ui.list.find('li a.prev').bind('click', {pager:this}, this.hGotoPreviousPage);
				//this.ui.list.find('li a.next').bind('click', {pager:this}, this.hGotoNextPage);
			
				// Render contents
				//this.render();

				// Start timer
				if (data.options.autoStart && children.size() > 1) {
					TIMERS[data.id] = setTimeout(function () {
						$this.kslider('start');
					}, data.options.interval);
				}
			});
		},

		start: function () {
			return this.each(function (i, el) {
				var $this = $(this);
				var data = $.data(this, 'kslider');

				if (!$this.hasClass('kslider-paused')) {
					$this.kslider('gotoNextSlide');
				} else {
					clearTimeout(TIMERS[data.id]);
					TIMERS[data.id] = setTimeout(function () {
						$this.kslider('start');
					}, data.options.interval);
				}

				/*TIMERS[data.id] = setTimeout(function () {
					$this.kslider('start');
				}, data.options.interval);*/
			});
		},

		gotoSlide: function(s) {
			return this.each(function() {
				var $this = $(this);
				var data = $.data(this, 'kslider');

				clearTimeout(TIMERS[data.id]);

				s = Math.min(s, data.meta.size - 1);
				$this.children('.kslider-wrap').children().stop().clearQueue();
				$this.children('.kslider-wrap').children(':nth-child(' + (data.meta.current+1) + ')').animate({
					opacity: 0
				}, 1000);
				$this.children('.kslider-wrap').children(':nth-child(' + (s+1) + ')').animate({
					opacity: 1
				}, 1000);
				data.meta.current = s;

				$.data(this, 'kslider', data);

				TIMERS[data.id] = setTimeout(function () {
					$this.kslider('start');
				}, data.options.interval);
			});
		},

		gotoNextSlide: function() {
			return this.each(function() {
				var $this = $(this);
				var data = $.data(this, 'kslider');

				var ns = (data.meta.current + 1) % data.meta.size;
				$this.kslider('gotoSlide', ns);
			});
		},

		gotoPreviousSlide: function() {
			return this.each(function() {
				var $this = $(this);
				var data = $.data(this, 'kslider');

				var ns = data.meta.current == 0 ? data.meta.size - 1 : (data.meta.current - 1);
				$this.kslider('gotoSlide', ns);
			});
		}
	};

	/**
	* Plugin declaration
	*/
	$.fn.kslider = function(method) {

		// Invoke plugin method
		if(methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}

		// Initialise plugin instance
		else if(typeof method==='object' || !method) {
			return methods.init.apply(this, arguments);
		}

		// Unknown call
		else {
			$.error('Method ' + method + ' does not exist on kslider');
			return false;
    }   
	}
})(jQuery);
