// ======================================================================================== 
//             ===== minimalist scrollTo implementation ===== 
// ------------------------------------------------------------------------- 
// script: Gerard Ferrandez - 19 February 2011 
// http://www.dhteumeuleu.com/ 
// use under a CC-BY-NC license 
// ------------------------------------------------------------------------- 
// Inspired by : http://www.deblauwebusdefilm.nl/en/ 
// http://demos.flesler.com/jquery/scrollTo/ 
// ======================================================================================== 
 
"use strict"; 
 
var st = function () { 
	// ----- private vars ----- 
	var pages = [], links = [], tween = {}, 
			div, pane, running, targetPage, currentPage, 
			hashMode, oldHash, updateHash = true; 
	///////////////////////////////////////////////////////////////////////////// 
	// ----- crossbrowsers addEvent ----- 
	function addEvent (o, e, f) { 
		if (window.addEventListener) o.addEventListener(e, f, false); 
		else if (window.attachEvent) r = o.attachEvent('on' + e, f); 
	} 
	///////////////////////////////////////////////////////////////////////////// 
	// ----- on screen resize (optional) ----- 
	var resize = function () { 
		var nw = div.offsetWidth; 
		var nh = div.offsetHeight; 
		for (var i in pages) { 
			var p = pages[i]; 
			var rx = nw / p.div.offsetWidth; 
			var ry = nh / p.div.offsetHeight; 
			var nx = Math.round(p.div.offsetLeft * rx); 
			var ny = Math.round(p.div.offsetTop  * ry); 
			p.div.style.left   = nx + 'px'; 
			p.div.style.top    = ny + 'px'; 
			p.div.style.width  = nw + 'px'; 
			p.div.style.height = nh + 'px'; 
			p.toX = -nx; 
			p.toY = -ny; 
			if (currentPage) goto(currentPage, -1); 
		} 
	}; 
	// ----- initialization ----- 
	var init = function (param) { 
		// ----- attach onclick targets to <a class="scrollTo"> elements ----- 
		pane = document.getElementById(param.paneID); 
		div  = document.getElementById(param.containerID); 
		var a = div.getElementsByTagName('a'); 
		var i = 0, p; 
		while (p = a[i++]) { 
			if (p.className && p.className.indexOf('scrollTo') >= 0) { 
				var h = p.href.split('#')[1]; 
				var o = document.getElementById(h); 
				if (o) { 
					// ---- pages array ---- 
					pages[h] = { 
						div:  o, 
						toX: -o.offsetLeft, 
						toY: -o.offsetTop 
					}; 
					// ---- links array ---- 
					links.push({ 
						a: p, 
						div: o 
					}); 
					// ---- on click ---- 
					p.onclick = function () { 
						goto( 
							this.href.split('#')[1], 
							param.duration 
						); 
						return false; 
					} 
				} 
			} 
		} 
		// ---- resize panes ---- 
		addEvent(window, 'resize', resize); 
		resize(); 
		// ----- on hash change event ----- 
		if ("onhashchange" in window) { 
			// ----- entry page ----- 
			if (location.hash !== '' && location.hash !== "#") { 
				// entry page specified by URL 
				oldHash = location.hash.substring(1); 
				goto(oldHash, -1); 
			} else goto(param.home, -1); 
			// ----- onhashchage event ----- 
			hashMode = true; 
			addEvent(window, 'hashchange', function() { 
				if (location.hash.substring(1) !== oldHash) { 
					oldHash = location.hash.substring(1); 
					if (oldHash == "") { 
						updateHash = false; 
						oldHash = param.home; 
					} 
					goto(oldHash, param.duration); 
				} 
				return false; 
			}); 
		} 
		// ---- remove loader ---- 
		//document.getElementById('loader').style.display = 'none'; 
	}; 
	// ----- goto pageID ---- 
	var goto = function (href, dur) { 
		tween.start = new Date() * 1; 
		tween.duration = dur; 
		tween.fromX = pane.offsetLeft; 
		tween.fromY = pane.offsetTop; 
		tween.toX   = pages[href].toX - tween.fromX; 
		tween.toY   = pages[href].toY - tween.fromY; 
		targetPage = href; 
		// ----- start animation loop ----- 
		if (!running) running = window.setInterval(scrolling, 16); 
	}; 
	// ----- scrolling loop ----- 
	var scrolling = function () { 
		var cTime = (new Date() * 1) - tween.start; 
		if (cTime < tween.duration) { 
			// ----- in-out easing ----- 
			var cur = Math.cos(Math.PI * (cTime / tween.duration)) - 1; 
			pane.style.left = Math.round(-tween.toX * .5 * cur + tween.fromX) + 'px'; 
			pane.style.top  = Math.round(-tween.toY * .5 * cur + tween.fromY) + 'px'; 
		} else { 
			// ----- end of scrolling ----- 
			pane.style.left = Math.round(tween.fromX + tween.toX) + 'px'; 
			pane.style.top  = Math.round(tween.fromY + tween.toY) + 'px'; 
			// ----- stop animation loop ----- 
			window.clearInterval(running); 
			running = false; 
			currentPage = targetPage; 
			// ----- update links class ---- 
			var i = 0, p; 
			while (p = links[i++]) { 
				if (p.div.id == currentPage) { 
					if (p.a.className.indexOf("visited") >= 0 ) { 
						p.a.className = p.a.className.replace("visited", "active"); 
					} else p.a.className += " active"; 
					p.visited = true; 
				} else if (p.visited) { 
					p.a.className = p.a.className.replace("active", "visited"); 
				} 
			} 
			// ----- update browser hash ----- 
			if (hashMode) { 
				if (updateHash) { 
					oldHash = currentPage; 
					window.location.hash = currentPage; 
				} 
				updateHash = true; 
			} 
		} 
	}; 
	return { 
		//////////////////////////////////////////////////////////////////////////// 
		/* ==== public functions ==== */ 
		init : function (param) { 
			addEvent(window, 'load', function () { 
				init(param); 
			}); 
		} 
	} 
}(); 
//////////////////////////////////////////////////////////////////////////////// 
// ---- start script ---- 
st.init({ 
	containerID: "screen", 
	paneID: "pane", 
	home: "", 
	duration: 1000 
}); 
// ---------------------- 
// First things first, but not necessarily in that order... 

