// JPlasma Copyright(c) Jani Penttinen 2003.
// To use the plasma as a background effet, call:
//
// CreatePlasma(xtiles,ytiles,zdepth);
//
// in the BODY. Good values are for example (10,10,-1).
// Negative z values place the plasma behind the HTML
// content.
//
// To use plasma within the document, call:
//
// CreatePlasma(xtiles,ytiles,zdepth,width,height);
//
// anywhere in the document.
//

var nums=new String("0123456789abcdef");
var plasmatablewidth;

var plasmatableheight;
var plasmaw;
var plasmah;

function WriteHex(s,n)
{
	n&=255;
	var upper=n>>4;
	s+=nums.charAt(upper);

	s+=nums.charAt(n&15);

	return s;
}

function CreatePlasma(w, h, z, tw, th)
{
	plasmaw=tw;
	plasmah=th;

	var size=Math.floor(1/w*100);
	plasmatablewidth=w;
	plasmatableheight=h;
	var tableh=plasmah;
	if (!tableh) tableh=document.body.clientHeight;
	var t="";
	if (z!=0) {
		t+="<SPAN ID='tablespanid' ";
		t+="style=\"POSITION: absolute; ";
		t+="Z-INDEX: " + z + "; ";
		t+="VISIBILITY: visible; \">";
	}

	t+="<TABLE ID='tableid' ";
	t+="HEIGHT='" + tableh + "px' ";
	t+="WIDTH='" + plasmaw + "' ";
	t+="BORDER=0 CELLSPACING=0>";
	for (var y=0;y<h;++y) {
		t+="<TR>";
		for (var x=0;x<w;++x) {
			var id="tdx" + x + "y" + y;
			t+="<TD WIDTH='" + size + "%' ";
			t+="HEIGHT='" + size + "%' ";
			t+="id='" + id + "'>";
			t+="</TD>";
		}
		t+="</TR>";
	}
	t+="</TABLE>";
	if (z!=0) {
		t+="</SPAN>";
	}
	document.write(t);

	SetPlasmaColors();
	setInterval("UpdatePlasma();",25);
}

function UpdatePlasma()

{
	var run=GetCookie("jplasma");
	if (run!="1") {
		return;
	}
	SetPlasmaColors();
}

function SetPlasmaColors()
{
	if (!plasmah) {
		document.all["tableid"].height=
			document.body.clientHeight;
	}

	var updatetime=new Date();

	var angle1=updatetime/1024.0;
	var angle2=updatetime/1400.0;
	var a1=angle1;
	var angle3=-updatetime/705.0;
	for (var y=0;y<plasmatableheight;++y) {
		angle2+=0.15;
		angle3+=0.27;
		angle1=a1;
		for (var x=0;x<plasmatablewidth;++x) {
			var r=Math.floor(255.0*
				((1.0+Math.sin(angle1))*0.5));
			var g=Math.floor(255.0*
				((1.0+Math.cos(angle2))*0.5));
			var b=Math.floor(255.0*
				((1.0+Math.sin(angle3))*0.5));
			var col="#";
			col=WriteHex(col,r);
			col=WriteHex(col,g);
			col=WriteHex(col,b);
			var id="tdx" + x + "y" + y;
			var o=document.all[id];
			if (o) {
				o.bgColor=col;
			}
			angle1+=0.23;

		}

	}

}

