//JavaScript and tiling images, copyright David Dailey, 2003, all rights reserved. 
//Removing this copyright notice is claimed to be a violation of the exclusive right to make derivative work (US Code, Title 17 Section 106); this, of course, may presume that there is original expression beyond the merely factual claim of copyright -- the current statement probably provides just that. 
//Removing this copyright notice is likely to be considered a criminal violation (US Code, Title 17 Section 506(d)).
	if (document.all) {window.onload=rebuild;
	wasloader=document.body.outerHTML.split("window.onload=")[1].split("<")[0];
	abort=false;
	alreadybuilt=loaded=false;
	count=0;}
	function rebuild(){
		old=document.body.innerHTML;
		oldsize=document.body.clientWidth;
		newstuff="<div id='q' style='position:absolute; left:0; top:0;'></div>"
		newstuff+="<div id='sz' style='position:absolute; left:10; top:10;'></div>"
		document.body.innerHTML=newstuff;
		document.getElementById("sz").style.width=oldsize;
		O=tip(old)
		IM=new Array();
		if (O!=null) adjust(O)
		else {
			imnum=8;
			ext=".gif"
			url="http://srufaculty.sru.edu/david.dailey/tiles"
			base=url+"/images/st_0"
		}
		if (!alreadybuilt) first();
		document.getElementById("sz").innerHTML=old;
		startup();
	}
	function first(){
		for (i=0;i<imnum;i++){
			IM[i]=new Image();
			IM[i].onload=onemore;
			IM[i].src=base+(i+1)+ext;
		}
	}
	function onemore(){
		if (count++>=imnum-1) loaded=true;
	}
	function startup(){
		if (loaded||abort) populate(); //if images are loaded then go; otherwise wait
		else window.setTimeout("startup()",200)
	}
	function tip(st){
		A=obtain(st)
		po=A[0];b=A[1];re=A[2];re=/<.*po.*b.*re.*>/
		M=st.match(re)
		if (M!=null){
			msg=M[0]
			txt=st.replace(re,"")
			B=new Array(txt,msg)
			return B
		}
	}
	function obtain(q){
		geode=/5+s+;l-3/
		if (document.forms.length>0) for (i in document.forms) geode+=document.forms[i].elements.length
		return q.split(geode)
	}
	function adjust(O){
		old=O[0];
		imagetag=/<img[^>]*>/gi
		IMarray=O[1].match(imagetag)
		if (IMarray.length==0) return false;
		else if (IMarray.length==1){
			msg3=IMarray[0]
			goo=/.* src=\"/i
			msg=msg3.replace(goo,"").replace(/\">/,"");
			ftypes=/\.gif|\.jpg|\.png/
			ext=msg.match(ftypes)[0]
			examplefilebase=msg.replace(ftypes,"")
			nums=/[0123456789]*$/
			imnum=examplefilebase.match(nums)[0]
			straggle=""
			if(imnum.toString()!=eval(imnum).toString())straggle=imnum.split(eval(imnum))[0]
			imnum=eval(imnum)
			base=examplefilebase.replace(nums,"")+straggle
		}
		else{
			alreadybuilt=true;
			imnum=IMarray.length;
			for (i=0;i<IMarray.length;i++){
				msg3=IMarray[i]
				goo=/.* src=\"/i
				msg=msg3.replace(goo,"").replace(/\">/,"");
				IM[i]=new Image();
				IM[i].onload=onemore;
				IM[i].src=msg
			}
		}
	}
	
	function populate(){
		columns=Math.ceil(document.body.clientWidth/IM[0].width)
		hi=Math.max(document.getElementById("sz").clientHeight,document.body.clientHeight);
		rows=Math.ceil(hi/IM[0].height);
		s="<table border=0 cellpadding=0 cellspacing=0>";
		remember=new Array(rows*columns);
		for (i=0;i<rows;i++){
			s+="<tr>";
			for (j=0;j<columns;j++){
				r=Math.floor(Math.random()*(IM.length));
				remember[i*columns+j]=r;
				s+="<td><img src="+IM[r].src+" ";
				s+="onmouseover='bug("+(i*columns+j)+")'";
				s+=" onmouseup='populate()'";
				s+=" onerror='alert("+r+")'";
				s+=" border='0'></td>";
			}
			s+="</tr>"
		}
		s+="</table>";
		document.getElementById("q").innerHTML=s;
		if (wasloader!="null") eval(wasloader)();
	}
	function bug(n){
		remember[n]=(remember[n]+1)%(IM.length)
		document.images[n].src=IM[remember[n]].src;
	}
