var MenuLines = new Array();
var MenuLinesBackgroundPosition = new Array();

var BrainHeight=376;
var BrainWidth=306;

initAnim();

function initAnim(){
	var i;
	var Ob;
	
	// Record all MenuLink background positions
	for(i=0; i<20; i++){
		if(Ob=document.getElementById("MenuLink"+i)){
			MenuLines.push(i);
			MenuLinesBackgroundPosition[i] = Ob.style.backgroundPosition;
		}
		
	}

	for(i=0; Ob=document.getElementById("BrainAnim"+i); i++){
		Ob.style.visibility="hidden";
	}
	
	BrainRandom();
	CorruptCircleRandom();
	CorruptMenuRandom();
}

// Play a random anim
function BrainRandom(){
	
	var r = Math.random()*100;
	var Count;
	var Type;
	var Wait=2000;
	
	
	if(r<20){
		Count = 6+Math.floor(Math.random()*20);
		setTimeout("BrainRandomBoxOverlay("+Count+");", Math.random()*Wait);
		
	}else if(r<40){
		Count = 5+Math.floor(Math.random()*5);
		Type=Math.floor(Math.random()*3);
		setTimeout("BrainFlashHighres("+Count+", "+Type+");", Math.random()*Wait);
		
	}else if(r<60){
		setTimeout("BrainFlashEnlarge(1);", Math.random()*Wait);
		
	}else if(r<70){
		Count = 20+Math.floor(Math.random()*20);
		setTimeout("BrainScrub("+Count+");", Math.random()*Wait);
	
	}else if(r<85){
		Count = 20+Math.floor(Math.random()*20);
		setTimeout("BrainRainbow("+Count+");", Math.random()*Wait);
	
	}else if(r<95){
		Count = 10+Math.floor(Math.random()*10);
		setTimeout("BrainScanError("+Count+");", Math.random()*Wait);
	
	}else{
		Count = 5+Math.floor(Math.random()*10);
		setTimeout("CorruptBrain("+Count+");", Math.random()*Wait);
	}
	
}
function CorruptCircleRandom()
{
	var Wait = 6000;
	var Count = 20+Math.floor(Math.random()*20);
	
	setTimeout("CorruptCircle("+Count+");", Math.random()*Wait);
	
	setTimeout("CorruptCircleRandom();", 1000+Math.random()*Wait);
}
function CorruptMenuRandom()
{
	var Wait = 2000;
	var Count = 20+Math.floor(Math.random()*20);
	var Div = Math.floor(Math.random()*MenuLines.length);
	var DivID = MenuLines[Div];
	
	setTimeout("CorruptMenuLines("+Div+","+Count+");", Math.random()*Wait);
	
	setTimeout("CorruptMenuRandom();", Math.random()*Wait);
}

// ---------------------------------------- Animate Brain --------------------------------------------------- //


function BrainFlashHighres(Count, Type){

	var off = Type>0?2:3;

	Ob = document.getElementById("Brain");
	Ob.style.backgroundPosition="0px -"+(Math.random()<0.7 ?376*off:0)+"px";
	if(Count--){
		setTimeout("BrainFlashHighres("+Count+", "+Type+");", 50);	
	}else{
		Ob.style.backgroundPosition="0px 0px";
		BrainRandom();
	}
}


function BrainFlashEnlarge(Count){

	Ob = document.getElementById("Brain");
	Ob.style.backgroundPosition="0px -"+(376*4)+"px";
	if(Count){
		setTimeout("BrainFlashEnlarge(0);", 100);	
	}else{
		Ob.style.backgroundPosition="0px 0px";
		BrainRandom();
	}
}


function BrainRandomBoxOverlay(Count){
	var Ob
	for(i=0; i<3; i++){
		Ob = document.getElementById("BrainAnim"+i);
		if(Math.random()<0.3){
			var Width = 64 + Math.floor(Math.random()*8)*16;
			var Height = 64 + Math.floor(Math.random()*8)*16;
			var X = Math.floor(Math.random()*(306-Width));
			var Y = Math.floor(Math.random()*(376-Height));
			var BgX = Math.floor(Math.random()*8)-4 - X;
			var BgY = Math.floor(Math.random()*8)-4 - Y - 376*4;
			
			if(Math.random()<0.2) BgX = Math.floor(Math.random()*80)-40 - X;
			if(Math.random()<0.2) BgY = BgY + 376;
			
			Ob.style.backgroundPosition=BgX+"px "+BgY+"px";
			Ob.style.left = X+"px";
			Ob.style.top = Y+"px";
			Ob.style.width = Width+"px";
			Ob.style.height = Height+"px";
			Ob.style.visibility="visible";
		}
		if(Count==0){
			Ob.style.visibility="hidden";
		}else{
			Ob.style.visibility=(Math.random()<0.5)?"hidden":"visible";
		}
	}
	
	if(Count>0){
		Count--;
		setTimeout("BrainRandomBoxOverlay("+Count+");", 30);	
	}else{
		BrainRandom();	
	}
}

