function sineInOut(t, b, c, d)
{
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

// Scroll an LM browser in the specified direction
function Scroller( direction, tnroot, currentTileVar, numTile, showTile, scrollBox, baseTile )
{
	currentTile = eval(currentTileVar);
	
	getEl = document.getElementById('set'+currentTile);
	getEl.className = "tileOff";
	
	if (direction < 0)
	{
		if (currentTile == 0)
		{
			toTile = numTile - showTile;
		}
		else
		{
			toTile = currentTile - 1;
		}
	}
	else
	{
		if ( currentTile == (numTile - showTile))
		{
			toTile = 0;
		}
		else
		{
			toTile = currentTile + 1;
		}
	}
	
	getEl = document.getElementById('set'+toTile);
	getEl.className = "tileOn";
	
	// Go to the requested tile
	ScrollToTile( $(tnroot+toTile), $(scrollBox), $(baseTile) );
	eval(currentTileVar + "=" + toTile);
	//alert("Scrolled to " + toTile + " and til var now " + eval(currentTileVar) );
}

function iconScroll(getTile,getBox,getBase,currentTileVar)
{
	currentTile = eval(currentTileVar);
	
	getEl = document.getElementById('set'+currentTile);
	getEl.className = "tileOff";
	
	ScrollToTile( $('slide'+getTile), $(getBox), $(getBase) );
	
	getEl = document.getElementById('set'+getTile);
	getEl.className = "tileOn";
	
	eval(currentTileVar + "=" + getTile);
}

function ScrollToTile( toTile, scrollBox, baseTile)
{
    toTilePos = Position.positionedOffset(toTile);
    offsetPos = Position.positionedOffset(baseTile);
    scrollStart( scrollBox, scrollBox.scrollLeft, toTilePos[0] - offsetPos[0], "horiz");
}

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);
	if (scrollanim.timer != null)
	{
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz")
	{
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else
	{
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollHorizAnim()
{
    if (scrollanim.time > scrollanim.duration)
	{
        clearInterval(scrollanim.timer);
        scrollanim.timer = null;
    }
    else
	{
        move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
        scrollanim.element.scrollLeft = move;
        scrollanim.time++;
    }
}
