var fetching_tooltip;
var fetching_named_tooltip;
var suppressToolTips = false;


function hide_tooltip() {
    document.getElementById('main_tooltip_content').innerHTML = '';
    document.getElementById( 'main_tooltip' ).style.display="none";
}


function get_item_tooltip( itemid, e ) {
	pos_tooltip(e);
    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
    document.getElementById( 'main_tooltip' ).style.display="inline";
    clearTimeout( fetching_named_tooltip );
    fetching_named_tooltip = setTimeout( 'fetch_item_tooltip( "' + itemid + '" )', 50 );
}


function get_alternate_item_tooltip( itemname, e ) {
	pos_tooltip(e);
    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
    document.getElementById( 'main_tooltip' ).style.display="inline";
    clearTimeout( fetching_named_tooltip );
    fetching_named_tooltip = setTimeout( 'fetch_alternate_item_tooltip( "' + itemname + '" )', 50 );
}


function get_character_tooltip( characterid, e ) {
	if (!suppressToolTips) {
		pos_tooltip(e);
	    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
	    document.getElementById( 'main_tooltip' ).style.display="inline";
	    clearTimeout( fetching_named_tooltip );
	    fetching_named_tooltip = setTimeout( 'fetch_character_tooltip( "' + characterid + '" )', 50 );
	}
}


function get_build_tooltip( buildtemplateid, e ) {
	if (!suppressToolTips) {
		pos_tooltip(e);
	    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
	    document.getElementById( 'main_tooltip' ).style.display="inline";
	    clearTimeout( fetching_named_tooltip );
	    fetching_named_tooltip = setTimeout( 'fetch_build_tooltip( "' + buildtemplateid + '" )', 50 );
	}
}


function get_bt_character_tooltip( characterid, lockoutCode, e ) {
	if (!suppressToolTips) {
		pos_tooltip(e);
	    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
	    document.getElementById( 'main_tooltip' ).style.display="inline";
	    clearTimeout( fetching_named_tooltip );
	    fetching_named_tooltip = setTimeout( 'fetch_bt_character_tooltip( "' + characterid + '", "' + lockoutCode + '" )', 50 );
	}
}


function get_guild_tooltip( guildid, e ) {
	if (!suppressToolTips) {
		pos_tooltip(e);
	    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
	    document.getElementById( 'main_tooltip' ).style.display="inline";
	    clearTimeout( fetching_named_tooltip );
	    fetching_named_tooltip = setTimeout( 'fetch_guild_tooltip( "' + guildid + '" )', 50 );
	}
}


function get_account_tooltip( accountid, e ) {
	if (!suppressToolTips) {
		pos_tooltip(e);
	    document.getElementById('main_tooltip_content').innerHTML = "<div style='text-align: center;'>Loading..</div>";
	    document.getElementById( 'main_tooltip' ).style.display="inline";
	    clearTimeout( fetching_named_tooltip );
	    fetching_named_tooltip = setTimeout( 'fetch_account_tooltip( "' + accountid + '" )', 50 );
	}
}


function fetch_item_tooltip(itemid) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/queryArmory.asp?itemid=" + itemid, true);
	xmlHttp.send(null);
}


function fetch_alternate_item_tooltip(itemname) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/getAlternativeItemTooltip.asp?itemname=" + itemname, true);
	xmlHttp.send(null);
}


function fetch_character_tooltip(characterid) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/getCharacterSummary.asp?characterid=" + characterid, true);
	xmlHttp.send(null);
}


function fetch_build_tooltip(buildtemplateid) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/getBuildSummary.asp?buildtemplateid=" + buildtemplateid, true);
	xmlHttp.send(null);
}


function fetch_bt_character_tooltip(characterid, lockoutCode) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/getBtCharacterSummary.asp?characterid=" + characterid + "&lockout=" + lockoutCode, true);
	xmlHttp.send(null);
}


function fetch_guild_tooltip(guildid) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/getGuildSummary.asp?guildid=" + guildid, true);
	xmlHttp.send(null);
}


function fetch_account_tooltip(accountid) {

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleTooltipResponse;
	xmlHttp.open("GET", "/common/ajaxfeeds/getAccountSummary.asp?accountid=" + accountid, true);
	xmlHttp.send(null);
}


function handleTooltipResponse() {
	
	if(xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			document.getElementById('main_tooltip_content').innerHTML = xmlHttp.responseText;
			move_tooltip();
		} else {
			document.getElementById('main_tooltip_content').innerHTML = "Problem loading tooltip";
		}
	}
}


function pos_tooltip(e) {
    if ( document.getElementById('main_tooltip') == null ) { return; }
    var x = parseInt( document.getElementById('main_tooltip').style.left);
	var y = parseInt( document.getElementById('main_tooltip').style.top);
	var d = document.getElementById('main_tooltip').style;
    
    var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
    
    document.getElementById('main_tooltip').style.top = posy + "px";
    document.getElementById('main_tooltip').style.left = posx + "px";
}


function move_tooltip() {
    if (!document || !document.body) return; // ie sucks
    if ( document.getElementById('main_tooltip_content') == null || document.getElementById('main_tooltip_content').style.display == 'none' ) { return; }

	var theDiv = document.getElementById("main_tooltip");
	var theContentDiv = document.getElementById("main_tooltip_content");
	
	// Get the height and width of the div	
	var w = theContentDiv.offsetWidth;
	var h = theContentDiv.offsetHeight;
	
	// Get the top and left of the div
	var t = parseInt(theDiv.style.top);
	var l = parseInt(theDiv.style.left);
	
	// Calculate the bottomright x y
	var brx = l+w;
	var bry = t+h;

	// Get the max visible screen size
	var maxvheight = getScrollY() + getWindowHeight() - 40;
	var maxvwidth = getWindowWidth();
	
	// Calculate the height overflow
	var heightOverflow = maxvheight - bry;
	
	// If we have an overflow then move it
	if (heightOverflow < 0) {
		theDiv.style["top"] = parseInt(theDiv.style["top"]) + heightOverflow - 40 +"px";
	}
}

function getWindowHeight() {
    var innerHeight;
    if (window.innerHeight) {
        innerHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        innerHeight = document.documentElement.clientHeight;
    } else if ( document.body ) {
        innerHeight = document.body.clientHeight;
    }
    /*
	if (navigator.appVersion.indexOf('MSIE')>0) {
        innerHeight = document.body.clientHeight;
    } else {
		innerHeight = window.innerHeight;
    }
    */
    return innerHeight;	
  }
  
function getWindowWidth() {
    var innerWidth;
    if ( navigator.appVersion.indexOf('MSIE') >0 ) {
	   innerWidth = document.body.clientWidth;
    } else {
	   innerWidth = window.innerWidth;
    }
    return innerWidth;	
}

function getScrollY() {
    var offset = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        offset = window.pageYOffset;
    } else if ( document.documentElement && document.documentElement.scrollTop ) {
        offset = document.documentElement.scrollTop;
    } else if ( document.body && document.body.scrollTop ) {
        offset = document.body.scrollTop;
    }
    return offset;
}