var alllinks=document.getElementById("atlasBody").getElementsByTagName("a");
var randomlinks = new Array();
var randomlinksX = new Array();
var randomlinksY = new Array();
for (var i = 0; i < 100; i++) { 
	randomlinks[i] = alllinks[Math.floor(Math.random()*alllinks.length)];
	//document.write(randomlinks[i].innerHTML);
}

for (var i = 0; i < randomlinks.length; i++) { 
	randomlinks[i].name = "random"+i;
	randomlinks[i].id = "random"+i;
	var c = getAnchorPosition("random"+i);
	randomlinksX[i] = c.x;
	randomlinksY[i] = c.y;
}

// <![CDATA[
var ie=document.all;
var nn6=document.getElementById&&!document.all;

var isdrag=false;
var mx,my;
var dobj;

function movemouse(e){
	if (isdrag){
		dobj.style.left = nn6 ? tx + e.clientX - mx : tx + event.clientX - mx;
		dobj.style.top  = nn6 ? ty + e.clientY - my : ty + event.clientY - my;
		return false;
	}
}

function selectmouse(e){
	var fobj       = nn6 ? e.target : event.srcElement;
	var topelement = nn6 ? "HTML" : "BODY";

	while (fobj.tagName != topelement && fobj.className != "dragme"){
		fobj = nn6 ? fobj.parentNode : fobj.parentElement;
	}

	if (fobj.className=="dragme"){
		isdrag = true;
		dobj = fobj;
		tx = parseInt(dobj.style.left+0);
		ty = parseInt(dobj.style.top+0);
		mx = nn6 ? e.clientX : event.clientX;
		my = nn6 ? e.clientY : event.clientY;
		document.onmousemove=movemouse;
		return false;
	}
}

