﻿/* スクロール */

function _BLINK(){
	this.BlinkSpeed=200;
	this.BlinkTimes=0;
	this.Bar=['h1','h2','h3','h4','h5','h6','h7','caption'];
	this.HighlightStyle={backgroundColor:'#ff9999',color:'#009900'};
	this.ScrollPixels=100;
	this.ScrollSpeed=70;
	this.ConstLeft=true;
	this.ConstTop=false;

	this.DefaultStyle={};//don't change;
	return this;
}

function _Blink(){
	var wlo=window.location || document.location;
	this.host=wlo.host;
	this.protocol=wlo.protocol;
	this.agt=navigator.userAgent.toLowerCase();
	this.platform=navigator.platform.toLowerCase();
	this.path=_getPath(wlo.pathname);
	this.filename=_getFilename(wlo.pathname);
	this.location=this.protocol+"//"+this.host+this.path+this.filename;
	this.tim=null;
	this.toX=0;
	this.toY=0;
	this.scrollx=0;
	this.scrolly=0;
	this.lights=0;
	this.hash=null;
	this.bar=null;

	this.Compare=function(anchor){
		var loc='';
		if(!anchor){return false;}
		var url=_getLocation(anchor);
		if(!url){return true;}
		if(url.indexOf('://')>=0) {
			loc=url;
		} else if(url.indexOf('/')==0) {
			loc=this.protocol+'://'+this.host+url;
		} else {
			var dirs=this.path.split('/');
			var udirs=url.split('/');
			for(var i=0;i<udirs.length;i++){
				if(udirs[i]=='..'){
					dirs.length--; //macie, < ie5.5
					//dirs.pop();
				} else if(udirs[i]=='.') {
					; // none
				} else {
					dirs[dirs.length]=udirs[i]; //macie, < ie5.5
					//dirs.push(udirs[i]);
				}
			}
			loc=this.protocol+'://'+this.host+'/'+dirs.join('/');
		}
		if(this.location==loc){
			return true;
		}
		return false;
	}

	this.doClick=function(e){
		e=e||window.event;
		Blink._Do(e);
		return false;
	}// doClick
	this.doKeydown=function(e){
		e=e||window.event;
		if(e.keyCode!=13){return true;}
		Blink._Do(e);
		return false;
	}//doKeyDown
	this._Do=function(e){
		var tar=e.target|| e.srcElement;
		while(!!tar && (tar.nodeName!='A')){// && !tar.href){
			tar=tar.parentNode;
		}
		if(!tar)return;
		Blink.hash=_getHash(tar.href);
		if(!Blink.hash)return;//need ?
		if(e.preventDefault){
			e.preventDefault();
			e.stopPropagation();
		} else {
			e.returnValue=false;
			e.cancelBubble=true;
		}
		if(Blink.tim){Blink.Reset();clearTimeout(Blink.tim);}
		Blink.scrollx=_MAX(window.pageXOffset,window.scrollLeft,window.scrollX,document.body.scrollLeft,document.documentElement.scrollLeft);
		Blink.scrolly=_MAX(window.pageYOffset,window.scrollTop,window.scrollY,document.body.scrollTop,document.documentElement.scrollTop);
		var p=_Pos(Blink.hash);
		if(this.agt.indexOf('safari') != -1){
			p.y-=50;
			if(p.y<0)p.y=0;
		}
		if(BLINK.ConstLeft){
			Blink.toX=Blink.scrollx;
		} else {
			var docw=_MAX(document.width,document.body.offsetWidth,document.body.clientWidth);
			var winw;
			if(document.all && !window.opera){
				winw=_MAX(document.documentElement.offsetWidth);
			} else {
				winw=_MAX(window.innerWidth);
			}
			if(doch==winh){
				Blink.toX=p.x;
			} else {
				var n=docw-winw;
				Blink.toX=(n<p.x)?n:p.x;
			}
		}
		if(BLINK.ConstTop){
			Blink.toY=Blink.scrolly;
		} else {
			var doch=_MAX(document.height,document.body.offsetHeight,document.body.clientHeight);
			var winh;
			if(document.all && !window.opera){
				winh=_MAX(document.documentElement.offsetHeight);
			} else {
				winh=_MAX(window.innerHeight);
			}
			if(doch==winh){
				Blink.toY=p.y;
			} else {
				var n=doch-winh;
				Blink.toY=(n<p.y)?n:p.y;
			}
		}
		Blink.lights=-10;
		var o=null;
		var ha=GetElement(Blink.hash);
		if(!ha)return;
		for(i=0;i<BLINK.Bar.length;i++){
			var b=ha.getElementsByTagName(BLINK.Bar[i]);
			if(!b){continue;}
			o=b.item(0);
			if(!!o)break;
		}
		if(!o){o=ha;}
		Blink.bar=o;
		for(var i in BLINK.HighlightStyle){
			BLINK.DefaultStyle[i]=_getStyle(o, i);
		}
		Blink._Scroll();
	}
	this._Scroll=function(){
		clearTimeout(Blink.tim);
		if(Blink.scrolly==Blink.toY && Blink.scrollx==Blink.toX){
			Blink.lights=0;
				if(this.platform.indexOf('mac')!=-1){
					if((this.agt.indexOf('msie')<0) && (this.agt.indexOf('safari')<0)){
						location.hash=Blink.hash;
					}
				}
				if (this.agt.indexOf('opera 8.01') != -1) {
					document.location.reload();
				}
			Blink._Blink();
			return;
		}
		if(Blink.scrolly==Blink.toY){
			;
		} else if(Blink.scrolly<Blink.toY) {
			Blink.scrolly+=Math.min(Math.ceil((Blink.toY-Blink.scrolly)/2),BLINK.ScrollPixels);
		} else {
			var s=Math.ceil((Blink.scrolly-Blink.toY)/2);
			if(s<BLINK.ScrollPixels){
				Blink.scrolly-=s;
			} else {
				Blink.scrolly-=Math.min(s,BLINK.ScrollPixels);
			}
		}
		if(Blink.scrollx==Blink.toX){
			;
		} else if(Blink.scrollx<Blink.toX) {
			Blink.scrollx+=Math.min(Math.ceil((Blink.toX-Blink.scrollx)/2),BLINK.ScrollPixels);
		} else {
			Blink.scrollx-=Math.max(Math.ceil((Blink.scrollx-Blink.toX)/2),BLINK.ScrollPixels);
		}
		window.scrollTo(Blink.scrollx, Blink.scrolly);
		Blink.tim=setTimeout("Blink._Scroll()",BLINK.ScrollSpeed);
	}
	this._Blink=function(){
	clearTimeout(Blink.tim);
		if(Blink.lights>=BLINK.BlinkTimes){
			Blink.Reset();
			//if(this.platform.indexOf('mac')!=-1){
			//	if((this.agt.indexOf('msie')!=-1) || (this.agt.indexOf('safari')!=-1)){
			//		location.hash=Blink.hash;
			//	}
			//}
			return;
		}
		Blink.lights++;
		if(Blink.lights%2!=0){
			Blink.Highlight();
		} else {
			Blink.Reset();
		}
		Blink.tim=setTimeout("Blink._Blink()",BLINK.BlinkSpeed);
	}

	this.Reset=function(){
		var os=Blink.bar.style;
		for(var i in BLINK.DefaultStyle){
			os[i]=BLINK.DefaultStyle[i];
		}
	}
	this.Highlight=function(){
		var os=Blink.bar.style;
		for(var i in BLINK.HighlightStyle){
			os[i]=BLINK.HighlightStyle[i];
		}
	}
	function _getPath(url){
		var str=url.toString();
		var agt=(!this.agt)?navigator.userAgent.toLowerCase():this.agt;
		var platform=(!this.platform)?navigator.platform.toLowerCase():this.platform;
		if((platform.indexOf('mac')!=-1) && (agt.indexOf('msie')!=-1)){
			var RE=/(.*\/)([^\/]+)$/;
			if(RE.test(str)){
				return RegExp.$1;
			}
		} else {
			var RE=RegExp("(.*\/)([^\/]+)$", "");
			if(RE.exec(str)){
				return RegExp.$1;
			}
		}
		return str;
	}
	function _getLocation(url){
		var loc=url.toString();
		loc=loc.replace(/#.*/, "");
		loc=loc.replace(/\?.*/, "");
		return loc;
	}
	function _getHash(url){
		var str=url.toString();
		var p=str.indexOf('#');
		if(p>=0)return str.substring(p+1, str.length);
		else return '';
	}
	function _getFilename(url){
		var str=url.toString();
		var agt=(!this.agt)?navigator.userAgent.toLowerCase():this.agt;
		var platform=(!this.platform)?navigator.platform.toLowerCase():this.platform;
		if((platform.indexOf('mac')!=-1) && (agt.indexOf('msie')!=-1)){
			var RE=/([^\/]+)$/;
			if(RE.test(str)){
				return RegExp.$1;
			}
		} else {
			var RE=RegExp("([^/]+)$","");
			if(RE.exec(str)){
				return RegExp.$1;
			}
		}
		return "";
	}
	function _MAX() {
		var mx=0;
		for(var i=0;i<_MAX.arguments.length;i++){
			if(isNaN(_MAX.arguments[i]))continue;
			if(mx<_MAX.arguments[i]){
				mx=_MAX.arguments[i];
			}
		}
		return mx;
	}
	function _getStyle(el, prop){
		if(el.style[prop]){
			return el.style[prop];
		} else if(document.defaultView && document.defaultView.getComputedStyle) {
			prop=prop.replace(/([A-Z])/g, "-$1");
			prop=prop.toLowerCase();
			return document.defaultView.getComputedStyle(el,"").getPropertyValue(prop);
		} else if(el.currentStyle) {
			return el.currentStyle[prop];
		} else {
			return null;
		}
	}
	function _Pos(id){
		var el=GetElement(id);
		var pos={x:0,y:0,w:0,h:0};
		var platform=(!this.platform)?navigator.platform.toLowerCase():this.platform;
		if(el.getBoundingClientRect){
			var rect=el.getBoundingClientRect();
			var top=_MAX(document.body.scrollTop,document.documentElement.scrollTop,window.scrollY,document.body.scrollTop);
			var left=_MAX(document.body.scrollLeft,document.documentElement.scrollLeft,window.scrollX,document.body.scrollLeft);
			var width=_MAX(rect.width,el.clientWidth);
			var height=_MAX(rect.height,el.clientHeight);
			pos={x:left+rect.left,y:top+rect.top,w:width,h:height};
		} else if(document.getBoxObjectFor) {
			var rect=document.getBoxObjectFor(el);
			var top=_MAX(_getStyle(el, "borderTopWidth"),0);
			var left=_MAX(_getStyle(el, "borderLeftWidth"),0);
			var width=_MAX(rect.width,el.clientWidth);
			var height=_MAX(rect.height,el.clientHeight);
			pos={x:rect.x-left,y:rect.y-top,w:width,h:height};
		} else {
			pos.w=el.clientWidth;
			pos.h=el.clientHeight;
			while (el) {
				pos.x += parseInt(el.offsetLeft);
				pos.y += parseInt(el.offsetTop);
				el = el.offsetParent;
			}
			pos.x-=document.body.offsetLeft; // -1* html margin-left
			pos.y-=document.body.offsetTop; // -1* html margin-top
			if (platform.indexOf("mac")!=-1){
				pos.x+=document.body.offsetLeft;
				pos.y+=document.body.offsetTop;
			}
		}
		return pos;
	}
	function GetElement(en) {
		if(!document.getElementById || !document.getElementsByName){ return null; }
		if(typeof en != 'string'){ return en; }
		if(!!document.getElementById(en)){ return document.getElementById(en); }
		var els=document.getElementsByName(en);
		if(!els){
			return null;
		} else if(!!els.item) {
			if(els.item.length==0 || els.item.length>1){
				return null;
			} else {
				return els.item(0);
			}
		} else if(els.length==0 || els.length>1) {
			return null;
		} else {
			return els[0];
		}
	}

	this.Create=function(){
		var o=document.body || document.documentElement||document;
		if(!o)return;
		Parse(o);
		if(!!window.RemoveEventListener){
			window.RemoveEventListener('load', Blink.Create, false);
		} else if(!!window.dettachEvent) {
			window.dettachEvent('onload', Blink.Create);
		}
		BLINK.BlinkTimes*=2;
		if(!!window.location.hash){
			var hash=location.hash.substring(1, location.hash.length);
			Blink._Do({target:{href:'#'+hash,nodeName:'A'}});
		}
	}
	function Parse(o){
		if(o.hasChildNodes){
			for(var i=0;i<o.childNodes.length;i++){
				oc=o.childNodes[i];
				if(!!oc && oc.nodeType==1){
					Parse(oc);
				}
			}
		}
		while(!!o && o.nodeType!=1){
			o=o.nextSibling;
		}
		if(!o)return;
		var hre=o.href;
		if(!Blink.Compare(hre)){return;}
		hre=(hre.indexOf('#')>=0)?hre.substring(hre.indexOf('#')+1, hre.length):hre;
		var to=GetElement(hre);
		if(!to) return;
		o.onclick=Blink.doClick;
		o.onkeydown=Blink.doKeydown;
		o=o.nextSibling;
	}
}//_Blink

_Blink.prototype.BlinkInit=function(){
	if(!!window.addEventListener){
		window.addEventListener('load', Blink.Create, false);
	} else if(!!window.attachEvent) {
		window.attachEvent('onload', Blink.Create);
	} else {
		// mac-ie etc.
		setTimeout('Blink.Create()', 1000);
	}
	return true;
}

var BLINK=_BLINK();
var Blink=new _Blink();
Blink.BlinkInit();
