﻿(function ($)
{
	if ($.fn.ticker)
		return;

	var methods = {};
	$.fn.ticker = function (method)
	{
		// Method calling logic
		if (methods[method])
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method)
		{
			return methods.init.apply(this, arguments);
		} else
		{
			$.error('Method ' + method + ' does not exist on jQuery.sortableExtended');
		}
	};
	methods.init = function (options)
	{
		return this.each(
		function ()
		{
			var thisO = this;
			var $this = $(this),
				data = $this.data('ticker');

			if (!data)
			{
				$this.data('ticker', {});
				data = $this.data('ticker');

				var settings = {
					interval: 5000,
					overblend: false,
					fade: "slow"
				};

				if (options)
				{
					$.extend(settings, options);
				}

				data.settings = settings;

				$this.css({ position: "relative" });
				if (data.settings.width != null)
					$this.css({ width: data.settings.width });
				if (data.settings.height != null)
					$this.css({ height: data.settings.height });

				slideInternal(thisO);

				// http://www.ozonesolutions.com/programming/2011/07/jquery-fadein-window-setinterval-a-bad-combination/
				//data.intervalId = setInterval(function () { try { console.log("interval for " + thisO.id); } catch (e) { } slideInternal(thisO); }, data.settings.interval);
			}
			return;
		});
	};
	var slideInternal = function (ticker)
	{
		try
		{
			var $this = $(ticker),
				$currentSlide = $(".currentSlide", $this),
				data = $this.data('ticker');

			if ($currentSlide.length == 0)
			{
				$currentSlide = $(":first", $this);
				$currentSlide.css({ position: "absolute", top: "0px" });
				$currentSlide.addClass("currentSlide");
				$currentSlide.fadeIn(data.settings.fade, function ()
				{
					window.setTimeout(function ()
					{
						slideInternal(ticker);
					}, data.settings.interval);
					$(this).dequeue();
				});
			}
			else
			{
				$currentSlide.removeClass("currentSlide");
				//$currentSlide.css({ "display": "none" });

				var $next = $currentSlide.next();
				if ($next.length == 0)
					$next = $(":first", $this);

				if ($next.get(0) == $currentSlide.get(0))
				{
					return; // 1 slide
				}

				$next.addClass("currentSlide");

				$next.css({ position: "absolute", top: "0px" });
				
				if (data.settings.overblend == true)
				{
					$next.fadeIn(data.settings.fade, function ()
					{
						window.setTimeout(function ()
						{
							slideInternal(ticker);
						}, data.settings.interval);
						$(this).dequeue();
					});
					$currentSlide.fadeOut(data.settings.fade);
				}
				else
				{
					$currentSlide.fadeOut(data.settings.fade, function ()
					{
						$next.fadeIn(data.settings.fade, function ()
						{
							window.setTimeout(function ()
							{
								slideInternal(ticker);
							}, data.settings.interval);
							$(this).dequeue();
						});
						$(this).dequeue();
					});
				}
			}
		}
		catch (e) { alert("error slide: " + e); }
	};
	methods.slide = function ()
	{
		return this.each(function ()
		{
			slideInternal(this);
		});
	};
	methods.destroy = function ()
	{
		return this.each(function ()
		{
			var $this = $(this),
						data = $this.data('ticker');

			clearInterval(data.intervalId);

			$this.removeData('ticker');
		});
	};
})(jQuery);
