function Scroll ()
{
	this.margin		=    0;
	this.cumMargin	=    0;
	this.minMargin  =    0;
	this.maxMargin 	= -2003;
	
	this.orient;
	this.scrolling;
	
	this.elements	= Array();
	
	this.step		=   10;
	this.bridge		=   89;
	this.speed		=    5;
	
	this.autoscroll	= false;
	this.infinite	= false;
	this.pause		= false;
	
	this.elementid	= null;
	this.startcount	= 0;
	
	this.timer;
}

	Scroll.prototype.run = function ()
	{
		// reference voor recursie goed instellen
		var self = this;
		
		// timer clearen
		clearTimeout (this.timer);
		
		if (this.elementid != null && this.autoscroll)
		{
			if (document.getElementById(this.elementid))
			{
				this.scrollRight(this.elementid);
				
				// start opnieuw laden
				this.timer = setTimeout(function(){self.run()}, 100);
			}
			else
			{
				this.timer = setTimeout(function(){self.run()}, 100);
			}
		}
		else
		{
			if (this.elementid != null)
			{
				if (document.getElementById(this.elementid))
				{
					// berekenen van de breedte van het element....
					
					this.maxMargin = 0 - document.getElementById(this.elementid).offsetWidth + (5 * 89);
				}
				else
				{
					this.timer = setTimeout(function(){self.run()}, 100);
				}
			}
		}
	}
	
	Scroll.prototype.stop = function ()
	{
		this.pause = true;
	}
	
	Scroll.prototype.resume = function ()
	{
		this.pause = false;
	}
	
	Scroll.prototype.changeDirection = function (direction)
	{
		if (direction == 'left')
		{
			this.scrollLeft(this.elementid);
		}
		
		if (direction == 'right')
		{
			this.scrollRight(this.elementid);
		}
	}

	Scroll.prototype.scrollLeft = function (elementid)
	{
		// reference voor recursie goed instellen
		var self = this;
		
		// timer clearen
		clearTimeout (this.timer);
		
		if (this.cumMargin < this.minMargin)
		{
			if (!this.pause)
			{
				// element aan de collectie toevoegen als deze nog niet bestaat
				if (elementid && !this.elements[elementid])
				{
					this.elements[elementid] = document.getElementById(elementid);
				}
				
				// margin berekenen
				this.cumMargin = this.cumMargin + this.step;
				this.cumMargin = ((this.margin + this.bridge) < this.cumMargin) ? this.margin + this.bridge : this.cumMargin;
				
				this.elements[elementid].style.marginLeft = this.cumMargin + 'px';
			}
			
			
			if ((this.margin + this.bridge) > this.cumMargin)
			{
				this.timer = setTimeout(function(){self.scrollLeft(elementid)}, this.speed);
			}
			else
			{
				this.margin = this.cumMargin;
				
				if (this.autoscroll)
				{
					this.timer = setTimeout(function(){self.scrollLeft(elementid)}, 0);
				}
			}
		}
		else
		{
			if (this.autoscroll)
			{
				this.timer = setTimeout(function(){self.scrollRight(elementid)}, 1000);
			}
		}
	}
	
	Scroll.prototype.scrollRight = function (elementid)
	{
		// reference voor recursie goed instellen
		var self = this;
		
		// timer clearen
		clearTimeout (this.timer);
		
		if (this.cumMargin > this.maxMargin)
		{
			if (!this.pause)
			{
				// element aan de collectie toevoegen als deze nog niet bestaat
				if (elementid && !this.elements[elementid])
				{
					this.elements[elementid] = document.getElementById(elementid);
				}
				
				// margin berekenen
				this.cumMargin = this.cumMargin - this.step;
				this.cumMargin = ((this.margin - this.bridge) > this.cumMargin) ? this.margin - this.bridge : this.cumMargin;
				
				this.elements[elementid].style.marginLeft = this.cumMargin + 'px';
			}
			
			if ((this.margin - this.bridge) < this.cumMargin)
			{
				this.timer = setTimeout(function(){self.scrollRight(elementid)}, this.speed);
			}
			else
			{
				this.margin = this.cumMargin;
				
				if (this.autoscroll)
				{
					this.timer = setTimeout(function(){self.scrollRight(elementid)}, 0);
				}
			}
		}
		else
		{
			if (this.autoscroll)
			{
				this.timer = setTimeout(function(){self.scrollLeft(elementid)}, 1000);
			}
		}
	}
	
	Scroll.prototype.addItem = function (item)
	{
		this.items[this.items.length] = item;
	}

function ScrollItem ()
{
	
}

var scroll = new Scroll();

scroll.elementid 	= 'photobookscrollbar';
//scroll.autoscroll 	= true;
//scroll.infinite 	= true;

scroll.run();
