var xmlHttpComment;
var elementX;
var elementY;
var commentTimer;
var commentType;
var loading = false;
var cancel;

function hideBar(message)
{
  if(loading == true)
  {
    cancel = true;
  } 
	if(commentTimer)
  {
    clearTimeout(commentTimer);
    loading = false;
  }
  var bar = document.getElementById('popupbar');
  if(bar)bar.parentNode.removeChild(bar);
  return false;
}

function startTimer(obj, id, type)
{
  var pos = findPos(obj);
  if(loading == false)
  {    
    elementX = parseInt(pos[0]) + 20;
    elementY = parseInt(pos[1]) + 12;
    commentTimer = setTimeout('getComment(\''+ type +'\', '+ id +')', 250);
  }

  return false;
}

function vytvorXMLHttpRequestComment() {
  if (window.ActiveXObject) {
    xmlHttpComment = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpComment = new XMLHttpRequest();
  }
}

function getComment(type, id)
{	 
  vytvorXMLHttpRequestComment();
  xmlHttpComment.onreadystatechange = zpracujZmenuStavuComment;
  var url = 'include/getcommentdata.php?type='+ type +'&id='+ id;
  commentType = type;
  xmlHttpComment.open("GET", url, true);
  xmlHttpComment.send(null);
  loading = true;
  cancel = false;
  return true;
}

function zpracujZmenuStavuComment() {
  if(xmlHttpComment.readyState == 4) 
  {
    if((xmlHttpComment.status == 200) && (cancel == false)) 
    {
      if(commentType == 'photo')showPhotoCommentBar()
      else showCommentBar();
      loading = false;
      cancel = false;      
    }
  }
}

function showCommentBar()
{
  if(document.getElementsByTagName)
  {
    var vysledek = xmlHttpComment.responseXML;
    var id = vysledek.getElementsByTagName("id")[0].firstChild.nodeValue;
    var type = vysledek.getElementsByTagName("type")[0].firstChild.nodeValue;
    var nick = vysledek.getElementsByTagName("nick")[0].firstChild.nodeValue;
    var caption = vysledek.getElementsByTagName("caption")[0].firstChild.nodeValue;
    var date = vysledek.getElementsByTagName("date")[0].firstChild.nodeValue;
    var comment = vysledek.getElementsByTagName("comment")[0].firstChild.nodeValue;
    var lastweek = vysledek.getElementsByTagName("lastweek")[0].firstChild.nodeValue;

    hideBar();
    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 = elementX;   
    wrapper.style.top = elementY;
    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 = nick + ', ' + date;
    header.appendChild(left);    

    var left = document.createElement('div');
    left.className = 'arialtextanketabold';
    //left.style.float = 'left';
    left.style.paddingTop = '2px';
    left.innerHTML = '<img border=0 align=absmiddle src="images/sectionicons/' + type + '.gif">&nbsp;&nbsp;' + 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">' + comment + '</span>';    
    wrapper.appendChild(content);    

    var bottom = document.createElement('div');
    bottom.style.padding = '3px';
    bottom.style.margin = '1px';
    bottom.style.textAlign = 'left';
    
    if(type == 'guestbook')mes = 'vzkazy';
    else mes = 'komentáře';
    
    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 = '<span class="arialtextanketa">' + lastweek + ' ' + newmes + ' za poslední týden</span>';
    wrapper.appendChild(bottom);    

    return false;    
  }
  return true;  
}

function showPhotoCommentBar()
{
  if(document.getElementsByTagName)
  {
    var vysledek = xmlHttpComment.responseXML;
    var id = vysledek.getElementsByTagName("id")[0].firstChild.nodeValue;
    var nick = vysledek.getElementsByTagName("nick")[0].firstChild.nodeValue;
    var caption = vysledek.getElementsByTagName("caption")[0].firstChild.nodeValue;
    var date = vysledek.getElementsByTagName("date")[0].firstChild.nodeValue;
    var text = vysledek.getElementsByTagName("comment")[0].firstChild.nodeValue;
    var lastweek = vysledek.getElementsByTagName("lastweek")[0].firstChild.nodeValue;
    var src = vysledek.getElementsByTagName("src")[0].firstChild.nodeValue;
    var width = vysledek.getElementsByTagName("width")[0].firstChild.nodeValue;
    var height = vysledek.getElementsByTagName("height")[0].firstChild.nodeValue;
    var type = 'photo';

    hideBar();
    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 = elementX;   
    wrapper.style.top = elementY;
    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">' + nick + ', ' + 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 + '" width="'+ width +'" height="'+ height +'">';
    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 = '<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];
}

/*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;
  }
}*/

