
//override the existing indexOf to be able to look after a specific index and check vartypes
//extended with begin and strict attributes (backward compatible to current ext-indexOf function!)

Array.prototype.indexOf = function( o, b, s ) {
    for( var i = +b || 0, len = this.length; i < len; i++) {
//check for objecttype to make this function work with date-objects also
        if( this[i]===o || s && this[i]==o  || Ext.isDate(o) && this[i].toString()==o.toString()) {
            return i;
        }
    }
    return -1;
};

//already fetched colors are cached in this array
var GTOitemCache = {};

function GTOdecorateItemLinks ()
{	
	var iL = Ext.select('a[href*="wowhead"]:not([class])');
	if (iL.elements.length == 0)
	{
		return false;
	}
	
	Ext.Ajax.timeout = 0;
	
	var list = GTOgetItemIds(iL);
	if (list.length == 0)
	{
		return false;
	}
	
	Ext.Ajax.request({
		url: 'content.php',
		params: 'load=ajax-decorate-tips&items='+list,
		success: function(r)
		{ 
			var d = Ext.util.JSON.decode(r.responseText);
			
			Ext.each(d.items, function(s, idx)
			{
				Ext.select('a[href$="'+s.item_id+'"]').addClass(s.item_color);
				eval('GTOitemCache.i'+s.item_id+' = function() {return '+Ext.encode(s)+';}');
			});
		}
	});
}

function GTOgetItemIds (i)
{
	var rs = '';
	var tA = new Array();
	i.each(function (s, idx)
	{
		var thisId = s.dom.href.substr(s.dom.href.indexOf('item=')+5);
		
		if ((tA.indexOf(thisId) == -1) && (Ext.type(eval('GTOitemCache.i'+thisId)) == false))
		{
			rs += '|'+thisId;
			tA.push(thisId);
		}
		
		//item color is already cached, grab it from there
		if (Ext.type(eval('GTOitemCache.i'+thisId)) == 'function')
		{
			var data = eval('GTOitemCache.i'+thisId+'()');
			Ext.select('a[href$="'+thisId+'"]').addClass(data.item_color);
		}
	});
	
	return rs.substr(1);
}
