//window scroll (modern browser only)
(function () {
	var isIE = false, isWK = false, isFX = false, isOP = !!(window.opera);
	if (window.ActiveXObject) {
		isIE = true;
		var isIE7 = !!(window.XMLHttpRequest);
		var isIE6 = !isIE7;
	}
	else if (document.childNodes && !document.all && !navigator.taintEnabled) {
		isWK = true;
		var isWK420 = !!(document.evaluate);
		var isWK419 = !isWK420;
	}
	else if (document.getBoxObjectFor != null) isFX = true;
	var isCSS1Compat = (document.compatMode == "CSS1Compat");
	//
	var scrj = 1;
	var softScrollBack = function softScrollBack() {
		if (isWK) {
			var scdist = document.body.scrollTop;
		} else if (isIE && !isCSS1Compat) {
			var scdist = document.body.scrollTop;
		} else {
			var scdist = document.body.parentNode.scrollTop;
		}
		if (scrj < 50 && scdist) {
			scdist = (scdist > 2) ? Math.ceil(scdist * .2) : 1;
			scrj++;
			window.scrollBy(0, -scdist);
			setTimeout(arguments.callee, 20);
		} else {
			window.scrollTo(0, 0);
			scrj = 1;
		}
	};
	//
	addEventToElement(window, 'load', function () {
		var els = document.getElementsByClassName('to-pagetop');
		for (var idx = 0; idx < els.length; idx++) {
			var el = els[idx];
			var link = el;
			if (el.tagName.toLowerCase() != 'a') {
				link = el.getElementsByTagName('a').item(0);
			}
			addEventToElement(link, 'mouseover', function () {
				addClassToElement(el, 'hover');
			});
			addEventToElement(link, 'mouseout', function () {
				removeClassFromElement(el, 'hover');
			});
			addEventToElement(link, 'click', function (event) {
				if (event && event.preventDefault)	event.preventDefault();
				else window.event.returnValue = false;
				softScrollBack();
			});
			// Footer...
			if (el.parentNode.id == 'footer') {
				var onResize = (function (el) {
					return function () {
						var clientHeight = (function () {
							if (isIE) return (isCSS1Compat
								? Math.max(document.documentElement.offsetHeight, document.documentElement.clientHeight)
								: Math.max(document.body.offsetHeight, document.body.clientHeight));
							if (isWK419) return window.innerHeight;
							return document.documentElement.clientHeight;
						})();
						var scrollHeight = (function () {
							if (isIE) return (isCSS1Compat
								? Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight)
								: Math.max(document.body.offsetHeight, document.body.scrollHeight));
							if (isWK) return document.body.scrollHeight;
							return document.documentElement.scrollHeight;
						})();
						if (clientHeight < scrollHeight) {
							el.style.visibility = 'visible';
						} else {
							el.style.visibility = 'hidden';
						}
					};
				}) (el);
				onResize.timer = null;
				addEventToElement(window, 'resize', function () {
					clearTimeout(onResize.timer);
					onResize.timer = setTimeout(onResize, 50);
				});
				onResize();
			}
		}
	});
})();
