
// -------------------------------------------------------------------------------------------------
// scrollto.js version 0.5
// http://www.glide.co.jp
// -------------------------------------------------------------------------------------------------

var G_PAGE_SCROLL_ID;

function pageScrollX(x) {
	
	ResetPageScroll();
	
	var xpos     = x;
	var xp       = 0;
	var yp       = 0;
	var millisec = 16;
	var speed    = 5;
	var size        = getPageSize();
	var pageWidth   = size[0];
	var windowWidth = size[2];
	var scrollMaxX  = pageWidth - windowWidth;
	
	if (typeof x == "number") {
		xpos = x;
		
	} else if (typeof x == "object") {
		xpos = getElementPosition(x)[0];
		
	} else if (typeof x == "string") {
		if (x.indexOf("#") >= 0) {
			xpos = getElementPosition(x.match(/#.*/i)[0].substring(1, x.match(/#.*/i)[0].length))[0];
		} else {
			xpos = getElementPosition(x)[0];
		}
	}
	
	// Webkit, Gecko
	if (typeof(window.pageXOffset) == "number") {
		xp = window.pageXOffset;
		yp = window.pageYOffset;
		loop();
		
	// IE(Standards mode)
	} else if (document.documentElement.clientHeight && document.documentElement) {
		xp = document.documentElement.scrollLeft;
		yp = document.documentElement.scrollTop;
		loopIEStandards();
		
	// Other IE
	} else if (document.body) {
		xp = document.body.scrollLeft;
		yp = document.body.scrollTop;
		loopIE();
	}
	
	if (xpos >= scrollMaxX) xpos = scrollMaxX;
	
	//alert(xpos + " / " + scrollMaxX);
	
	function loop() {
		var x = window.pageXOffset;
		result(x, loop);
	}
	
	function loopIEStandards() {
		var x = document.documentElement.scrollLeft;
		result(x, loopIEStandards);
	}
	
	function loopIE() {
		var x = document.body.scrollLeft;
		result(x, loopIE);
	}
	
	function result(x, func) {
		G_PAGE_SCROLL_ID = window.setTimeout(func, millisec);
		xp += (xpos - x) / speed;
		window.scrollTo(xp, yp);
		
		//if(x == xpos) {
		if(x < xpos + 5 && x > xpos - 5) {
			window.clearTimeout(G_PAGE_SCROLL_ID);
			window.scrollTo(xpos, yp);
		}
	}
}


function pageScrollY(y) {
	
	ResetPageScroll();
	
	var ypos     = y;
	var yp       = 0;
	var xp       = 0;
	var millisec = 16;
	var speed    = 5;
	var size         = getPageSize();
	var pageHeight   = size[1];
	var windowHeight = size[3];
	var scrollMaxY   = pageHeight - windowHeight;
	
	if (typeof y == "number") {
		ypos = y;
		
	} else if (typeof y == "object") {
		ypos = getElementPosition(y)[1];
		
	} else if (typeof y == "string") {
		if (y.indexOf("#") >= 0) {
			ypos = getElementPosition(y.match(/#.*/i)[0].substring(1, y.match(/#.*/i)[0].length))[1];
		} else {
			ypos = getElementPosition(y)[1];
		}
	}
	
	// Webkit, Gecko
	if (typeof(window.pageXOffset) == "number") {
		xp = window.pageXOffset;
		yp = window.pageYOffset;
		loop();
		
	// IE(Standards mode)
	} else if (document.documentElement.clientHeight && document.documentElement) {
		xp = document.documentElement.scrollLeft;
		yp = document.documentElement.scrollTop;
		loopIEStandards();
		
	// Other IE
	} else if (document.body) {
		xp = document.body.scrollLeft;
		yp = document.body.scrollTop;
		loopIE();
	}
	
	if (ypos >= scrollMaxY) ypos = scrollMaxY;
	
	function loop() {
		var y = window.pageYOffset;
		result(y, loop);
	}
	
	function loopIEStandards() {
		var y = document.documentElement.scrollTop;
		result(y, loopIEStandards);
	}
	
	function loopIE() {
		var y = document.body.scrollTop;
		result(y, loopIE);
	}
	
	function result(y, func) {
		G_PAGE_SCROLL_ID = window.setTimeout(func, millisec);
		yp += (ypos - y) / speed;
		window.scrollTo(xp, yp);
		
		//if(y == ypos) {
		if(y < ypos + 5 && y > ypos - 5) {
			window.clearTimeout(G_PAGE_SCROLL_ID);
			window.scrollTo(xp, ypos);
		}
	}
}


function pageScroll(x, y) {
	
	ResetPageScroll();
	
	var xpos;
	var ypos;
	var yp       = 0;
	var xp       = 0;
	var millisec = 16;
	var speed    = 5;
	var size         = getPageSize();
	var pageWidth    = size[0];
	var pageHeight   = size[1];
	var windowWidth  = size[2];
	var windowHeight = size[3];
	var scrollMaxX   = pageWidth - windowWidth;
	var scrollMaxY   = pageHeight - windowHeight;
	
	if (typeof x == "number") {
		xpos = x;
		
	} else if (typeof x == "object") {
		xpos = getElementPosition(x)[0];
		
	} else if (typeof x == "string") {
		if (x.indexOf("#") >= 0) {
			
			xpos = getElementPosition(x.match(/#.*/i)[0].substring(1, x.match(/#.*/i)[0].length))[0];
		} else {
			xpos = getElementPosition(x)[0];
		}
	}
	
	if (typeof y == "number") {
		ypos = y;
		
	} else if (typeof y == "object") {
		
		ypos = getElementPosition(y)[1];
		
	} else if (typeof y == "string") {
		if (y.indexOf("#") >= 0) {
			ypos = getElementPosition(y.match(/#.*/i)[0].substring(1, y.match(/#.*/i)[0].length))[1];
		} else {
			ypos = getElementPosition(y)[1];
		}
	}
	
	if (ypos >= scrollMaxY) ypos = scrollMaxY;
	if (xpos >= scrollMaxX) xpos = scrollMaxX;
	
	// Webkit, Gecko
	if (typeof window.pageYOffset == "number") {
		xp = window.pageXOffset;
		yp = window.pageYOffset;
		loop();
		
	// IE(Standards mode)
	} else if (document.documentElement.clientHeight && document.documentElement) {
		xp = document.documentElement.scrollLeft;
		yp = document.documentElement.scrollTop;
		loopIEStandards();
		
	// Other IE
	} else if (document.body) {
		xp = document.body.scrollLeft;
		yp = document.body.scrollTop;
		loopIE();
	}
	
	function loop() {
		var cx = window.pageXOffset;
		var cy = window.pageYOffset;
		result(cx, cy, loop);
	}
	
	function loopIEStandards() {
		var cx = document.documentElement.scrollLeft;
		var cy = document.documentElement.scrollTop;
		result(cx, cy, loopIEStandards);
	}
	
	function loopIE() {
		var cx = document.body.scrollLeft;
		var cy = document.body.scrollTop;
		result(cx, cy, loopIE);
	}
	
	function result(x, y, func) {
		G_PAGE_SCROLL_ID = window.setTimeout(func, millisec);
		xp += (xpos - x) / speed;
		yp += (ypos - y) / speed;
		window.scrollTo(xp, yp);
		
		//if(y == ypos) {
		if((y < ypos + 5 && y > ypos - 5) && (x < xpos + 5 && x > xpos - 5)) {
			window.clearTimeout(G_PAGE_SCROLL_ID);
			window.scrollTo(xpos, ypos);
		}
	}
}


function ResetPageScroll() {
	if(G_PAGE_SCROLL_ID) {
		window.clearTimeout(G_PAGE_SCROLL_ID);
	}
}


function getPageSize() {
	
	var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight, arraySizes;
	
	// Gecko
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
		
	// Webkit, IE(Standards mode)
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		
	// Other IE
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	// not IE
	if (window.innerHeight) {
		windowWidth  = window.innerWidth;
		windowHeight = window.innerHeight;
		
	// IE(Standards mode)
	} else if (document.documentElement.clientHeight && document.documentElement) {
		windowWidth  = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		
	// Other IE
	} else if (document.body) {
		windowWidth  = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	yScroll < windowHeight ? pageHeight = windowHeight : pageHeight = yScroll;
	xScroll < windowWidth  ? pageWidth  = windowWidth  : pageWidth  = xScroll;
	
	arraySizes = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	
	return arraySizes;
}


function getElementPosition(element) {
	
	var target = typeof element == "string" ? document.getElementById(element) : element;
	var left   = 0;
	var top    = 0;
	
	while (target) {
		left  += target.offsetLeft;
		top   += target.offsetTop;
		target = target.offsetParent;
	}
	
	return new Array(left, top);
}
