/***************************
	----------------------------
	* This script is based on code found @ Dynamicdrive.com *
	Using:
		- Elastic Trail script (By Philip Winston @ pwinston@yahoo.com, URL: http://www.geocities.com/pwinston/)
		- Cross browser cursor trailer script- By Brian Caputo (bcaputo@icdc.com)
	----------------------------
	(C) 2006 by Charleshan www.Denninger.nl
	http://www.paranoid.demon.nl
	Free for all users, but leave in this header.
		Started:	14-09-2006
		Last change:	21-06-2006
	==========================
	Function / Variable:	What is does:
	var int_MaxDots=#	Number of (#.gif) images, starting from 0 till var_MaxDots. In which # is a number.
	DrawDots(PathToGif);	Relative path to .gif images.
	==========================
	For example:
	var int_MaxDots=6;
	DrawDots("./images/mouse/trail"
		Will draw 7 (0 till 6) ./images/mouse/trail#.gif that follow your mouse!
	==========================
	v1.0T	14-09-2006	Started as Elastic Trail script found @ Dynamicdrive.com.
				Won't work om Mozilla based browsers thoug, so disabeled this function for them...
		15-09-2006	Found the Elastic Trail script @ Dynamicdrive.com.
	v1.0B	21-06-2006	Combined the above scripts, added still some cleanup to do...
		25-06-2006	Added IE 7 int_FixScrollXY.
		28-06-2006	noneed to follow the dot0, use the arr_AniDot[0] array instead.
	==========================
****************************/

//- =Asking for trouble in FireFox! -	var obj_DocEid=document.getElementById;
//- =Asking for trouble in FireFox! -	var obj_DocAll=document.all;
//- =Asking for trouble in FireFox! -	var obj_DocLay=document.layers;
var str_DotLoc="./MouseTrail/images/mouse/trail";
var arr_Dot=new Array();
//var arr_Dot=new Array(str_DotLoc+"1",38,35,str_DotLoc+"2",30,31,str_DotLoc+"3",28,26,str_DotLoc+"4",22,21,str_DotLoc+"5",16,16,str_DotLoc+"6",10,10)
var arr_AniDot=new Array();

var flg_FollowMouse=true;	//Always on for now, could be played with to let dots fall to botton, get thrown, etc.
var con_Bounce=0.75;	//Is percent of velocity retained when bouncing off a wall.
var con_Delay=.01;	//Fixed time step, no relation to real time.
var con_GravityX=0; 	//Positive Xgravity pulls right, negative pulls left.
var con_GravityY=50;	//Positive Ygravity pulls down,	negative up.
var con_Mass=1;		//All the physics is bogus, just picked stuff to make it look okay.
var con_Resistance=10;	//Determines a slowing force proportional to velocity.
var con_SegLen=10;	//Size of one spring in pixels.
var con_Speed=50;	//Animation Speed.
var con_Spring=10;	//Spring constant, stiffness of springs.
//Stopping criterea to prevent endless jittering doesn't work when sitting on bottom since floor doesn't push back so acceleration always as big as gravity.
var con_StopVelocity=0.1;
var con_StopAccelerate=0.1;

var int_OffsetX=0;//15;	//X offset of trail from mouse pointer.	Be carefull with this one, not tested on toolbars!
var int_OffsetY=0;//10;	//Y offset of trail from mouse pointer.	Be carefull with this one, not tested on toolbars!
var int_FixScrollX=0;	//Extra X offset for IE?, 0 for Netscape.
var int_FixScrollY=0;	//Extra Y offset for IE?, 0 for Netscape.
//Initiate Window edges
var int_EdgeRight =screen.width;	//Initialize as full screen ALL browsers.
var int_EdgeBottom=screen.height;
GetEdges();
if(document.all){	//IE browsers
	window.onload	=GetEdges();
	window.onresize	=GetEdges();
}	//ELSE see: Initiate Window edges for Mozilla browsers

