// global variables

var totalSeconds = 0;
var pageSeconds = 0;
var lastSeconds = 0;
var reportDelay = 5;
var firstReport = true;

// functions

function HTTPRequest(url, elementName) {

	var xmlhttp;


	if (document.all)
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	
	else if (!xmlhttp && typeof XMLHttpRequest!=undefined) {
	  xmlhttp = new XMLHttpRequest();
	} else {
	  xmlhttp = false;
	}
	// find a get parameter
	var strpos = url.indexOf("?");
		
	// make an HTTP request

	if (strpos > 0)
		and = '&';
	else and = '?';

	xmlhttp.open("GET", url + and + 'random=' + Math.random(), false);
	xmlhttp.send(null);
	
	return xmlhttp.responseText;

}


function setBox(num) {
var box;

result = HTTPRequest('/rand.php');

//box = document.getElementById("box");

//box.innerHTML = result;
//box.style.background = 'blue';

if (num == 10) return;

for (boxNum=0; boxNum < num; boxNum++) {
	curBox = document.getElementById("box" + boxNum);
	curBox.style.background = "black";	
}


self.setTimeout("setBox(" + (num+1) + ")", 1000);

}

function reportSeconds() {
	result = HTTPRequest('/reportseconds.php?seconds=' + (pageSeconds - lastSeconds));
	lastSeconds = pageSeconds;
	totalSeconds = result;
	reportDelay *= 1.1;
	self.setTimeout("reportSeconds()", 1000 * reportDelay);
}

function displaySeconds() {
	
	secondsBox = document.getElementById('seconds');
	seconds = totalSeconds % 60;
	if (seconds < 10) seconds = '0' + seconds;
	secondsBox.innerHTML = Math.floor(totalSeconds/60) + ':' + seconds;
	totalSeconds++;
	pageSeconds++;
	self.setTimeout("displaySeconds()", 1000);
}



function randProfile(id) {

	result = HTTPRequest('/catdisplay.php');
	
	box = document.getElementById(id);
	box.innerHTML = result;	
}

function getElementsByClass(searchClass, node) {
	if (node == null)
		node = document;
	var classElements = new Array();
	var elements = document.getElementsByTagName('*');
	var elementsLength = elements.length;
	for (i=0, j=0; i < elementsLength; i++) {
		if (elements[i].className == searchClass) {
			classElements[j] = elements[i];
			j++;
		}
	}
	return classElements;
}

function getElementsByClass2(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function assignEventToClass(searchClass, event, efunction) {
	elements = getElementsByClass(searchClass, null);
	//alert(elements.length);
	for(i=0; i < elements.length; i++) {
		if( elements[i].addEventListener ) {
		  elements[i].addEventListener(event , efunction, false);
		} else if ( elements[i].attachEvent ) {
		  elements[i].attachEvent('on' + event, efunction);
		  //elements[i].setCapture(true);
		} 
	}
}

