dojo.require("dojo.html");

//Lets make our tooltip global so that we can reference it from anywhere
//If you put this file first you can create tooltips for anything
var tt = dojo.byId('tempdata');

//At this pint we always want the tooltip to be hidden
var clearTT = true;

//Lets also global out interval variable so that we can also reference it anywhere
var clear;

//For IE to set the clear since we cannot access globals from the page directly
function setClear(val) {clearTT = val;}

function showToolTip(ttMsg, ttId) {
    clearInterval(clear);

    var activeCell = dojo.byId(ttId);

    tt = dojo.byId('tempdata');

    dojo.style(tt, {
        'border': '0',
        'left': '0',
        'top': '0'
    });

    dojo.html.set(tt, ttMsg);
    dojo.style(tt, {
        'border': '1px solid black',
        'background': '#fff'
    });

    clearTT = false;

    //For Firefox we can set the attributes on the tooltip to not close if the mouse has entered the frame
    //For IE we have to set it in the page itself <table id="tblShowcaseProfile" onmouseover="setClear(false)" onmouseout="setClear(true)">
    dojo.attr(tt, 'onmouseover', 'javascript:clearTT = false;')
    dojo.attr(tt, 'onmouseout', 'javascript:clearTT = true;')

    //Set initial styles
    dojo.style(tt, { 'top': '10px' });
    dojo.style(tt, { 'left': ((dojo.coords(activeCell).x - dojo.coords(dojo.byId('wrapper')).x) + dojo.coords(activeCell).w) + 'px' });

    //Override style if the tooltip will go above the window and push it down
    if ((dojo.coords(activeCell).y - dojo.coords(tt).h) > 0) {
        dojo.style(tt, { 'top': (dojo.coords(activeCell, true).y - dojo.coords(tt).h) + 'px' });
    }

    //override the style if the tooltip will go to far right and push it to the left
    if ((dojo.coords(activeCell).x + dojo.coords(activeCell).w + 230) > dojo.coords(dojo.body()).w) {
        dojo.style(tt, { 'left': (dojo.coords(activeCell).x - dojo.coords(activeCell).w) + 'px' });
    }

    return;
}

function hideToolTip() {
	//Before we hide the tooltip give it a delay in case the user is moving into the tooltip space
	clearTT = true;
	clear = setInterval(cleartt, 500);
}

function cleartt() {
	//Has the user entered the space? If so cancel
	if(!clearTT){return;}
	
	//Otherwise kill the tooltip
	clearInterval(clear);
	dojo.style(tt, {
		'border': '0',
		'left'  : '0',
		'top'   : '0'
	});
	dojo.html.set(tt, '');	
}

function ajaxTooltip( self) {
	if (dojo.attr(self, 'agentUrl') != '') {
		if (self.alt) { self.alt='';}
		dojo.xhrGet({
			url: dojo.attr(self, 'agentUrl'),
			load: function( data ) {showToolTip(data, self.id)},
			error: function( error ){alert(error);}
		});
	}
	return;
}
