$(document).ready(function() { //perform actions when DOM is ready
  var z = 2; //for setting the initial z-index's
  var inAnimation = false; //flag for testing if we are in a animation
  
  $('#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
    // Zufallszahl aus Anzahl der Bilder als z-Index
    //var zufall=z*(Math.random());
    //zufall=Math.round(zufall);
    $(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 = 3; inDeCrease = 1;// } //set variables for "next" action
    //else { processZindex = 3; 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
        objId = this.id;
        objZahl = objId.replace(/nr/g, "");
        //if(vorheriges==""){
          weniger = objZahl-1;
          if(weniger==0){
            weniger=8;
          }
        dieses = objZahl;
        /*o1 = new Parallel();
        o1.addChild(new OpacityTween(document.getElementById('inhalt_konsole2_'+this.id),Tween.strongEaseInOut,100,0,0.5));
        o1.addChild(new OpacityTween(document.getElementById('inhalt_konsole2_'+weniger),Tween.strongEaseInOut,0,100,0.5));
        var horcher = new Object();
        horcher.onMotionStarted= function(){
           document.getElementById('inhalt_konsole2_'+weniger).style.display="block";
        }
        horcher.onMotionFinished= function(){
           document.getElementById('inhalt_konsole2_'+dieses).style.display="none";
        }
        o1.addListener(horcher);
        o1.start(); */
        
        
        window.setTimeout("TextBlende('"+weniger+"','"+dieses+"')", 500);
        
        $(this).animate({ 'left' : 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({ 'left' : '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({ 'left' : '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
        });
        /*weniger = processZindex-1;
        if($(this).css('z-index') == weniger) {
          text=document.getElementById('inhalt_konsole2_'+this.id);
          text.style.display="block";
          o2 = new OpacityTween(text,Tween.strongEaseInOut,0,100,0.5);
          o2.start();
        } */
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#another a').click(function() {
    return swapFirstLast(); //swap first image to last position
  });
}); 


function TextBlende(weniger, dieses){
   document.getElementById('inhalt_konsole2_'+weniger).style.display="block";
   document.getElementById('inhalt_konsole2_'+dieses).style.display="none";
}   