/******************
blinkhash.js
ver. 2007-0801_0.11
(C) 2005-2007 Wataru Kanzaki, Wings-Winds, http://wi-wi.jp/
http://home.wi-wi.jp/software/web/blinkhash/
******************/
function _BLINK(){
	this.BlinkSpeed=200;
	this.BlinkTimes=3;
	this.Bar=['h1','h2','h3','h4','h5','h6','h7','caption'];
	this.HighlightStyle={'background-color':'#ff0000',color:'#009900'};
	this.ScrollPixels=100;
	this.ScrollSpeed=10;
	this.ConstLeft=true;
	this.ConstTop=false;
	this.UseParent=true;

 	this.DefaultStyle={};//do not 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.usrscroll=false;

	this.Compare=function(anchor){
		var loc='';
		if(!anchor){return false;}
		var url=_getLocation(anchor);
		if(!url){return true;}//anchor='#xxx', same as the document
		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--;
				}else if(udirs[i]=='.'){
					; // none
				}else{
					dirs[dirs.length]=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;
	};
	this.doKeydown=function(e){
		e=e||window.event;
		if(e.keyCode!=13){return true;}
		Blink._Do(e);
		return false;
	};
	this._Do=function(e){
		Blink.usrscroll=false;
		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?
		var o=null;
		var ha=GetElement(Blink.hash);
		if(!ha)return;
		if(e.preventDefault){
			e.preventDefault();
			e.stopPropagation();
		}else{
			e.returnValue=false;
			e.cancelBubble=true;
		}
		if(Blink.tim){Blink.Reset();clearTimeout(Blink.tim);}
		Blink.scrollx=GetScrollLeft();
		Blink.scrolly=GetScrollTop();
		var p;
		if(window.opera && ha.nodeName.toUpperCase()=='A' && ha.innerHTML==''){
			var ns=ha.nextSibling;
			while(!!ns && ns.nodeType!=1){
				ns=ns.nextSibling;
			}
			if(!!ns){
				p=_Pos(ns);
			}else{
				return;
			}
		}else{
			p=_Pos(ha);
		}
		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=GetDocumentWidth();
			var winw=(document.all && !window.opera)?_MAX(document.documentElement.offsetWidth):_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=GetDocumentHeight();
			var winh=(document.all && !window.opera)?_MAX(document.documentElement.offsetHeight):_MAX(window.innerHeight);
			if(doch==winh){
				Blink.toY=p.y;
			}else{
				var n=doch-winh;
				Blink.toY=(n<p.y)?n:p.y;
			}
		}
		SEARCHBAR:do{
		if(BLINK.UseParent && ha.nodeName && ha.nodeName.toUpperCase()=='A'){
			var b=ha.parentNode;
			var nodeName=b.nodeName.toUpperCase();
			for(i=0;i<BLINK.Bar.length;i++){
				if(nodeName==BLINK.Bar[i]){
					o=b;
					break SEARCHBAR;
				}
			}
			b=ha.nextSibling;
			while(b && b.nodeType!=1){
				b=b.nextSibling;
			}
			if(!!b){
				var nodeName=b.nodeName.toUpperCase();
				for(i=0;i<BLINK.Bar.length;i++){
					if(nodeName==BLINK.Bar[i]){
						o=b;
						break SEARCHBAR;
					}
				}
			}
		}
		for(i=0;i<BLINK.Bar.length;i++){
			if(!ha.getElementsByTagName)break SEARCHBAR;
			var b=ha.getElementsByTagName(BLINK.Bar[i]);
			if(!b){continue;}
			o=b[0];
			if(!!o)break SEARCHBAR;
		}
		}while(false);
		if(!o){o=ha;}
		Blink.bar=o;
		for(var i in BLINK.HighlightStyle){
			BLINK.DefaultStyle[i]=_getStyle(o, i);
		}
		Blink._Scroll();
	};
	this.Scroll=function(){
		var arlen=arguments.length;
		var arg=arguments;
		var p=null;
		Blink.usrscroll=true;
		if(arlen==1 && isNaN(arg[0])){
			Blink.usrscroll=false;
			Blink.hash=arg[0];
			ha=GetElement(arg[0]);
			if(!ha)return;
			if(!!window.opera && ha.nodeName.toUpperCase()=='A' && ha.innerHTML==''){
				var ns=ha.nextSibling;
				while(!!ns && ns.nodeType!=1){
					ns=ns.nextSibling;
				}
				if(!!ns){
					p=_Pos(ns);
				}else{
					return;
				}
			}else{
				p=_Pos(ha);
			}
			if(this.agt.indexOf('safari') != -1){
				p.y-=50;
				if(p.y<0)p.y=0;
			}
			SEARCHBAR:do{
			if(BLINK.UseParent && ha.nodeName && ha.nodeName.toUpperCase()=='A'){
				var b=ha.parentNode;
				var nodeName=b.nodeName.toUpperCase();
				for(i=0;i<BLINK.Bar.length;i++){
					if(nodeName==BLINK.Bar[i]){
						o=b;
						break SEARCHBAR;
					}
				}
				b=ha.nextSibling;
				while(b && b.nodeType!=1){
					b=b.nextSibling;
				}
				if(!!b){
					var nodeName=b.nodeName.toUpperCase();
					for(i=0;i<BLINK.Bar.length;i++){
						if(nodeName==BLINK.Bar[i]){
							o=b;
							break SEARCHBAR;
						}
					}
				}
			}
			for(i=0;i<BLINK.Bar.length;i++){
				if(ha.getElementsByTagName){
					var b=ha.getElementsByTagName(BLINK.Bar[i]);
					if(!b){continue;}
					o=b[0];
				}else{
					o=ha;
				}
				if(!!o)break SEARCHBAR;
			}
			}while(false);
			if(!o){o=ha;}
			Blink.bar=o;
			for(var i in BLINK.HighlightStyle){
				BLINK.DefaultStyle[i]=_getStyle(o, i);
			}
		}else if(arlen==1){
			p={y:arg[0],x:arg[0]};
		}else if(arlen>1){
			p={y:arg[0],x:arg[1]};
		}else{
			arlen=1;
			p={y:0,x:0};
		}
		if(Blink.tim){Blink.Reset();clearTimeout(Blink.tim);}
		Blink.scrollx=GetScrollLeft();
		Blink.scrolly=GetScrollTop();
		if(BLINK.ConstLeft && arlen==1){
			Blink.toX=Blink.scrollx;
		}else{
			var docw=GetDocumentWidth();
			var winw=(document.all && !window.opera)?_MAX(document.documentElement.offsetWidth):_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 && arlen==1){	
			Blink.toY=Blink.scrolly;
		}else{
			var doch=GetDocumentHeight();
			var winh=(document.all && !window.opera)?_MAX(document.documentElement.offsetHeight):_MAX(window.innerHeight);
			if(doch==winh){
				Blink.toY=p.y;
			}else{
				var n=doch-winh;
				Blink.toY=(n<p.y)?n:p.y;
			}
		}
		Blink._Scroll();
	};
	this._Scroll=function(){
		clearTimeout(Blink.tim);
		if(Blink.scrolly==Blink.toY && Blink.scrollx==Blink.toX){
			Blink.lights=0;
			if(!Blink.usrscroll){
				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{
			Blink.scrolly-=Math.min(Math.ceil((Blink.scrolly-Blink.toY)/2),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) || ((this.agt.indexOf('msie')==-1) && (this.agt.indexOf('safari')==-1))){
				location.hash=Blink.hash;
			}
			if (this.agt.indexOf('opera 8.01') != -1) {
				document.location.reload();
			}
			return;
		}
		if(++Blink.lights%2!=0){
			Blink.Highlight();
		}else{
			Blink.Reset();
		}
		Blink.tim=setTimeout("Blink._Blink()",BLINK.BlinkSpeed);
	};
	this.Reset=function(){
		if(!Blink.bar||!Blink.bar.style)return;
		var os=Blink.bar.style;
		for(var i in BLINK.DefaultStyle){
			os[i]=BLINK.DefaultStyle[i];
		}
	};
	this.Highlight=function(){
		if(!Blink.bar||!Blink.bar.style)return;
		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<arguments.length;i++){
			if(isNaN(arguments[i]))continue;
			if(mx<arguments[i]){
				mx=arguments[i];
			}
		}
		return mx;
	}
	function _getStyle(el, prop){
		if(!!el.style && 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=GetScrollTop();
			var left=GetScrollLeft();
			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(parseInt(_getStyle(el, "borderTopWidth")),0);
			var left=_MAX(parseInt(_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 += el.offsetLeft||el.left||0;
				pos.y += el.offsetTop||el.top||0;
				el = el.offsetParent;
			}
			if(document.body){
				pos.x-=document.body.offsetLeft;
				pos.y-=document.body.offsetTop;
			}
			if (platform.indexOf("mac")!=-1 && !!document.body){
				pos.x+=document.body.offsetLeft;
				pos.y+=document.body.offsetTop;
			}
		}
		return pos;
	}
	function GetElement(en) {
		if(typeof en != 'string'){ return en; }
		if(!document.getElementById || !document.getElementsByName){ return GetElementByLayer(en); }
		var els=document.getElementById(en);
		if(!!els){return els;}
		var els=document.getElementsByName(en);
		if(!els){
			return null;
		}else if(!!els.length && els.length==1){
			return els[0];
		}else if(!!els.item){
			if(els.item.length==0 || els.item.length>1){
				return null;
			}else{
				return els.item(0);
			}
		}else{
			return null;
		}
	}
	function GetElementByLayer(en, doc){
		var el;
		if(document.all){
			el=document.all[en];
		}else if(document.layers){
			doc=doc||document;
			el=doc[en];
			for(var i=0;!el&&i<doc.layers.length;i++){
				el=GetElementByLayer(en, doc.layers[i].document);
			}
		}
		for(var i=0;!el&&i<doc.forms.length;i++){
			el=doc.forms[i][en];
		}
		return el;
	}
	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;
		if(!!o.addEventListener){
			o.addEventListener('click', Blink.doClick, false);
			o.addEventListener('keydown', Blink.doKeydown, false);
		}else if(!!o.attachEvent){
			o.attachEvent('onclick', Blink.doClick);
			o.attachEvent('onkeydown', Blink.doKeydown);
		}else{
			o.onclick=Blink.doClick;
			o.onkeydown=Blink.doKeydown;
		}
		o=o.nextSibling;
	}//
	function GetScrollTop(){
		var db=(!!document.body)?document.body.scrollTop:0;
		var de=(!!document.documentElement)?document.documentElement.scrollTop:0;
		return _MAX(db,de,window.scrollY,window.pageYOffset);
	}//
	function GetScrollLeft(){
		var db=(!!document.body)?document.body.scrollLeft:0;
		var de=(!!document.documentElement)?document.documentElement.scrollLeft:0;
		return _MAX(db,de,window.scrollX,window.pageXOffset);
	}//
	function GetDocumentWidth(){
		if (document.body){
			return _MAX(document.width,document.body.offsetWidth,document.body.clientWidth);
		}else{
			return _MAX(document.width);
		}
	}
	function GetDocumentHeight(){
		if(document.body){
			return _MAX(document.height,document.body.offsetHeight,document.body.clientHeight);
		}else{
			return _MAX(document.height);
		}
	}
}//_Blink
_Blink.prototype.BlinkInit=function(){
	var RE=new RegExp("(.*)\-([a-z])(.*)");
	for(var i in HighlightStyle){
		var v=i;
		while(RE.test(v)){
			v=RegExp.$1+RegExp.$2.toUpperCase()+RegExp.$3;
		}
		HighlightStyle[v]=HighlightStyle[i];
	}
	for(i=0;i<BLINK.Bar.length;i++){
		BLINK.Bar[i]=BLINK.Bar[i].toUpperCase();
	}
	if(!!window.addEventListener){
		window.addEventListener('load', Blink.Create, false);
	}else if(!!window.attachEvent){
		window.attachEvent('onload', Blink.Create);
	}else{
		setTimeout('Blink.Create()', 1000);
	}
	return true;
};

var BLINK=_BLINK();
var Blink=new _Blink();
Blink.BlinkInit();
