
s = document.getElementById("wowmateapi")

var st = document.createElement("script")
st.setAttribute("type","text/javascript");
st.setAttribute("src","http://static.wowmate.ru/api/jquery.js");
s.appendChild(st);

var st = document.createElement("link");
st.setAttribute("rel","stylesheet");
st.setAttribute("href","http://static.wowmate.ru/api/global.css");
s.appendChild(st);

window.onload = function(){JT_init();};

function JT_init(){
 var aList= document.getElementsByTagName('a');
 var ll = '';
 var reg_ach=/wowmate.ru\/ach(\d+).php/;
 var reg_item=/wowmate.ru\/item(\d+).php/;
 var reg_npc=/wowmate.ru\/npc(\d+).php/;
 for (var i=0;i<aList.length;i++)
 {    
   var ah = aList[i];
   //achieve
   arr = reg_ach.exec(ah.href); 
   if (arr != null) {
    ah.name ='ach'+arr[1];
    ah.className= 'jTip wow-achievement';
   } else {
    //item
    arr = reg_item.exec(ah.href); 
    if (arr != null) {
     ah.tp = "items"
     ah.code = arr[1]
     ah.className= 'jTip wow-item';
    } else {
     //npc
     arr = reg_npc.exec(ah.href); 
     if (arr != null) {
      ah.name ='npc'+arr[1];
      ah.className= 'jTip wow-npc';
     }
    }
   }
   if (arr != null) {
    ah.id ='wmtt'+i;
    ah.onmouseover = function(){JT_show(this.id,this.tp,this.code)};
    ah.onmouseout = function(){$('#JT').remove()};
   }
 }           	   
}

function JT_show(linkId,tp,code){
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; 
        $("body").append("<div id='JT'></div>");     
	if(hasArea>(500+75)){
          //right
          var arrowOffset = getElementWidth(linkId) + 11;
	  var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	  $('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	}else{
          //left
	  var clickElementx = w - getAbsoluteLeft(linkId) - 22; //set x position
	  $('#JT').css({right: clickElementx+"px", top: clickElementy+"px"});
	}	
        $.getJSON("http://static.wowmate.ru/json/?type="+tp+"&code="+code+"&callback=?", 
                  function(json) {
                   document.getElementById("JT").innerHTML = 
                    "<table class='wowhead-tooltip'><tr><td class='wt'>"+   
                    json+
                    "</td><td class='wt-right'></td></tr>"+                          
                    "<tr><td class='wt-bottom'></td><td class='wt-bottom-r'></td>"+  
                    "</tr></table>"; 
                  });
}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft           
	while(o.offsetParent!=null) {   
		oParent = o.offsetParent    
		oLeft += oParent.offsetLeft 
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	o = document.getElementById(objectId)
	oTop = o.offsetTop            
	while(o.offsetParent!=null) { 
		oParent = o.offsetParent  
		oTop += oParent.offsetTop 
		o = oParent
	}
	return oTop
}
