﻿(function ($) {

	$.fn.simpleCycle = function (options) {

		var settings = {
			interval: 10000,
			direction: 'vertical',
			slideTime: 500,
			autoHeight: true
		};

		return this.each(function () {

			if (options) {
				$.extend(settings, options);
			}

			// if called on a list item add the outer container
			// otherwise assume $(this) is the outer container
			if ($(this).is('ul')) {
				var $OuterContainer = $('<div />');
				var $List = $(this);
				$List.after($OuterContainer);
				$OuterContainer.append($List);
			}
			else {
				var $OuterContainer = $(this);
				var $List = $(this).find('ul').first();
			}

			$OuterContainer.addClass('simpleCycleOuterBox');
			$List.addClass('simpleCycleList');

			// check list was found and that it has more than 1 element
			if ($List.size() > 0 && $List.children('li').size() > 1) {
				// add the inner container
				var $Container = $('<div class="simpleCycleListBox" />');
				$List.after($Container);
				$Container.append($List);

				// copy first item to the end so we can cycle around
				$List.append($List.children('li').first().clone());

				var lastSlideIndex = $List.children('li').size() - 1;
				var slideIndex = 0;

				var itemWidth = $List.children('li').first().width();
				var itemHeight = $List.children('li').first().height();

				if (settings.direction == 'vertical') {
					$List.css('height', (itemHeight * $List.children('li').size()) + 'px');
				}
				else if (settings.direction == 'horizontal') {
					$List.css('width', (itemWidth * $List.children('li').size()) + 'px');
				}

				$Container.css('width', itemWidth + 'px');
				$Container.addClass('simpleCycle-' + settings.direction);

				// setup autoheight
				if (settings.autoHeight) {
					var maxHeight = 0;
					$List.children().each(function () {
						if ($(this).outerHeight(true) > maxHeight)
							maxHeight = $(this).height();
					});
					$List.children().each(function () {
						$(this).css('height', maxHeight + 'px');
					});
					$Container.css('height', $List.children().first().outerHeight(true) + 'px');
				}
				else {
					$Container.css('height', itemHeight + 'px');
				}

				// autoscroll function
				function autoScroll() {
					slideIndex++;

					if (settings.direction == 'vertical') {
						if (slideIndex == lastSlideIndex) { // if moving to last slide
							$List.stop(true, true).animate({ top: ($Container.height() * slideIndex * -1) + 'px' }, settings.slideTime, function () {
								$List.css('top', 0);
								slideIndex = 0;
							});
						}
						else {
							$List.stop(true, true).animate({ top: ($Container.height() * slideIndex * -1) + 'px' });
						}
					}
					else if (settings.direction == 'horizontal') {
						if (slideIndex == lastSlideIndex) { // if moving to last slide
							$List.stop(true, true).animate({ left: ($Container.width() * slideIndex * -1) + 'px' }, settings.slideTime, function () {
								$List.css('left', 0);
								slideIndex = 0;
							});
						}
						else {
							$List.stop(true, true).animate({ left: ($Container.width() * slideIndex * -1) + 'px' });
						}
					}
				}

				// init autoscroll
				var seIntervalID = setInterval(autoScroll, settings.interval);

				// don't scroll on hover
				$OuterContainer.hover(
					function () {
						clearInterval(seIntervalID);
					},
					function () {
						seIntervalID = setInterval(autoScroll, settings.interval);
					}
				);
			}
		});
	};

})(jQuery);