//var int_MaxDots=7;	//var int_MaxDots=parseInt(arr_Dot.length/3);
var int_DotSizeX=0;	//=11
var int_DotSizeY=0;	//=11
//DrawDots("./images/mouse/trail");	//You can call this here or from a webpage. Set the path to the trail#.gif's in which # is a number from 1 to int_MaxDots.
function DrawDots(str_DotLoc){	//int_MaxDots,int_DotSizeX,int_DotSizeY){
	var int_DotSizeX=int_MaxDots+4;	//=11
	var int_DotSizeY=int_MaxDots+4;	//=11
	var str_Position=(document.all)?"absolute":"fixed";	//"absolute";	//
	/*	<style type="text/css">
	#fixme {
		//Used by: Netscape 4, IE 4.x-5.0/Win and other lesser browsers will use this.
		position: absolute; left: 20px; top: 10px;
	}
	body > div#fixme {
		//Used by: Opera 5+, Netscape6+/Mozilla, Konqueror, Safari, OmniWeb 4.5+, iCab, ICEbrowser.
		position: fixed;
	}
	</style>	*/
	for (int_Count01=0;int_Count01<=int_MaxDots;int_Count01++){
		arr_Dot[(int_Count01*3  )]=int_Count01;
		arr_Dot[(int_Count01*3+1)]=int_DotSizeX;	//=width
		arr_Dot[(int_Count01*3+2)]=int_DotSizeY;	//=height
		document.writeln("<div id=\"dot"+arr_Dot[(int_Count01*3)]+"\" style=\"position:"+str_Position+";z-index:100;left:"+(int_Count01*int_DotSizeX/2)+";top:"+(int_Count01*int_DotSizeY/2)+";\"><img src=\""+str_DotLoc+arr_Dot[(int_Count01*3)]+".gif\" width="+arr_Dot[(int_Count01*3+1)]+" height="+arr_Dot[(int_Count01*3+2)]+" alt=\"Sparkle!\"></div>");	//Use "top:" and "left:" for a nice startposition;
		//alert("Testing: (int_Count01="+int_Count01+" int_MaxDots="+int_MaxDots+")\narr_Dot["+(int_Count01*3  )+"] = (name)   = '"+arr_Dot[(int_Count01*3  )]+"'\narr_Dot["+(int_Count01*3+1)+"] = (width)  = '"+arr_Dot[(int_Count01*3+1)]+"'\narr_Dot["+(int_Count01*3+2)+"] = (height) = '"+arr_Dot[(int_Count01*3+2)]+"'\n\n"+"<div id=\"dot"+arr_Dot[(int_Count01*3)]+"\" style=\"position:absolute;z-index:100;left:"+(int_Count01*int_DotSizeX/2)+";top:"+(int_Count01*int_DotSizeY/2)+";\"><img src=\""+str_DotLoc+arr_Dot[(int_Count01*3)]+".gif\" width="+arr_Dot[(int_Count01*3+1)]+" height="+arr_Dot[(int_Count01*3+2)]+" alt=\"Sparkle!\"></div>");
		arr_AniDot[int_Count01]=new obj_Dot(int_Count01);
		int_DotSizeX++;	//int_DotSizeX--	//You can increase (++) decrease (--) or keep (remove this line) 'em, up to you!
		int_DotSizeY++;	//int_DotSizeY--	//You can increase (++) decrease (--) or keep (remove this line) 'em, up to you!
	}
	//Hide dot0, is only used to follow the mouse...
	var tmp_Id=document.layers?document.layers["dot0"]:document.getElementById("dot0");
	var tmp_IdStyle=document.layers?tmp_Id:tmp_Id.style;
	tmp_IdStyle.visibility=document.layers?"hide":"hidden";
	//Make dot0 As fat? as a toolbar...
	/*
	if(document.all&&window.print){	//IE.
		arr_Dot[(0*3+1)]=0;	//=int_FixScrollX
		arr_Dot[(0*3+2)]=0;	//=int_FixScrollY
	}else{	//Other.
	*/
		arr_Dot[(0*3+1)]=10;	//=Rigt toolbar.
		arr_Dot[(0*3+2)]=10;	//=Lower toolbar.
	//}
	/*if(flg_Netscape){
		StartAnimate();	//Start right away since they are positioned at 0,0
	}else{*/
		setTimeout("StartAnimate()",1000);	//Let dots sit there for a few seconds since they're hiding on the real bullets
	//}
	//return parseInt(arr_Dot.length/3);	//=int_MaxDots
}

