$(document).ready(function() {

 // external pages
  $('a[rel=external]').attr("target", "_blank");
  
  /* popups erweiterung */

   $('div.gallery_bigpic img').click(function() {
	  $(this).prev('div.popup').toggle();
   });

   $('div.popup').click(function() {
	  $('div.popup').hide();
   });


  // nav hover
  $('#mainnav img.nav').each( function(i) {

    //hide all but first
    var src = $(this).attr('src');
    if (! src.match(/_active\.png/)) {
      var hrc = src.replace(/\.png/g, "_active.png");
      $.preloadImages(hrc);
      $(this).hover(
        function () {
          //alert(hrc);
          $(this).attr('src', hrc);
        }, 
        function () {
          $(this).attr('src', src);
        }
      );
    }

  });


  // manage multipage
  $('div.page').each( function(i) { 

    // hide all but first
    if (i != 0 ) {
      $(this).hide(); 
    }

  });

  // show "zurück/weiter" buttons
  if ($('div.page').length > 1) {
    $('#pagination').show();
    $('#bt-fwd').click( function() { page(1); } );
  }

  page(0);


  // make small pics clickable
  var bigpics = $('#headerpics img:not(.first),#headerpics2 img:not(.first),#headerpics3 img:not(.first)');
  bigpics.each( function(i) {
    $(this).css('cursor','pointer');
    $(this).click( function() { 
      var small = $(this).attr('src');
      var big = small.replace(/\.jpg/, '-big.jpg');
      //alert(i+' '+big); 
      $('#bigpic img').attr('src', big);
      $('#header, #main, #galerie').hide();
      $('#pictures').show();
      $('#bigpic_next').click( function() { bigpic( i+1 >= bigpics.length ? 0 : i+1 ) } );
      $('#bigpic_prev').click( function() { bigpic( i==0 ? bigpics.length : i-1 ) } );
      $('#navigate img').css('cursor','pointer');
    });
  });

  // close
  $('#closebutton').css('cursor','pointer');
  $('#closebutton').click(function() {
    $('#pictures').hide();  
    $('#bigpic img').attr('src', '/images/rotor.gif');
    $('#header, #main, #galerie').show();
  });

  // global gallery
  //$('#galerie a').click( function() {
  //  $('#header, #main, #galerie').hide();
  //  $('#pictures').show();
  //  $('#navigate img').css('cursor','pointer');
  //  gallery(0); 
  //});

  // handle toggle on /aktuelles.html
  $('#listblock dl').hide();
  $('#listblock h3').css("cursor","pointer").click(function() {$(this).next('dl').toggle();} );

});

jQuery.preloadImages = function() {
  for (var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


// function gallery (i) {
//   alert("X:" + i);
//   var small = global_gallery[i];
//   var big = small.replace(/\.jpg/, '-big.jpg');
//   $('#bigpic img').attr('src', big);
//   $('#bt-fwd').unbind( "click" ).click( function() { gallery( i+1 >= global_gallery.length - 1 ? 0 : i+1 ) } );
//   $('#bt-back').unbind( "click" ).click( function() { gallery( i==0 ? global_gallery.length - 1 : i-1 ) } );
// }

function bigpic (i) {
  //alert(i);
  var bigpics = $('#headerpics img:not(.first),#headerpics2 img:not(.first),#headerpics3 img:not(.first)');
  var c = bigpics.get(i);
  var small = $(c).attr('src');
  var big = small.replace(/\.jpg/, '-big.jpg');
  $('#bigpic img').attr('src', big);
  $('#bigpic_next').unbind( "click" ).click( function() { bigpic( i+1 >= bigpics.length ? 0 : i+1 ) } );
  $('#bigpic_prev').unbind( "click" ).click( function() { bigpic( i==0 ? bigpics.length : i-1 ) } );
  //alert($(c).attr('src'));
}


function page (p) {
  //alert(p);
  $('div.page').each( function(i) { 
    if (i == p ) { $(this).show(); } else { $(this).hide();  }
  });

  //var x = $('div.page').get(0);
  //if (p==0) {alert('0'); }
  //if ($($('div.page').get(0)).hasClass('first')) { alert('has'); }

  // startpage or not
  if (p==0 && $($('div.page').get(p)).hasClass('first')) {
    //alert("case A");
    $('#headerpics,#headerpics2,#headerpics3').hide();
    $('#startbild').show();
  } else if ($($('div.page').get(p)).hasClass('header2') && $('#headerpics2').size() > 0) {
    //alert("case C");
    $('#startbild,#headerpics,#headerpics3').hide();
    $('#headerpics2').show();
  } else if ($($('div.page').get(p)).hasClass('header3') && $('#headerpics3').size() > 0) {
    //alert("case C");
    $('#startbild,#headerpics,#headerpics2').hide();
    $('#headerpics3').show();
  } else {
    //alert("case B");
    $('#startbild,#headerpics2,#headerpics3').hide();
    $('#headerpics').show();
  }

  var cls = $('#pagination').attr('class');
  
  // unbind
  $('#bt-fwd').unbind( "click" );
  $('#bt-back').unbind( "click" );

  // ie hacks
  //$('#imprint').css('bottom', '1px');
  //$('#imprint').css('bottom', '0px');

  // set forward
  if (p < $('div.page').length - 1) {
    $('#bt-fwd').click( function() { page(p+1); } );
    $('#bt-fwd').attr('src', '/images/button-vor-' + cls + '.gif');
  } else {
    $('#bt-fwd').attr('src', '/images/button-vor_out.png');
  }

  // set back
  if (p > 0) {
    $('#bt-back').click( function() { page(p-1); } );
    $('#bt-back').attr('src', '/images/button-zurueck-' + cls + '.gif');
  } else {
    $('#bt-back').attr('src', '/images/button-zurueck_out.png');
  }

}



