/**
 * Esempio HTML:
 
<style type="text/css">
	div#slidemarche {
		height: 70px;
		line-height: 60px;
		margin-left: 15px;;
		overflow: hidden;
		position: relative;
		width: 710px;
	}
	div#slidemarche div.slidein {
		width: 20000em;
		position: absolute;
	}
	div#slidemarche ul {
		position: absolute;
		left: 10px;
	}
	div#slidemarche ul.last {
		left: 710px;
	}
	div#slidemarche li {
		float: left;
		width: 70px;
	}
</style>
 <div id="slidemarche">
	<div class="slidein">
	<ul class="first">
	<?php foreach ($marche as $i => $marca): ?>
		<li>
			<?php if ($marca['url']): ?>
				<a href="<?php echo $marca['url'] ?>"><img style="vertical-align:middle" src="<?php echo $percorsi["path_assoluto"]."photos/imm/loghi/".$marca["logo_url"]; ?>" title="<?php echo $marca['alt'] ?>" alt="<?php echo $marca['alt'] ?>" /></a>
			<?php else: ?>
				<img style="vertical-align:middle" src="<?php echo $percorsi["path_assoluto"]."photos/imm/loghi/".$marca["logo_url"]; ?>" title="<?php echo $marca['alt'] ?>" alt="<?php echo $marca['alt'] ?>" />
			<?php endif ?>
		</li>
		<?php if ($i == round(count($marche) / 2)): ?>
		</ul>
		<ul class="last">
		<?php endif ?>
	<?php endforeach ?>
	</ul>
	</div>
</div>
<div class="blocca">&nbsp;</div>
	
<script type="text/javascript">
	$(document).ready(function() {
		$("#slidemarche").marquee();
	});
</script>
  
 */
(function($){
	$.fn.marquee = function(options) {
		var defaults = {
			duration: 1000,
			axis: 'x'
		};
		
		var options = $.extend(defaults, options);

		var c;
		var first;
		var last;
		var i;
		var axis;
   	 
	
		return this.each(function() {
		   
			c = $(this);
			first = c.find("ul.first");
			last = c.find("ul.last");
				
			if (options.axis == 'y') {
				axis = 'top';

				var totalHeight = first.height() + last.height();
		
				c.find("ul").each(function () {
					var lis = $(this).find("li");
					var s = $(lis[0]).height();
					var c = lis.length;
				    
					$(this).height(c * s);
				});
			} else {
				axis = 'left';

				var totalWidth = first.width() + last.width();
				c.find("ul").each(function () {
					var lis = $(this).find("li");
					var s = $(lis[0]).width();
					var c = lis.length;
				
					$(this).width(c * s);
				});
			}

			first.data('step', 1);
			last.data('step', 3);
			
			animateFirst(false);

			c.hover(function () {
				c.find('ul').stop();
			}, function () {
				if (first.data('step') == 1)
					animateFirst();
				if (first.data('step') == 2)
					animateFirst2();
				if (last.data('step') == 1)
					animateLast();
				if (last.data('step') == 2)
					animateLast2();
			});
		});
	    
	    function test(){
	    
	    }
		function animateFirst() {
					
			var d = 0;
			var x = (first.width() - c.width()) * -1;
			var y = (first.height() - c.height()) * -1;
			
			if (options.axis == 'y') {
				d = y;
				x = 0;
			} else {
				d = x;
				y = 0;
			}
			
			var p = parseInt(first.css(axis)) || 0;
						
			i++;
			if (i > 100) { // Previene loops
				return;
			}	

			
			if (first.data('step') == 1) {		
			
				first.animate({ 
					left: x,
					top: y
				}, getDuration(p, d),
				'linear',
				function () {
					first.data('step', 2);
					last.data('step', 1);
					
					animateFirst2();
					animateLast(true);
				});
			} else {
				animateFirst2();
				animateLast();
			}
		}
		function animateFirst2() {
			var d = 0;
			var x = first.width() * -1;
			var y = first.height() * -1;
			
			if (options.axis == 'y') {
				d = y;
				x = 0;
			} else {
				d = x;
				y = 0;
			}
			
			var p = parseInt(first.css(axis));
			
			if (first.data('step') == 2) {
				first.animate({ 
					left: x,
					top: y
				}, getDuration(p, d),
				'linear',
				function () {
					first.data('step', 3);
					
					if (axis == 'top')
						first.css(axis, c.height()+'px');
					else
						first.css(axis, c.width()+'px');
				});
			}
		}
		function animateLast() {
			var d = 0;
			var x = (last.width() - c.width()) * -1;
			var y = (last.height() - c.height()) * -1;
			
			if (options.axis == 'y') {
				d = y;
				x = 0;
			} else {
				d = x;
				y = 0;
			}
			
			var p = parseInt(last.css(axis));
			
			if (last.data('step') == 1) {
				last.animate({ 
					left: x,
					top: y
				}, getDuration(p, d),
				'linear',
				function () {
					first.data('step', 1);
					last.data('step', 2);
					
					animateLast2();
					animateFirst(true);
				});
			} else {
				animateLast2();
				animateFirst();
			}
		}
		function animateLast2() {
			var d = 0;
			var x = last.width() * -1;
			var y = last.height() * -1;
			
			if (options.axis == 'y') {
				d = y;
				x = 0;
			} else {
				d = x;
				y = 0;
			}
			
			var p = parseInt(last.css(axis));
			
			if (last.data('step') == 2) {
				last.animate({ 
					left: x,
					top: y
				}, getDuration(p, d),
				'linear',
				function () {
					last.data('step', 3);
					
					if (axis == 'top')
						last.css(axis, c.height()+'px');
					else
						last.css(axis, c.width()+'px');
				});
			}
		}
		
		function getDuration(p, x) {
			var y = Math.abs(p - x);
			
			return (15000 * y) / options.duration;
		}
	};
})(jQuery);	
