// JavaScript Document
itemsListContainer = false;
queuedItems = 0;
currentListItem = 0;
currentList = 0;
isLoadingList = false;
maxQueueLength = 1; //arbitrary number, but limiter necessary thanks to IE
itemTimeout = 0;

function loadItemsList(thisList)
{
	if(!isLoadingList)
	{
		if(!itemsListContainer) itemsListContainer = document.getElementById("itemsContainer");
		itemsListContainer.innerHTML = "";
		currentList = thisList;
		isLoadingList = true;
		timedLoadItemsList(thisList);
	}
}

function timedLoadItemsList(thisList)
{
//check if queue is full
//queue not full? load current item, attempt to load next item unless end of list has been reached
//queue full? wait 250ms, try again

	if(queuedItems < maxQueueLength)
	{
		if(iconNameBuffer[parseInt(thisList[currentListItem])] == null)
		{
			queuedItems++;
			itemsListContainer.appendChild(createNewListItem(thisList[currentListItem]));
			itemAddInfoRequest(thisList[currentListItem]);
		}
		else
		{
			itemsListContainer.appendChild(reloadListItem(thisList[currentListItem]));
		}
		currentListItem++;
		if(currentListItem == thisList.length)
		{
			currentListItem = 0;
			queuedItems = 0;
			isLoadingList = false;
		}
		else
		{
			timedLoadItemsList(thisList);
		}
	}
	else
	{
		if(itemTimeout < 20)
		{
			itemTimeout++;
			setTimeout("timedLoadItemsList(currentList)",250);
		}
		else
		{
			itemTimeout = 0;
			currentListItem++;
			if(currentListItem == thisList.length)
			{
				currentListItem = 0;
				queuedItems = 0;
				isLoadingList = false;
			}
			else
			{
				timedLoadItemsList(thisList);
			}
		}
	}
}

function itemAddInfoRequest(thisID)
{
	if(iconNameBuffer[parseInt(thisID)] == null)
	{
		currentlyViewing = thisID;
		requestHTML(thisID);
	}
}

function createNewListItem(thisID)
{
	var newListItem = document.createElement("LI");
	var newItemThumb = document.createElement("IMG");
	var newItemLink = document.createElement("A");
	newListItem.id = thisID+"ListLocation";
	newItemThumb.id = thisID+"Thumbnail";
	newItemThumb.src = "http://www.wowarmory.com/wow-icons/_images/43x43/inv_misc_questionmark.png";
	newItemLink.id = thisID+"itemLink";
	newItemLink.appendChild(document.createTextNode(itemLoadingString));
	newListItem.appendChild(newItemThumb);
	newListItem.appendChild(newItemLink);
	return newListItem;
}

function reloadListItem(thisID)
{
	var newListItem = document.createElement("LI");
	var newItemThumb = document.createElement("IMG");
	var newItemLink = document.createElement("A");
	newItemThumb.src = "http://www.wowarmory.com/wow-icons/_images/21x21/"+iconNameBuffer[parseInt(thisID)][1]+".png";
	newItemLink.href = "http://www.wowarmory.com/item-info.xml?i="+thisID;
	newItemLink.onmouseover = function() { enhancedXmlTTip(thisID); }
	newItemLink.onmouseout = function() { hideTip(); }
	newItemLink.target = "_blank";
	newItemLink.appendChild(document.createTextNode(iconNameBuffer[parseInt(thisID)][0]));
	newListItem.appendChild(newItemThumb);
	newListItem.appendChild(newItemLink);
	return newListItem;
}

function insertListItem(thisID)
{
	document.getElementById(thisID+"Thumbnail").src = "http://www.wowarmory.com/wow-icons/_images/21x21/"+iconNameBuffer[parseInt(thisID)][1]+".png";
	document.getElementById(thisID+"Thumbnail").onmouseover = function(){enhancedXmlTTip(thisID);}
	document.getElementById(thisID+"Thumbnail").onmouseout = function(){hideTip();}
	document.getElementById(thisID+"itemLink").innerHTML = iconNameBuffer[parseInt(thisID)][0];
	document.getElementById(thisID+"itemLink").target = "_blank";
	document.getElementById(thisID+"itemLink").href = "http://www.wowarmory.com/item-info.xml?i="+thisID;
	document.getElementById(thisID+"itemLink").onmouseover = function(){enhancedXmlTTip(thisID);}
	document.getElementById(thisID+"itemLink").onmouseout = function(){hideTip();}
}