﻿/*
This plugin apply simple ticker

HTML structure sample
---------------------
.simpleticker-wrapper {
	position: relative; 
	overflow: hidden; 
	height: 200px;
	width: 200px;
}

<ul id="ticker">
	<li><a href="#">Link content</li>
	<li><a href="#">Link content</li>
	<li><a href="#">Link content</li>
	<li><a href="#">Link content</li>
</ul>

jQuery code sample
------------------
$("#ticker").pmSimpleTicker();

or

$("#ticker").pmSimpleTicker({
	slowdown: 2, //	1-fast to 10-slow
	orientation: "vertical",
	delay: 1000 // delay before each tick 
	pause: true // enable or disable ticker pause on mouse over										  
});

HTML structure sample after plugin application
----------------------------------------------
<div class="simpleticker-wrapper" style="height: 120px; width: 600px; overflow: hidden">
	<ul id="ticker" style="height: 120px; width: 600px;">
		<li><a href="#">Link content</li>
		<li><a href="#">Link content</li>
		<li><a href="#">Link content</li>
		<li><a href="#">Link content</li>
	</ul>
</div>
*/

(function($){

	$.fn.pmSimpleTicker = function(options) {
	
		var opts = $.extend("", $.fn.pmSimpleTicker.defaults, options);
		
		return this.each(function(e){
			
			var $ticker = $(this);
			
			var o = $.meta ?  $.extend({}, opts, $this.data()) : opts;
			
			$ticker.wrap("<div class=\"simpleticker-wrapper\" style=\"overflow: hidden; width: " + $ticker.width() + "px; height: " + $ticker.height() + "px\"></div>");
			
			var tickerItemsWidth = 0;
			var tickerItemsHeight = 0;
			
			$ticker.children().each(function(i){
				$tickerItem = $(this);
				
				//	ticker item width or height depend on o.orientation property
				var tickerItemSpace = 0;
				
				if (o.orientation == "vertical") {
					//	rotate ticker from botom to up
					var tickerItemSpace = ($tickerItem.height() > $tickerItem[0].offsetHeight)? $tickerItem.height() : $tickerItem[0].offsetHeight;
					
					var tickerItemDuration = (tickerItemSpace + parseInt($tickerItem.css("marginTop"))) / 0.025 * o.slowdown;
					
					tickerItemsHeight = tickerItemsHeight + tickerItemSpace;
					
					$tickerItem.data({
						height: tickerItemSpace,
						duration: tickerItemDuration
					});
				}
				else {	
					//	rotate ticker from right to left
					var tickerItemSpace = ($tickerItem.width() > $tickerItem[0].offsetWidth) ? $tickerItem.width() : $tickerItem[0].offsetWidth;
					
					var tickerItemDuration = (tickerItemSpace + parseInt($tickerItem.css("marginLeft"))) / 0.04 * o.slowdown;
					
					tickerItemsWidth = tickerItemsWidth + tickerItemSpace;
					
					$tickerItem.data({
						width: tickerItemSpace,
						duration: tickerItemDuration
					});
				};
				
			});
			
			$ticker.css((o.orientation == "vertical") ? {height: tickerItemsHeight + "px"} : {width: tickerItemsWidth + "px"});
			
			function rotate(ticker) {
				
				var tickerItem = ticker.children().first();
				
				if (o.orientation == "vertical") {
					
					var currentTopMargin = parseInt(tickerItem.css("marginTop"));
					var duration = (currentTopMargin < 0) ? (tickerItem.data().width + currentTopMargin) / 0.025 * o.slowdown : tickerItem.data().duration;
					
					tickerItem.delay(o.delay).animate({ marginTop: -tickerItem.data().height }, tickerItem.data().duration, "linear", function() {
					
						//	move current item to the bottom
						tickerItem.appendTo(tickerItem.parent()).css("marginTop", 0);
					
						//	recursiv animation
						rotate(ticker);
					});
				} 
				else {
					
					var currentLeftMargin = parseInt(tickerItem.css("marginLeft"));
					var duration = (currentLeftMargin < 0) ? (tickerItem.data().width + currentLeftMargin) / 0.04 * o.slowdown : tickerItem.data().duration;
					
					tickerItem.delay(o.delay).animate({ marginLeft: -tickerItem.data().width }, duration, "linear", function() {
					
						//	move current item to the bottom
						tickerItem.appendTo(tickerItem.parent()).css("marginLeft", 0);
					
						//	recursiv animation
						rotate(ticker);
					});
					
				};
				
			};
			
			// Start ticker initialy
			if ((o.orientation == "vertical" && $ticker.parent().height() < tickerItemsHeight) || (o.orientation == "horizontal" && $ticker.parent().width() < tickerItemsWidth)) {
				
				if (o.pause) {
					//	stop ticker on mouse over 
					$ticker.mouseenter(function() {
					  $(this).children().stop();
					});
					
					//	resume ticker on mouse leave
					$ticker.mouseleave(function() {
					  rotate($(this));
					});
				}
				
				//	start ticker for the first time
				rotate($ticker);
			};
			
		});
	
	};
	
	$.fn.pmSimpleTicker.defaults = {
		slowdown: 2,
		orientation: "vertical",
		delay: 0,
		pause: true
	};
	 
})(jQuery);
