jQuery.fn.delayme = function(time, callback)
{
    return this.animate({ opacity: '+=0' }, time, callback);
}

jQuery.fn.initial = function (pixeltomove) {
    var pixelsmoved = 0;
    var pixeltomove = pixeltomove;
   // jQuery(this).switchIDs();
    jQuery(this).children().each(function(){
    var pixeltomoveinside = pixeltomove;
   // alert(pixeltomoveinside);
   // var pixeltomove = pixeltomove;
      //pixelsmoved = jQuery(this).prev().css('left');
      jQuery(this).animate({left:'+='+pixelsmoved}, 1000);
      pixelsmoved = pixelsmoved + pixeltomove;
      return true;
      });
    }
    
jQuery.fn.slide = function(){

    var IDArray = this.switchIDs();
    var counter = 0;
    var devisor = IDArray.length;
    var pixeltomove = ((980-593)/ ((devisor)-1));
    var initialize = 0;
      if (initialize == 0 ){
      jQuery(this).initial(pixeltomove);
      initialize = 2;
      } 
    var slidesTimer = null;
    var iterations = 0;
    var i=0;
    var slideLast = IDArray[IDArray.length-1];
    var slideNext = slideLast;
    var positionin = devisor -1;
    jQuery(this).shuffleslides('slide-'+ positionin, positionin, pixeltomove, 0, slidesTimer);

}

jQuery.fn.gorightShuff = function(numr){
  return this.animate({left:'+='+numr }, 1000, function () { jQuery(this).parent().switchIDs(); });
  
}

jQuery.fn.goright = function(numr){
  return this.animate({left:'+='+numr }, 1000);
  
}

jQuery.fn.goleft = function(numl){
  return this.animate({left:'-='+numl}, 1000);
  
}

jQuery.fn.switchIDs = function (){
  var IDS =new Array();
  jQuery(this).children().each(function(index){
    var opacityChange = (100 - (index * 15))/100;
    var numPos = index;
      jQuery(this).attr('id', '').attr('id', 'slide-' + index).css('z-index', 100 - (index*2)).attr('class', 'slide-' + index).children('img').css('opacity', opacityChange).next().css('opacity', function(numPos) {
      //alert(index);
      var myOpaVa = 0;
      if (index == 0){myOpaVa = 1;}
      return myOpaVa;
      });
     IDS[index]='slide-' + index;
    });
    return IDS;
}


jQuery.fn.shuffleslides = function (slideID, position, pixelstoMove, initialSlide, timer){
    var pixeltomove = pixelstoMove;
    jQuery.fn.bindClickSlides = function(index){  
       if (index !== 0 ){
          jQuery(this).bind('click', function(event) {
              slidesTimer = window.clearInterval(slidesTimer); 
             // alert(timer); 
              clearInterval(timer); 
               jQuery(this).delayme(1, myClickUnBind);
              var thisIDName = jQuery(this).attr("id");
              var thisID = thisIDName.substring(thisIDName.length-1, thisIDName.length);
              jQuery(this).shuffleslides('slide-'+ thisID, thisID, pixeltomove, 5);
            });     
        }
      }
      
      
      jQuery.fn.unBindClickSlides = function(index){  
           jQuery(this).unbind();
      }      

      function myClickBind(){
     // alert('in click cuntion');
         jQuery(this).parent().children().each(function(index){
    jQuery(this).bindClickSlides(index);
            });
        }
      function shuffling() {
    jQuery(this).shuffleslides('slide-'+ position, position, pixelstoMove, 5, slidesTimer);  
                    //alert('in initial loop');
      }
      function myClickUnBind(){
    //  alert('in un-click cuntion');
         jQuery(this).parent().children().each(function(index){
    jQuery(this).unBindClickSlides(index);
            });
        }
        if(initialSlide == 0) {
        jQuery(this).children('#slide-'+ position).delayme(1, myClickBind);
    var slidesTimer = window.setInterval( shuffling,6000);
                       }
                       else {
    var moveleftnum = ((position * pixelstoMove) + 600);
    
    jQuery('#' + slideID).delayme(1, myClickUnBind).gorightShuff(600).delayme(100).goleft(moveleftnum).delayme(1, myClickBind).parent().prepend( jQuery('#' + slideID) );
    var iterations = position - 1;
    var i=0;
    while (i<=iterations)
      {
        jQuery('#slide-' + i).goright(pixelstoMove);
        i++;
      }
      }
      return;
}

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


$('#homeCaro').slide();

});