/*for (int_Count=0;int_Count<int_MaxDots;int_Count++){
	createContainer("dot"+int_Count,int_Count*10,int_Count*10,int_Count*3+1,int_Count*3+2,"","<img src=\""+arr_Dot[int_Count*3]+".gif\" border=\"0\" width=\""+arr_Dot[(int_Count*3+1)]+"\" height=\""+arr_Dot[(int_Count*3+2)]+"\">");
}

function createContainer(str_Dot,int_Xpos,int_Ypos,int_Width,int_Height,str_Img,HT,Op,St){
	with (document){
		write((!document.getElementById&&!document.all)?"<layer id=\""+str_Dot+"\" left=\""+int_Xpos+"\" top=\""+int_Ypos+"\" width=\""+int_Width+" height=\""+int_Height+"\"":"<div id=\""+str_Dot+"\""+" style=\"position:absolute;width:"+int_Width+";height:"+int_Height+";");//left:"+int_Xpos+";top:"+int_Ypos+";
		if(St){
			if(document.layers) write(" style=\"");
			write(St+";\" ");
		} else write((document.getElementById||document.all)?"\"":"");
		write((str_Img)?str_Img+">":">");
		write((HT)?HT:"");
		if(!Op) closeContainer(str_Dot);
	}
}

function closeContainer(){
	document.write((document.getElementById||document.all)?"</div>":"</layer>");
}*/

function GetEdges(){
	int_EdgeRight =(document.all)?document.body.clientWidth				:window.innerWidth;
	int_EdgeBottom=(document.all)?document.body.scrollTop+document.body.clientHeight:window.pageYOffset+window.innerHeight;	//document.body.scrollHeight	//Won't work if there's nothing to scroll...
	//alert(int_EdgeRight+" x "+int_EdgeBottom)
}

function GetXpos(str_Dot){
	if(document.getElementById) return parseInt(document.getElementById(str_Dot).style.left);
	else if(document.all) return parseInt(document.all[str_Dot].style.left);
	else return document.layers[str_Dot].left;
}

function GetYpos(str_Dot){
	if(document.getElementById) return parseInt(document.getElementById(str_Dot).style.top);
	else if(document.all) return parseInt(document.all[str_Dot].style.top);
	else return document.layers[str_Dot].top;
}

function MoveDot(int_Count01,int_FutureX,int_FutureY){
	if(!document.all)GetEdges();	//Initiate Window edges for Mozilla browsers
	arr_AniDot[int_Count01].obj.left=Math.min(int_EdgeRight ,int_FutureX+int_OffsetX);
	arr_AniDot[int_Count01].obj.top =Math.min(int_EdgeBottom,int_FutureY+int_OffsetY);
}

function obj_Dot(int_Count01){
	this.int_CurrentX=0;
	this.int_CurrentY=0;
	this.int_FutureX=0;
	this.int_FutureY=0;
	//Used by MoveDot();
	this.obj=(document.getElementById)?document.getElementById("dot"+int_Count01).style:(document.all)?document.all["dot"+int_Count01].style:(document.layers)?document.layers["dot"+int_Count01]:"";
}

function obj_Vector(int_X,int_Y){
	this.int_CurrentX=int_X;
	this.int_CurrentY=int_Y;
}