document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
var previouslink = "";
var previousclass = "";
var InternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);
var flashobj = InternetExplorer ? document.getElementById("thepet") : document.embeds[0]; 
var mypet = document.getElementById("mypet");
var direction = 1;
var stopflag = 0;
var y = 2;
var x = 2;
if  ((document.getElementById) && window.addEventListener || window.attachEvent){
	(
	function(){
		var imgh = parseInt(mypet.style.height);  
		var imgw = parseInt(mypet.style.width); 
		var timer = 100; //setTimeout speed.
		var min = 3;    //slowest speed.
		var max = 5;    //fastest speed.
		
		var h,w,r;
		var d = document;
		
		var dir = 45;   //direction.
		var acc = 10;    //acceleration.
		var newacc = new Array(20,30,20);
		var vel = 10;    //initial speed.
		var sev = 10;
		var newsev = new Array(1,-1,2,-2,2,-2,1,-1,2,-2);

		//counters.
		var c1 = 0;    //time between changes.
		var c2 = 0;    //new time between changes.

		var pix = "px";
		var domWw = (typeof window.innerWidth == "number");
		var domSy = (typeof window.pageYOffset == "number");

		if (domWw) r = window;
		else{ 
			if (d.documentElement && typeof d.documentElement.clientWidth == "number" && d.documentElement.clientWidth != 0)
				r = d.documentElement;
			else{ 
			  if (d.body && typeof d.body.clientWidth == "number")
				r = d.body;
			}
		}



		function winsize(){
			var oh,sy,ow,sx,rh,rw;
			if (domWw){
				if (d.documentElement && d.defaultView && typeof d.defaultView.scrollMaxY == "number"){
					oh = d.documentElement.offsetHeight;
					sy = d.defaultView.scrollMaxY;
					ow = d.documentElement.offsetWidth;
					sx = d.defaultView.scrollMaxX;
					rh = oh-sy;
					rw = ow-sx;
				}else{
					rh = r.innerHeight;
					rw = r.innerWidth;
				}
				h = rh - imgh; 
				w = rw - imgw;
			}else{
				h = r.clientHeight - imgh; 
				w = r.clientWidth - imgw;
			}
		}


		function scrl(yx){
			var y,x;
			if (domSy){
				y = r.pageYOffset;
				x = r.pageXOffset;
			}else{
				y = r.scrollTop;
				x = r.scrollLeft;
			}
			return (yx == 0)?y:x;
		}

		function newpath(){
			sev = newsev[Math.floor(Math.random()*newsev.length)];
			acc = newacc[Math.floor(Math.random()*newacc.length)];
			c2 = Math.floor(200+Math.random()*50);
		}


		function moveit(){
		
			if(stopflag ==0 && tempstopflag==0){
				var vb,hb,dy,dx,curr;
				if (acc == 1) vel +=0.1;
				if (acc == 0) vel -=0.1;
				if (vel >= max) vel = max;
				if (vel <= min) vel = min;
				c1++;
				if (c1 >= c2){
					newpath();
					c1=0;
				}
				dir+=sev;
				curr = dir;
				dy = vel * Math.sin(curr*Math.PI/180);
				dx = vel * Math.cos(curr*Math.PI/180);
				
				if(dx > 0){
					if(direction != 1){
						flashobj.changeDirection(1);
						direction = 1;
					}
				}else{
					if(direction !=2 ){
						flashobj.changeDirection(9);
						direction = 2;
					}
				}
				
				y+=dy;
				x+=dx;
				//horizontal-vertical bounce.
				vb = 180-parseInt(dir);
				hb = 0-parseInt(dir);
				
				//Corner rebounds?
				if ((y < 1) && (x < 1)){y = 1; x = 1; dir = 45;flashobj.changeDirection(1);}
				if ((y < 1) && (x > w)){y = 1; x = w; dir = 135;flashobj.changeDirection(1);}
				if ((y > h) && (x < 1)){y = h; x = 1; dir = 315;flashobj.changeDirection(9);}
				if ((y > h) && (x > w)){y = h; x = w; dir = 225;flashobj.changeDirection(9);}
				//edge rebounds.
				
				if (y < 1) {y = 1; dir = parseInt(hb);}  
				if (y > h) {y = h; dir = parseInt(hb);}  
				if (x < 1) {x = 1; dir = parseInt(vb);flashobj.changeDirection(1);} 
				if (x > w) {x = w; dir = parseInt(vb);flashobj.changeDirection(9);} 
				
				//Assign it all to image.
				var overposx = x + scrl(1);
				var overposy = y + scrl(0);
				mypet.style.top = overposy + pix;
				mypet.style.left = overposx + pix;
				
				overposx = overposx + 183;
				overposy = overposy + 137;
				
				for (var posi = 0; posi < randomlinks.length; posi++) { 
					if(previouslink != randomlinks[posi]){
						if(overposx >= randomlinksX[posi] && overposx <= randomlinksX[posi]+30 && overposy >= randomlinksY[posi] && overposy <= randomlinksY[posi]+20){
							//alert(randomlinks[posi]);
							previousclass = randomlinks[posi].className;
							randomlinks[posi].className = "ptned";
							flashobj.checkoutLink("去這裡看看吧!!<br/>口以嗎?! 口以嗎?!&nbsp;<br/><font color='#990000'>[<a href='"+randomlinks[posi]+"' target='_blank'>點這裡!一起去看看...</a>]</font>");
							previouslink = randomlinks[posi];
							stopflag = 1;
						}
					}else{
						previouslink.className = previousclass;
					}
				}
			}
			setTimeout(moveit,timer);
			
		}

		function init(){
			winsize();
			moveit();
		}
		
		if (window.addEventListener){
			window.addEventListener("resize",winsize,false);
			window.addEventListener("load",init,false);
		}else if (window.attachEvent){
			window.attachEvent("onresize",winsize);
			window.attachEvent("onload",init);
		} 
		
		
	})();
}//End.

function standstill(){
	stopflag = 1;
}

function keepwalking(){
	y = parseInt(mypet.style.top)-document.body.scrollTop;
	x = parseInt(mypet.style.left);
	stopflag = 0;
}

function hidePet(isalways) {
	if(isalways){
		window.location.reload(true);
		return false;
	}else{
	
	}
}

function doTempStop(onswitch){
	tempstopflag = onswitch;
	if(onswitch == 1){
		//mypet.style.top = (document.body.clientHeight - 270) + "px";
		//mypet.style.left = "0px";
		mypet.style.top = "0px";
		mypet.style.left = document.body.clientWidth - 480 + 100;
	}
}

if(tempstopflag == 1){
	//mypet.style.top = (document.body.clientHeight - 270) + "px";
	//mypet.style.left = "0px";
	mypet.style.top = "0px";
	mypet.style.left = document.body.clientWidth - 480 + 100;
}

// ]]>