var Scroller = function(scrollerName, imageID, textID)
{

	var _scrollerName = scrollerName;
	var _scrollerWaggon = scrollerName + "-waggon";
	var _imageID = imageID;
	var _textID = textID;
	
	var _thumbnailCount = 0;
	
	var _moveLeft = false;
	var _moveRight = false;
	
	var _timer = null;
	
	var _prefix = "scroller-thumbnail";
	
	var _moveSpeed = 15;
	
	this.setMoveSpeed = function(moveSpeed)
	{
		_moveSpeed = moveSpeed;
	};
	
	this.setPrefix = function(prefix)
	{
		_prefix = prefix;
	};
	
	this.startRendering = function()
	{
		
		_timer  = window.setInterval(tick, 40);
	};


	this.startMoveRight = function()
	{
		recalculateWidth();
		_moveRight = true;
	};
	
	this.stopMoveRight = function()
	{
		_moveRight = false;
	};
	
	this.startMoveLeft = function()
	{
		recalculateWidth();
		_moveLeft = true;
	};
	
	this.stopMoveLeft = function()
	{
		_moveLeft = false;
	};
	
	function moveRight()
	{
		if (_thumbnailCount > 0)
		{

			if ((parseInt(document.getElementById(_scrollerWaggon).offsetLeft) + parseInt(document.getElementById(_scrollerWaggon).offsetWidth - 5)
				- parseInt(document.getElementById(_scrollerName).offsetWidth)) > 0
			) 
			{
				var obj = document.getElementById(_scrollerWaggon);
				
				obj.style.left = (parseInt(obj.style.left) - _moveSpeed) + "px";
				
			}
		}
		
	};
	
	
	function moveLeft()
	{		
		if (_thumbnailCount > 0)
		{
			

			if ( (parseInt(document.getElementById(_scrollerWaggon).style.left) ) < 0 ) 
			{
				
				var obj = document.getElementById(_scrollerWaggon);
				
				obj.style.left = (parseInt(obj.style.left) + _moveSpeed) + "px";
					
			}
		}
	};

	function recalculateWidth()
	{
		var width = 0;
		for (var i = 0; i < _thumbnailCount; i++)
		{
			width = width + parseInt(document.getElementById(_prefix + "-" + i).offsetWidth) + 5;
		}

		document.getElementById(_scrollerWaggon).style.width = width + "px";

	};
	
	function tick()
	{
		
		if (_moveLeft)
		{
			moveLeft();
		} else if (_moveRight)
		{
			moveRight();
		}
	};
	
	this.addThumbnail = function(thumbnail, picture, text, title)
	{
		var title_html = "";
		if (title != null && title != "")
		{
			title_html = 'alt="' + title + '"';
		}
		
		var html = '<div style="display: inline; top: 0px; margin-right: 5px;" class="scroller-thumbnail" id="'+_prefix+'-'+_thumbnailCount+'"><a href="' + picture + '" rel="lightbox[gallery]"><img ' + title_html + ' src="'+thumbnail+'" /></a></div>';

		//image = new Image();

		//image.src = thumbnail;

		

		//MM_preloadImages(thumbnail);
		
		document.getElementById(_scrollerWaggon).innerHTML = document.getElementById(_scrollerWaggon).innerHTML + html;
		
		//alert(document.getElementById(_prefix + "-" + _thumbnailCount).offsetWidth);
		
		//document.getElementById(_scrollerWaggon).style.width = (parseInt(document.getElementById(_scrollerWaggon).style.width) + parseInt(document.getElementById(_prefix + "-" + _thumbnailCount).offsetWidth) + 5) + "px";
		_thumbnailCount++;
	};
	
	this.addThumbnailCount = function(count)
	{
			_thumbnailCount+= count;
	};
	
	
	
};
