function WoWItems(){}

WoWItems.INIT = 0;
WoWItems.LOADING = 1;
WoWItems.LOADED = 2;
WoWItems.ERROR = 3;
WoWItems.URL_AJAX = 'http://www.darluok-server.com/plugins/icones/item_popup.php';
WoWItems.URL_ITEM_INFO = 'http://www.darluok-server.com/plugins/icones/item_info.php';
WoWItems.LOGIN_TXT = 'Chargement...';
WoWItems.ERROR_TXT = 'Erreur';

WoWItems.start = function () {
	$(function(){
		var allItems = $('.WoWItem');
		allItems.each(function(k,value) {
			var href = $(value).get(0).href;
			(new WoWItems).init(value,href.substr(href.lastIndexOf('=')+1));
	})});
};

WoWItems.prototype = {

	init: function(elem, id){
		var self = this;
		this.elem = $(elem);
		var img = $('<img>');
		img.css('width','44px').css('height','44px');
		// this.elem.css('width','44px').css('height','44px').css('background-image','url("http://static.wowhead.com/images/Icon/medium/border/default.png")');
		// this.elem.css('float','left').css('width','44px').css('height','44px').css('background-image','url("http://static.wowhead.com/images/Icon/medium/border/default.png")');
		// a.css('display','block').css('height','100%').css('width','100%');
		this.itemId = id;
		this.state = WoWItems.INIT;
		this.popup = $('<div>').addClass('wowhead-tooltip').hide();
		this.popup.css('width','320px').css('position','absolute').css('visibility','visible').css('float','left');
		this.popup.html(this.innerHTML);
		this.elem.append(img);
		$('body').append(this.popup);
		this.elem.mouseenter(function(e){self._onMouseEnter(e)}).mouseleave(function(e){self._onMouseLeave(e)}).mousemove(function(e){self._onMouseEnter(e)});
		$.ajax({url: WoWItems.URL_ITEM_INFO+"?item="+id,dataType: 'json',success:function(data){
			if (!data.icon) {
				img.attr('src','http://www.darluok-server.com/img/icon_border/icon_border_0.png');
			} else {
				img.css('background','url("http://static.wowhead.com/images/wow/icons/medium/'+(data.icon)+'.jpg") center center no-repeat');
				img.attr('src','http://www.darluok-server.com/img/icon_border/icon_border_'+(data.quality)+'.png');
			}
		}});
		this.server = this.elem.attr('server') ? this.elem.attr('server') : 0;
		this.guid = this.elem.attr('guid') ? this.elem.attr('guid') : 0;
		return this;
	},
	
	_onMouseEnter:function(e){
		this.load().show(e.pageX+5,e.pageY);
	},
	
	_onMouseLeave:function(e){
		this.hide();
	},
	
	_onLoading: function () {
		this.state = WoWItems.LOADING;
		this.popup.html(WoWItems.LOGIN_TXT);
	},
	
	_onLoaded: function (data) {
		this.state = WoWItems.LOADED;
		this.popup.html(data);
	},
	
	_onError: function () {
		this.state = WoWItems.ERROR;
		this.popup.html(WoWItems.ERROR_TXT);
	},
	
	show:function(x,y){
		if (x != undefined && y != undefined) {
			this.popup.css('top',y+'px').css('left',x+10+'px');
		}
		this.popup.show();
		return this;
	},
	
	hide:function(){
		this.popup.css('top','').css('left','');
		this.popup.hide();
		return this;
	},
	
	load: function() {
		if (this.state != WoWItems.INIT) return this;
		return this.reload();
	},
	
	reload: function() {
		var self = this;
		$.ajax({
			dataType:'html',
			url:WoWItems.URL_AJAX+'?entry='+this.itemId+'&server='+this.server+'&guid='+this.guid,
			success:function(data){self._onLoaded(data)},
			error:function(){self._onError()}
		});
		this._onLoading();
		return this;
	}
	
}

WoWItems.start();
