var itemTrack = ""; //Tracks the Last Item Used. 
var x,y; //mouse coords
var cacheItems = new Array(); //holds cached items
var itemHTML; //associated html for items. 

var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}


/*
 * Given an event, will calculate the X and Y position of the Mouse. 
 * This is the location (+offset) where the Item will be displayed. 
 * This method is called every time the mouse moves so the item content
 * will always follow the mouse. 
 */
function divLocation(e, id) {

	/* because IE following standards would just be to much to ask for */
	if(Browser.Version() < 9) {
		x = window.event.x+document.body.scrollLeft;
		y = window.event.y+document.body.scrollTop;
	} else {
		x = e.pageX;
		y = e.pageY;
	}

    /* Offset the Item so the Div box isn't directly over the mouse area. */
    document.getElementById("itemHover").style.top = (y+10) +'px';
    document.getElementById("itemHover").style.left = (x+10) +'px';

    /* Pull the data for the Div box. Only if the Item Numbers are different */
    itemHover(id, e);

}

function itemHover(id, e) {
	/* 
     * If the item track is null, or its a different ID is the only time
     * the database should be queried for a new item string. Otherwise, 
     * use the existing item string because the person is
     * rehovering over the same item. 
     */

	//Item Not Found
	if(id==-1) {
		itemTrack = id;
		itemHTML = "Item Not Found.";
		document.getElementById('itemHover').innerHTML = itemHTML;
	} else if(itemTrack=="" || itemTrack==null || itemTrack != id) {

		//Item Change
		itemTrack = id;
	
		//Verifies local cache.	
		if(cacheItems["\""+id+"\""] != null) {
			itemHTML = cacheItems["\""+id+"\""];
			document.getElementById('itemHover').innerHTML = itemHTML;
		} else {
				
			/* Query for new item information */
			var url = "/includes/items/itemFunctions.php?id="+id;

			var itemQuery = new XMLHttpRequest();
			itemQuery.open("GET", url, true);
			itemQuery.onreadystatechange = function() {
				if (itemQuery.readyState === 4) {
					if (itemQuery.status === 200) {
						itemHTML = itemQuery.responseText; 
						if(itemHTML.length > 1) {
							cacheItems["\""+id+"\""] = itemHTML;
						}
					}
				} else {
					itemHTML = "Loading...";
					document.getElementById('itemHover').innerHTML = itemHTML;

				}
			}

			itemQuery.send(null);
			document.getElementById('itemHover').innerHTML = itemHTML;
		}

    } else if(itemTrack == id) {
		document.getElementById('itemHover').innerHTML = itemHTML;
    }

}

    $(".itemhoverclass").live('mouseover mouseleave', function(event) {
		if(event.type=='mouseover') {
            $(".itemHover").fadeIn("fast"); 
		} else {
            $(".itemHover").fadeOut("fast"); 
		}

        }); 

