//
// Scrolling pages Horizontally ...
//

var numPages = 9;
var currPageIdx = 1;
var currPagesX = 0;

function initSite(startIdx) {
	currPageIdx = startIdx;
	currPagesX = -startIdx * getWindowWidth();
}

function getWindowWidth() {
	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    // myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    // myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    // myHeight = document.body.clientHeight;
  }
  return myWidth; // Math.max(920, 
}
function setWindowWidth(wd) {
	if( typeof( window.innerWidth ) == 'number' ) {
		window.innerWidth = wd;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  	document.documentElement.clientWidth = wd;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  	document.body.clientWidth = wd;
  }
}

function pagesNextPrev(val) {
	if(currPageIdx + val > -1 && currPageIdx + val < numPages) {
		pagesScrollX(currPageIdx + val);
	}
}

function pagesScrollX(pos) {
	
	// var page1Div = document.getElementById("page_0");
	
	windowWidth = getWindowWidth();
	
	var startX = currPagesX; // parseInt(page1Div.style.left);
	// if(isNaN(startX)) startX = - windowWidth;
	
	var steps = 50 + 25*(Math.abs(currPageIdx - pos)-1);
	
	var goX = -(pos) * windowWidth;
	currPageIdx = pos;
		
	for(var i=0; i<=steps; i++) {
		var frac = 0.5 - Math.cos(Math.PI * i/steps)/2;
		var px = (1-frac)*startX + frac*goX;
		setTimeout("setPagesX("+px+");", i*20);
	}
}

function setPagesX(px) {
	
	currPagesX = px;
	
	windowWidth = getWindowWidth();

	var i=0
	pageDiv = document.getElementById("page_"+i);
	while(pageDiv) {
		pageDiv.style.left = String(px+i*windowWidth)+"px";
		i++;
		pageDiv = document.getElementById("page_"+i);
	}
	// document.getElementById("debug").innerHTML = String(px);
}

function adjustToWindowWidth() {
	var goX = -(currPageIdx) * getWindowWidth();
	setPagesX(goX);
	
	// document.getElementById("debug").innerHTML = String(goX);
}

function pageLoaded() {
	/*
if(getWindowWidth() < 920) {
		setWindowWidth(920);
	}
*/
	adjustToWindowWidth();
	
	// 	alert("done.");

}




