jQuery.noConflict();
(function($) {
	$('.hideforscript').css('visibility', 'hidden'); 
	
	$(function()
	{
		var scrollerSlideSpeed=300;
		var autoscrollInterval=5000;
		var scrollerIndex=0;
	
		$('.featurescroller').each(function() {
			scrollerInit($(this), scrollerIndex);
			scrollerIndex++;
		});
	
		function scrollerInit(scroller, scrollerIndex)
		{
			var scrollerClasses=scroller.attr('class');
			
			scroller.attr('id', 'featurescroller-inner2_'+scrollerIndex).removeClass('hideforscript')
				.removeClass('featurescroller').addClass('featurescroller-inner2');
				
			scroller.wrap('<div class="featurescroller-inner1" id="featurescroller-inner1_'+scrollerIndex+'"></div>');
			scroller=scroller.parent();
			scroller.wrap('<div id="featurescroller_'+scrollerIndex+'" class="'+scrollerClasses+' js"></div>');
			scroller=scroller.parent();
			
			$('li', scroller).addClass('item');

			
			//set sliderWidth
			var sliderWidth=scroller.getItemWidth() * scroller.getItemCount();
			$('.featurescroller-inner2', scroller).css('width', sliderWidth);
			$('.featurescroller-inner2', scroller).css('left', 0);

			//prev/next controls
			scroller.append('<div class="featurescroller-prev" id="featurescroller-prev_'+scrollerIndex+'" style="display:none">Prev</div>');
			scroller.append('<div class="featurescroller-next" id="featurescroller-next_'+scrollerIndex+'" style="display:none">Next</div>');
			
			setControls(scroller);
		
			$('.featurescroller-prev', scroller).bind('click', function() {
				slide(-1, scroller);
			});
	
			$('.featurescroller-next', scroller).bind('click', function() {
				slide(1, scroller);
			});
		}
		
		function slide(items, scroller)
		{
			window.clearTimeout(scroller.autoscrollID);
			
			var left = $('.featurescroller-inner2', scroller).css('left').toInt();
			
			//if the left is not a multiple of itemWidth, we're actually sliding right now, so ditch out
			if (0 != Math.abs(left) % scroller.getItemWidth())
			{
				return;
			}
			
			var newLeft = left + (-1 * (items * scroller.getItemWidth()));
			
			$('.featurescroller-inner2', scroller).animate({left: newLeft}, scrollerSlideSpeed, 'linear', function() {
				setControls(scroller);
			});
		}

		function setControls(scroller)
		{
			var left = $('.featurescroller-inner2', scroller).css('left').toInt();
			
			if (0 == left)
			{
				$('.featurescroller-prev', scroller).hide();
			}
			else
			{
				$('.featurescroller-prev', scroller).show();
			}

			if (scroller.atFarRight())
			{
				$('.featurescroller-next', scroller).hide();
			}
			else
			{
				$('.featurescroller-next', scroller).show();
			}
			
			if (scroller.hasClass('autoscroll') && scroller.getItemCount() > scroller.getItemsPerPage())
			{
				scroller.autoscrollID=window.setTimeout(function() {autoScroll(scroller)}, autoscrollInterval);
			}
		}
		
		function autoScroll(scroller)
		{
			if (scroller.atFarRight())
			{
				$('.featurescroller-inner2', scroller).animate({left: 0}, scrollerSlideSpeed, 'linear', function() {
					setControls(scroller);
				});
			}
			else
			{
				slide(1, scroller);
			}
		}
	});
		
	$.fn.getItemsPerPage=function()
	{
		return Math.round($(this).css('width').toInt() / $(this).getItemWidth());
	}
	
	$.fn.getItemWidth=function()
	{
		var result = $('li', $(this)).css('width').toInt() + 
			$('li', $(this)).css('margin-left').toInt() + 
			$('li', $(this)).css('margin-right').toInt();
		return result;

	}
	
	$.fn.getIndex=function()
	{
		return $(this).attr('id').replace(/[^0-9]/gi, '');
	}
	
	$.fn.getItemCount=function()
	{
		return $('.item', $(this)).length;
	}
	
	$.fn.atFarRight=function()
	{
		var left = $('.featurescroller-inner2', $(this)).css('left').toInt();
		var right = $('.featurescroller-inner2', $(this)).css('width').toInt() - Math.abs(left);
		return (right - $('.featurescroller-inner1', $(this)).css('width').toInt() <= 0);
	}

	String.prototype.toInt = function()
	{
		return parseInt(this.replace(/px/,''), 10);
	}
})(jQuery);

