var timer;

function MenuScroll(newName, newPanel, newID, newOpen)
{
	var name = newName;
	var open = newOpen;
	var openPanel = 1;
	var first = newOpen;
	var id = newID;
	var panel = document.getElementById(newPanel);
	var poz=0;
	var startPos=0;
	
	function init()
	{
		var doKonca=0;
		var nr=open
		var dlugosci = new Array();
		
		var obj = document.getElementById('panelLink'+nr)
		while(obj!=null)
		{
			doKonca+=obj.clientWidth;
			dlugosci[dlugosci.length]=obj.clientWidth;
			nr+=1;
			obj = document.getElementById('panelLink'+nr);
		}
		
		var doPoczatku=0;
		var nr=open-1;
		
		var obj = document.getElementById('panelLink'+nr)
		while(obj!=null)
		{
			doPoczatku+=obj.clientWidth;
			dlugosci[dlugosci.length]=obj.clientWidth;
			nr-=1;
			obj = document.getElementById('panelLink'+nr);
		}
		
		if(doKonca>document.getElementById('scroll-panels').clientWidth-82)
		{
			poz=-doPoczatku;
			panel.style.marginLeft=poz+'px';
		}
		else
		{
			doPoczatku=0;
			var ilosc=0;;
			var suma=0;
			for(var a=0;a<dlugosci.length;a++)
			{
				suma+=dlugosci[a];
				if(suma>document.getElementById('scroll-panels').clientWidth-82)
				{
					doPoczatku+=dlugosci[a];
					ilosc++;
				}
			}
			open=ilosc+1;
			poz=-doPoczatku;
			panel.style.marginLeft=poz+'px';
		}
		
	}
	this.init = init;
	
	function goRight()
	{
		var a = Math.ceil((document.getElementById('panelLink'+open).clientWidth-(Math.abs(poz)-Math.abs(startPos)))/4);
		poz-=a;
		panel.style.marginLeft=poz+'px';
		
		if(document.getElementById('panelLink'+open).clientWidth==Math.abs(poz)-Math.abs(startPos))
		{
			open++;
			this.reset();
		}
	}
	this.goRight = goRight;
	
	function goLeft()
	{
		var a = Math.ceil((document.getElementById('panelLink'+(open-1)).clientWidth-(Math.abs(startPos)-Math.abs(poz)))/4);
		poz+=a;
		panel.style.marginLeft=poz+'px';
		
		if(document.getElementById('panelLink'+(open-1)).clientWidth==Math.abs(startPos)-Math.abs(poz))
		{
			open--;
			this.reset();
		}
	}
	this.goLeft = goLeft;
	
	function moveRight()
	{
		if(!timer)
		{
			var doKonca=0;
			var nr=open
			
			var obj = document.getElementById('panelLink'+nr)
			while(obj!=null)
			{
				doKonca+=obj.clientWidth;
				nr+=1;
				obj = document.getElementById('panelLink'+nr);
			}
			
			if(doKonca>document.getElementById('scroll-panels').clientWidth-82)
			{
				startPos = poz;
				this.reset();
				timer = setInterval(name+'.goRight()', 20);
			}
		}
	}
	this.moveRight = moveRight;
	
	function moveLeft()
	{
		if(!timer)
		{
			if(open>1)
			{
				startPos = poz;
				this.reset();
				timer = setInterval(name+'.goLeft()', 20);
			}
		}
	}
	this.moveLeft = moveLeft;
	
	function setOpenPanel(nr)
	{
		openPanel = nr;
	}
	this.setOpenPanel = setOpenPanel;
	
	function getOpenPanel()
	{
		return openPanel;
	}
	this.getOpenPanel = getOpenPanel;
	
	function reset()
	{
		try
		{
			clearInterval(timer);
		} catch (e) { }
		timer=null;
	}
	this.reset = reset;
}
function showPanel(id, nr)
{
	
	try
	{
		document.getElementById(id).className = document.getElementById(id).className.replace(/open([0-9])+/gi, 'open'+nr);
	} catch (e) {}
}

function Rotator(newId, newCount)
{
	var open = 0;
	var count = newCount;
	var id = newId;
	
	function hideAll()
	{
		for(var a=1;a<=newCount;a++)
		{
			document.getElementById(id+a).style.display='none';
		}
	}
	
	function show()
	{
		document.getElementById(id+open).style.display='block';
	}
	
	function next()
	{
		hideAll();
		open++;
		if(open>count) { open=1; }
		show();
	}
	this.next = next;
	
	function prev()
	{
		hideAll();
		open--;
		if(open<1) { open=count; }
		show();
	}
	this.prev = prev;
}