var curazb=0,pn='',opn,pnt=false;

function nextAzb() {
  var nazb=$('<img>').attr('src','/static/azb/'+azbs[(++curazb>=azbs.length?curazb=0:curazb)]).hide();
  $('.azb').append(nazb.load(function(){
    $(this).fadeIn(1700,function(){
      $(this).prev().remove();
    });
  }));
}

function setPnr(pn) {
  $('#pageNr').html(pn+(new Array(4-pn.length).join('-')));
}

function valign() {
  var h = ($(document).height() - $('.main').height() )/2;
  $('.main').removeClass('acenter').css({top:(h>0?h:0)+'px'});
}

function zoom() {
  var css,
    gal=($('.gallerypics').length>0),
    $this=$(this),
    src=$(this).attr('src'),
    zimg=$('<img>').css({visibility:"hidden",position:'absolute'}).attr('src',src.replace(/thumb/,"full"));

  function reverse(){
    zimg.animate(css,800,function(){
      $this.css('visibility','visible');
      $(this).fadeOut(500,function(){$(this).remove();});
    });
    $('.content .pad').fadeIn(500);
    $('.overlay').unbind().fadeOut(600);
  }

  zimg.load(function(){
    var w=$(this).width(), h=$(this).height();
    $this.css('visibility','hidden');
    css={
      width:$this.width()+'px',
      height:$this.height()+'px',
      top:$this.offset().top+'px',
      left:$this.offset().left+'px',
      visibility:'visible'
    };
    $(this).css({position:'absolute'}).css(css).click(reverse).animate({
      top:($('.main').offset().top+(gal?100:215))+'px',
      left:($('.main').offset().left+((990-w)/2))+'px',
      width:w+'px',
      height:h+'px'
    },1300,function(){
      $('.overlay').click(reverse);
    });
    $('.overlay').fadeTo(1000,(gal?.75:.1));
    $('.content .pad').fadeOut(1100);
  });
  $('body').append(zimg);
}

function page(nr) {
  $('.vcenter>div:visible').fadeOut(300,function(){
    $('.page'+nr).fadeIn(500);
    navi();
    setPnr($('#pageNr').text().substr(0,1)+"0"+nr);
  });
}

var zto=false;
function navi(loc,t) {
  if (zto) clearInterval(zto);
  if (!loc) return;
  zto=setInterval('$("#pageNr").text($("#pageNr").text()<999?$("#pageNr").text()*1+1:100);',7);
  setTimeout('location.href="'+loc+'";',t||300);
}

$(document).ready(function(){
  $(window).resize(valign).resize(function(){
    $(".overlay").css({
      position:"fixed",
      top:0,
      left:0,
      background:"black",
      width:window.innerWidth+"px",
      height:window.innerHeight+"px"
    });
  }).resize();

  if (typeof azbs!=='undefined') setInterval(nextAzb,4000);

  $('.live .ort,.live .datum').toggle(function(){
    $(this).parent().find('.detail').fadeIn(1000);
  },function(){
    $(this).parent().find('.detail').fadeOut(500);
  }).css('cursor','pointer').first().click();

  $('.gallery_index a').mouseenter(function(){
    $(this).children('img').stop().fadeTo(300,1);
  }).mouseleave(function(){
    $(this).children('img').stop().fadeTo(600,0.85);
  }).children('img').fadeTo(1,0.85);

  $('a').click(function(){
    if ($(this).attr('target')) return;
    navi($(this).attr('href'));
    return false;
  });

  $('.shop img').click(zoom);
  $(".gallerypics img").click(zoom);

  opn=$('#pageNr').text();
  $(document).keyup(function(ev){
    if (ev.target.nodeName!="BODY"&&ev.target.nodeName!="HTML") return;
    var k=ev.keyCode;
    if (k==36) return navi("/home");
    if (ev.currentTarget.activeElement.contentEditable=='true') return;
    if (k>95 && k<106) n=k-96; else if (k>47 && k<58) n=k-48; else return;
    if (pn.length<3) pn+=n.toString();
    setPnr(pn);
    if (pnt) clearTimeout(pnt);
    if (pn.length==3) { $.getScript('/page/'+pn); pn=""; } else pnt=setTimeout('pn="";setPnr(opn);pnt=false;',2000);
  });
});

