function hideBar()
{
	if(this.popupbar_timer)
  {
    clearTimeout(this.popupbar_timer);
  }
  var bar = document.getElementById('popupbar');
  if(bar)bar.parentNode.removeChild(bar);
  return false;
}

function changeLocation(obj, photo)
{
  hideBar();
  if(photo)
  {
     return !window.open(obj.href, '_blank', 'top=0, left=0, location=no, scrollbars=yes, resizable=yes, width='+(screen.availWidth-12)+', height='+(screen.availHeight-30));
  }
  else
  {
    window.location.href = obj.href;
    return false;
  }
}

function startTimer(akce, obj, id, type)
{
  var pos = findPos(obj);
  var action = 'getData(\'' + akce + '\', \'' + obj + '\', ' + id + ', \'' + type + '\', ' + pos[0] + ', ' + pos[1] + ')';
  this.popupbar_timer = setTimeout(action, 250);
}

function getData(akce, obj, id, type, x, y)
{
	//alert('stahuji data, akce: ' + akce + ', id: ' + id + ', type: ' + type);

  //var pos = findPos(obj);

	// odmazeme stary skript, pokud existoval
	var hlavicka = document.getElementsByTagName('head')[0];
	var dataLoader = document.getElementById('scriptLoader');
	if(dataLoader) hlavicka.removeChild(dataLoader);
	
	// vytvorime novy element script
	script = document.createElement('script');
	script.id = 'scriptLoader';
	script.src = 'include/getcommentdata.php?akce='+akce+'&r='+Math.random() + '&id=' + id + '&type=' + type + '&x=' + x + '&y=' + y;
	
	//alert(script.src);
	
	// POZOR, DULEZITE!! Skript musime vlozit do stranky pomoci DOM - appendChild()
	// Ihned po vlozeni prohlizec stahne skript a spusti jej
	x = document.getElementsByTagName('head')[0];
	x.appendChild(script);	
	
	return false;
}

function showEventCommentBar(id, type, name, caption, date, text, lastweek, ref, x, y)
{
  if(document.getElementsByTagName)
  {
    hideBar();
    //alert(name + ', ' + caption + ', ' + date + ', ' + text);
    var body = document.getElementsByTagName('body')[0];   
    
    var wrapper = document.createElement('div');
    wrapper.id = 'popupbar';
    wrapper.className = 'arialtextanketa';
    wrapper.style.width = '400px';    
    wrapper.style.position = 'absolute';
    wrapper.style.left = x;   
    wrapper.style.top = y;
    wrapper.className = 'table1';
    wrapper.style.textAlign = 'left';
    wrapper.onmouseout = function()
          {
            return hideBar();
          }
    body.appendChild(wrapper);
   
    var header = document.createElement('div');
    //header.className = 'background1';
    header.style.width = '100%';
    header.style.padding = '3px';
    header.style.margin = '1px';
    wrapper.appendChild(header);
    
    var right = document.createElement('div');
    //right.className = 'arialtextanketabold';
    //right.style.float = 'right';
    //right.style.marginLeft = '300px';
    right.style.position = 'absolute';
    //right.style.float = 'right';
    right.style.width = '100px';
    right.style.left = '290px';
    right.style.textAlign = 'right';
    right.innerHTML = '<a class=small href="#" onclick="hideBar();">zavřít</a>';
    //right.innerHTML += '&nbsp;<span class="close"><a href="#" onclick="hideBar();">X</a></span>';
    header.appendChild(right);

    var left = document.createElement('div');
    left.className = 'arialtextanketabold';
    //left.style.float = 'left';
    //left.style.position = 'relative';
    left.innerHTML = name + ', ' + date;
    header.appendChild(left);    

    var left = document.createElement('div');
    left.className = 'arialtextanketabold';
    //left.style.float = 'left';
    left.style.paddingTop = '2px';
    left.innerHTML = caption;
    header.appendChild(left);    

    var content = document.createElement('div');    
    content.className = 'arialtextanketa';
    content.style.padding = '3px';
    content.style.margin = '1px';
    content.innerHTML = '<span class="odkazarialanketa">' + text + '</span>';    
    wrapper.appendChild(content);    

    var bottom = document.createElement('div');
    bottom.style.padding = '3px';
    bottom.style.margin = '1px';
    bottom.style.textAlign = 'left';
    
    if(type == 'guestbook')
    {    
      if(lastweek == 1)newmes = 'nový vzkaz';
      else if((lastweek > 0) && (lastweek < 5))newmes = 'nové vzkazy';
      else newmes = 'nových vzkazů';
    }
    else
    {
      if(lastweek == 1)newmes = 'nový komentář';
      else if((lastweek > 0) && (lastweek < 5))newmes = 'nové komentáře';
      else newmes = 'nových komentářů';    
    }        
    
    //bottom.innerHTML = '<a class=small href="' + ref + '" onclick="return changeLocation(this)">starší ' + mes + ' (' + lastweek + ' ' + newmes + ' za poslední týden) >></a>';
    bottom.innerHTML = '<span class="arialtextanketa">' + lastweek + ' ' + newmes + ' za poslední týden</span>';
    wrapper.appendChild(bottom);    

    return false;    
  }
  return true;  
}

