cSlide = 1;
cswelcome = 1;
csbrowse = 1;
cswork = 1;
lastSlide = 1;

function ScrollSection(Loc, Req) 
{
	// SET CURRENT FOR ARROWS
	if ( Loc == 'welcome' ) { cswelcome = Req; }
	if ( Loc == 'browse' ) { csbrowse = Req; }
	if ( Loc == 'work' ) 
	{
		cswork = Req;
		//$('ind'+lastSlide).src = 'images/ind/dot_off.jpg'; 
		//$('ind'+cswork).src = 'images/ind/dot_on.jpg';
		//$('ind').style.left = ((cswork-1)*33) + 'px';
		lastSlide = cswork;
	}
	
	sReq = Loc+Req;
	oSet = Loc+'1'
	
	if (cSlide == sReq) 
	{
		return;
	}
	
	lastSection = cSlide;
	cSlide = sReq;
	
	theScroll = $(Loc);
	position = findElementPos($(sReq));
	
	if (oSet != "") 
	{
		offsetPos = findElementPos($(oSet));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
}

// SCROLLS
var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, 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;
	scrollanim.timer = setInterval("scrollHorizAnim();", 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++;
	}
}

// EASING
function sineInOut(t, b, c, d) { return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b; }

// FIND POSITION OF ELEMENT
function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	
	do 
	{
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} 
	while(elemFind = elemFind.offsetParent)

	//console.log("Found element "+elemFind+" at "+elemY+"/"+elemX);

	return Array(elemX, elemY);
}

function sArrow(Dir, Loc, Max) {
	
	//alert(cswork);
	
	if ( Loc == 'welcome' ) { cs = cswelcome; }
	if ( Loc == 'browse' ) { 
		cs = csbrowse; 
		//Toggle the indicator
		if ( oneortwo==1 ) { 
			$('nav-left').src='images/browseL.png';
			$('nav-ind1').src='images/dot_off.png';
			$('nav-ind2').src='images/dot_on.png';
			oneortwo=2; 
		} else { 
			$('nav-left').src='images/browseR.png'; 
			$('nav-ind1').src='images/dot_on.png';
			$('nav-ind2').src='images/dot_off.png';
			oneortwo=1; 
		}
	}
	if ( Loc == 'work' ) { cs = cswork; pwork = cswork; }
	
	if ( Dir == 'L' ) {
		if ( cs == 1 ) { cs = Max; } else { cs--;	}
	} else if ( Dir == 'R' ) {
		if ( cs == Max ) { cs = 1; } else { cs++; }
	}
	
	
	//alert(cs);
	
	ScrollSection(Loc, cs);
}

//Disable Text
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}