// global variables

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

var xmlhttp = getHTTP();

// functions

function getHTTP() {
	
	var xmlhttp;


	if (document.all)
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	
	else if (!xmlhttp && typeof XMLHttpRequest!=undefined) {
	  xmlhttp = new XMLHttpRequest();
	} else {
	  xmlhttp = false;
	}
	
	return xmlhttp;
}

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
	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 HTTPRequestCallback(url, callback) {
	
	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(), true);
	xmlhttp.onreadystatechange = callback;
	xmlhttp.send(null);
	
	return xmlhttp;
}

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;
	
}

/*

Animate the turing string image

*/

function updateTuring() {

	document.images.turingimage.src = '/turingimage.php?r=' + (Math.random() % 1000);
	this.setTimeout('updateTuring()', 5000);
}


function fetchBlogs(percent) {
	var div = document.getElementById("blogbrowser");
	
	xmlhttp = HTTPRequestCallback('/blogservice.php?percent=' + percent, fetchBlogsCallback);
	
}

function fetchBlogsCallback() {


	
  if (xmlhttp.readyState == 4) {

    // Split the comma delimited response into an array

    results = xmlhttp.responseText;
	div = document.getElementById('blogbrowser');
    div.innerHTML = results;
	//xmlhttp = false;
  }

}

function updateScrollbar(event) {
	
	var posx = 0;
	var posy = 0;
	
	if (event.pageX || event.pageY)
	{
		posx = event.pageX;
		posy = event.pageY;
	}
	else if (event.clientX || event.clientY)
	{
		posx = event.clientX + document.body.scrollLeft;
		posy = event.clientY + document.body.scrollTop;
	}
	
	//var div = document.getElementById('links');
	var imgPos = document.scrollbar.offsetLeft;
	var pos = posx - imgPos;
	document.scrollbar.src = '/scrollbar.php?xpos=' + pos;
	//div.innerHTML = event.layerX;
	percent = pos/800 * 100;
	fetchBlogs(percent);
}