﻿(function($)
{
	$.newsTicker = function(parent, options)
	{
		return $.newsTicker.impl.init(parent, options);
	};

	$.fn.newsTicker = function(options)
	{
		return $.newsTicker.impl.init(this, options);
	};

	$.newsTicker.defaults = {
		duration: 2000,
		timeout: 2000,
		orientation: 'vertical'
	};

	$.newsTicker.impl = {
		opts: {},
		init: function(parent, options)
		{
			parent = (parent instanceof jQuery) ? parent : $(parent);
			this.opts = $.extend({}, $.newsTicker.defaults, options);
			var self = this;

			if (parent.children().size() > 1)
			{
				parent.animate({ width: parent.width() }, this.timeout, function()
				{
					self.go(parent);
				});
			}
		},
		go: function(parent)
		{
			var self = this;
			var item = parent.children('*:first');

			if (this.opts.orientation == 'vertical')
			{
				item.animate({ marginTop: -(item.height() + 10) }, this.opts.duration, function()
				{
					item.appendTo(parent).css({ marginTop: 0, opacity: 0 }).animate({ opacity: 1 }, self.opts.timeout, function()
					{
						self.go(parent);
					});
				});
			}
			else
			{
				item.animate({ marginLeft: -item.width() }, this.opts.duration, function()
				{
					item.appendTo(parent).css({ marginLeft: 0, opacity: 0 }).animate({ opacity: 1 }, self.opts.timeout, function()
					{
						self.go(parent);
					});
				});
			}
		}
	};
})(jQuery)
