/// <reference path="../jquery-1.3.2-vsdoc.js" />
/// <reference path="Plugin.Timer.js" />

// --------------------------------
//  Plugin author: Robert Koritnik
// --------------------------------
//  Depends on Plugin.Timer.js
// --------------------------------

(function($) {
	$.fn.carousel = function(options) {
		///	<summary>Converts a set of elements into an automatically shifting carousel with manual controls.</summary>
		///	<param name="options" type="Object" optional="true">
		///	Configuration options object with properties (defaults):
		///		1. delay (5000) - shift time delay in milliseconds
		///		2. animationSpeed (500) - animation transition time in milliseconds
		///		3. itemSelector ("div.item") - jQuery CSS selector that will select carousel items
		///		4. linkSelector ("div.controls ul li a") - jQuery CSS selector that will select links to carousel items
		///		5. linkParentSelector ("ul") - jQuery CSS selector that will select item links parent node where controls (stop/play, previous, next) will be appended
		///		6. autPlay (true) - boolean whether to auto start carousel shifting at start or not
		///		7. addControls (true) - boolean whether to add controls play/pause, previous, next
		///		8. replaceLinkContent (false) - boolean to change link content to numbers
		///	</param>
		///	<returns type="jQuery" />

		// Check if Plugin.Timer.js plugin is loaded
		if (!$.run)
		{
			return this;
		}

		// set default options
		options = $.extend({
			delay: 5000,
			animationSpeed: 500,
			itemSelector: "div.item",
			linkSelector: "div.controls ul li a",
			linkParentSelector: "ul",
			autoPlay: true,
			addControls: true,
			replaceLinkContent: false,
			timerKey: $.fn.carousel.id++
		}, options || {});

		return this.each(function() {
			var scope = this;

			// add next/prev controls
			options.addControls === true && $(options.linkSelector, this)
				.eq(0)
				.closest(options.linkParentSelector)
				.append("<li class=\"stop\"><a href=\"\">Play/Stop</a></li>")
				.append("<li class=\"previous\"><a href=\"\">Nazaj</a></li>")
				.append("<li class=\"next\"><a href=\"\">Naprej</a></li>");

			// private elements
			var items = $(options.itemSelector, this);
			var links = $(options.linkSelector, this);
			if (items.length + (options.addControls === true ? 3 : 0) != links.length)
			{
				return;
			}

			// private variables
			var count = items.length;
			var currentItem = 0;
			var running = options.autoPlay;
			var animating = false;

			// prepare carousel items
			// var maxHeight = 0;
			for (var i = 0; i < count; i++)
			{
				// maxHeight = $(items[i]).height() > maxHeight ? $(items[i]).height() : maxHeight;
				$(items[i]).css({
					position: "absolute",
					display: i === 0 ? "block" : "none"
				});
//				$(links[i]).data("itemNumber", i).click(function(event) {
//					event.preventDefault();
//					var old = currentItem;
//					currentItem = $(this).data("itemNumber");
//					if (old != currentItem)
//					{
//						show.call(scope, old, true);
//					}
//				});
				options.replaceLinkContent === true && $(links[i]).text(i + 1);
				i === 0 && $(links[i]).parent().addClass("selected");
			}
			// $("div.items", this).height(maxHeight);

			// prepare navigation controls
			if (options.addControls)
			{
				$(links[count]).click(function(event) {
					event.preventDefault();
					if (running)
					{
						stop.call(scope);
					}
					else
					{
						next.call(scope);
						run.call(scope);
					}
				});
				$(links[count + 1]).click(function(event) {
					event.preventDefault();
					previous.call(scope, true);
				});
				$(links[count + 2]).click(function(event) {
					event.preventDefault();
					next.call(scope, true);
				});
			}

			// private functions
			var next = function(stopAnimation) {
				if (animating)
				{
					return;
				}
				var i = currentItem;
				if (++currentItem == count)
				{
					currentItem = 0;
				}
				show.call(this, i, stopAnimation);
			};

			var previous = function(stopAnimation) {
				if (animating)
				{
					return;
				}
				var i = currentItem;
				if (--currentItem < 0)
				{
					currentItem = count - 1;
				}
				show.call(this, i, stopAnimation);
			};

			var show = function(hideIndex, stopAnimation) {
				if (stopAnimation === true)
				{
					stop.call(this);
				}
				// animate
				animating = true;
				$(items[hideIndex]).fadeOut(options.animationSpeed, function() {
					$(links[hideIndex]).parent().removeClass("selected");
				});
				$(items[currentItem]).fadeIn(options.animationSpeed, function() {
					$(links[currentItem]).parent().addClass("selected");
					animating = false;
				});
			};

			var run = function() {
				// start animation
				if (options.addControls)
				{
					$(links[count]).parent().removeClass("play").addClass("stop");
				}
				running = true;
				$.run("Carousel-" + options.timerKey, next, options.delay, true, this);
			};

			var stop = function() {
				// stop animation
				$.stopRun("Carousel-" + options.timerKey);
				running = false;
				if (options.addControls === true)
				{
					$(links[count]).parent().removeClass("stop").addClass("play");
				}
			};

			if (options.autoPlay === true)
			{
				run();
			}
			else
			{
				stop();
			}
		});
	};
	$.fn.carousel.id = 1;
})(jQuery);