// DOM2 create tooltip element.
function initTooltip() {
  var tt = document.createElement("div");
  tt.setAttribute("id", "tt");
  var bodyPage = document.getElementById("bodyPage");
  bodyPage.appendChild(tt);

    var tt_header = document.createElement("div");
    tt_header.setAttribute("id", "tt_header");
    var tt_header_text = document.createTextNode("Tip pre Vás!");
    tt_header.appendChild(tt_header_text);
    tt.appendChild(tt_header);

    var tt_body = document.createElement("div");
    tt_body.setAttribute("id", "tt_body");
    tt.appendChild(tt_body);

    var tt_footer = document.createElement("div");
    tt_footer.setAttribute("id", "tt_footer");
    var tt_footer_text = document.createTextNode("SK Model s.r.o.");
    tt_footer.appendChild(tt_footer_text);
    tt.appendChild(tt_footer);
}

// DOM2 delete tooltip element.
function deleteTooltip() {
  var bodyPage = document.getElementById("bodyPage");
  var tt = document.getElementById("tt");
  bodyPage.removeChild(tt);
}

// jQuery selectors for tooltips.
$(document).ready(function() {
  $("a[rel]").mouseover(function() {
    showTooltip(this, this.rel);
  });
  $("a[rel]").mouseout(function() {
    hideTooltip();
  });
});

// Show tooltip.
function showTooltip(elem, text) {
  initTooltip();
  document.getElementById("tt_body").innerHTML = text;  
  var tt = document.getElementById("tt");

  var x = getX(elem);
  var y = getY(elem);
  if(navigator.platform == "Linux" && navigator.userAgent.indexOf("Firefox") !=-1)
    x += getSide();

  var way = whichWay(elem);
  var moveValue = moveT(way, x, y);
  tt.style.left = moveValue[0]+"px";
  tt.style.top = moveValue[1]+"px";
}

// Hide tooltip.
function hideTooltip() {
  deleteTooltip();
}

// Side of main container.
function getSide() {
  var side = Math.round((document.documentElement.clientWidth - 1000)/2);
  return (side > 0 ? side : 0);
}

// Decide which way will be tooltip displayed.
function whichWay(elem) {
  var x = getX(elem);
  var y = getY(elem);

  var tt_width = 200;
  var tt_height = 100;
  
  var win = windowSize(); // [x, y]
  // window_width - tooltip_width >= x_element + 25_side
  if((win[0]-tt_width) >= (x+25)) {
    if((win[1]-tt_height) >= (y+25)) {
      return 1; // right down
    }
    else if((win[1]-tt_height) < (y+25)) {
      return 2; // right up
    }
    else {
      return 0;
    }
  }
  else
  if((win[0]-tt_width) < (x+25)) {
    if((win[1]-tt_height) >= (y+25)) {
      return 3; // left down
    }
    else if((win[1]-tt_height) < (y+25)) {
      return 4; // left up
    }
    else {
      return 0;
    }
  }
  else {
    return 0;
  }
}

// Return move values.
function moveT(way, x, y) {
  var down = 10;
  var right = 10;

  if(way == 1) {
    return [x+right+23, y+down+16];
  }
  else if(way == 2) {
    return [x+right+23, y-100-down];
  }
  else if(way == 3) {
    return [x-200-right, y+down+16];
  }
  else if(way == 4) {
    return [x-200-right, y-100-down];
  }
  else {
    return [0, 0];
  }
}

function getY(elem) {
  var y = 0;
  while( elem != null ) {
    y += elem.offsetTop;
    elem= elem.offsetParent;
  }
  return y;
}

function getX(elem) {
  var x = 0;
  while( elem != null ) {
    x += elem.offsetLeft;
    elem = elem.offsetParent;
  }
  return x;
}

// Get window size.
function windowSize() {
  var docWidth = document.documentElement.clientWidth + window.pageXOffset;
  var docHeight = document.documentElement.clientHeight + window.pageYOffset;
  // IE.
  if(navigator.userAgent.indexOf("MSIE") !=-1) {
    docWidth = document.documentElement.clientWidth + document.body.offsetLeft;
    docHeight = document.documentElement.clientHeight + document.body.offsetHeight;
  }
  return [docWidth, docHeight];
}
