/********************************************************
**
** name           Dokushuu Tooltip
** description    Add a tooltip on item links
** version        0.1
** author         NoGoal
**
********************************************************/

/********************************************************
** Preferences
********************************************************/

// Distance between mouse cursor and the tooltip
var xOffset = 10;
var yOffset = 30;
// array with websites (using regexp) we want to have the tooltip on
var webDBs = new Array(	/http:\/\/[www.]*xyphos.com\/ao\/aodb.php\?id=\d{4,7}/, 
						/http:\/\/[www.]*aomainframe.info\/showitem.aspx\?AOID=\d{4,7}/,
						/http:\/\/[www.]*auno.org\/ao\/db.php\?id=\d{4,7}/,
						/http:\/\/[www.]*aodb.info\/showitem.asp\?AOID=\d{4,7}/);
// various (cosmetic|links) variables
var loadingGIF	= 'http://www.dokushuu.com/tooltip/loading.gif';
var cssURL		= 'http://www.dokushuu.com/tooltip/styles.css';
var toolTipDB	= 'http://www.dokushuu.com/tooltip/index2.php';
var iconsURL	= 'http://www.dokushuu.com/xml/aoicons/';

/********************************************************
** Script starts here, nothing to modify under this point
********************************************************/

var dkshttp = $('<div class="dkshttp"></div>');

var styleElement=document.createElement('link');
	styleElement.rel = 'stylesheet';
	styleElement.type = 'text/css';
	styleElement.href = cssURL;
	document.getElementsByTagName("head")[0].appendChild(styleElement);
		

function getAOID(link)
{
	var vAOID = 0;
	for(i=0;i<webDBs.length;i++)
	{
		if(vAOID==0)
		{
			var vAOID = (webDBs[i].exec(link)) ? /\d{4,7}/.exec(link) : 0 ;
		}
	}
	return vAOID;
}

$(document).ready
(
	function()
	{

		
		$('body').append(dkshttp);
		$('a').hover
		(
			function(e)
			{
				var itemAOID = getAOID(this.href);
				if(itemAOID!=0)
				{
					$(".dkshttp").css("top", (e.pageY - xOffset) + "px");
					$(".dkshttp").css("left",(e.pageX + yOffset) + "px");
					$(".dkshttp").css("z-index", "99");
					$(".dkshttp").fadeIn("fast");
					$('.dkshttp').html('<table><tr><td><img src="' + loadingGIF + '" alt="loading..." /></td></tr></table>'); 
					$.ajax
					({
						url:		toolTipDB,
						dataType:	'jsonp',
						jsonp:		'item',
						data:		'aoid=' + getAOID(this.href),
						success:	function(data)
									{
										var text =	'<table>' + 
														'<tr>' +
															'<td class="dkctgr" colspan="2"><img src="' + iconsURL + data.icon + '.gif" alt= "' + data.icon + '"/></td><td colspan="2">' + data.name + '</td>' +
														'</tr>' +
														'<tr>' +
															'<td colspan="4" class="dkctgr">Attributes</td>' +
														'</tr>' +
														'<tr>' +
															'<td colspan="4">' + data.attr + '</td>' +
														'</tr>' +
														'<tr>' +
															'<td colspan="4" class="dkctgr">Reqs</td>' +
														'</tr>' +
														'<tr>' +
															'<td colspan="4">' + data.reqs + '</td>' +
														'</tr>' +
														'<tr>' +
															'<td colspan="4" class="dkctgr">Effects</td>' +
														'</tr>' +
														'<tr>' +
															'<td colspan="4" class="dklist">' + data.effects + '</td>' +
														'</tr>' +
															'</table>';
										$('.dkshttp').html(text); 
									}
					});
				}
			},
			function()
			{
				$('.dkshttp').empty();
			}
		)
	}
);
