$(function(){
	$('ul.article').itemScroller('',4000).bind('mouseenter',function(){
		$(this).trigger('stop');
	}).bind('mouseleave',function(){
		$(this).trigger('start');
	// });
	}).trigger('start');
	// $('ul.article').find('> li').filter(':gt(3)').remove();
	// $('ul.article').find('> li').css({'margin-right' : '10px'});
	// $('ul.article').find('> li:last').css({'margin-right' : '0px'});
});


(function ($){
	$.fn.itemScroller = function(limit, interval){
		limit    = limit 	|| 3; 	//aantal = +1 (omdat javascript begint bij 0)
		interval = interval || 4000;
		return this.each(function(){
			var $list = $(this),
				running = false,
				items = [],
				currentItem = limit + 1,
				total = 0;
				width = $list.find('> li:first').width();
				// console.log(width);
			$list.find('> li').each(function(){
				items.push('<li>' + $(this).html() + '</li>');
			});
			total = items.length;


			$list.find('> li').filter(':gt('+ (limit) + ')').remove();
			$list.find('> li').css({'margin-right' : '10px'});
			$list.find('> li:last').css({'margin-right' : '0px'});
			
			$list.bind('start',function(){
				running = true;
			}).bind('stop',function(){
				running = false;
			});
			
			function scroller () {
				if (running) {
					var $insert = $(items[currentItem]).css({
						display	: 	"none",
						opacity	: 	1,
						width	: 	0
					}).prependTo($list);
					//Add id to new element.
					var productId = $insert.find('input[name=id]').val();
					$insert.attr('id',productId);
					//add click event to element
					add_cart_link($insert);
					
					//check if item is already in the cart, if so .. replace button with text
					var isInCart     = $('#' + productId + '_cart');
					if (isInCart.size() == 1) {
						$insert.find('p.articleBuy').html('Dit item zit in je winkelwagen!');
					}
					
					
					$list.find('> li:last').animate({ 'width': '0px'},800, function () {
						$insert.css({'margin-right':'10px'}).animate({'width': width},800);
						$(this).remove();
						$list.find('> li:last').css({'margin-right' : '0px'});
					});
					// console.log(currentItem);
					// console.log($(items[currentItem]));
				}
				currentItem++;
				if (currentItem >= total) {
					currentItem = 0;
				}
				setTimeout(scroller, interval);
			}
			scroller();
		});
	};
})(jQuery);

