
var z = 0; //for setting the initial z-index's
var inAnimation = false; //flag for testing if we are in a animation

$(document).ready(function() { //perform actions when DOM is ready

  $('#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
    img = $(this);

    img.css('z-index', z); //apply increased z-index to <img>

    var ran = randomNumber(-5,10);

    img.css({
        '-moz-transform': "rotate("+ran+"deg)",
        '-o-transform': "rotate("+ran+"deg)",
        '-webkit-transform': "rotate("+ran+"deg)",
        '-ms-transform': "rotate("+ran+"deg)",
        'transform': "rotate("+ran+"deg)"
    });

    if(jQuery.browser.msie){
        var sizeopt={
            w: img.width(),
            h: img.height()
        };

        var rad = (ran * Math.PI) / 180.0;
        cos = Math.cos(rad),
        sin = Math.sin(rad);

        var filter='progid:DXImageTransform.Microsoft.Matrix(sizingMethod="auto expand", M11 = ' + cos + ', M12 = ' + (-sin) + ', M21 = ' + sin + ', M22 = ' + cos + ')';
        img.css({'filter': filter, '-ms-filter': filter, 'zoom': 1});

        //узнаем какие размеры у обьекта сейчас
        var w=img.width();
        var h=img.height();

        //и сместим на дельту разницы
        img.css({'margin-left':-Math.round((w-sizeopt.w)/2),'margin-top':-Math.round((h-sizeopt.h)/2)});
    }

  });

});

  function randomNumber (m,n) { m = parseInt(m); n = parseInt(n); return Math.floor( Math.random() * (n - m + 1) ) + m; }


  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
  }

  timeout_id = window.setInterval("swapFirstLast()", ImageRotateTimeOut);


