// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
Effect.Swap = Class.create();
Object.extend(Object.extend(Effect.Swap.prototype, Effect.Base.prototype), {
  initialize: function(element, newImage, newAlt, doX, doY) {
    this.element      = $(element);
	
    this.newImage = newImage;
    this.newAlt = newAlt;
    this.revert = false;
    this.doX = doX;
    this.doY = doY;
    this.start(arguments[5]);
  },
  
  setup: function() {
    if (this.revert == false) {
      this.width = this.element.width;
      this.height = this.element.height;
    }
  },

  update: function(position) {
    var w;
    var h;
    if (this.revert) {
      w = this.width * position;
      h = this.height * position;
    }
    else {
      w = this.width * (1 - position);
      h = this.height * (1 - position);
    }
    
    if (this.doX) {
      this.element.style.marginLeft =  Math.floor((this.width - w)/2) + "px";
      this.element.style.marginRight = 6 + Math.floor((this.width - w)/2) + "px";
      var delta = this.width + 6 - ( Math.floor(w)+ parseFloat(this.element.style.marginLeft) + parseFloat(this.element.style.marginRight))
      this.element.width = Math.floor(w) + delta;
    }
      
    if (this.doY) {
      this.element.style.marginTop = (this.height - h)/2 + "px";
      this.element.height = h;
    }
  },
  
  finish: function(position) {
    if (this.revert == false) {
      this.revert = true;
      this.element.src = this.newImage;
      this.element.alt = this.newAlt;
      this.element.title = this.newAlt;
      this.start(this.options)
    } else
      this.revert = false
  }
});


grid = [[1, 2, 3], [4, 5, 6], [7, 8, 0]];
emptyX = 2;
emptyY = 2;
lastDX = 0;
lastDY = 0;
lastIndex = 0;
missingSrc = "/images/not.jpg"
missingAlt = "Product"
function randomMove() {
	var thumb = $('thumb1') || null;
	if (thumb == null) {
	  startTimeOut();
	  return;
	}
	
  var moves = new Array();
  // Can move right
  if (emptyX > 0) 
    moves.push({x: emptyX-1, y: emptyY, dx:1, dy:0})

  // Can move left
  if (emptyX < 2) 
    moves.push({x: emptyX+1, y: emptyY, dx:-1, dy:0})

  // Can move down
  if (emptyY > 0) 
    moves.push({x: emptyX, y: emptyY-1, dx:0, dy:1})

  // Can move up
  if (emptyY < 2) 
    moves.push({x: emptyX, y: emptyY+1, dx:0, dy:-1})

  // Choose randomly a movement
  do {
    var randomIndex = Math.floor(Math.random()*moves.length)
    var move = moves[randomIndex];
  } while (move.dx == -lastDX && move.dy == -lastDY);
  
  // Find image 
  var imageIndex = grid[move.y][move.x];
  		    
  // Swap empty position
  grid[emptyY][emptyX] = imageIndex;
  grid[move.y][move.x] = 0;
  emptyX = move.x;
  emptyY = move.y;
  
  // Store move to avoid to do it backworth next time
  lastDX = move.dx;
  lastDY = move.dy;
  // Run effect
  new Effect.MoveBy("thumb" + imageIndex, move.dy*80, move.dx*80, {duration:0.5, afterFinish: restartRandomMove});
}

function randomSwap() {
  // Find a thumbnails different fromthe last one
  var randomIndex;
  do {
    randomIndex = 1 + Math.floor(Math.random()*8)
  } while (randomIndex == lastIndex);

  var newMissingSrc = $("thumb" + randomIndex).src;
  var newMissingAlt = $("thumb" + randomIndex).alt;
  new Effect.Swap("thumb" + randomIndex, missingSrc, missingAlt, true, false, {duration:0.5, afterFinish: afterSwap});
  
  missingSrc = newMissingSrc;
  missingAlt = newMissingAlt;
  lastIndex = randomIndex;
}

function restartRandomMove(event) {
  setTimeout("randomMove()", 2000 + 3000 * Math.random()) ;
}

function restartSwap(event) {
  setTimeout("randomSwap()", 2000 + 1000 * Math.random()) ;
}

function afterSwap(effect) {
  if (effect.revert == false) 
    restartSwap()
}

function start() {
//  restartRandomMove();
//  restartSwap()
}
//Event.observe(window, 'load',startTimeOut);
Event.observe(window, 'load', start);



function tous_les_jours()
{
  for(var i = 0; i < document.forms.length; i++)
  {
          var form = document.forms[i], z = 0;
          for(var z = 0; z < form.length; z++)
          {
                  if(form[z].type == 'checkbox' && form[z].name == 'jours[]')
                          form[z].checked = true;
          }
  }
}