function StartAnimate(){
	if(document.layers)document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove=FollowMouse;
	setInterval("Animate()",con_Speed);
}

/* This is to line up the bullets with actual LI tags on the page
// Had to add -arr_Dot[(int_Count01*3+1)] to X and 2*arr_Dot[(int_Count01*3+2)] to Y for IE 5, not sure why
// Still doesn't work great.
function setInitPositions(arr_Dot){	//Initialize dot positions to be on top of the bullets in the <ul>
	var startloc=document.all.tags("LI");
	var int_Count01=0;
	for (int_Count01=0;int_Count01<startloc.length&&int_Count01<(int_MaxDots-1);int_Count01++){
		arr_Dot[int_Count01+1].int_CurrentX=startloc[int_Count01].offsetLeft
			startloc[int_Count01].offsetParent.offsetLeft-arr_Dot[(int_Count01*3+1)];
		arr_Dot[int_Count01+1].int_CurrentY=startloc[int_Count01].offsetTop +
			startloc[int_Count01].offsetParent.offsetTop+2*arr_Dot[(int_Count01*3+2)];
	}
	arr_Dot[0].int_CurrentX=arr_Dot[1].int_CurrentX;	//Put 0th dot above 1st (it is hidden)
	arr_Dot[0].int_CurrentY=arr_Dot[1].int_CurrentY-con_SegLen;
}*/

function SpringForce(int_Count01,int_Count02,obj_Spring){	//Adds force in X and Y to spring for dot[int_Count01] on dot[int_Count02]
	var int_FutureX=(arr_AniDot[int_Count01].int_CurrentX-arr_AniDot[int_Count02].int_CurrentX);
	var int_FutureY=(arr_AniDot[int_Count01].int_CurrentY-arr_AniDot[int_Count02].int_CurrentY);
	var int_Len=Math.sqrt(int_FutureX*int_FutureX+int_FutureY*int_FutureY);
	if(int_Len>con_SegLen){
		var int_SpringF=con_Spring*(int_Len-con_SegLen);
		obj_Spring.int_CurrentX+=(int_FutureX/int_Len)*int_SpringF;
		obj_Spring.int_CurrentY+=(int_FutureY/int_Len)*int_SpringF;
	}
}

