﻿
function myToolTip(el, event, width, height, content) {
    $("#myToolTip").remove();
    var offsetxpoint = 5 //Customize x offset of tooltip
    var offsetypoint = 5 //Customize y offset of tooltip
    var ie = document.all
    function ietruebody() { return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body };
    var ns6 = document.getElementById && !document.all
    var curX = (ns6) ? event.pageX : event.clientX + ietruebody().scrollLeft + 15;
    var curY = (ns6) ? event.pageY : event.clientY + ietruebody().scrollTop - 30;

    //Find out how close the mouse is to the corner of the window
    var rightedge = ie && !window.opera ? ietruebody().clientWidth - event.clientX - offsetxpoint : window.innerWidth - event.clientX - offsetxpoint - 20
    var bottomedge = ie && !window.opera ? ietruebody().clientHeight - event.clientY - offsetypoint : window.innerHeight - event.clientY - offsetypoint - 20

    var leftedge = (offsetxpoint < 0) ? offsetxpoint * (-1) : -1000

    var left = 0;
    var top = 0;

    if (rightedge < width)
        left = ie ? ietruebody().scrollLeft + event.clientX - width : window.pageXOffset + event.clientX - width;
    else if (curX < leftedge)
        left = 5;
    else
        left = curX + offsetxpoint;

    //same concept with the vertical position
    if (bottomedge < height)
        top = ie ? ietruebody().scrollTop + event.clientY - height - offsetypoint : window.pageYOffset + event.clientY - height - offsetypoint;
    else
        top = curY + offsetypoint;

    $("body").append($("<div>").attr("id", "myToolTip"));
    $("#myToolTip").css({ "left": left + "px", "top": top + "px", "width": width + "px", "height": height + "px" }).html(content).show();
    $("#myToolTip").append($("<a>").attr("href", "#").addClass("btn_close").html("zavřít okno").click(function () { $("#myToolTip").remove(); return false; }));
}

$(function () {

    $.fn.imgToolTip = function () {
        var a = arguments;

        return this.each(
      function () {
          $(this).mouseover(function (event) {
              if ($("#imgToolTip").length == 0) {
                  $("body").append($("<div id='imgToolTip'></div>").hide());
              } /* else {
                  $("#imgToolTip").hide().html("");
              }*/
              if ($(this).attr("src")) {
                  var xZoomImg = $(this).attr("src");
                  if (xZoomImg.lastIndexOf('_s.jpg') == xZoomImg.length - '_s.jpg'.length) {
                      xZoomImg = xZoomImg.replace('_s.jpg', '_b.jpg');
                      //var oImage = new Image();
                      //oImage.src = xZoomImg;
                      $("#tmp_img").remove();
                      $("body").append($("<img src='" + xZoomImg + "' id='tmp_img'>").hide());
                      $("#tmp_img").load(function () {
                          $("#imgToolTip").html("<img src='" + xZoomImg + "' alt='' />").css({ "width": $("#tmp_img").width() + "px", "height": $("#tmp_img").height() + "px" });
                          $("#tmp_img").remove();
                      });
                      //alert($("#tmp_img").width());
                      //alert('a');
                      /*oImage.onload = function () {
                      var xWidthImg = this.width;
                      var xHeightImg = this.height;
                      //alert(xWidthImg);
                      $("#imgToolTip").html("<img src='" + xZoomImg + "' alt='' />").css({ "width": xWidthImg + "px", "height": xHeightImg + "px" });
                      //ddrivetip("<img src='" + xZoomImg + "' alt='' />", "#FFFFFF", xWidthImg, 0);
                      }*/
                  }
              } else {
                  $("#imgToolTip").html($(this).attr("data-title"));
              }

              //$("#imgToolTip").css({"left":left+"px", "top":top+"px"}).show();
          });
          $(this).mouseout(function () {
              $("#imgToolTip").hide();
          });
          $(this).unload(function () {
              $("#imgToolTip").hide();
          });
          $(this).mouseleave(function () {
              $("#imgToolTip").hide();
          });
          $("#imgToolTip").mouseout(function () {
              $("#imgToolTip").hide();
          });
          $(this).mousemove(function (event) {
              var offsetxpoint = -10 //Customize x offset of tooltip
              var offsetypoint = 30 //Customize y offset of tooltip
              var ie = document.all
              function ietruebody() { return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body };
              var ns6 = document.getElementById && !document.all
              var curX = (ns6) ? event.pageX : event.clientX + ietruebody().scrollLeft + 15;
              var curY = (ns6) ? event.pageY : event.clientY + ietruebody().scrollTop - 30;

              //Find out how close the mouse is to the corner of the window
              var rightedge = ie && !window.opera ? ietruebody().clientWidth - event.clientX - offsetxpoint : window.innerWidth - event.clientX - offsetxpoint - 20
              var bottomedge = ie && !window.opera ? ietruebody().clientHeight - event.clientY - offsetypoint : window.innerHeight - event.clientY - offsetypoint - 20

              var leftedge = (offsetxpoint < 0) ? offsetxpoint * (-1) : -1000

              var left = 0;
              var top = 0;

              if (rightedge < $("#imgToolTip").width())
                  left = ie ? ietruebody().scrollLeft + event.clientX - $("#imgToolTip").width() : window.pageXOffset + event.clientX - $("#imgToolTip").width();
              else if (curX < leftedge)
                  left = 5;
              else
                  left = curX + offsetxpoint;

              //same concept with the vertical position
              if (bottomedge < $("#imgToolTip").height())
                  top = ie ? ietruebody().scrollTop + event.clientY - $("#imgToolTip").height() - offsetypoint : window.pageYOffset + event.clientY - $("#imgToolTip").height() - offsetypoint;
              else
                  top = curY + offsetypoint;

              $("#imgToolTip").css({ "left": left + "px", "top": top + "px", "display": "block" }).show();
          });
      }
   );
    };
});
