$(document).ready(function() { //perform actions when DOM is ready
  
	function getMaxZimgAlt(zelement) {
		mz = 29;
		$(zelement).each(function() {
			tz = $(this).css('z-index');
			if(tz==mz) {
				me = $(this).attr('alt');
			}
		});
		return me;
	}
	
	var z = 0; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation
  var imgLoaded = 0; //for checking if all images are loaded
	
  $('#gallery').append('<div id="loader">Bitte kurz warten. Die Bilder werden geladen.</div>'); //append the loader div, it overlaps all pictures
  
	$('#pictures').append('<img id="img29" src="gallery-images/picture29.png" alt="28. Platz: Romy B., 6 Jahre" /><img id="img28" src="gallery-images/picture28.png" alt="27. Platz: Christina R., 3 Jahre" /><img id="img27" src="gallery-images/picture27.png" alt="26. Platz: Leni Sophie F., 4 Jahre" /><img id="img26" src="gallery-images/picture26.png" alt="25. Platz: Alexander M., 14 Jahre" /><img id="img25" src="gallery-images/picture25.png" alt="24. Platz: Kadjsna S., 12 Jahre" /><img id="img24" src="gallery-images/picture24.png" alt="23. Platz: Julia S., 13 Jahre" /><img id="img23" src="gallery-images/picture23.png" alt="22. Platz: Andreas M., 15 Jahre" /><img id="img22" src="gallery-images/picture22.png" alt="21. Platz: Katharina Sideridou M., 9 Jahre" /><img id="img21" src="gallery-images/picture21.png" alt="20. Platz: Matthias R., 13 Jahre" /><img id="img20" src="gallery-images/picture20.png" alt="19. Platz: Katharina E., 11 Jahre" /><img id="img19" src="gallery-images/picture19.png" alt="18. Platz: Vanessa T., 14 Jahre" /><img id="img18" src="gallery-images/picture18.png" alt="17. Platz: Anna W., 12 Jahre" /><img id="img17" src="gallery-images/picture17.png" alt="16. Platz: Luca K., 5 Jahre" /><img id="img16" src="gallery-images/picture16.png" alt="15. Platz: Tim O., 5 Jahre" /><img id="img15" src="gallery-images/picture15.png" alt="14. Platz: Adrian R., 5 Jahre" /><img id="img14" src="gallery-images/picture14.png" alt="13. Platz: Tim B., 5 Jahre" /><img id="img13" src="gallery-images/picture13.png" alt="12. Platz: Tobias W., 5 Jahre" /><img id="img12" src="gallery-images/picture12.png" alt="11. Platz: Milena G., 5 Jahre" /><img id="img11" src="gallery-images/picture11.png" alt="10. Platz: Julia K., 5 Jahre" /><img id="img10" src="gallery-images/picture10.png" alt="9. Platz: Lisann B., 5 Jahre" /><img id="img9" src="gallery-images/picture9.png" alt="8. Platz: Laura Marie B., 6 Jahre" /><img id="img8" src="gallery-images/picture8.png" alt="7. Platz: Xenia Lara B., 5 Jahre" /><img id="img7" src="gallery-images/picture7.png" alt="6. Platz: Gerrit R., 13 Jahre" /><img id="img6" src="gallery-images/picture6.png" alt="5. Platz: Laurenz B., 12 Jahre" /><img id="img5" src="gallery-images/picture5.png" alt="4. Platz: Kasimir W., 5 Jahre" /><img id="img4" src="gallery-images/picture4.png" alt="4. Platz: Gregor W., 8 Jahre" /><img id="img3" src="gallery-images/picture3.png" alt="3. Platz: Maximilian H., 13 Jahre" /><img id="img2" src="gallery-images/picture2.png" alt="2. Platz: Jessica G., 12 Jahre" /> <img id="img1" src="gallery-images/picture1.png" alt="1. Platz: Alina O., 7 Jahre" />');
	
	// $('#pictures').fadeOut('slow');
  $('#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>
    
    $(new Image()).attr('src', $(this).attr('src')).load(function() { //create new image object and have a callback when it's loaded
      imgLoaded++; //one more image is loaded
      //alert(imgLoaded);
      if(imgLoaded==29) { //do we have all pictures loaded?
				// $('#pictures').fadeOut('slow');
				$('#loader').fadeOut('slow'); //if so fade out the loader div
				$('#loader').css('display','none');
				$('#imgdesc').empty();
				$('#imgdesc').append('1. Platz: Alina O., 7 Jahre');
      }
    });

  });

  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) { 
			//set variables for "next" action
			processZindex = z; 
			direction = '-'; 
			newZindex = 1; 
			inDeCrease = 1; 
		} else { 
			//set variables for "previous" action
			processZindex = 1; 
			direction = ''; 
			newZindex = z; 
			inDeCrease = -1; 
		} 

		$('#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
            });
					if(direction == '') {
						$('#imgdesc').empty();
						$('#imgdesc').append($(this).attr('alt'));
					} else {
						$('#imgdesc').empty();
						if($(this).attr('alt')=='28. Platz: Romy B., 6 Jahre') {
							$('#imgdesc').append('1. Platz: Alina O., 7 Jahre');
						} else {
							$('#imgdesc').append(getMaxZimgAlt('#pictures img'));
						}
					}
        });
      } 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
  }
  
  $('#nexta a').click(function() {
		swapFirstLast(true); 
		return false; //don't follow the clicked link
  });
  
  $('#prev a').click(function() {
    swapFirstLast(false); 
		return false;  //don't follow the clicked link
  });
});
