var imgA = [{name:"images/home/ferrari 246 red.jpg", img:null}, 
            {name:"images/home/ferrari 246 white.jpg", img:null}, 
            {name:"images/home/ferrari 246 wing.jpg", img:null}, 
            {name:"images/home/ferrari 246 rear quarter.jpg", img:null}, 
            {name:"images/home/dino 206GT Prototipo.jpg", img:null}, 
            {name:"images/home/ferrari 246 seats.jpg", img:null}, 
            {name:"images/home/ferrari 246gt front.jpg", img:null}, 
            {name:"images/home/dino berlinetta speciale top.jpg", img:null}];

var imgA2 = [{name:"images/home/2000 spider front.jpg", img:null}, 
            {name:"images/home/2000 spider nose.jpg", img:null}, 
            {name:"images/home/2000 spider interior.jpg", img:null}, 
            {name:"images/home/2000 spider dash.jpg", img:null},
            {name:"images/home/2000 spider rear.jpg", img:null},
            {name:"images/home/2400 spider front.jpg", img:null},
            {name:"images/home/2000 spider white rear.jpg", img:null},
            {name:"images/home/2000 engine.jpg", img:null}, 
            {name:"images/home/2000 spider front side.jpg", img:null}
            ];
var imgA3 = [{name:"images/home/2000 coupe side.jpg", img:null}, 
            {name:"images/home/2000 coupe vineyard.jpg", img:null}, 
            {name:"images/home/2000 engine.jpg", img:null}, 
            {name:"images/home/2000 coupe interior.jpg", img:null}, 
            {name:"images/home/2000 coupe switches.jpg", img:null}, 
            {name:"images/home/2400 coupe side front.jpg", img:null}, 
            {name:"images/home/2000 coupe dash.jpg", img:null}, 
            {name:"images/home/2400 coupe green side.jpg", img:null}];

var imgA4 = [{name:"images/home/lancia stratos red.jpg", img:null}, 
            {name:"images/home/lancia stratos side window.jpg", img:null}, 
            {name:"images/home/lancia stratos rally.jpg", img:null}, 
            {name:"images/home/lancia stratos stradale blu.jpg", img:null}, 
            {name:"images/home/lancia stratos engine.jpg", img:null}, 
            {name:"images/home/lancia stratos rear window slats.jpg", img:null}, 
            {name:"images/home/lancia stratos turbo.jpg", img:null}, 
            {name:"images/home/lancia stratos zero.jpg", img:null}];

var imgMove = function(opts) {
  this.opts = opts || {};
  
  this.curImg = 0;
  
  this.loadNextImage();
  this.animateImg(this.opts.aMove);
  
};
imgMove.prototype = {
  loadNextImage: function() {
    var img;
    
    this.curImg += 1;
    if (this.curImg >= this.opts.imgA.length) {
      this.curImg = 0;
    }
    if (this.opts.imgA[this.curImg].img === null) {
      img = new Image();
      img.src = this.opts.imgA[this.curImg].name;
      this.opts.imgA[this.curImg].img = img;
    }
    
  },
  
  resetPos: function() {
    var moves = [ {x:-100, y:-150, xTo:0, yTo:-50},
                  {x:0, y:-150, xTo:-100, yTo:-50},
                  {x:0, y:-50, xTo:-100, yTo:-150},
                  {x:-100, y:-50, xTo:0, yTo:-150}],
        idx = Math.floor(Math.random()*4);
  
    $(this.opts.selWrap).css({left:moves[idx].x, top:moves[idx].y});
    return moves[idx];
  },
  
  animateImg: function(pos) {
    var self = this;
    
    $(this.opts.selWrap).animate({left:pos.xTo,top:pos.yTo}, 6000, "linear", function() {
                                                                $(this).fadeOut(500,  function() {
                                                                                        $(self.opts.selImg).attr({src: self.opts.imgA[self.curImg].img.src});
                                                                                        var pos = self.resetPos();
                                                                                        self.loadNextImage();
                                                                                        self.fadeImgIn();
                                                                                        self.animateImg(pos);
                                                                                      });
                                                              });
  },
  
  fadeImgIn: function() {
    $(this.opts.selWrap).fadeIn(500);
  }
};




$(function() {
  
  new imgMove({ selWrap:" #imgHomeWrap",
            selImg: "#imgHome", 
            imgA: imgA, 
            aMove: {x:-100, y:-75, xTo:0, yTo:0}});
            
  new imgMove({ selWrap:" #imgHomeWrap2",
            selImg: "#imgHome2", 
            imgA: imgA2, 
            aMove: {x:0, y:-75, xTo:-100, yTo:0}});
            
  new imgMove({ selWrap:" #imgHomeWrap3",
            selImg: "#imgHome3", 
            imgA: imgA3, 
            aMove: {x:0, y:-75, xTo:-100, yTo:0}});
            
  new imgMove({ selWrap:" #imgHomeWrap4",
            selImg: "#imgHome4", 
            imgA: imgA4, 
            aMove: {x:0, y:-75, xTo:-100, yTo:0}});
  
});
