/******************
followyou.js
ver. 2007-0730_0.4b
(C) 2007 Wataru Kanzaki, Wings-Winds, http://www.wi-wi.jp/
http://home.wi-wi.jp/software/followyou/
******************/
function _Followyou(){
	this.Pixcels=20;
	this.Speed=50;
	this.ConstLeft=true;
	this.ConstTop=false;
	this.Elements=['followyou']; // className
	this.Immediate=false;
	
	/*********************************
	Do not edit below.
	*********************************/
	this.tim=null;
	this.els=[];
	this.ws=[0,0];
	this.onDo=false;
	this.useTimer=false;
	this._Do=function(){
		Followyou.onDo=true;
		if(!Followyou.useTimer && Followyou.tim){Followyou.tim=clearTimeout(Followyou.tim);}
		Followyou.ws[1]=GetScrollLeft();//_MAX(window.pageXOffset,window.scrollX,document.body.scrollLeft,document.documentElement.scrollLeft);
		Followyou.ws[0]=GetScrollTop();//_MAX(window.pageYOffset,window.scrollY,document.body.scrollTop,document.documentElement.scrollTop);
		for(var i=0;i<Followyou.els.length;i++){
			Followyou.els[i][3]=_Pos(Followyou.els[i][0]);
			//var top=_MAX(document.body.scrollTop,document.documentElement.scrollTop,window.scrollY,window.pageYOffset);
			//var left=_MAX(document.body.scrollLeft,document.documentElement.scrollLeft,window.scrollX,window.pageXOffset);
		}
		Followyou._Run();
	};//_Do
	this._Run=function(e){
		var move=false;
		this.onDo=false;
		for(var i=0;i<this.els.length;i++){
			var el=this.els[i];
			var tox, toy;
			var p=el[3];
			if(this.ConstLeft){
				tox=el[2];
			}else{
			if(this.Immediate){
				tox=this.ws[1]+el[2];
			}else{
				var def=el[2]+this.ws[1]-p.x;
				var sign=(def<0)?-1:1;
				def=Math.abs(def);
				tox=(this.Pixcels<def)?this.Pixcels:parseInt(def/2+0.9);
				to*=sign;
				tox+=p.x;
			}}
			if(Followyou.ConstTop){
				toy=el[1];
			}else{
			if(this.Immediate){
				toy=this.ws[0]+el[1];
			}else{
				var def=el[1]+this.ws[0]-p.y;
				var sign=(def<0)?-1:1;
				def=Math.abs(def);
				toy=(this.Pixcels<def)?this.Pixcels:parseInt(def/2+0.9);
				toy*=sign;
				toy+=p.y;
			}}
			if(this.onDo){return;}
			el[3]={y:toy,x:tox};
			if(toy!==null) el[0].style.top=toy+'px';
			if(tox!==null) el[0].style.left=tox+'px';
			if((toy!==null && toy!=el[1]+this.ws[0]) || (tox!==null && tox!=el[2]+this.ws[1])){move=true;}
		}
		if(move && !this.onDo && !this.useTimer){
			this.tim=setTimeout('Followyou._Run()', this.Speed);
		}
	};// _Run
	
	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;
	}//_Max
	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;
		}
	}//_GetStyle
	function _Pos(el){
		var pos={x:0,y:0,w:0,h:0};
		pos.y=_MAX(parseInt(_GetStyle(el, "top")),0);
		pos.x=_MAX(parseInt(_GetStyle(el, "left")),0);
		var y=(pos.y==0);
		var x=(pos.x==0);
		var chkstyle=function(el){
			var elstyle=_GetStyle(el, 'position');
			var pa=el.parentNode;
			var pastyle;
			if(!!pa){
				var pastyle=_GetStyle(pa, 'position');
			}
			if((elstyle!='absolute' && elstyle!='relative') || (!!pa && pastyle!='absolute' && pastyle!='relative')){
				return true;
			}
			return false;
		}
		if(el.getBoundingClientRect){
			var rect=el.getBoundingClientRect();
			pos.w=_MAX(parseInt(_GetStyle(el, "width")),0) || _MAX(rect.width,el.clientWidth);
			pos.h=_MAX(parseInt(_GetStyle(el, "height")),0) || _MAX(rect.height,el.clientHeight);
			if(chkstyle(el)){
				//if(x) pos.x=rect.left+_MAX(document.body.scrollLeft,document.documentElement.scrollLeft,window.scrollX,window.pageXOffset);
				//if(y) pos.y=rect.top+_MAX(document.body.scrollTop,document.documentElement.scrollTop,window.scrollY,window.pageYOffset);
				if(y) pos.y=rect.top+GetScrollTop();
				if(x) pos.x=rect.left+GetScrollLeft();
			}
		}else if(document.getBoxObjectFor){
			var rect=document.getBoxObjectFor(el);
			pos.w=_MAX(parseInt(_GetStyle(el, "width")),0) || _MAX(rect.width,el.clientWidth);
			pos.h=_MAX(parseInt(_GetStyle(el, "height")),0) || _MAX(rect.height,el.clientHeight);
			if(chkstyle(el)){
				if(x) pos.x=rect.x-_MAX(parseInt(_GetStyle(el, "borderLeftWidth")),0);
				if(y) pos.y=rect.y-_MAX(parseInt(_GetStyle(el, "borderTopWidth")),0);
			}
		}else{
			pos.w=_MAX(parseInt(_GetStyle(el, "width")),0) || el.clientWidth;
			pos.h=_MAX(parseInt(_GetStyle(el, "height")),0) || el.clientHeight;
			var elstyle=_GetStyle(el, 'position');
			var pa=el.parentNode;
			var pastyle;
			if(!!pa){
				var pastyle=_GetStyle(pa, 'position');
			}
			if((elstyle!='absolute' && elstyle!='relative') || (!!pa && pastyle!='absolute' && pastyle!='relative')){
				var platform=navigator.platform.toLowerCase();
				var ua=navigator.userAgent.toLowerCase();
				var isS=false;
				if(ua.indexOf('applewebkit')!=-1){
					av=navigator.appVersion.toLowerCase();
					if(av.indexOf('/85.')!=-1) isS=true;
				}
				while (el) {
					if(x) pos.x += parseInt(el.offsetLeft);
					if(y) pos.y += parseInt(el.offsetTop);
					el = el.offsetParent;
				}
				if(x) pos.x-=document.body.offsetLeft;
				if(y) pos.y-=document.body.offsetTop;
				if (platform.indexOf("mac")!=-1 && !isS){
					if(x) pos.x+=document.body.offsetLeft;
					if(y) pos.y+=document.body.offsetTop;
				}
			}
		}
		return pos;
	}//_Pos
	this.Recreate=function(){
		Followyou.els=new Array();
		var o=document.body||document.documentElement||document;
		if(!o)return;
		Parse(o);
	};//_Recreate
	this.Create=function(){
		if(!!window.RemoveEventListener){
			window.RemoveEventListener('load', Followyou.Create, false);
		}else if(!!window.dettachEvent){
			window.dettachEvent('onload', Followyou.Create);
		}
		var o=document.body||document.documentElement||document;
		if(!o)return;
		Parse(o);
		
		var ua=navigator.userAgent.toLowerCase();
		if(ua.indexOf('applewebkit')!=-1){
			ua=navigator.appVersion.toLowerCase();
			if(ua.indexOf('/85.')!=-1){
				Followyou.useTimer=true;
			}
		}else if(ua.indexOf('konqueror')!=-1 || ua.indexOf('icab')!=-1){
			Followyou.useTimer=true;
		}else if(!window.opera && (!!navigator.userLanguage && !!navigator.systemLanguage && !!navigator.language)){
			Followyou.useTimer=true;
		}else if(!window.addEventListener && !window.attachEvent){
			Followyou.useTimer=true;
		}
		
		if(Followyou.useTimer){
			this.tim=setInterval('Followyou._Do()', Followyou.Speed);
		}else if(!!window.addEventListener){
			window.addEventListener('scroll', Followyou._Do, false);
		}else if(!!window.attachEvent){
			window.attachEvent('onscroll', Followyou._Do);
		}
		Followyou._Do();
	};//Create
	function Parse(o){
		while(!!o && o.nodeType!=1){
			o=o.nextSibling;
		}
		if(!o)return;
		if(o.hasChildNodes){
			for(var i=0;i<o.childNodes.length;i++){
				oc=o.childNodes[i];
				if(!!oc && oc.nodeType==1){
					Parse(oc);
				}
			}
		}
		var cn=o.className;
		if(!cn){
			return;
		}
		cn+=' ';
		for(var i=0;i<Followyou.Elements.length;i++){
			if (cn.indexOf(Followyou.Elements[i]+' ')==-1){
				continue;
			}
			var p=_Pos(o);
			if(Followyou.ConstLeft) p.x=null;
			if(Followyou.ConstTop) p.y=null;
			Followyou.els[Followyou.els.length]=new Array(o,p.y,p.x);
			var st=_GetStyle(o,'position');
			if(st!='absolute'&&st!='relative'){o.style.position='absolute';}
		}
	}//Parse
	function GetScrollTop(){
		return _MAX(document.body.scrollTop,document.documentElement.scrollTop,window.scrollY,window.pageYOffset);
	}
	function GetScrollLeft(){
		return _MAX(document.body.scrollLeft,document.documentElement.scrollLeft,window.scrollX,window.pageXOffset);
	}
}//_Followyou

_Followyou.prototype.Init=function(){
	var ua=navigator.userAgent.toLowerCase();
	if(ua.indexOf('konqueror')!=-1 && ua.indexOf('applewebkit')==-1){
		setTimeout('Followyou.Create()', 1000);
		return true;
	}
	
	if(!!window.addEventListener){
		window.addEventListener('load', Followyou.Create, false);
	}else if(!!window.attachEvent){
		window.attachEvent('onload', Followyou.Create);
	}else{
		// mac-ie etc.
		//return false;
		setTimeout('Followyou.Create()', 1000);
	}
	return true;
};//

var Followyou=new _Followyou();
Followyou.Init();

