/* bad_guys.js
 * --
 * Code to supplement the "Bad Guys" layout.
 * 
 * !! REQUIRES ruthsarian_utilities.js TO BE LOADED !!
 *
 * also uses a crappy hack to call its own instance via setInterval that
 * requires the variable name of the instance be hard-coded into the code.
 * prolly a better solution for this out there.
 */

// create class container
var s8n_bgHighlights = function () {

	var highlights_id = "masthead-highlight";
	var highlights_content_prepend = "<p><strong>&gt; </strong>";
	var highlights_content_append = "</p>";
	var data_url = "/gargoyles/highlights.php";
	var pause = 10000;
	var interval;
	var data;
	var h;

	// unhide the highlights box and start pulling highlights
	this.init = function () {

		// reference to the box where highlights go
		h = document.getElementById( highlights_id );

		// CSS workarounds until this is official
		var m = document.getElementById( 'masthead' );
		m.style.paddingTop = "15px";
		m.style.paddingBottom = "35px";

		// start by grabbing highlights from the server
		this.get_highlights();
	}

	this.ready = function () {
		if ( data.length > 0 ) {
			h.innerHTML = "<p>loading highlights...</p>";
			h.style.display = "block";
			this.start();
		}
	}

	// grab a highlight and set a timer to pull another 
	this.start = function() {
		this.highlight();
		interval = setInterval( function(){ bgh.highlight(); }, pause );
	}

	// stop pulling highlights
	this.stop = function () {
		clearInterval( interval );
	}

	// init AJAX request for a highlight
	this.get_highlights = function () {
		var aj = new AjaxContainer();
		data = null;
		aj.getByURL( this, data_url );
	}

	// handle AJAX response
	this.ajax_handler = function ( xmlHttp ) {

		// make sure we've got a highlights document
		if ( xmlHttp.responseXML.documentElement.tagName.toLowerCase() != 'highlights' ) { return; }

		// array where our data will be stored.
		data = new Array();

		// get an array of highlight elements
		var h = xmlHttp.responseXML.documentElement.getElementsByTagName( "highlight" );

		// loop over highlights
		for ( i=0; i<h.length; i++ ) {

			tmp = new Object();
			tmp.length = 0;

			// loop over elements in the highlight
			for ( j=0; j<h[i].childNodes.length; j++ ) {

				// make sure we've got a good tag
				if ( (typeof h[i].childNodes[j].tagName).toLowerCase() == 'string' && h[i].childNodes[j].tagName.length > 0 ) {

					// assign the node to our temp array
					tmp[ h[i].childNodes[j].tagName.toLowerCase() ] =  h[i].childNodes[j].childNodes.length < 1 ? '' :  h[i].childNodes[j].childNodes[0].nodeValue;
					tmp.length++;
				}
			}
			if ( tmp.length > 0 ) {

				// push the element into our data stack
				data.push( tmp );
			}

		}

		// let the system know we've got data and we're ready to go.
		this.ready();
	}

	this.highlight = function () {
		var pick;

		// make sure we have highlights to pick from
		if ( data == null || data.length < 1 ) {
			return;
		}

		// pick a highlight at random
		pick = Math.round( (Math.random() * data.length) - 0.5 );

		// display the highlight
		h.innerHTML = highlights_content_prepend + " "
			+ this.get_hl_attr( data[pick], "prefix" ) + " "
			+ "<a href=\"" + this.get_hl_attr( data[pick], "link" ) + "\" target=\"_blank\">" 
			+ this.get_hl_attr( data[pick], "text" ) + "</a>" + " "
			+ this.get_hl_attr( data[pick], "suffix" ) + " "
			+ highlights_content_append;
	}

	// tests to see if a variable exists within an object (a highlight object)
	// and returns a default value (empty string if not defined) if that variables
	// is not found.
	this.get_hl_attr = function ( collection, attribute, attr_default ) {
		if ( (typeof attr_default).toLowerCase() == 'undefined' ) { attr_default = ''; }
		if ( (typeof collection[attribute]).toLowerCase() == 'undefined' ) {
			return attr_default;
		} else {
			return collection[attribute];
		}
	}

}

// load the system on page load.
if (( typeof( AjaxContainer )).toLowerCase() == 'function'
	&& ( typeof( event_attach )).toLowerCase() == 'function' 
	&& document.getElementById
) {
	var bgh = new s8n_bgHighlights();

	// must not init this system until the page loads.
	event_attach( "onload", function () { bgh.init(); } );
}

