$(document).ready(function() {
	JT_init();
	alert (123);
});
var timer, timer1;
var href, id, name;

function closeTimer()
{
	timer1 = setTimeout(function(){$('#JT').remove();}, 3000);
}

function JT_init(){
	       $("a.slovar")
		   .hover(function()
		   {
		   		href = this.href;
		   		id = this.id;
		   		name = this.name;

				$('#JT').each(function(){$(this).remove()});

		   		timer = setTimeout(function() { JT_show(href, id, name); }, 1500);
		   },function(){ clearTimeout(timer);  closeTimer(); });

		   $("a.slovar").click(function()
		   	{
		   		clearTimeout(timer);
		   		href = this.href;
	   			id = this.id;
	   			name = this.name;
		   		JT_show(href, id, name);
		   		return false;
		   		}
		   );
	       $("a.trevda")
		   .hover(function()
		   {
		   		href = this.href;
		   		id = this.id;
		   		name = this.name;

				$('#JT').each(function(){$(this).remove()});

		   		timer = setTimeout(function() { JT_show(href, id, name); }, 1500);
		   },function(){ clearTimeout(timer);  closeTimer(); });

		   $("a.trevda").click(function()
		   	{
		   		clearTimeout(timer);
		   		href = this.href;
	   			id = this.id;
	   			name = this.name;
		   		JT_show(href, id, name);
		   		return false;
		   		}
		   );
}



function JT_show(url,linkId,title){



	var hasArea = $(window).width() - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var scrollTop = $(document)[0].scrollY;



	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = parseQuery( queryString );
	if(params['width'] === undefined){params['width'] = 270};
	if(params['link'] !== undefined){
	$('#' + linkId).bind('click',function(){window.location = params['link']});
	$('#' + linkId).css('cursor','help');
	}
	$("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='jt_loader'><img src='/img2/loading.gif'></div><div id='JT_copy'> </div></div>");//right side

	clearTimeout(timer1);

	$('#JT').hover(function()
	{
		clearTimeout(timer1);
	}, function()
	{
		timer1 = setTimeout(function(){$('#JT').remove();}, 3000);
	});

	if(hasArea>((params['width']*1)+75)){

		var arrowOffset = $('#' + linkId).width() + 11;
		if(getAbsoluteLeft(linkId) + arrowOffset > $(document).width())
		{
			arrowOffset = -(params['width']*1) + 15;
		}
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else
	{

		var clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 15); //set x position
	}

	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();

	var digitRegexp = new RegExp("\\d{1,}");

	var id = linkId;
	var word_id = id.match(digitRegexp);
	var type = id.replace(digitRegexp,'');

	$.ajax({
			type: "POST",
			url: "/tooltip.php",
			data: "id="+word_id+"&type="+type,
			dataType: "json",
			success: function(msg)
			{
				$('#jt_loader').hide();
				$('#JT_copy').html(msg.value);
				var w = $('#JT_copy').height();

				if(clickElementy + w > scrollTop + $(window).height())
				{
					clickElementy -= w - 20;;
				}

				$('#JT').css({top: clickElementy+"px"});
			}
	});

}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}
