/**
 * @author Abaco.hu
 */
/*
 * Scroll to top position
 * 
 */

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/, "");
}

function addClassName(elem, className) {
	removeClassName(elem, className);
	elem.className = (elem.className + " " + className).trim();
}

function removeClassName(elem, className) {
	elem.className = elem.className.replace(className, "").trim();
}

function setHeight() {

	var contentH = document.getElementById('pScroller').offsetHeight;
	var footerH = 55;
	var maxH = window.innerHeight - footerH - 1;

	if (mode == 'advanced') {
		document.getElementById('pW').style.height = maxH + 'px';
	}
	if (mode == 'default') {
		if (contentH < maxH) {
			document.getElementById('pW').style.height = maxH + 'px';
		}
	}

}

function loaded() {

	// hide footer
	document.getElementById('pF').style.visibility = "hidden";

	if (unescape(self.document.location.hash.substring(1)).length == 0) {
	    //hide url bar
		window.scrollTo(0, 1);
	}

	// display footer
	// setTimeout("document.getElementById('pF').style.display='';", 1000);
	document.getElementById('pF').style.visibility = '';

	// set "body" height
	setHeight();

	if (mode == 'advanced') {
		myScroll = new iScroll('pScroller', {
			vScrollbar : true
		});
		
		//?touchstart touchend
		
		if (anchorId != '') {
			anchor(anchorId);
		}	
	}		
}

document.addEventListener('DOMContentLoaded', loaded, false);

// Detect whether device supports orientationchange event, otherwise fall back
// to the resize event.
window.addEventListener('onorientationchange' in window ? 'orientationchange'
		: 'resize', setHeight, false);


function anchor(anchor) {

	if (mode == 'advanced') {
		myScroll.scrollToElement('#'+anchor, '0s');
		return false;
	}
	
	if (mode == 'default') {
		if (anchor == 'pH') {			
			window.scrollTo(0, 1);			
			return false;			
		}
	}

}

function anchorPage(link) {
	if (mode == 'advanced') {
		window.location.href = link;
		return false;
	}
}


