//グローバル変数の設定
var speed = 8;
var offset = 10;
var distance;
var scrollLimit;
var links;
var mintanchorElm;
var currentScrollTop;

//ロード時の処理
dom.event.addEventListener(window, 'load', initScroll);
function initScroll(){
	initScrollLimit();
	documentHeight = getDocumentHeight();
	links = document.getElementsByTagName('a');
	for(var i=0; i<links.length; i++){
		if(links[i].href && links[i].href.indexOf('#') != -1){
			if(((links[i].pathname == location.pathname) || ('/'+links[i].pathname == location.pathname)) && (links[i].search == location.search)){
				links[i].anchor = links[i].href.substr(links[i].href.indexOf('#')+1,  links[i].href.length);
				links[i].anchorPos = dom.misc.getElementAbsPos(document.getElementById(links[i].anchor)).y;
				dom.event.addEventListener(links[i], 'click', startScroll);
			}else if(isInternalLink(links[i].getAttribute('href')) == true){
				if(links[i].href.indexOf('?')!=-1){
					links[i].href = links[i].href.replace('#','&mintanchor=');
				}else{
					links[i].href = links[i].href.replace('#','?mintanchor=');
				}
			}
		}
	}
	if(location.search.indexOf('mintanchor')!=-1){
		setTimeout("fromOtherScroll()",400);
	}
}

//リサイズ時の処理
dom.event.addEventListener(window, 'resize', initScrollLimit);

//スクロールの限界を初期化する
function initScrollLimit(){
	scrollLimit = getDocumentHeight() - dom.misc.getWindowSize().height;
}

//スクロールの初期化
function startScroll(evt){
	var target = dom.event.target(evt);
	destinationY = Math.min(target.anchorPos-offset,scrollLimit);
	currentScrollTop = getScrollTop();
	if(destinationY>currentScrollTop){
		destinationY += 7;
	}
	count = 0;
	myTimer = setInterval("mintscroll()",10);
	dom.event.preventDefault(evt);
}

//ミントスクロール
function mintscroll(){
	distance = destinationY - currentScrollTop;
	currentScrollTop += distance/speed;
	scrollTo(0,currentScrollTop);
	count ++;
	if(count>50){
		clearInterval(myTimer);
	}
}

//ドキュメントの高さを取得するファンクション
function getDocumentHeight(){
	return document.body.offsetHeight;
}

//現在のスクロール量を取得するファンクション
function getScrollTop(){
	return document.body.scrollTop|| document.documentElement.scrollTop;
}

//他のページからクエリを渡された場合のスクロールの初期化
function fromOtherScroll(){
	var queryStrings = location.search.split('=');
	var mintanchor = queryStrings[queryStrings.length-1];
	mintanchorElm = document.getElementById(mintanchor);
	var mintanchorPos = dom.misc.getElementAbsPos(mintanchorElm).y;
	destinationY = Math.min(mintanchorPos-offset,scrollLimit)+7;
	count = 0;
	currentScrollTop = 0;
	myTimer = setInterval("mintscroll()",10);
	//ハイライト
	mintanchorElm.style.backgroundColor = "#ffdddd";
	mintanchorElm.firstChild.style.backgroundColor = "#ffdddd";
}

//ハイライト解除
dom.event.addEventListener(document, 'mousedown', removeHighlight);
function removeHighlight(){
	if(mintanchorElm){
		mintanchorElm.style.backgroundColor = "#ffffff";
		mintanchorElm.firstChild.style.backgroundColor = "#ffffff";
	}
}

//内部リンク判別
function isInternalLink(url) {
	if (typeof(url) != 'string') {
		return false;
	} else if (url.indexOf(location.protocol + '//' + location.host) == 0) {
		return true;
	} else if (url.match(/^https?:\/\//)) {
		return false;
	} else {
		return true;
	}
}
