// отображение модального окна с инфой о пользователе
var container = $("<div class='user-popup'></div>").css("display", "none");
var activeLink = null;

/**
 * detected mouse leave event on small info box to user
 */
function hasLeave(event, box) {

	var pos = box.position();
	var mouseoutX = true;
	var mouseoutY = true;

	var link = activeLink;
	var mouseoutLinkX = true;
	var mouseoutLinkY = true;

	if(pos.left < event.pageX && (pos.left + box.width()) > event.pageX) {
		mouseoutX = false;
	}
	if(pos.top < event.pageY && (pos.top + box.height()) > event.pageY) {
		mouseoutY = false;
	}
	if(link.offset().left < event.pageX && (link.offset().left + link.width()) > event.pageX) {
		mouseoutLinkX = false;
	}
	if(link.offset().top < event.pageY && (link.offset().top + link.height()) > event.pageY) {
		mouseoutLinkY = false;
	}

	return (mouseoutX || mouseoutY) && (mouseoutLinkX || mouseoutLinkY);
}


$(document).ready(function() {

	container.mouseleave(function(e) {
		if(hasLeave(e, $(this))) {
			container.empty().css("display", "none");
			$("#smallinfobox").attr("id", "");
		}
	});

	$("a.smallinfo").mouseenter(function(e){
  	 
    activeLink = $(this);
    container.css("position", "absolute").css("z-index", "9999")
			.css("top", ($(this).offset().top-120+$(this).height()) + "px").css("left", $(this).offset().left+$(this).width() + "px")
             .css("width", "300px").css("height", "130px")
             .css("display", "block");
            
    container.appendTo("body");
    if(!container.html()) {
      container.load(this.href+"smallinfo/"); 
    }
  }).mouseleave(function(e){

  	if(hasLeave(e, container)) {
  		container.empty().css("display", "none");
  		$(this).attr("id", "");
    }
  });

});