function Animate(){	//arr_AniDot[0] follows the mouse, though no dot is drawn there.
	var start=0;
	if(flg_FollowMouse){
		//arr_AniDot[0].int_CurrentX=GetXpos("dot0");
		//arr_AniDot[0].int_CurrentY=GetYpos("dot0");
		start=1;
	}
	for (int_Count01=start;int_Count01<=int_MaxDots;int_Count01++){
		var obj_Spring=new obj_Vector(0,0);
		if(int_Count01>0){
			SpringForce(int_Count01-1,int_Count01,obj_Spring);
		}
		if(int_Count01<(int_MaxDots-1)){
			SpringForce(int_Count01+1,int_Count01,obj_Spring);
		}
		//Air Resisitance/Friction.
		var obj_Resistance=new obj_Vector(-arr_AniDot[int_Count01].int_FutureX*con_Resistance,-arr_AniDot[int_Count01].int_FutureY*con_Resistance);
		//Compute new Acceleration, including Gravity.
		var obj_Accelerate=new obj_Vector((obj_Spring.int_CurrentX+obj_Resistance.int_CurrentX)/con_Mass+con_GravityX,(obj_Spring.int_CurrentY+obj_Resistance.int_CurrentY)/con_Mass+con_GravityY);
		//Compute new Velocity.
		arr_AniDot[int_Count01].int_FutureX+=(con_Delay*obj_Accelerate.int_CurrentX);
		arr_AniDot[int_Count01].int_FutureY+=(con_Delay*obj_Accelerate.int_CurrentY);
		//Stop dead so it doesn't jitter when nearly still.
		if(Math.abs(arr_AniDot[int_Count01].int_FutureX)<con_StopVelocity && Math.abs(arr_AniDot[int_Count01].int_FutureY)<con_StopVelocity && Math.abs(obj_Accelerate.int_CurrentX)<con_StopAccelerate && Math.abs(obj_Accelerate.int_CurrentY)<con_StopAccelerate){
			arr_AniDot[int_Count01].int_FutureX=0;
			arr_AniDot[int_Count01].int_FutureY=0;
		}
		//Move to new position.
		arr_AniDot[int_Count01].int_CurrentX+=arr_AniDot[int_Count01].int_FutureX;
		arr_AniDot[int_Count01].int_CurrentY+=arr_AniDot[int_Count01].int_FutureY;
		//Bounce off 3 walls (leave ceiling open).
		if(arr_AniDot[int_Count01].int_CurrentY>=int_EdgeBottom-arr_Dot[(int_Count01*3+2)]-1){
			if(arr_AniDot[int_Count01].int_FutureY>0){
				arr_AniDot[int_Count01].int_FutureY=con_Bounce*-arr_AniDot[int_Count01].int_FutureY;
			}
			arr_AniDot[int_Count01].int_CurrentY=int_EdgeBottom-arr_Dot[(int_Count01*3+2)]-1;
		}
		if(arr_AniDot[int_Count01].int_CurrentX>=int_EdgeRight-arr_Dot[(int_Count01*3+1)]){
			if(arr_AniDot[int_Count01].int_FutureX>0){
				arr_AniDot[int_Count01].int_FutureX=con_Bounce*-arr_AniDot[int_Count01].int_FutureX;
			}
			arr_AniDot[int_Count01].int_CurrentX=int_EdgeRight-arr_Dot[(int_Count01*3+1)]-1;
		}
		if(arr_AniDot[int_Count01].int_CurrentX<0){
			if(arr_AniDot[int_Count01].int_FutureX<0){
				arr_AniDot[int_Count01].int_FutureX=con_Bounce*-arr_AniDot[int_Count01].int_FutureX;
			}
			arr_AniDot[int_Count01].int_CurrentX=0;
		}
		//Move img to new position.
		//if(int_Count01>int_MaxDots)	alert("Moving Dot: '"+int_Count01+"'.\n\t='dot"+int_Count01+"'.");
		//if(int_Count01<0)		alert("Moving Dot: '"+int_Count01+"'.\n\t=' "+int_Count01+"'.");
		MoveDot(int_Count01,arr_AniDot[int_Count01].int_CurrentX,arr_AniDot[int_Count01].int_CurrentY);
	}
}

function FollowMouse(obj_Mouse){
	if(document.all&&window.print){	//Fix IE 4,5,6 and 7.	Found on: http://www.howtocreate.co.uk/fixedPosition.html
		int_FixScrollX=(document.documentElement.scrollLeft)?document.documentElement.scrollLeft:document.body.scrollLeft;	//window.document.body.scrollLeft;
		int_FixScrollY=(document.documentElement.scrollTop) ?document.documentElement.scrollTop :document.body.scrollTop;	//document.body.scrollTop;	//+document.body.scrollHeight-;
	//}else{ //if (document.layers)
	//	int_FixScrollX=window.pageXOffset;
	//	int_FixScrollY=window.pageYOffset;
	}
	//Toolbar BorderCheck
	var int_FixEdgeX=Math.min(int_EdgeRight -arr_Dot[(0*3+1)],(document.all)?event.clientX+int_FixScrollX:obj_Mouse.pageX);
	var int_FixEdgeY=Math.min(int_EdgeBottom-arr_Dot[(0*3+2)],(document.all)?event.clientY+int_FixScrollY:obj_Mouse.pageY);
	arr_AniDot[0].int_CurrentX=int_FixEdgeX;
	arr_AniDot[0].int_CurrentY=int_FixEdgeY;
	//-Only needed for testing-	MoveDot(0,int_FixEdgeX,int_FixEdgeY);
}


