function ieHover()
{
	var comment_ = document.getElementById("navigation");
	if (comment_)
	{
		var nodes = comment_.getElementsByTagName("li");
		for (var i=0; i<nodes.length; i++)
		{
			nodes[i].onmouseover = function() 
			{
				this.className += " hover";
			}
			nodes[i].onmouseout = function()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}

if (window.attachEvent && !window.opera){
	window.attachEvent("onload", ieHover);
}

var searchOrderId = 0;
var highestSearchOrderId = 0;
var selectedSearchIndex=-1;
var oldSelectedSearchIndex=-1;
var selectedUrl="";
var originalQ="";
function ajaxSearch(evt) {
	searchOrderId++;
	q = document.getElementById("headerSearch").value;
	if (q.length<3) {
		return null;
	}
	if (selectedSearchIndex==-1) {
		originalQ = q;
	} else {
		q = originalQ;
	}
	qobj = document.getElementById("headerSearch");
	if (!evt) {
		evt = window.event;
	}
	if (q=="") {
		hideSearch();
	} else {
		$.ajax({
		    url: '/ajax.php',
		    type: 'GET',
			data: "module=tvg_search&key="+evt.keyCode+"&selected="+selectedSearchIndex+"&q="+q+"&orderId="+searchOrderId,
		    dataType: 'json',
		    timeout: 3000,
		    error: function(){
		    },
		    success: function(j){
		    orderId = +j['orderId'];
		    //Ensure results are returned in the correct order (only accept the latest request)
		    if(orderId > highestSearchOrderId) {
		    	highestSearchOrderId = orderId;
				searchBox = document.getElementById('searchAjax');
				searchBox.innerHTML = j['html'];
				oldSelectedSearchIndex = selectedSearchIndex;
				selectedSearchIndex = j['selected'];

				if (selectedSearchIndex>=0) {
					qobj.value = j['selectedName'];
				} else if (selectedSearchIndex!=oldSelectedSearchIndex){
					//Only update if changed, otherwise slow updates can overwrite typing text
					qobj.value = originalQ;
				}

				showSearch();
			}
		    }
		});
	}
}

function showSearch() {
	leftOffset = 0;
	topOffset = 7;
	searchBox = document.getElementById('searchAjax');
	obj = document.getElementById('headerSearch');
	position = getXY(obj);
	searchBox.style.left = (position.x - leftOffset) + "px";
	searchBox.style.top = (position.y + obj.offsetHeight + topOffset) + "px";
	searchBox.style.display="block";
	searchBox.style.zIndex = 100000;
}
var hideSearchTimer;

function hideSearch() {
	searchBox = document.getElementById('searchAjax');
	searchBox.style.display="none";
	selectedSearchIndex=-1;
	oldSelectedSearchIndex=-1;
}
function getXY(obj){
	var x=0;
	var y=0;
	while (obj!=null){
		x += obj.offsetLeft - obj.scrollLeft;
		y += obj.offsetTop - obj.scrollTop;
		obj = obj.offsetParent;
	}
	return {x:x,y:y};
}

function showComments(divId, type, id, page) {
	$.ajax({
	    url: '/ajax.php', type: 'GET', data: "module=tvg_comments&type="+type+"&id="+id+"&page="+escape(page), dataType: 'json', timeout: 3000,
	    error: function(){ },
	    success: function(j){
	    	document.getElementById(divId).innerHTML = j['html'];
	    }
	});
}
function submitPost(comments, type, id, textId) {
	if (tinyMCE!=undefined) {
		var ed = tinyMCE.get(textId);
		text = ed.getContent();
	} else {
		textbox = document.getElementById(textId);
		text = textbox.value;
	}
	if (text.length<10) {
		alert("Error, you must enter at least 10 characters");
		return false;
	}

	$.ajax({
	    url: '/ajax.php', type: 'GET', data: "module=tvg_comments&type="+type+"&id="+id+"&post=1&text="+escape(text), dataType: 'json', timeout: 3000,
	    error: function(){
	    	alert("Sorry, there was a problem sending your post, please try again");
	    	return null;
	    },
	    success: function(j){
	    	if(j['error']!=null) {
	    		alert(j['error']);
	    		return null;
	    	} else {
	    		//Refresh the comments
				showComments(comments, type, id, 1);
				if (tinyMCE!=undefined) {
					ed.setContent("");
				}
	    	}
	    }
	});
}

function ajaxArticleSearch(type, start, limit) {
	title = document.getElementById("gameTitle").value;
	if (title!="" && title!="Game Title") {
		ajaxArticleSearchByName(type);
		return null;
	}
	genre = document.getElementById("genreId");
	genreId = genre.options[genre.selectedIndex].value;
	format = document.getElementById("formatId");
	formatId = format.options[format.selectedIndex].value;
	sc = document.getElementById("score");
	if (sc != undefined) {
		score = sc.options[sc.selectedIndex].value;
	} else {
		score = "";
	}
	rel = document.getElementById("released");
	if (rel != undefined) {
		released = rel.options[rel.selectedIndex].value;
	} else {
		released = 0;
	}

	if (type == undefined || type=="") {
		type = "review";
	}
	if (start == undefined) {
		start = 0;
	}
	if (limit == undefined) {
		limit = 0;
	}
	showLoadingBox(300, -70, "article-finder");
	$.ajax({
	    url: '/ajax.php',
	    type: 'GET',
		data: "module=tvg_articleSearch&genreId="+genreId+"&type="+type+"&formatId="+formatId+"&score="+score+"&released="+released+"&start="+start+"&limit="+limit,
	    dataType: 'json',
	    timeout: 30000,
	    error: function(error){
	    	hideLoadingBox();
	    },
	    success: function(j){
	    	hideLoadingBox();
			searchBox = document.getElementById('article-finder-results');
			searchBox.innerHTML = j['html'];
	    }
	});
}

function ajaxArticleSearchByName(type) {
	format = document.getElementById("formatId");
	formatId = format.options[format.selectedIndex].value;
	genre = document.getElementById("genreId");
	genreId = genre.options[genre.selectedIndex].value;
	if (type == undefined || type=="") {
		at = document.getElementById("articleTypeBox");
		if (at==undefined) {
			type = "";
		} else {
			type = at.options[at.selectedIndex].value;
		}
	}
	title = document.getElementById("gameTitle").value;
	sc = document.getElementById("score");
	if (sc != undefined) {
		score = sc.options[sc.selectedIndex].value;
	} else {
		score = "";
	}
	rel = document.getElementById("released");
	if (rel != undefined) {
		released = rel.options[rel.selectedIndex].value;
	} else {
		released = 0;
	}
	
	searchOrderId++;
	showLoadingBox(300, -70, "article-finder");
	$.ajax({
	    url: '/ajax.php',
	    type: 'GET',
		data: "module=tvg_articleSearch&type="+type+"&formatId="+formatId+"&title="+title+"&genreId="+genreId+"&orderId="+searchOrderId+"&score="+score+"&released="+released,
	    dataType: 'json',
	    timeout: 30000,
	    error: function(error){
	    	hideLoadingBox();
	    },
	    success: function(j){
	    	hideLoadingBox();
			searchBox = document.getElementById('article-finder-results');
			searchBox.innerHTML = j['html'];
	    }
	});
}

function fanOfGame(gid, addRemove) {
	gid = gid+0;
	if (addRemove==undefined) {
		$.ajax({
		    url: '/ajax.php',
		    type: 'GET',
			data: "module=tvg_fanOfGame&gid="+gid,
		    dataType: 'json',
		    timeout: 20000,
		    error: function(error){
		    },
		    success: function(j){
				$('#gameBoxes').html(j['html']);
		  	}
		});
	} else if (addRemove=="add" || addRemove=="remove") {
		$.ajax({
		    url: '/ajax.php',
		    type: 'GET',
			data: "module=tvg_fanOfGame&gid="+gid+"&action="+addRemove,
		    dataType: 'json',
		    timeout: 20000,
		    error: function(error){
		    },
		    success: function(j){
				if (addRemove=="remove") {
					$('#fanOfListing').html(j['html']);
					$('.become-a-fan-of-this-game').html('<div><strong>Become a fan of this game</strong><p>Keep alerted to recent updates...</p></div><a class="click-to-become-a-fan" href="#" onclick="fanOfGame('+gid+', \'add\'); return false;">Click To Become A Fan</a>');
				} else if (addRemove=="add") {
					$('.become-a-fan-of-this-game').html('<div><strong>I am a fan of this game</strong></div><a class="click-to-become-a-fan" href="#" onclick="fanOfGame('+gid+', \'remove\'); return false;">Click To Stop Being A Fan</a>');
					alert("You are now a fan of this game, if you wish to remove it at any time, go to your 'My Page' (link at the top of the page) and click the red X.");
				}
		  	}
		});
	}
}

function showCheatBoxPage(p, currentPage) {
	if (currentPage>0) {
		document.getElementById("cheatBoxPage-"+currentPage).style.display="none";
	} else {
		document.getElementById("latest-articles").style.display="none";
		document.getElementById("latest-cheats").style.display="block";
	}
	document.getElementById("cheatBoxPage-"+p).style.display="block";
	return false;
}

function showHide(showId, hideId) {
	document.getElementById(hideId).style.display="none";
	document.getElementById(showId).style.display="block";
	return false;
}

function submitUserEditorForm(uid) {
	items=$(".requiredField");
	for(i=0; i<=items.length; i++) {
		if (items[i].value=="") {
	    	alert("This field is required, please fill it in/select the option");
	    	items[i].focus();
	    	return false;
	    }
	}
	pass1 = document.getElementById('userDetails['+uid+'][pass1]');
	pass2 = document.getElementById('userDetails['+uid+'][pass2]');
	if (pass1.value.length<5) {
		alert("Passwords should be at least 5 characters long");
		pass1.value="";
		pass2.value="";
		pass1.focus();
		return false;
	}
	if (pass1.value!=pass2.value) {
		alert("Passwords do not match, please enter them again");
		pass1.value="";
		pass2.value="";
		pass1.focus();
		return false;
	}
	return true;
}

function showLoadingBox(x, y, parentDivId) {
	id = 'AJAXLoadingBox';
	newdiv = document.getElementById(id);
	if (newdiv == undefined) {
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id', id);
		newdiv.style.width = 32;
	    newdiv.style.height = 32;
		newdiv.style.position = "relative";
		newdiv.innerHTML = '<img src="/css/jquery/lightBox/lightbox-ico-loading.gif"/>';
		document.getElementById(parentDivId).appendChild(newdiv);
	}
	leftOffset=100;
	topOffset=0;
	newdiv.style.left = x + "px";
	newdiv.style.top = y + "px";
	newdiv.style.display="block";
	newdiv.style.marginBottom="-32px";
	newdiv.style.zIndex = 100000;
}
function hideLoadingBox() {
	id = 'AJAXLoadingBox';
	newdiv = document.getElementById(id);
	if (newdiv != undefined) {
		newdiv.style.display="none";
	}
}

function showPage(pageNumber, pageParentClass, activeLinkClass) {
	$("."+pageParentClass).hide();
	$("."+pageParentClass+".page"+pageNumber).show();
	$("."+activeLinkClass).removeClass('active');
	$("."+activeLinkClass+".page"+pageNumber).addClass('active');
	return false;
}
function showProductItem(item, numItems) {
	for(i=1; i<=numItems; i++) {
		$('.productListing li.mainBlock').hide();
	}
	$('.productListing .product-'+item).fadeIn(300); 
	return false;
}
//disable autocomplete on all items that have the class "autocompleteOff"
$(document).ready(function() {$(".autocompleteOff").attr("autocomplete","off");});