// 50Hz Gamer Featured Box Scripts
var curItem = 0;
var isSetup = 0;

var tempBgs = new Array();			
var tempURLs = new Array();
var tempHead = new Array();
var tempDesc = new Array();

var int1; // Interval variable. Don't remove.
var currentSize = 0;
var currentItem = 1;
var oldItem = 1;

function makeRequest(url) 
{
	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		request = new ActiveXObject("MSXML2.XMLHTTP");
	}
	
	sendRequest(url);
}

function sendRequest(url) {
	request.onreadystatechange = onResponse;
	request.open("GET", url, true);
	request.send(null);
}

function checkReadyState(obj) {
	/*if(obj.readyState == 0) { document.getElementById('featured_1').innerHTML = "<a><span class=\"header2\">Loading</span></a>"; }
	if(obj.readyState == 1) { document.getElementById('featured_1').innerHTML = "<a><span class=\"header2\">Loading..</span></a>"; }
	if(obj.readyState == 2) { document.getElementById('featured_1').innerHTML = "<a><span class=\"header2\">Loading..</span></a>"; }
	if(obj.readyState == 3) { document.getElementById('featured_1').innerHTML = "<a><span class=\"header2\">Loading...</span></a>"; }*/
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
			return true;
		}
		else if(obj.status == 404)
		{
			// Add a custom message or redirect the user to another page
			document.getElementById('main_area').innerHTML = "Problem finding XML file.";
		}
		else
		{
			document.getElementById('main_area').innerHTML = "There was a problem retrieving data... Reload, try again.";
		}
	}
}

function onResponse()  {
	if(checkReadyState(request)) {
		var response = request.responseXML.documentElement;
		
		if (isSetup == 0) {
			isSetup = 1;
			
			for (var i = 0; i < 4; i++) {
				tempHead[i] = response.getElementsByTagName('header')[i].firstChild.data;
				tempDesc[i] = response.getElementsByTagName('description')[i].firstChild.data;
				tempBgs[i] = response.getElementsByTagName('image')[i].firstChild.data;
				tempURLs[i] = response.getElementsByTagName('url')[i].firstChild.data;
			}
			
			controlTimer("start");
		}
	}
}

function changeItem(next) {
	for (var i = 0; i < 4; i++) {
		document.getElementById("feat_" + i).style.borderColor = "#999";
		document.getElementById("feat_" + i + "_c").style.display = "none";
	}
	
	// Implement new artefacts
	document.getElementById("feat_" + next).style.borderColor = "#C90";
	document.getElementById("feat_" + next + "_c").style.display = "block";
	
	curItem = next; // So now it's easier to scroll through items.
	document.getElementById('main_area').style.background = "url(" + tempBgs[next] + ")";
	document.getElementById('main_area').innerHTML = '<a href="' + tempURLs[next] + '"><span id="box_description"><span class="header">' + stripslashes(tempHead[next]) + '</span>' + stripslashes(tempDesc[next]) + '</a>';
	
	controlTimer("refresh");
}

function controlTimer(action) {
	if (action == "refresh") {
		clearInterval(int1);
	}
	var nextItem = curItem + 1;
	
	if (nextItem >= 4) {
		nextItem = 0;
	}
	int1 = setInterval("changeItem(" + nextItem + ")", 7000);
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}

// On Load functionality, code from... Somewhere.
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(makeRequest('xml/featured.php?nocahce=' + Math.random(9000) ));  // FUCK YOU INTERNET EXPLORER!