//javascript scrollSlider
//update : 
//
function scrollPosition() {
	var sp = new Object();
	sp.x = document.documentElement.scrollLeft || document.body.scrollLeft;
	sp.y = document.documentElement.scrollTop || document.body.scrollTop;
	return sp;
}//end scrollPosition

function elementPosition(elem) {
	var ep = new Object();
	ep.x = elem.offsetLeft;
	ep.y = elem.offsetTop;
	while(elem.offsetParent) {
		elem = elem.offsetParent;
		ep.x += elem.offsetLeft;
		ep.y += elem.offsetTop;
	}//end while
	return ep;
}//end elementPosition

function screenSize() {
	var scr = new Object();
	var brwOpera = (navigator.userAgent.toLowerCase().indexOf('opera') + 1 ? 1 : 0);
	var brwSafari = (navigator.userAgent.toLowerCase().indexOf('safari') + 1 ? 1 : 0);
	
	if(!brwOpera && !brwSafari) {
		scr.x = document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth;
		scr.y = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
	} else {
		scr.x = window.innerWidth;
		scr.y = window.innerHeight;
	}//end if
	return scr;
}//end screenSize

function documentSize(){
	var doc = new Object();
	doc.x = document.documentElement.scrollWidth || document.body.scrollWidth;
	doc.y = document.documentElement.scrollHeight || document.body.scrollHeight;
	return doc;
}//end documentSize

function maxScrollValue() {
	var scrSize = screenSize();
	var docSize = documentSize();
	
	var scrollMax = new Object();
	scrollMax.x = docSize.x - scrSize.x;
	scrollMax.y = docSize.y - scrSize.y;
	
	return scrollMax;
}//end maxScrollValue

//実行処理
function scrollSlider(ID) {
	
	var idName = document.getElementById(ID);
	var pos = scrollPosition();
	var target = elementPosition(idName);
	
	var moveX = target.x - pos.x;
	var moveY = target.y - pos.y;
	
	var direction = new Object();
	direction.x = moveX > 0 ? 'p' : 'm';
	direction.y = moveY > 0 ? 'p' : 'm';
	
	var mxs = maxScrollValue();
	target.x = target.x > mxs.x ? mxs.x : target.x;
	target.y = target.y > mxs.y ? mxs.y : target.y;
	
	move(target, direction);
	
}//end scrollSlider

//動作処理
function move(targert, direction) {
	var pos = scrollPosition();
	var arrival = new Object();
	var timerID;
	
	if(direction.x == 'p') {
		arrival.x = Math.floor(pos.x + ((targert.x - pos.x) / 5)) + 1;
	} else {
		arrival.x = Math.max(Math.floor(pos.x + ((targert.x - pos.x) / 5)),targert.x);
	}
	
	if(direction.y == 'p') {
		arrival.y = Math.floor(pos.y + ((targert.y - pos.y) / 5)) + 1;
	} else {
		arrival.y = Math.max(Math.floor(pos.y + ((targert.y - pos.y) / 8)), targert.y);
		//alert(arrival.y);
	}
	
	scrollTo(arrival.x, arrival.y);
	
	if(direction.y == 'p' && direction.x == 'p') {
			if(arrival.y < targert.y || arrival.x < targert.x){
				timerID = setTimeout( function(){ move(targert,direction) },10);
				if(arrival.y == targert.y) {
					clearTimeout(timerID);
				}
			}
		}
		else if(direction.y == 'p' && direction.x == 'm') {
			if(arrival.y < targert.y || arrival.x != targert.x){
				timerID = setTimeout( function(){ move(targert,direction) },10);
				if(arrival.y == targert.y) {
					clearTimeout(timerID);
				}
			}
		}
		else if(direction.y == 'm' && direction.x == 'p') {
			if(arrival.y != targert.y || arrival.x < targert.x){
				timerID = setTimeout( function(){ move(targert,direction) },10);
				if(arrival.y == targert.y) {
					clearTimeout(timerID);
				}
			}
		}
		else if(direction.y == 'm' && direction.x == 'm') {
			if(arrival.y != targert.y || arrival.x != targert.x){
				timerID = setTimeout( function(){ move(targert,direction) },10);
				if(arrival.y == targert.y) {
					clearTimeout(timerID);
				}
			}
		}
	
}//end move


function removeMove(){
		dom.event.addEventListener(document,'mousedown',function(){clearTimeout(timerID)},false);
		dom.event.addEventListener(document,'mousewheel',function(){clearTimeout(timerID)},false);
		dom.event.addEventListener(document,'keydown',function(){clearTimeout(timerID)},false);
}//end removeMove

