var p = 1.5;
var intR;

//var numItems = document.getElementById('thumbcontainer').getElementsByTagName('img').length;
var numItems;
var speed = 210;

var pArray= new Array();

var ellipseWidth = 320;
var ellipseHeight = 50;
var picWidth = 120;
var firstRotation = true;

function curveX(x) {
	return ellipseWidth/2*(1+Math.cos(Math.PI*x));
}

function curveY(x) {
	return ellipseHeight/2*(1+Math.sin(Math.PI*x));
}

function setZindeX(n) {

	var cacheArray = new Array();
	for (j=0;j<n;j++)
	{
		cacheArray[j] = new Array(parseInt(document.getElementById('p'+pArray[j]).style.top), (j+1) );
	}
	cacheArray.sort();

	for (j=0;j<cacheArray.length;j++)
	{
		document.getElementById( 'p' + pArray[(parseInt(cacheArray[j][1])-1)] ).style.zIndex=j+1;
	}

}

function scroll(spd) {
	
	p+=(spd/10000);
	for (i=0;i<numItems;i++)
	{
		layer = document.getElementById('p'+pArray[i]);


		//animate location
		layer.style.left = parseInt(curveX(p+2*pArray[i]/numItems))-layer.offsetWidth+"px";
		layer.style.top =  parseInt(curveY(p+2*pArray[i]/numItems))+"px";

		if (parseInt(layer.style.top) == (ellipseHeight-1) || parseInt(layer.style.top) < 2)
		{
			setZindeX(numItems);
			if(p>2 && firstRotation ==  true)
			{	pause();
				firstRotation = false;
				getInfo(1);
				return;
			}
		}
		
		//animate size
		document.images['pic'+pArray[i]].width = 20+picWidth/ellipseHeight*parseInt(layer.style.top);
		//document.images['sh'+pArray[i]].width = 20+picWidth/ellipseHeight*parseInt(layer.style.top);

		//animate opacity
		setOpacity(layer, 100/ellipseHeight*parseInt(layer.style.top));
	}
	if (!intR) intR = setInterval("scroll("+spd+")",17);
}

function pause() {	
	lastPos = p;
	if (intR) clearInterval(intR);
	intR=false;
}


function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	obj.style.filter = "alpha(opacity:"+opacity+")";
	obj.style.MozOpacity = opacity/100;
	obj.style.opacity = opacity/100;
}


function initScroll() {
	numItems = document.getElementById('thumbcontainer').getElementsByTagName('img').length;
	
	for(i=0;i<numItems;i++)
		pArray[i] = ''+(i+1);
		
	preloadImage('scroll_right');
	preloadImage('scroll_left');
	
	addEvent(document.getElementById('scroll_left'), 'mouseover', function(){imgOver('scroll_left');document.getElementById('scroll_left').style.cursor = 'pointer'; scroll(-150);});
	addEvent(document.getElementById('scroll_left'), 'mouseout', function(){imgOut('scroll_left');pause();});
	addEvent(document.getElementById('scroll_right'), 'mouseover', function(){imgOver('scroll_right');document.getElementById('scroll_right').style.cursor = 'pointer';scroll(150);});
	addEvent(document.getElementById('scroll_right'), 'mouseout', function(){imgOut('scroll_right');pause();});
	
	scroll(speed);
}

