function goto(id_new) {
  id_old=cuid;
  cuid=id_new;
  location.hash="#"+id_new;
  $('#btn_next').attr("href", "https://w4eg.de/"+(cuid+1))
  $('#btn_prev').attr("href", "https://w4eg.de/"+(cuid-1))
  if (cuid==1)
    $('#btn_prev').fadeOut();
  else
    $('#btn_prev').fadeIn();
  if (cuid==latest)
    $('#btn_next').fadeOut();
  else 
    $('#btn_next').fadeIn();
  $("#container").append("<div id='box_"+id_new+"' class='box' style='display:none;'>...</div>");
  $('#box_'+id_new).load("/?ajax=yes&id="+id_new,function(){
    $('#box_'+id_old).fadeOut("fast",function(){
      $('#box_'+(id_old)).remove();
      $('#box_'+id_new).fadeIn("fast");
      document.title = 'w4eg.de | '+$('#box_'+id_new+" h2").html() ;
    });
  });
}

$(document).ready(function(){
  if(location.hash) {
    var str=location.hash;
    hashid =str.replace("#","")
    if (!(hashid==cuid)) {
      $('#box_'+cuid).remove();
      $("#container").append("<div id='box_"+cuid+"'></div>"); // ugly dummy hack
      goto(hashid);
    }
    else
      $('#btn_next').hide();
      document.title = 'w4eg.de | '+$('#box_'+cuid+" h2").html();
  }
  else {
    location.hash=cuid;
    document.title = 'w4eg.de | '+$('#box_'+cuid+" h2").html() ;
    $('#btn_next').hide();
  }
  $("#btn_prev").click(function(event){
    goto(cuid-1);
    event.preventDefault();
  });
  $("#btn_next").click(function(event){
    goto(cuid-1+2);
    event.preventDefault();
  });
});

$(document).keypress(function(event) {
  if (event.keyCode == 27) {
      // back();
    }
});
