var curSection = lastSection = 'home';
var curColor = 'FFA109';
var sections = {
	home:{index:0,color:'0080D2',nextColor:'FFA109'},
	about:{index:1,color:'FFA109',nextColor:'9BC936'},
	products:{index:2,color:'9BC936',nextColor:'0080D2'},
	services:{index:3,color:'0080D2',nextColor:'FFA109'},
	technology:{index:4,color:'FFA109',nextColor:'9BC936'},
	process:{index:5,color:'9BC936',nextColor:'0080D2'},
	contact:{index:6,color:'0080D2',nextColor:'FFA109'}
};
var sectionWidth = 915;

var curNav = prevNav = 1;
var navHeight = 400;

function scrollToSection(section) {
	if ( section == curSection ) return;
	//$('movingLogo').show();
	
	lastSection = curSection;
	curSection = section;
	
	//console.log('GO TO '+section)
	
	$(lastSection+'_but').src = 'images/menu/'+lastSection+'.png';
	$(curSection+'_but').src = 'images/menu/on/'+section+'.png';
	
	lastSectionIndex = sections[lastSection].index;
	curSectionIndex = sections[curSection].index;
	
	//console.log ('going from '+lastSectionIndex+' to '+curSectionIndex);

	new Ajax.Updater(curSection,curSection+'/index.html',{method:'get'});	
	deactivateNav(curSection);
	if ( $(curSection+'_nav_1') ) $(curSection+'_nav_1').addClassName('active');
	
	startFrom = lastSectionIndex * sectionWidth;
	endOff  = curSectionIndex * sectionWidth;
	
	colorTween = new ColorTween($('continue').style, 'backgroundColor', Tween.regularEaseOut, curColor, sections[curSection].nextColor, 1);
	colorTween.start();
		
	curColor = sections[curSection].nextColor;
	
	curNav = prevNav = 1;
	
	scrollIt($('contentScroll'),startFrom,endOff,'horiz');
}

var curPage;
var h;
var timer;

function gotoNav(nav,page) {
	if ( nav == curNav ) return;
	curNav = nav;
	curPage = page;
	
	deactivateNav(curSection);
	$(curSection+'_nav_'+curNav).addClassName('active');

	new Effect.Fade($(curSection),{queue:'end',afterFinish:getNavContent,duration:.5});
	//h = $(curSection).getHeight()+150;
	//shrinkNav(0,(h*-1));
	
}

function getNavContent() {
	new Ajax.Updater(curSection,curSection+'/'+curPage,{method:'get',onComplete:showNav});
}

function showNav() {
	new Effect.Appear($(curSection),{queue:'end',duration:.5});
}


function deactivateNav(section) {
	items = $$('#'+section+'_nav li');
	for (i=0;i<items.length;i++) {
		items[i].removeClassName('active');
	}
}

function submitContact() {
	args=Form.serialize('cForm');
	new Ajax.Updater('contactForm','/contact/submit-form.php',{parameters:args});
}

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

function scrollIt(elem, start, end, direction) {
	 //console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scroller.timer != null) {
		clearInterval(scroller.timer);
		scroller.timer = null;
	}
	scroller.time = 0;
	scroller.begin = start;
	scroller.change = end - start;
	scroller.duration = 25;
	scroller.element = elem;
	
	if (direction == "horiz") {
		scroller.timer = setInterval("scrollHoriz();", 15);
	}
	else {
		scroller.timer = setInterval("scrollVert();", 15);
	}
}

function scrollVert()
{
	if (scroller.time > scroller.duration) {
		clearInterval(scroller.timer);
		scroller.timer = null;
	}
	else {
		move = sineInOut(scroller.time, scroller.begin, scroller.change, scroller.duration);
		scroller.element.scrollTop = move; 
		scroller.time++;
	}
}

function scrollHoriz()
{
	if (scroller.time > scroller.duration) {
		clearInterval(scroller.timer);
		scroller.timer = null;
		//$('movingLogo').hide();	
	}
	else {
		move = sineInOut(scroller.time, scroller.begin, scroller.change, scroller.duration);
		scroller.element.scrollLeft = move;
		scroller.time++;
	}
}

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

function cubicInOut(t, b, c, d)
{
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}