function BrainScrub(Count){

	var offset=Math.floor(Math.random()*38)+1;
	var noffset=40-offset;

	var toff=0;
	var Ob;
	
	Ob = document.getElementById("Brain"); Ob.style.visibility="hidden";
	
	for(i=0; i<10; i++){
	
		Ob = document.getElementById("BrainAnim"+i);
		var Static = Math.floor(Math.random()*2);
		var BgX= Static * Math.floor(Math.random()*153)*2;
		Ob.style.left = Math.floor(Math.random()*4-2)+"px"; Ob.style.top = toff+"px";
		Ob.style.width = "306px"; 
		var BgY = 376*Math.floor(Static*8)+toff;
		if(i==0){
			Ob.style.height = offset+"px";
			toff+=offset;
		}else{
			Ob.style.height = "38px";
			toff+=38;
		}
		Ob.style.backgroundPosition=BgX+"px -"+BgY+"px";
		Ob.style.visibility="visible";
	
	}
	Count--;
	
	if(Count>0){
		setTimeout("BrainScrub("+Count+");", 30);	
	}else{
		for(i=0; i<10; i++) document.getElementById("BrainAnim"+i).style.visibility="hidden";
		document.getElementById("Brain").style.visibility="visible";
		BrainRandom();	
	}
}

function BrainRainbow(Count){

	var offset=Math.floor(Math.random()*38)+1;
	var noffset=40-offset;

	var toff=0;
	var Ob;
	
	Ob = document.getElementById("Brain"); Ob.style.visibility="hidden";
	
	for(i=0; i<10; i++){
	
		Ob = document.getElementById("BrainAnim"+i);
		Ob.style.left = Math.floor(Math.random()*4-2)+"px"; Ob.style.top = toff+"px";
		Ob.style.width = "306px"; 
		var BgY = 376*Math.floor(3+Math.random()*5)+toff;
		if(i==0){
			Ob.style.height = offset+"px";
			toff+=offset;
		}else{
			Ob.style.height = "38px";
			toff+=38;
		}
		Ob.style.backgroundPosition="0px -"+BgY+"px";
		Ob.style.visibility="visible";
	
	}
	Count--;
	
	if(Count>0){
		setTimeout("BrainRainbow("+Count+");", 30);	
	}else{
		for(i=0; i<10; i++) document.getElementById("BrainAnim"+i).style.visibility="hidden";
		document.getElementById("Brain").style.visibility="visible";
		BrainRandom();	
	}
}

function CorruptBrain(Count){
	var Ob = document.getElementById("Brain");
	if(Count>0){
		var RanX = Math.floor(Math.random()*3000)*2;
		var RanY = Math.floor(Math.random()*3000)*2;
		Ob.style.backgroundPosition=RanX+"px "+RanY+"px";
		Count--;
		setTimeout("CorruptBrain("+Count+");", 15);
	}else{
		Ob.style.backgroundPosition="0px 0px";
		BrainRandom();		
	}
}


function BrainScanError(Count){
	var Ob = document.getElementById("Brain");
	if(Count>0){
		var RanY = Math.max(0, 336-Math.floor(Math.random()*Math.random()*Math.random()*Math.random()*Math.random()*256)*4);
		Ob.style.backgroundPosition="0px -"+RanY+"px";
		Count--;
		setTimeout("BrainScanError("+Count+");", 15);
	}else{
		Ob.style.backgroundPosition="0px 0px";
		BrainRandom();
	}
}

// ---------------------------------------- Animate menu links --------------------------------------------------- //

function CorruptMenuLines(DivID, Count){
	var Ob = document.getElementById("MenuLink"+DivID);
	if(Count>0){
		var RanX = Math.floor(Math.random()*2000)*2;
		var RanY = Math.floor(Math.random()*2000)*2;
		Ob.style.backgroundPosition=RanX+"px "+RanY+"px";
		Count--;
		setTimeout("CorruptMenuLines("+DivID+", "+Count+");", 15);
	}else{
		Ob.style.backgroundPosition=MenuLinesBackgroundPosition[DivID];
		if(Math.random()>0.4){
			var NextTime=Math.floor(Math.random()*400);
			var NextLength=Math.floor(Math.random()*20);
			setTimeout("CorruptMenuLines("+DivID+", "+NextLength+");", NextTime);
		}
	}
}


// ---------------------------------------- Animate circle --------------------------------------------------- //

function CorruptCircle(Count){
	var Ob = document.getElementById("Circle");
	if(Count>0){
		var RanX = Math.floor(Math.random()*100)*8;
		var RanY = Math.floor(Math.random()*200)*2;
		Ob.style.backgroundPosition=RanX+"px "+RanY+"px";
		Count--;
		setTimeout("CorruptCircle("+Count+");", 15);
	}else{
		Ob.style.backgroundPosition="0px 0px";
	}
}