/**
 * Prevede rozmer s 'px' na cislo
 *
 * @param string str
 * @return int
 */
function toInt(str) {
  var val = 0;
  if (str.length>0) {
    val = parseInt(str.substring(0,str.indexOf('p')));
    if (isNaN(val)){
      val = 0;
    }
  } 
  return val;
}

/**
 * Vycentruje prvek
 *
 * @param Object elem - pozicovany element
 * @param int width - sirka prvku
 * @param int heigh - vyska prvku
 */
function centerElement(elem, width, height) {
  centerElementHorizontal(elem, width);
  centerElementVertical(elem, height);
}


/**
 * Vycentruje prvek na vertikalni ose
 *
 * @param Object elem - pozicovany element
 * @param int heigh - vyska prvku, default 250
 */
function centerElementVertical(elem, height) {
  if (height == null) { height = 250; }
//  elem.style.height = height+'px';

  var winHeight = window.innerHeight ? window.innerHeight : document.body.clientHeight;

  var top = Math.round((winHeight - height) / 2);
  if (top < 0) { top = 0; }

  elem.style.top = top+'px';
}



/**
 * Vycentruje prvek na horizontalni ose
 *
 * @param Object elem - pozicovany element
 * @param int width - sirka prvku, default 250
 */
function centerElementHorizontal(elem, width) {
  if (width == null) { width = 250; }
  elem.style.width = width+'px';

//  var winWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;
  var winWidth = elem.parentNode.offsetWidth ? elem.parentNode.offsetWidth : 800;

  var left = Math.round((winWidth - toInt(elem.style.width)) / 2);
  if (left < 0) { left = 0; }

  elem.style.left = left+'px';
}


/**
 * Zobrazi popup
 *
 * @param string elem_id - ID prvku s obsahem, ktery se ma prelit do popupu
 * @param int width - sirka prvku, default 250
 */
function showPopup(elem_id, width, top) {
  hideImage();

  if (width == null) width = 250;
  if (top==null) top = 170;

  var popup = document.getElementById('popup');
  var innerHTML = document.getElementById(elem_id).innerHTML;
  var divs = popup.getElementsByTagName('div');
  var content = null;
  for (var i=0; i<divs.length; i++) {
    if (divs[i].className == 'content') {
      content = divs[i];
    }
  }
  content.innerHTML = innerHTML;
//  centerElement(popup);
  popup.style.top = top+'px';
  popup.style.width = width+'px';
	centerElementHorizontal(popup, width);
  popup.style.visibility = 'visible';
}


/**
 * Schova popup
 */
function hidePopup() {
  var popup = document.getElementById('popup');
  if (popup != null) {
    popup.style.visibility = 'hidden';
  }
}


/**
 * Zobrazi popup s obrazkem
 *
 * @param string src - sdroj ciloveho obrazku
 */
function showImage(src, info, top) {
  hidePopup();

  if (info==null) info = '';
  if (top==null) top = 60;

  var popup = document.getElementById('image');
  if (popup == null) { return false; }

  popup.style.visibility = 'hidden';
  centerElementHorizontal(popup, 700);
  centerElementVertical(popup, 500);
//  popup.style.top = '110px';
//  popup.style.top = top+'px'; -- nastaveno v CSS
  var divs = popup.getElementsByTagName('div');
  for (var i=0; i<divs.length; i++) {
    if (divs[i].className == 'info') {
      divs[i].innerHTML = info;
    }
  }

  var img = popup.getElementsByTagName('img')[0];

  img.src = 'css/img/loading.jpg';

  img.src = src;

  popup.style.visibility = 'visible';
}


/**
 * Schova popup s obrazkem
 */
function hideImage() {
  var popup = document.getElementById('image');
  if (popup != null) {
    popup.style.visibility = 'hidden';
  }
}
