var blocEditeurs = Class.create();
blocEditeurs.logo = Class.create();
blocEditeurs.logo.prototype = {
  marge: 8,
  ratio: 6/8,
  rayon: 110,
  coef: 3,

  initialize: function(bloc, place, src, alt, lien, titre) {
    this.bloc = bloc;

    this.node = document.createElement('A');
    this.node.href = lien;
    this.node.title = titre;
    this.node.className = 'logoJS';

    this.img = document.createElement('IMG');
    this.img.src = src;
    this.img.alt = alt;
    this.node.appendChild(this.img);

    this.place = place;
    this.part = 1;
    this.partsTilMe = { x: place.col, y: place.row};
    this.size = {x: 0, y: 0};

    this.hide = (titre)? false : true;

    with(this.node.style) {
      position = 'absolute';
      width = '0px';
      height = '0px';
      if(this.hide) visibility = 'hidden';
    }


    this.bloc.node.appendChild(this.node);

    this.mouseMove = this.updatePart.bindAsEventListener(this);
    Event.observe(document, 'mousemove', this.mouseMove);
  },

  updatePart: function() {
    if(this.hide || !this.bloc.mouse)
      return this.newPart(1);

    var l = {
      x: Math.pow(this.pos.x + this.bloc.left + (this.size.x/2) - this.bloc.mouse.x, 2),
      y: Math.pow(this.pos.y + this.bloc.top + (this.size.y/2) - this.bloc.mouse.y, 2)
    };

    var d = parseInt(Math.sqrt(l.x + l.y));

    return this.newPart((d < this.rayon) ? 1 + ((1 - (d / this.rayon)) * this.coef) : 1);
  },

  newPart: function(p) {
    if(this.part != p) {
      this.bloc.updateParts(this.place, p - this.part);
      this.part = p;
      return true;
    }
    return false;
  },

  updateSize: function() {
    var dx = (this.part / this.bloc.rowsPart[this.place.row]) * (this.bloc.node.offsetWidth);
    var dy = (this.part / this.bloc.colsPart[this.place.col]) * (this.bloc.node.offsetHeight) / this.ratio;

    var d = (dx <= dy) ? dx : dy;

    var newSize = {x: parseInt(d), y: parseInt(d*this.ratio)};
    if(this.size.y != newSize.y)
      this.size = newSize;
    this.partsTilMe = {x: this.getXPartsTilMe(), y: this.getYPartsTilMe() };
  },

  updatePos: function() {
    var x = this.partsTilMe.x * (this.bloc.node.offsetWidth) / this.bloc.rowsPart[this.place.row];
    var y = this.partsTilMe.y * (this.bloc.node.offsetHeight) / this.bloc.colsPart[this.place.col];

    this.pos = {x: parseInt(x), y: parseInt(y)};
  },

  updateStyle: function() {
    if(this.size.x-(this.marge*2) > 0)
        this.node.style.width = (this.size.x-(this.marge*2))+'px';
    if(this.size.y-(this.marge*2) > 0)
        this.node.style.height = (this.size.y-(this.marge*2))+'px';

    this.node.style.left = (this.pos.x+this.marge)+'px';
    this.node.style.top = (this.pos.y+this.marge)+'px';

    this.node.style.zIndex = parseInt(this.part*4);
  },

  getXPartsTilMe: function() {
    var prev = this.Xprevious();
    return (prev) ? prev.partsTilMe.x + prev.part : 0;
  },

  getYPartsTilMe: function() {
    var prev = this.Yprevious();
    return (prev) ? prev.partsTilMe.y + prev.part : 0;
  },

  Xprevious: function() {
    return (this.bloc.tab[this.place.row]) ? this.bloc.tab[this.place.row][this.place.col-1] : false;
  },

  Yprevious: function() {
    return (this.bloc.tab[this.place.row-1]) ? this.bloc.tab[this.place.row-1][this.place.col] : false;
  },

  destroy: function() {
    Event.stopObserving(document, 'mousemove', this.mouseMove);
    this.mouseMove = null;
    this.bloc = null;
    this.node = null;
    this.img = null;
    this.place = null;
    this.size = null;
    this.pos = null;
    this.partsTilMe = null;
  }
};

