﻿function scrollerClass() {
}
scrollerClass.prototype = {
	points : [],
	offset : {
		x : 0,
		y : 0
	},
	vias : 8,
	timer : null,
	setScroll : function() {
		if (!arguments.length) return false;

		this.resetScroll();

		var scrolled = {
			x : parseInt(window.scrollX || document.scrollLeft || document.body.scrollLeft || document.body.parentNode.scrollLeft || 0),
			y : parseInt(window.scrollY || document.scrollTop  || document.body.scrollTop  || document.body.parentNode.scrollTop  || 0)
		}
		var current = {
			x : parseInt(scrolled.x),
			y : parseInt(scrolled.y)
		}
		var point;
		if (typeof(arguments[0]) == "object") {
			point = {
				x : arguments[0].offsetLeft,
				y : arguments[0].offsetTop
			}
			while (object.offsetParent) {
				object = object.offsetParent;
				point.x += object.offsetLeft;
				point.y += object.offsetTop;
			}
		} else if (typeof(arguments[0]) == "string") {
			var idName = (arguments[0].indexOf("#")) ? (arguments[0].split("#"))[1] : arguments[0];
			var object = document.anchors[idName] || document.getElementById(idName);
			if (!object) return false;

			point = {
				x : object.offsetLeft,
				y : object.offsetTop
			}
			while (object.offsetParent) {
				object = object.offsetParent;
				point.x += object.offsetLeft;
				point.y += object.offsetTop;
			}
/*
			location.hash = arguments[0];
			window.scroll(current.x, current.y);
*/
		} else {
			point = {
				x : arguments[0],
				y : arguments[1]
			}
		}
		point.x = parseInt(point.x - this.offset.x);
		point.y = parseInt(point.y - this.offset.y);
		if (isNaN(point.x) && isNaN(point.y)) return false;

		var contentSize = {
			w : parseInt(document.body.scrollWidth  || document.body.clientWidth  || 0),
			h : parseInt(document.body.scrollHeight || document.body.clientHeight || 0)
		}
		if (window.opera) {
			contentSize.w = document.documentElement.clientWidth;
			contentSize.h = document.documentElement.clientHeight;
		}
		var windowSize = {
			w : parseInt(window.innerWidth  || document.documentElement.clientWidth  || document.body.clientWidth  || 0),
			h : parseInt(window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0)
		}

		if (point.x > (contentSize.w - windowSize.w)) point.x = contentSize.w - windowSize.w;
		if (point.y > (contentSize.h - windowSize.h)) point.y = contentSize.h - windowSize.h;

		if (point.x < 0) point.x = 0;
		if (point.y < 0) point.y = 0;

		if (point.x == current.x && point.y == current.y) {
			if (typeof(this.onscrollcomplete) == "function") this.onscrollcomplete();
			return true;
		}

		do {
			this.points[this.points.length] = {
				x : Math.round(scrolled.x),
				y : Math.round(scrolled.y)
			}
			var move = {
				x : (point.x - scrolled.x)/this.vias,
				y : (point.y - scrolled.y)/this.vias
			}
			scrolled.x = (Math.abs(move.x) < (1/this.vias)) ? point.x : scrolled.x + move.x;
			scrolled.y = (Math.abs(move.y) < (1/this.vias)) ? point.y : scrolled.y + move.y;
		} while (scrolled.x != point.x || scrolled.y != point.y);

		if (this.points[this.points.length-1].x != point.x || this.points[this.points.length-1].y != point.y) {
			this.points[this.points.length] = {
				x : Math.round(point.x),
				y : Math.round(point.y)
			}
		}
		if (typeof(this.onscrollstart) == "function") this.onscrollstart();
		window._scroller = this;
		this.timer = setInterval("window._scroller.startScroll();" , 1);
		return true;
	},
	startScroll : function() {
		var scrolled = {
			x : window.scrollX || document.scrollLeft || document.body.scrollLeft || document.body.parentNode.scrollLeft || 0,
			y : window.scrollY || document.scrollTop  || document.body.scrollTop  || document.body.parentNode.scrollTop  || 0
		}

		if (this.points[0].x != scrolled.x || this.points[0].y != scrolled.y) {
			this.resetScroll();
			return;
		}

		this.points.reverse();
		--this.points.length;
		this.points.reverse();

		if (this.points.length) {
			window.scrollTo(this.points[0].x, this.points[0].y);
		} else {
			this.resetScroll();
			if (typeof(this.onscrollcomplete) == "function") this.onscrollcomplete();
			return;
		}
	},
	resetScroll : function() {
		if (typeof(this.onscrollreset) == "function") this.onscrollreset();
		if (window._scroller && window._scroller != this) {
			window._scroller.resetScroll();
		}
		if (this.timer) {
			clearInterval(this.timer);
		}
		if (!this.vias) {
			this.vias = 1;
		}
		this.points = [];
		return;
	}
}