function showPhotoCommentBar(id, type, name, caption, date, text, lastweek, ref, src, imgattr, x, y)
{
  if(document.getElementsByTagName)
  {
    hideBar();
    //alert(name + ', ' + caption + ', ' + date + ', ' + text);
    var body = document.getElementsByTagName('body')[0];   
    
    var wrapper = document.createElement('div');
    wrapper.id = 'popupbar';
    wrapper.className = 'arialtextanketa';
    wrapper.style.width = '350px';    
    wrapper.style.position = 'absolute';
    wrapper.style.left = x;   
    wrapper.style.top = y;
    wrapper.className = 'table1';
    wrapper.style.textAlign = 'left';
    wrapper.onmouseout = function()
          {
            return hideBar();
          }
    body.appendChild(wrapper);
   
    var header = document.createElement('div');
    //header.className = 'background1';
    header.style.width = '100%';
    header.style.padding = '3px';
    header.style.margin = '1px';
    wrapper.appendChild(header);
    
    var right = document.createElement('div');
    //right.className = 'arialtextanketabold';
    //right.style.float = 'right';
    //right.style.marginLeft = '300px';
    right.style.position = 'absolute';
    //right.style.float = 'right';
    right.style.width = '100px';
    right.style.left = '240px';
    right.style.textAlign = 'right';
    right.innerHTML = '<a class=small href="#" onclick="hideBar();">zavřít</a>';
    //right.innerHTML += '&nbsp;<span class="close"><a href="#" onclick="hideBar();">X</a></span>';
    header.appendChild(right);

    var left = document.createElement('div');
    left.className = 'arialtextanketabold';
    //left.style.float = 'left';
    //left.style.position = 'relative';
    left.innerHTML = caption;
    header.appendChild(left);    

    var comment = document.createElement('div');
    comment.style.position = 'absolute';
    comment.style.width = '190px';
    comment.style.left = '160px';
    comment.style.textAlign = 'left';
    comment.innerHTML = '<span class="arialtextanketabold">' + name + ', ' + date + '<br></span>';
    comment.innerHTML += '<span class="arialtextanketa">' + text + '</span>';
    //right.innerHTML += '&nbsp;<span class="close"><a href="#" onclick="hideBar();">X</a></span>';
    wrapper.appendChild(comment);
    
    var image = document.createElement('div');
    //image.className = 'arialtextanketabold';
    //left.style.float = 'left';
    image.style.padding = '3px';
    image.style.margin = '1px';
    image.style.paddingTop = '2px';
    image.innerHTML = '<img class=table1 align=absmiddle src="' + src + '">';
    wrapper.appendChild(image);    

    if(lastweek == 1)newmes = 'nový komentář';
    else if((lastweek > 0) && (lastweek < 5))newmes = 'nové komentáře';
    else newmes = 'nových komentářů';
    
    var bottom = document.createElement('div');
    bottom.style.padding = '3px';
    bottom.style.margin = '1px';
    bottom.style.textAlign = 'left';
//    bottom.innerHTML = '<a class=small href="' + ref + '" onclick="return changeLocation(this, true)">detail fotografie (' + lastweek + ' ' + newmes + ' za poslední týden) >></a>';
    bottom.innerHTML = '<span class="arialtextanketa">' + lastweek + ' ' + newmes + ' za poslední týden</span>';
    wrapper.appendChild(bottom); 

    return false;    
  }
  return true;  
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

