// Array to hold the retunred ajax info - kind a local cache, so we dont need to look up again
var ajaxArray=new Array();
// the currently selected id of the mouseover
var currentId="";

// Adding game to WishList
function addToWishList(gid)
{
	ajax.AddToWishList(gid,callBack_AddToWishList);
}

// Adding game to CollectionList
function addToCollectionList(gid)
{
	ajax.AddToCollectionsList(gid,callBack_AddToCollectionList);
}

//Move to CollectionList from WishList
function moveToCollectionList(gid)
{
	ajax.MoveToCollectionList(gid,callBack_moveToCollectionList);
}

function callBack_AddToWishList(response)
{
	if (response.error != null)
	{
		alert(response.error);
		return;
	}
	else
	{
		var lnk = document.getElementById("liWishList");
		if(lnk!=null)
		{
			lnk.innerHTML = "On Wish List";
		}
		lnk = document.getElementById("liCollectionList");
		if(lnk!=null)
		{
			rExp = /addToCollectionList/gi;
			result = lnk.innerHTML.replace(rExp,"moveToCollectionList");
			rExp = /Add To Collection List/gi;
			result = result.replace(rExp,"Move To Collection List");
			lnk.innerHTML = result;
		}
	}
}

function callBack_AddToCollectionList(response)
{
	if (response.error != null)
	{
		alert(response.error);
		return;
	}
	else
	{
		var lnk = document.getElementById("liCollectionList");
		if(lnk!=null)
		{
			lnk.innerHTML = "On Collection List";
			var lnk = document.getElementById("liWishList");
			if(lnk!=null)
			{
				lnk.style.display="none";
			}
		}
	}
}

function callBack_moveToCollectionList(response)
{
	if (response.error != null)
	{
		alert(response.error);
		return;
	}
	else
	{
		var lnk = document.getElementById("liCollectionList");
		if(lnk!=null)
		{
			lnk.innerHTML = "On Collection List";
		}
		var lnk = document.getElementById("liWishList");
		if(lnk!=null)
		{
			lnk.style.display="none";
		}
		var lnk = document.getElementById("liOnWLorCL");
		if(lnk!=null)
		{
			lnk.style.display="none";
		}
	}
}

// load a tooltip
// looks to see if we already have it in the cache
// if we do, just display it
// if not, call the backend through AJAX to get the html
// args
//	gid (int) = gameid
//	boxart (bool) = display the boxart or not
//	div (object) = the div that is being called for this
function LoadTitleMouseOver(gid,boxart, div, event)
{
	//check and see if it is already in the array
	var arrayIndex=gid + '_' + boxart;
	if(ajaxArray[arrayIndex]==null)
	{
		// we dont
		currentId=div.id;
		// display the basic pne
		var tipRich = '<div class="tp1">Loading Game Description.....</div>';
		doTooltip(event,tipRich);
		ajax.GetGameMouseOver(gid,boxart,currentId,CallBack_LoadTitleMouseOver);
	}
	else
	{
		doTooltip(event,ajaxArray[arrayIndex]);
		//div.innerHTML=ajaxArray[arrayIndex];
	}
}

// ajax callback function
// called when a tooltip is looked up on hte backend, and called for the return
// retunrs an object
//		public class GetGameMouseOverData
//		{
//			public int gameId=0;
//			public string html="";
//			public bool boxart=false;
//			public string div="";
//		}
function CallBack_LoadTitleMouseOver(response)
{
	if (response.error != null)
	{
		// not sure what to do here
		return;
	}
	
	// add to the array cache
	var arrayIndex=response.value.gameId + '_' + response.value.boxart;
	ajaxArray[arrayIndex]='<div class="tp1">' + response.value.html + '</div>';
	
	// redo the ToolTip, if the same is still displayed
	if(response.value.div==currentId)
	{
		Tooltip.writeTip('<div class="tp1">' + response.value.html + '</div>');
	}
}
