function initGuestbook(start)
{
   start=0;
   buildNaviGB(start,false);
   loadContentGB(start);
   
}
function buildNaviGB(start,end)
{
   $('nLbl').update(end-start);
   $('bLbl').update(start);
   start=parseInt(start);
   end=parseInt(end);
   var back=start-10;
   if(start+10>=end){
     start=start-10;
   }
   if(back<0){back=0;}
   $('naviB').value=back;
   $('naviN').value=start+10;
   $('naviB').removeAttribute('href');
   $('naviN').removeAttribute('href');
   $('naviB').style.cursor="pointer";
   $('naviN').style.cursor="pointer";
   $('naviB').onclick = function(){loadContentGB(this.value);};
   $('naviN').onclick = function(){loadContentGB(this.value);};

}
function loadContentGB(start)
{
    opt='start='+start;
    setRequestGb(opt,buildContentGB);

}
function buildContentGB(res)
{
  $('gbContent').update('');
  var data=res.responseJSON;
  var i=0;
  var first = true;
  data.each(function(e){
  if(i==0)
  {
    buildNaviGB(e[0],e[1]); 
  }else{
    for(x=0;x<=e[1].length;x++)
    {      
      el=new Element('div',{id:'entry'+e[x][0]});
      el.className='gbEntry';
      el.style.cursor="pointer";
      el.onclick = function(){
        $('cont'+this.id.substring(5)).toggle();
      };
      spanD=new Element('span').update(e[x][2]);
      spanD.className='gbTime';
      var l=17;
      var title=e[x][3];
      if(title.length>l){
        title=title.substring(0,l)+"...";
      }
      title=html_entity_decodeGB(title);
      //h4T=new Element('h4').insert(title);
      spanT=new Element('span').update(title);
      spanT.className='gbTitel';
      var l=20;
      var autor=e[x][4];
      if(autor.length>l){
        autor=autor.substring(0,l)+"...";
      }
      autor=html_entity_decodeGB(autor);
      spanA=new Element('span').update(autor);
      spanA.className='gbAutor';
      el.insert(spanD);
      el.insert(spanT);
      el.insert(spanA);
      $('gbContent').insert(el);         
      el=new Element('div',{id:'cont'+e[x][0]});
      h4A=new Element('h4').update('Autor:&nbsp;&nbsp;&nbsp;'+html_entity_decodeGB(e[x][4]).substring(0,50));
      h4T=new Element('h4').update('Titel:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+html_entity_decodeGB(e[x][3]).substring(0,50));
      el.insert(h4A);
      el.insert(h4T);
      el.insert(new Element('br'));
      el.insert(html_entity_decodeGB(e[x][8]));     
      el.className='gbInhalt';
      if(first==false)
      {
         el.toggle();
      }else{
        first=false;
      }
      div=new Element('div');
      p=new Element('p').update(e[x][5]);
      
      p.className='gbMail';
      div.insert(p);
      if(e[x][6]!='')
      {
        var link=html_entity_decodeGB(e[x][6]);
        a=new Element('a').insert(link);
        a.href=link;
        if(link.substring(0,4)!='http')
        {
          a.href='http://'+link;
        }        
        div.insert(a);
      }
      el.insert(div);
      $('gbContent').insert(el);

           
    }
  }
  
  i++;
  });
  
}
//####################################################################
//####################################################################
//####################################################################
function setRequestGb(opt,handle,handleLoad)
{
  new Ajax.Request('php/guestbook/guestbook_request.php',{
     methode:'post',
     postBody:opt,
     onSuccess:handle,
     onCreate:handleLoad
  });
}
function html_entity_decodeGB(str) {
  str= str.replace(/&amp;/g, "&");
  str= str.replace(/&lt;/g, "<");
  str= str.replace(/&gt;/g, ">");
  str= str.replace(/\r\n/g, "<br/>");
  str= str.replace(/\r/g, "<br/>");
  //str= str.replace(/\\/g, "");
  return str;

}
