/***
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {  
    var $active = $('#headerimages IMG.active');

    if ( $active.length == 0 ) $active = $('#headerimages IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#headerimages IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
            ind = $('#headerimages IMG.active').index()+1;
        });    
}


jQuery(document).ready(function($) {

/* ------------------------- */
/* --- Slideshow - Fotos --- */
/* ------------------------- */

  // keine slideshow am iPhone
  var u = navigator.userAgent.toLowerCase();
  var iphone = ((u.indexOf('iphone'))!=-1);
  if (!iphone) {
  
    img_src = img_src.split(',');
    img_src.shift();
    
    var img_length = img_src.length - 1; // weil der string mit einem , aufhört
    for (var i = 0; i < img_length; ++i) {
      $('#slidshowControls').css({'visibility':'visible'});
	  img_alt = img_src[i].split('|');
	  //$('#headerimages').append('<img src="'+ img_alt[0] +'" alt="'+ img_alt[1] +'" title="'+ img_alt[1] +'" />');
	  $('#headerimages').append('<img src="'+ img_alt[0] +'" alt="" />');
    }
    
	/* ------------------- */
    /* --- bgmaximizer --- */
    /* ------------------- */

    //resize();

    
    if($('#headerimages').children().length > 1) {
      $(function() {
          slideInterval = setInterval( "slideSwitch()", 5000 );
      });
    }
    
    $('#slidshowNext').click(function(){
      var ind = $('#headerimages IMG.active').index() + 2;
      if(ind > $('#headerimages IMG').length) {
        ind = 1;
      }
      clearInterval(slideInterval);
      var $active = $('#headerimages IMG.active');
      $active.addClass('last-active');
      var $next = $('#headerimages IMG:nth-child(' + ind + ')');
      $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    });
    
    $('#slidshowPrev').click(function(){
      var ind = $('#headerimages IMG.active').index();
      if(ind < 1) {
        ind = $('#headerimages IMG').length
      }
      clearInterval(slideInterval);
      var $active = $('#headerimages IMG.active');
      $active.addClass('last-active');
      var $next = $('#headerimages IMG:nth-child(' + ind + ')');
      $next.css({opacity: 0.0})
        .addClass('active')
  
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    });
  }

});
