var linkArray       = new Array();
var offColour       = new ColourObject("#1010FF");
var onColour        = new ColourObject("#000030");
var delay           = 1;
var onSubdivisions  = 30;
var offSubdivisions = 30;

function start(){

	var i = 0;
	var b = document.body.innerHTML.toString();
	var c = b.indexOf("<A", 0);
	if(c < 0) c = b.indexOf("<a", 0);
	while(c >= 0){
		var pos = c;
		var z = b.substring(0, pos + 2);
		addLink();
		z += " id = 'link" + i + "'";
		z += " onMouseOver = 'JavaScript:linkArray[" + i + "].linkOn()'";
		z += " onMouseOut = 'JavaScript:linkArray[" + i + "].linkOff()'";
		z += b.substring(pos + 2);
		b = z;
		i++;
		c = b.indexOf("<A", pos + 1);
		if(c < 0) c = b.indexOf("<a", pos + 1);
	}
	document.body.innerHTML = b;
}

function addLink(){
	var l = linkArray.length;
	linkArray[l] = new Link(l);
}

function Link(theNumber){
	this.theNumber =  theNumber;
	
	this.linkOn  = LinkOn;
	this.linkOff = LinkOff;
	this.setColour = setColour;
	this.currentColour = new ColourObject(offColour.toColourCode());
	this.intervalID = window.setTimeout("linkArray[" + this.theNumber + "].linkOff()", delay);
}

function LinkOff(){
	window.clearInterval(this.intervalID);
	this.currentColour.red   += ((offColour.red   - this.currentColour.red)   / offSubdivisions);
	this.currentColour.green += ((offColour.green - this.currentColour.green) / offSubdivisions);
	this.currentColour.blue  += ((offColour.blue  - this.currentColour.blue)  / offSubdivisions);
	this.setColour(this.currentColour.toColourCode());
	if (!(this.currentColour.equals(offColour))) this.intervalID = window.setTimeout("linkArray[" + this.theNumber + "].linkOff()", delay);
}

function LinkOn(){
	window.clearInterval(this.intervalID);
	this.currentColour.red   += ((onColour.red   - this.currentColour.red)   / onSubdivisions);
	this.currentColour.green += ((onColour.green - this.currentColour.green) / onSubdivisions);
	this.currentColour.blue  += ((onColour.blue  - this.currentColour.blue)  / onSubdivisions);
	this.setColour(this.currentColour.toColourCode());
	if (!(this.currentColour.equals(onColour))) this.intervalID = window.setTimeout("linkArray[" + this.theNumber + "].linkOn()", delay);
}

function setColour(colourCode){
	var elem = document.getElementById("link" + this.theNumber);
	if (elem)
		elem.style.color = colourCode;
}

function ColourObject(colourCode){
	this.red   = hexToDec(colourCode.substr(1,2));
	this.green = hexToDec(colourCode.substr(3,2));
	this.blue  = hexToDec(colourCode.substr(5,2));
	this.toColourCode = toColourCode;
	this.equals = ColourObjectEquals;
}

function ColourObjectEquals(other){
	return ( (Math.abs(this.red   - other.red)   < 0.01) &&
	         (Math.abs(this.green - other.green) < 0.01) &&
	         (Math.abs(this.blue  - other.blue)  < 0.01) );
}

function toColourCode(){
	return("#" + decToHex(Math.round(this.red))   +
	             decToHex(Math.round(this.green)) +
	             decToHex(Math.round(this.blue))  );
}

function hexToDec(letters){
	var number = 0;
	for (var i = letters.length - 1; i >= 0; i--){
		var d = letters.charCodeAt(i);
		d -= 48;
		if (d > 9){
			d -= 7;
		}
		number += d*(Math.pow(16, i));
	}
	return(number);
}

function decToHex(number){
	var letters = new String();
		var d = Math.floor(number / 16);
		if (d > 9){
			d += 7;
		}
		d += 48;
		letters += String.fromCharCode(d);
		d = number % 16;
		if (d > 9){
			d += 7;
		}
		d += 48;
		letters += String.fromCharCode(d);
	return letters;
}
  