cSlide = 1;
cshome = 1;
csoverons = 1;
lastSlide = 1;

function ScrollSection(Loc, Req) 
{
	// SET CURRENT FOR ARROWS
	if ( Loc == 'home' ) 
	{ cshome = Req; 
	//$('ind'+lastSlide).src = 'images/ind/dot_off.jpg'; 
		//$('ind'+cswork).src = 'images/ind/dot_on.jpg';
		$('ind').style.left = ((cshome-1)*33) + 'px';
		lastSlide = cshome;
	}
	if ( Loc == 'overons' ) 
	{
		csoverons = Req;
		//$('ind'+lastSlide).src = 'images/ind/dot_off.jpg'; 
		//$('ind'+cswork).src = 'images/ind/dot_on.jpg';
		$('ind').style.left = ((csoverons-1)*33) + 'px';
		lastSlide = csoverons;
	}
	
	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 initSlider(Loc)
{
	if ( Loc == 'home' || Loc == 'overons') {
		if(typeof cs == "undefined" && Loc == 'home'){cs = cshome;}
		if(typeof cs == "undefined" && Loc == 'overons'){cs = csoverons;}
		if (cs==1) { 
			$('nav-ind1').src='images/dot_on.png';
			$('nav-ind2').src='images/dot_off.png';
			$('nav-ind3').src='images/dot_off.png';
			$('nav-ind4').src='images/dot_off.png';
		} else if (cs==2) { 
			$('nav-ind1').src='images/dot_off.png'; 
			$('nav-ind2').src='images/dot_on.png';
			$('nav-ind3').src='images/dot_off.png';
			$('nav-ind4').src='images/dot_off.png';
		} else if (cs==3) { 
			$('nav-ind1').src='images/dot_off.png'; 
			$('nav-ind2').src='images/dot_off.png';
			$('nav-ind3').src='images/dot_on.png';
			$('nav-ind4').src='images/dot_off.png';
		} else if (cs==4) { 
			$('nav-ind1').src='images/dot_off.png'; 
			$('nav-ind2').src='images/dot_off.png';
			$('nav-ind3').src='images/dot_off.png';
			$('nav-ind4').src='images/dot_on.png';
		}
	}
}

function sDot(cs, Loc) {
	//Toggle the indicator
	if ( Loc == 'home' || Loc == 'overons') {
		if (cs==1) { 
			$('nav-ind1').src='images/dot_on.png';
			$('nav-ind2').src='images/dot_off.png';
			$('nav-ind3').src='images/dot_off.png';
			$('nav-ind4').src='images/dot_off.png';
		} else if (cs==2) { 
			$('nav-ind1').src='images/dot_off.png'; 
			$('nav-ind2').src='images/dot_on.png';
			$('nav-ind3').src='images/dot_off.png';
			$('nav-ind4').src='images/dot_off.png';
		} else if (cs==3) { 
			$('nav-ind1').src='images/dot_off.png'; 
			$('nav-ind2').src='images/dot_off.png';
			$('nav-ind3').src='images/dot_on.png';
			$('nav-ind4').src='images/dot_off.png';
		} else if (cs==4) { 
			$('nav-ind1').src='images/dot_off.png'; 
			$('nav-ind2').src='images/dot_off.png';
			$('nav-ind3').src='images/dot_off.png';
			$('nav-ind4').src='images/dot_on.png';
		}
		cshome = cs;
	}

	if ( Loc == 'home'){
		timerID = setInterval("RotateHeaders()", 8000);
		function RotateHeaders()
		{
			sArrow("R", Loc, 4);
		}
	}
	ScrollSection(Loc, cs);
}

function sArrow(Dir, Loc, Max) {
	
	//alert(Dir);
	if ( Loc == 'home'){cs = cshome;}
	if ( Loc == 'overons'){cs = csoverons;}
	if ( Loc == 'home' || Loc == 'overons' ) {
		if ( Dir == 'L' ) {
			if ( cs == 1 ) { cs = Max; } else { cs--;	}
		} 
		else if ( Dir == 'R' ) 
		{
			if ( cs-1 == Max ) { cs = 1; } else { cs++; }
		}
	}
	if(cs > Max){
		cs=1;
	}

	if (cs==1) { 
		$('nav-ind1').src='images/dot_on.png';
		$('nav-ind2').src='images/dot_off.png';
		$('nav-ind3').src='images/dot_off.png';
		$('nav-ind4').src='images/dot_off.png'; 
	} else if ( cs==2) { 
		$('nav-ind1').src='images/dot_off.png'; 
		$('nav-ind2').src='images/dot_on.png';
		$('nav-ind3').src='images/dot_off.png';
		$('nav-ind4').src='images/dot_off.png'; 
	} else if ( cs==3) { 
		$('nav-ind1').src='images/dot_off.png'; 
		$('nav-ind2').src='images/dot_off.png';
		$('nav-ind3').src='images/dot_on.png';
		$('nav-ind4').src='images/dot_off.png'; 
	} else if ( cs==4) { 
		$('nav-ind1').src='images/dot_off.png'; 
		$('nav-ind2').src='images/dot_off.png';
		$('nav-ind3').src='images/dot_off.png'; 
		$('nav-ind4').src='images/dot_on.png';
	}
	
	//alert(cs);
	
	if ( Loc == 'home'){cshome = cs;}
	if ( Loc == 'overons'){csoverons = 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"
}
