$(document).ready(function() { //perform actions when DOM is ready
  var z = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation
  
  /************Galerie 1 **********************/
  $('#pictures img').each(function() { //set the initial z-index's
    z++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z); //apply increased z-index to <img>
  });

  function swapFirstLast(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next a').click(function() {
    return swapFirstLast(true); //swap first image to last position
  });
  
  $('#prev a').click(function() {
    return swapFirstLast(false); //swap last image to first position
  });
  
  
  /*****************Fin galerie 1*******************/
  
  /************Galerie 2 **********************/
  var z2 = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation  
  $('#pictures2 img').each(function() { //set the initial z-index's
									
    z2++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z2); //apply increased z-index to <img>
  });

  function swapFirstLast2(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z2; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z2; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures2 img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next2 a').click(function() {
    return swapFirstLast2(true); //swap first image to last position
  });
  
  $('#prev2 a').click(function() {
    return swapFirstLast2(false); //swap last image to first position
  });
  
  
  /*****************Fin galerie 2*******************/
    /************Galerie 3 **********************/
  var z3 = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation  
  $('#pictures3 img').each(function() { //set the initial z-index's
									
    z3++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z3); //apply increased z-index to <img>
  });

  function swapFirstLast3(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z3; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z3; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures3 img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next3 a').click(function() {
    return swapFirstLast3(true); //swap first image to last position
  });
  
  $('#prev3 a').click(function() {
    return swapFirstLast3(false); //swap last image to first position
  });
  
  
  /*****************Fin galerie 3*******************/
  
   /************Galerie 4 **********************/
  var z4 = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation  
  $('#pictures4 img').each(function() { //set the initial z-index's
									
    z4++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z4); //apply increased z-index to <img>
  });

  function swapFirstLast4(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z4; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z4; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures4 img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next4 a').click(function() {
    return swapFirstLast4(true); //swap first image to last position
  });
  
  $('#prev4 a').click(function() {
    return swapFirstLast4(false); //swap last image to first position
  });
  
  
  /*****************Fin galerie 4*******************/
     /************Galerie 5 **********************/
  var z5 = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation  
  $('#pictures5 img').each(function() { //set the initial z-index's
									
    z5++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z5); //apply increased z-index to <img>
  });

  function swapFirstLast5(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z5; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z5; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures5 img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next5 a').click(function() {
    return swapFirstLast5(true); //swap first image to last position
  });
  
  $('#prev5 a').click(function() {
    return swapFirstLast5(false); //swap last image to first position
  });
  
  
  /*****************Fin galerie 5*******************/
  
       /************Galerie 6 **********************/
  var z6 = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation  
  $('#pictures6 img').each(function() { //set the initial z-index's
									
    z6++; //at the end we have the highest z-index value stored in the z variable
    $(this).css('z-index', z6); //apply increased z-index to <img>
  });

  function swapFirstLast6(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z6; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z6; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures6 img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next6 a').click(function() {
    return swapFirstLast6(true); //swap first image to last position
  });
  
  $('#prev6 a').click(function() {
    return swapFirstLast6(false); //swap last image to first position
  });
  
  
  /*****************Fin galerie 6*******************/
});


