$.fn.plagScroller = function(oOptions) {

	$(this).each(function(i,elmContainer) {
		$(elmContainer).data('plagScrollerOptions',oOptions);
		$(elmContainer).bind({
			mouseenter:function(){ $(this).plagScroller.stopInterval(); },
			mouseleave:function(){ $(this).plagScroller.startInterval(); }
		});
		$(elmContainer).startPlagScroller();
	});

};

$.fn.startPlagScroller = function() {
	if ($(this).attr('id') == '') {
		$(this).attr('id','scroller-'+Math.random().toString().replace('.',''));
	}

	var sChildrenIdentifier = $(this).data('plagScrollerOptions').scrollChildren;

	var aChildren = $(this).children(sChildrenIdentifier);
	var iLeft = 0;

	var elmContainer = $(this);

	var totalWidth = aChildren.outerWidth() * aChildren.length;

	// lower amount of images than the width of the container
	if (totalWidth <= $(elmContainer).innerWidth()) {
		if (!$(elmContainer).hasClass('still')) {
			$(elmContainer).addClass('still');
		}
		return false;

	} else if (totalWidth <= $(elmContainer).innerWidth()+aChildren.outerWidth()) {
		// just one image more compared to the width of the container so clone images to be sure we have enough to show...

		$(aChildren).each(function(i,elm){
			var elmNew = $(elm).clone();
			$(elmContainer).append(elmNew);
		});

		aChildren = $(this).children(sChildrenIdentifier);

	} 

	

	$(aChildren).each(function(i,elmChild){
		$(elmChild).css({
			position: 'absolute',
			top: $(elmContainer).data('plagScrollerOptions').top,
			left: iLeft
		});
		var iWidth = parseInt($(elmChild).outerWidth());
		iLeft += iWidth;
	});
	$(elmContainer).data('plagScrollerImageTotalWidth',iLeft);

	$(this).data('accellerator', setInterval('$.fn.slidePlagScrollerItems("'+$(this).attr('id')+'")', 75));
	$(this).startPlagScrollerInterval();
};

$.fn.startPlagScrollerInterval = function() {
	clearInterval($(this).data('interval'));
	$(this).data('interval', setInterval('$.fn.slidePlagScrollerItems("'+$(this).attr('id')+'")', 50));
}

$.fn.stopPlagScrollerInterval = function() {
	clearInterval($(this).data('interval'));
}
$.fn.slidePlagScrollerItems = function(scrollerId) {
	var elmContainer = $('#'+scrollerId);
	var sChildrenIdentifier = $(elmContainer).data('plagScrollerOptions').scrollChildren;
	var aChildren = $(elmContainer).children(sChildrenIdentifier);

	aChildren.each(function(i,elm){
		var iLeft = parseInt($(elm).css('left').replace('px',''));
		if (iLeft < 0-$(elm).outerWidth())
		{
			iLeft += $(elmContainer).data('plagScrollerImageTotalWidth');
		}
		$(elm).css('left',iLeft-1);
	});
}