blocEditeurs.prototype = {
  cols: 3,
  rayonActif: 250,

  initialize: function(blocId) {
    this.logosNb = 0;
    this.tab = [];
    this.rowsPart = [];
    this.colsPart = [];
    if(!(this.node = $(blocId)))
      return;

    this.node.style.position = 'relative';
    this.node.style.height = '18em';

    this.left = this.node.offsetLeft + this.node.parentNode.offsetLeft;
    this.top = this.node.offsetTop + this.node.parentNode.offsetTop;

    for(var i = this.node.childNodes.length-1; i >= 0; i--) {
      if(this.node.childNodes[i].tagName == 'UL') {
        var ul = this.node.childNodes[i];
        for(var j = 0; j < ul.childNodes.length; j++) {
          if(ul.childNodes[j].tagName == 'LI') {
            this.addLogo(ul.childNodes[j]);
          }
        }
      }
      this.node.removeChild(this.node.childNodes[i]);
    }

    var p = document.createElement('P');
    p.appendChild(document.createTextNode('Passez votre souris sur les logos pour mieux les visualiser'));
    this.node.appendChild(p);

    this.completeTab();
    this.updateLogos();

    blocEditeurs.updateLogos = this.updateLogos.bindAsEventListener(this);
    this.mouseMove = this.refreshActivation.bindAsEventListener(this);
    this.windowBlur = this.stopRefresh.bindAsEventListener(this);
    this.windowUnload = this.destroy.bindAsEventListener(this);
    Event.observe(document, 'mousemove', this.mouseMove);
    Event.observe(window, 'blur', this.windowBlur);
    Event.observe(window, 'unload', this.windowUnload);
  },

  addLogo: function(li) {
    var data;
    if(!(data = this.getLiData(li)))
      data = {src: '', alt: '', lien: '#', titre: ''};

    var ln = this.logosNb++;
    place = {col: (ln)%this.cols, row: Math.floor((ln)/this.cols)};

    if(!this.rowsPart[place.row])
      this.rowsPart[place.row] = 0;
    this.rowsPart[place.row]++;

    if(!this.colsPart[place.col])
      this.colsPart[place.col] = 0;
    this.colsPart[place.col]++;

    if(!this.tab[place.row])
      this.tab[place.row] = [];
    this.tab[place.row][place.col] = new blocEditeurs.logo(this, place, data.src, data.alt, data.lien, data.titre);
  },

  getLiData: function(li) {
    var a, img;
    if(!li) return false;
    if(!((a = li.firstChild) && a.tagName == 'A')) return false;
    if(!((img = a.firstChild) && img.tagName == 'IMG')) return false;
    return {src: img.src, alt: img.alt, lien: a.href, titre: a.title};
  },

  completeTab: function() {
    if(this.logosNb%this.cols != 0) {
      this.addLogo();
      this.completeTab();
    }
  },

  updateParts: function(place, delta) {
    this.rowsPart[place.row] += delta;
    this.colsPart[place.col] += delta;
  },

  updateLogos: function(e) {
    this.left = this.node.offsetLeft + this.node.offsetParent.offsetLeft;
    this.top = this.node.offsetTop + this.node.offsetParent.offsetTop;

    for(var i = 0; i < this.logosNb; i++) {
      var p = {col: (i)%this.cols, row: Math.floor((i)/this.cols)};
      if(this.tab[p.row] && this.tab[p.row][p.col]) {
        this.tab[p.row][p.col].updateSize();
      }
    }
    for(var i = 0; i < this.logosNb; i++) {
      var p = {col: (i)%this.cols, row: Math.floor((i)/this.cols)};
      if(this.tab[p.row] && this.tab[p.row][p.col]) {
        this.tab[p.row][p.col].updatePos();
        this.tab[p.row][p.col].updateStyle();
      }
    }
  },

  refreshActivation: function(e) {
    this.mouse = {x: Event.pointerX(e), y: Event.pointerY(e)};

    var inX = this.mouse.x > (this.left - this.rayonActif);
    inX = inX && this.mouse.x < (this.left + this.node.offsetWidth + this.rayonActif);
    var inY = this.mouse.y > (this.top - this.rayonActif);
    inY = inY && this.mouse.y < (this.top + this.node.offsetHeight + this.rayonActif);

    if(inX && inY)
      this.startRefresh();
    else
      this.stopRefresh();
  },

  startRefresh: function() {
    if(!this.refreshInterval) {
      this.refreshInterval = window.setInterval('blocEditeurs.updateLogos()', 10);
    }
  },

  compteur: 0,

  stopRefresh: function() {
    if(this.refreshInterval) {
      window.clearInterval(this.refreshInterval);
      this.refreshInterval = false;
      return;
    }
    if(this.compteur == 50) {
      this.updateLogos();
      this.compteur = 0;
      return;
    }
    this.compteur++;
  },

  destroy: function() {
    Event.stopObserving(document, 'mousemove', this.mouseMove);
    Event.stopObserving(window, 'blur', this.windowBlur);
    Event.stopObserving(window, 'unload', this.windowUnload);
    this.windowUnload = null;
    this.windowBlur = null;
    this.mouseMove = null;
    this.stopRefresh();
    for(var i = 0; i < this.logosNb; i++) {
      var p = {col: (i)%this.cols, row: Math.floor((i)/this.cols)};
      if(this.tab[p.row] && this.tab[p.row][p.col]) {
        this.tab[p.row][p.col].destroy();
        this.tab[p.row][p.col] = null;
      }
    }
    this.tab = null;
    this.node = null;
    this.rowsPart = null;
    this.colsPart = null;
    this.colsHeight = null;
  }
};

blocEditeurs.windowLoad = function(e) { new blocEditeurs('editeurs'); }
Event.observe(window, 'load', blocEditeurs.windowLoad);