
var head_list = [];

if (window.addEventListener) window.addEventListener('load',init_head,false); 
else if (document.addEventListener) document.addEventListener('load',init_head,false); 
else if (window.attachEvent) window.attachEvent('onload',init_head); 
else { 
	if (typeof window.onload=='function') { 
		var oldload=window.onload; 
		window.onload=function(){ 
			oldload(); 
			init_head(); 
		} 
	} else window.onload=init_head; 
}

window.onresize = init_head;

function init_head() {
	head_list = [];
	var divs = document.getElementById('we').getElementsByTagName("a");
	for(var i=0; i<divs.length; i++)
         {
			head_list.push(divs[i]);
			divs[i].screenXY = getScreenXY(divs[i]);
		}	
	document.onmousemove = move_head;
}

function move_head(e) {
	if (!e) var e = window.event;
	
	for(var i=0; i<head_list.length; i++) {
		var x = head_list[i].screenXY[0]+head_list[i].offsetWidth/2;
		var y = head_list[i].screenXY[1]+head_list[i].offsetHeight/2;
		var w = head_list[i].offsetWidth;
		var scrXY = getScrollXY();

		if (Math.abs(scrXY[0]+e.clientX-x)<head_list[i].offsetWidth/2 && Math.abs(scrXY[1]+e.clientY-y)<head_list[i].offsetHeight/2)
			head_list[i].style.backgroundPosition = "0px 0px";
		else {
			var ang = Math.atan2(y-(scrXY[1]+e.clientY), scrXY[0]+e.clientX-x);
			ang += (ang<0) ? Math.PI*2 : 0;
			if (ang<Math.PI/8 || ang>=Math.PI/8*15)
				head_list[i].style.backgroundPosition = -w*3+"px 0";
			if (ang<Math.PI/8*3 && ang>=Math.PI/8)
				head_list[i].style.backgroundPosition = -w*2+"px 0";
			if (ang<Math.PI/8*5 && ang>=Math.PI/8*3)
				head_list[i].style.backgroundPosition = -w+"px 0";
			if (ang<Math.PI/8*7 && ang>=Math.PI/8*5)
				head_list[i].style.backgroundPosition = -w*8+"px 0";
			if (ang<Math.PI/8*9 && ang>=Math.PI/8*7)
				head_list[i].style.backgroundPosition = -w*7+"px 0";
			if (ang<Math.PI/8*11 && ang>=Math.PI/8*9)
				head_list[i].style.backgroundPosition = -w*6+"px 0";
			if (ang<Math.PI/8*13 && ang>=Math.PI/8*11)
				head_list[i].style.backgroundPosition = -w*5+"px 0";
			if (ang<Math.PI/8*15 && ang>=Math.PI/8*13)
				head_list[i].style.backgroundPosition = -w*4+"px 0";	
		}
	}
}


function getScreenXY(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent)	
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
