var scrollInt;
function goToTop() {
	scrollInt = setInterval("moveTop()", 12);
}

function moveTop() {
	if (window.scrollY > 0) {
		window.scrollBy(0, -30);
	} else {
		clearInterval(scrollInt);
	}
}

function changeOption(id) {
	var newOp = document.getElementById('op' + id).innerHTML;
	document.getElementById('type').value = newOp;
	document.getElementById('op_text').innerHTML = newOp;
	document.getElementById('sel').style.display = "none";
}

var opShow = true;
function showOptions() {
	opShow = !opShow;
	if (opShow) {
		document.getElementById('sel').style.display = "none";
	} else {
		document.getElementById('sel').style.display = "block";
	}
}

function hideOptions() {
	document.getElementById('sel').style.display = "none";
}

var rotateInt;
var curLatest = new Array(0, 0, 0);
function rotateLatest() {
	for (var i = 0; i < 3; i++) {
		var nextLatest = curLatest[i] + 1;
		curLatest[i] = nextLatest;
		if (nextLatest >= latestArray[i].length - 1) {
			nextLatest = 0;
			curLatest[i] = 0;
		}
		document.getElementById('l' + i).innerHTML = document.getElementById('l' + i).innerHTML + "<span class=\"text\" style=\"margin-top:0px\">" + latestArray[i][nextLatest] + "</span>"; // In the end, the simplest things are the best.
	}
	
	rotateInt = setInterval("animateLatest()", 50);
	setTimeout("rotateLatest()", 6000);
}

function animateLatest() {
	var elm0 = document.getElementById('l0').getElementsByTagName('span')[0];
	var elm2 = document.getElementById('l2').getElementsByTagName('span')[0];
	var elm1 = document.getElementById('l1').getElementsByTagName('span')[0];
	if (parseInt(elm0.style.marginTop) > (elm0.clientHeight - (elm0.clientHeight * 2))) {
		elm0.style.marginTop = parseInt(elm0.style.marginTop) - 4 + "px";
		elm1.style.marginTop = parseInt(elm1.style.marginTop) - 4 + "px";
		elm2.style.marginTop = parseInt(elm2.style.marginTop) - 4 + "px";
	} else {
		clearInterval(rotateInt);
		for (var i = 0; i < 3; i++) {
			document.getElementById('l' + i).removeChild(document.getElementById("l" + i).getElementsByTagName('span')[0]);
		}
	}
}

var yesIHateMyself;
function createElmBox() {
	var elmBoxContainer = document.createElement('div');
	elmBoxContainer.id = "elmBox";
	elmBoxContainer.innerHTML = "<span class=\"close\" onclick=\"closeElmBox()\">Close</span>";
	
	var elmBoxFader = document.createElement('div');
	elmBoxFader.id = "whiteout";
	elmBoxFader.style.height = window.innerHeight + "px";
	var elmBoxFaderClose = document.createAttribute('onclick');
	elmBoxFaderClose.nodeValue = "closeElmBox()";
	elmBoxFader.setAttributeNode(elmBoxFaderClose);
	
	document.body.appendChild(elmBoxFader);
	document.body.appendChild(elmBoxContainer);
	
	// Dear whoever is reviewing this code. Please do not do as I have done here and taken the easy way out of your problems. I hate myself so much for this solution. Signed, Me.
	//yesIHateMyself = setInterval("positionElmBox()", 250);
	//setTimeout('clearInterval(yesIhateMyself)', 90000);
	
	return true;
}

function positionElmBox() {
	var elm = document.getElementById('elmBox');
	var left = ((window.innerWidth / 2) + window.scrollX) - (elm.clientWidth / 2);
	if (left < 0) {
		left = 0;
	}
	
	var top = ((window.innerHeight / 2) + window.scrollY) - (elm.clientHeight / 2);
	if (top < 0) {
		top = 0;
	}
	elm.style.marginLeft = left + "px";
	elm.style.marginTop = top + "px";
}

function closeElmBox() {
	document.body.removeChild(document.getElementById('elmBox'));
	document.body.removeChild(document.getElementById('whiteout'));
	//clearInterval(yesIHateMyself);
}

function launchImage(image) {
	createElmBox();
	
	loadImage(image);
	
	positionElmBox();
}

function changeImage(image) {
	document.getElementById('elmBox').innerHTML = "";
	
	loadImage(image);
	
	positionElmBox();
}

function loadImage(image) {
	var loadSpan = document.createElement('div');
	loadSpan.style.textAlign = "center";
	loadSpan.id = "temp_loading";
	var loadImg = document.createElement('img');
	loadImg.src = "http://www.50hzgamer.co.uk/img/image_load.gif";
	loadImg.alt = "Loading...";
	loadImg.style.textAlign = "center";
	loadSpan.appendChild(loadImg);
	document.getElementById('elmBox').appendChild(loadSpan);
	
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	else
	  {
	  alert("Your browser does not support this functionality.");
	  }
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
		document.getElementById('elmBox').removeChild(document.getElementById('temp_loading'));
		document.getElementById('elmBox').innerHTML = document.getElementById('elmBox').innerHTML + xmlhttp.responseText;
		positionElmBox();
	  }
	}
	xmlhttp.open("GET", "http://www.50hzgamer.co.uk/info/image/" + image + "/", true);
	xmlhttp.send(null);
}

function launchVideo(video) {
	createElmBox();
	var loadSpan = document.createElement('div');
	loadSpan.style.textAlign = "center";
	loadSpan.id = "temp_loading";
	var loadImg = document.createElement('img');
	loadImg.src = "http://www.50hzgamer.co.uk/img/image_load.gif";
	loadImg.alt = "Loading...";
	loadImg.style.textAlign = "center";
	loadSpan.appendChild(loadImg);
	document.getElementById('elmBox').appendChild(loadSpan);
	
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	else
	  {
	  alert("Your browser does not support this functionality.");
	  }
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
		document.getElementById('elmBox').removeChild(document.getElementById('temp_loading'));
		document.getElementById('elmBox').innerHTML = document.getElementById('elmBox').innerHTML + xmlhttp.responseText;
		VideoJS.setup();
		positionElmBox();
	  }
	}
	xmlhttp.open("GET", "http://www.50hzgamer.co.uk/info/video/" + video + "/", true);
	xmlhttp.send(null);
	
	positionElmBox();
}

function changeFilter(type, value) {
	var nVal = "";
	if (value == '') {
		nVal = "None";
	} else {
		nVal = value;
	}
	document.getElementById(type).value = value;
	document.getElementById(type + "_label").innerHTML = nVal;
	hideFilter(type);
}

function showFilter(type) {
	document.getElementById(type + "_ops").style.display = "block";
}

function hideFilter(type) {
	document.getElementById(type + "_ops").style.display = "none";
}

function checkValid(id, type) {
	if (type == "num") {
		if (isNaN(document.getElementById(id).value)) {
			return false;
		}
		return true;
	}
}

function dbg(msg) {
	document.getElementById('debug').innerHTML = document.getElementById('debug').innerHTML + "<br />" + msg;
}

function getHTTPdata(path) {
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	else
	  {
	  alert("Your browser does not support this functionality.");
	  }
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
		return xmlhttp.responseText;
	  }
	}
	xmlhttp.open("GET", path, true);
	xmlhttp.send(null);
}