var scroller = new scrollerClass;
scroller.onscrollstart = function() {
	var header = document.getElementById("header");
	var nav = document.getElementById("pageNavigation");
	var footer = document.getElementById("footer");
	if (nav && (nav.currentStyle || window.opera)) {
		if (!nav.defaultStyle) {
			nav.defaultStyle = {}
			nav.defaultStyle.position = (window.getComputedStyle) ? window.getComputedStyle(nav, "").position : nav.currentStyle.position;
			nav.defaultStyle.display  = (window.getComputedStyle) ? window.getComputedStyle(nav, "").display  : nav.currentStyle.display;
		}
		nav.style.display = "none";
		if (!window.opera) nav.style.position = "absolute";

		if (header) {
			if (!header.defaultStyle) {
				header.defaultStyle = {}
				header.defaultStyle.position = (window.getComputedStyle) ? window.getComputedStyle(header, "").position : header.currentStyle.position;
				header.defaultStyle.visibility = (window.getComputedStyle) ? window.getComputedStyle(header, "").visibility : header.currentStyle.visibility;
				header.defaultStyle.display  = (window.getComputedStyle) ? window.getComputedStyle(header, "").display  : header.currentStyle.display;
			}
			if (!window.opera) header.style.display = "none";
			header.style.visibility = "hidden";
			if (!window.opera) header.style.position = "absolute";
		}

		if (footer) {
			if (!footer.defaultStyle) {
				footer.defaultStyle = {}
				footer.defaultStyle.position = (window.getComputedStyle) ? window.getComputedStyle(footer, "").position : footer.currentStyle.position;
				footer.defaultStyle.display  = (window.getComputedStyle) ? window.getComputedStyle(footer, "").display  : footer.currentStyle.display;
			}
			footer.style.display = "none";
			if (!window.opera) footer.style.position = "absolute";
		}

		if (navigator.userAgent.indexOf("Win") != -1 && navigator.userAgent.indexOf("MSIE") != -1 && !window.opera) {
			document.body.style.backgroundAttachment = "scroll";
		}
	}
}
scroller.onscrollcomplete = scroller.onscrollreset = function() {
	var nav = document.getElementById("pageNavigation");
	var footer = document.getElementById("footer");
	if (nav && (nav.currentStyle || window.opera)) {
		if (nav.defaultStyle) {
			nav.style.position = nav.defaultStyle.position;
			nav.style.display  = nav.defaultStyle.display;
		}

		if (header && header.defaultStyle) {
			header.style.position = header.defaultStyle.position;
			header.style.visibility = header.defaultStyle.visibility;
			header.style.display  = header.defaultStyle.display;
		}

		if (footer && footer.defaultStyle) {
			footer.style.position = footer.defaultStyle.position;
			footer.style.display  = footer.defaultStyle.display;
		}

		if (navigator.userAgent.indexOf("Win") != -1 && navigator.userAgent.indexOf("MSIE") != -1 && !window.opera) {
			document.body.style.backgroundAttachment = "fixed";
		}
	}
}

document.onclick = function(e) {
	if (navigator.userAgent.indexOf("AppleWebKit") != -1) {
		return true;
	}
	if (!e) e = window.event;
	scroller.resetScroll();
	var targetElement = e.target || e.srcElement;
	while (targetElement && (targetElement.tagName && !targetElement.tagName.match(/A(REA)?/i))) {
		targetElement = targetElement.parentNode;
	}
	if (targetElement && targetElement.href) {
		if (scroller.setScroll(targetElement.href)) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}
