/* auto_update.js - Update util for PointlessCounter.com feed 
 *
 * -- TODO:
 *   .. Get the Autoupdating SVG feeder working, -- DONE!
 *   .. Get an Auto Updating HTML Feed -- Using the SAME js code... -- DONE!
 *   .. SVG Animation Trigger.
 *
 * Code inspired by infocraft's firefox counter.
 * Infocraft: http://www.infocraft.com/
 * FFCounter: http://www.infocraft.com/projects/ffcounter/
 * FFCounter JS: http://www.infocraft.com/projects/ffcounter/ffcounter.js
 */


/**** Adjustables ****/

var initial_url = "http://www.pointlesscounter.com/live/update.php"; // URL to initially request the count from (This causes an index)
var readonly_url = "http://www.pointlesscounter.com/live/viewonly.php"; // URL to request all subsequent counts from (No Index)
var counter_id = "pcounter"; // ID of the counter element
var countinit_id = "pinit";

var tick_rate = 10000; // Redownload every 30 seconds;
var tick_counter = 0; // Times the counter was updated;
var tick_updates = 0; // Times the counter was different after an update;
var previous_count = "0";
var tick_watch_id = "pticker";
var start_value = "0";
var have_initial = false;
var count_updated_callback = function(previousValue, newValue) {};
var started = false;

/****** Public ******/

function set_count_update_callback(callbck)
{
	count_updated_callback = callbck;
}

// Sets an HTML element with element_id to the given content.
function set_element(element_id, content)
{
	var element;
	var svgDoc;
	element = document.getElementById(element_id);
	if (element !== null)
	{
		element.firstChild.data = content;
	}
	
}


// Now that the data from the XML request has been extracted, this updates the
// global variables with that data.
function update_count(count)
{
	tick_counter++;
	if (count != previous_count)
	{
		tick_updates++;
		set_element(counter_id, count);
		if (!have_initial)
		{
			have_initial = true;
			set_element(countinit_id, count);
		}
		count_updated_callback(previous_count, count);
		previous_count = count;
	}
	set_element(tick_watch_id, tick_updates + '/' + tick_counter);
}

// This function is called once the request state changes.  It pulls the
// relevant data from the response and sends to update_count().
function process_request()
{
	var response;
	var time;
	var count_item;
	var count;

	if (request.readyState == 4) {
		if (request.status == 200) {
			response = request.responseXML;
			count = response.getElementsByTagName('c')[0].firstChild.data;
			//count = count_item.getElementsByTagName('c')[0].firstChild.data;
			update_count(count);
		} else {
			if (!have_initial)
			{
				// only set the error if the FIRST request fails...
				update_count("Error");
				have_initial=false;
			}
				
		}

		request = null;
	}
}

function load_xml(url) 
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
		request.onreadystatechange = process_request;
		request.open("GET", url, true);
		request.send(null);

	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) {
			request.onreadystatechange = process_request;
			request.open("GET", url + "?cacheKill=" + Math.random(), true);
			request.send();
		}
	}
}

function get_count_readonly()
{
	// readonly request can only be made IF the client already has a number
	have_initial=true;
	// For readonly don't load XML the first CALL.
	if (started === true)
	{
	    load_xml(readonly_url);
	}
	started = true;
	setTimeout(get_count_readonly, tick_rate);
}

function get_count()
{
	started = true;
	load_xml(initial_url);
	setTimeout(get_count_readonly, tick_rate);
}


