var xml;
var RGB = new Array(256);
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
k=0;
for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) {
	this.RGB[k] = hex[i] + hex[j];
	k++;
}
var symbol = new Array("radic", "infin", "ne", "part", "nabla", "radic", "exist", "asymp", "there4", "prod", "forall", "isin", "equiv", "int", "sum");
var home = new Array(3,10, 3,11, 3,12, 3,13, 3,14, 4,12, 5,12, 6,10, 6,11, 6,12, 6,13, 6,14, 8,10, 8,11, 8,12, 8,13, 8,14, 9,10, 9, 14, 10,10, 10,14, 11,10, 11,11, 11,12, 11,13, 11,14, 13,10, 13,11, 13,12, 13,13, 13,14, 14,11, 14,12, 15,12, 15,13, 16,11, 16,12, 17,10, 17,11, 17,12, 17,13, 17,14, 19,10, 19,11, 19,12, 19,13, 19,14, 20,10, 20,12, 20,14, 21,10, 21,14);

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function loadxml(filename) {
	var file = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
	file.open('GET', filename, false);
	file.send(null);
	return file.responseXML;
}

function getColor(c) {
	c=c%768;
	if (c<256) {r=c; g=0; b=255-c;}
		else if (c<512) {r=255; g=c-256; b=0;}
			else if (c<640) {r=2*(639-c); g=767-c; b=0;}
				else if (c<768) {r=0; g=767-c; b=2*(c-640);}
	return RGB[r]+RGB[g]+RGB[b];
}

function colorcycle(element) {
	this.element = element;
	this.cc=0;
	this.step();
}
colorcycle.prototype.step = function() {
	this.cc=this.cc+2;
	if (this.cc>=768) this.cc=0;
	this.element.bgColor = getColor(this.cc);
	var self = this;
	timer = setTimeout(function(){self.step(this.element);},500);
}
function CA(element, color, text) {
	this.element = element;
	this.N = 25;
	this.color = color;
	this.text = text;
	this.life = new Array(this.N);
	this.moore = new Array(this.N);
	for (i=0;i<this.N;i++) this.life[i] = new Array(this.N);
	for (i=0;i<this.N;i++) this.moore[i] = new Array(this.N);
	for (x=0;x<this.N;x++) for (y=0;y<this.N;y++) this.life[x][y] = Math.floor(Math.random()*2);
//	var self = this;
//	timer = setTimeout(function(){self.update();},1000);
	this.update();
}
CA.prototype.inhome = function (x, y) {
	if (this.text) for (i=0;i<home.length/2;i++) if ((x==home[2*i])&&(y==home[2*i+1])) return 1;
	return 0;
}
CA.prototype.update = function () {
	for (x=0;x<this.N;x++) for (y=0;y<this.N;y++) {
		xm=(x==0)?this.N-1:x-1; xp=(x==(this.N-1))?0:x+1;
		ym=(y==0)?this.N-1:y-1; yp=(y==(this.N-1))?0:y+1;
		this.moore[x][y]=this.life[xm][ym]+this.life[xm][y]+this.life[xm][yp]+this.life[x][ym]+this.life[x][yp]+this.life[xp][ym]+this.life[xp][y]+this.life[xp][yp];
	}
	for (x=0;x<this.N;x++) for (y=0;y<this.N;y++) this.life[x][y]=((this.moore[x][y]==3)||(this.life[x][y]&&(this.moore[x][y]==2)))?1:0;
	this.life[Math.floor(Math.random()*this.N)][Math.floor(Math.random()*this.N)] = 1;
	offset = findPos(this.element);
	html = "";
	
	html+= "<table style='width:100%; height:100%; border-spacing:1px; margin:0px; padding:0px;'>"
	for (y=0;y<this.N;y++) {
		html+= "<tr>";
		for (x=0;x<this.N;x++) html+= "<td"+(this.life[x][y]||this.inhome(x,y)?" style='background-color:#"+this.color+";'":"")+"></td>";
		html+= "</tr>";
	}
	html+= "<table>"
	
	this.element.innerHTML = html;
	var self = this;
	timer = setTimeout(function(){self.update();},200);
}

function Slideshow(element, time) {
	this.showElement = element;
	this.displayTime = time;
	this.imageList = new Array();
	this.numberOfImages = 0;
	this.thisImage = 0;
}
Slideshow.prototype.addImage = function(source) {
	this.imageList[this.numberOfImages] = new Image();
	this.imageList[this.numberOfImages].src = source;
//	this.imageList[this.numberOfImages].style.height = 360 + 'px';
	this.numberOfImages++;
}
Slideshow.prototype.showSlides = function() {
	this.showNext();
	document.getElementById(this.showElement).innerHTML = "";
	var self = this;
	if (this.thisImage>=0) setTimeout(function(){self.showSlides();}, this.displayTime);
	else window.location.reload();
}
Slideshow.prototype.showPrev = function() {
	this.thisImage = (this.thisImage == 0)?this.numberOfImages-1:this.thisImage-1;
	document.getElementById(this.showElement).style.backgroundImage = 'url('+this.imageList[this.thisImage].src+')';
}
Slideshow.prototype.showNext = function() {
	this.thisImage = (this.thisImage == this.numberOfImages-1)?0:this.thisImage+1;
	document.getElementById(this.showElement).style.backgroundImage = 'url('+this.imageList[this.thisImage].src+')';
}
