/**
 * Controlleur du popup
 */
var ControllerPopup = Class.create({
    initialize: function(idPopup, idFondNoir, hauteurBG, imgCadre, imgCroix, controller, callbackFermeture){
      this.platformMac = navigator.platform.match("Mac") ? true : false;
      /* Menage */
      if($(idPopup)) $(idPopup).remove();
      if($(idFondNoir)) $(idFondNoir).remove();

      /* Création des éléments du popup */
      this.fondNoir = new Element("div");
      this.popupContenu = new Element("div");
      this.croix = new Element("img");
      this.popup = new Element("div");
      this.controller = controller;

      this.fondNoir.hide();
      this.popup.hide();

      this.fondNoir.writeAttribute("id", idFondNoir);
      this.fondNoir.setStyle({height: hauteurBG + "px"});

      this.popupContenu.writeAttribute("id", idPopup + "Contenu");

      this.croix.writeAttribute("src", imgCroix).writeAttribute("alt", "Fermer la popup").writeAttribute("title", "Fermer la popup");
      this.croix.setStyle({"cursor": "default", "marginLeft": "490px"});

      this.popup.writeAttribute("id", idPopup);
      this.popup.setStyle({background : "url(" + imgCadre +") no-repeat"});

      this.popup.insert(this.croix);
      this.popup.insert(this.popupContenu);

      if(!$("contenuPopup"))
        throw("La div contenuPopup n'est pas définie, la popup ne peut pas se créer");

      $("contenuPopup").insert( {top : this.popup});
      $("contenuPopup").insert( {top : this.fondNoir});

      /* Création des événements utilisateurs. */
      this.croix.observe("click", this.onFermerPopup.bindAsEventListener(this, callbackFermeture) );
      this.fondNoir.observe("click", this.onFermerPopup.bindAsEventListener(this, callbackFermeture) );
      Event.observe(window, "resize", this.setPosition.bind(this));
      document.observe("scroll", this.setPosition.bind(this));

      if(!this.platformMac)
        new Draggable(idPopup, { starteffect : null, endeffect: null, zindex: 20, snap:[50, 50]});
      else
        $(idPopup).setStyle({"cursor": "default"});

      this.show();

    },

    onFermerPopup: function(event, callbackFermeture){
      if(callbackFermeture)
        this.hide(callbackFermeture.bind(this.controller));
      else
        this.hide();
    },

    setHauteur: function(hauteur){
      this.hauteur = hauteur;
      this.popup.setStyle({"height" : hauteur + 40 +"px"});
      this.popupContenu.setStyle({"height" : hauteur +"px", "marginBottom": 20});
    },

    setLargeur: function(largeur){
      this.largeur = largeur;
      this.popup.setStyle({"width" : largeur +"px"});
      this.popupContenu.setStyle({"width" : largeur + "px"});
    },

    setPosition: function(){
      var mLeft = document.body.getWidth() / 2;
      mLeft -= this.largeur / 2;

     var mTop = document.viewport.getScrollOffsets()[1] + 50;

      this.popup.setStyle({marginLeft: mLeft + "px", marginTop: mTop + "px"});
    },

    show: function(callback){
      this.fondNoir.appear({duration: 1, to:0.5});
      if(!callback)
        this.popup.appear({duration: 1});
      else
        this.popup.appear({duration: 1, afterFinish : callback});
    },

    hide: function(callback){
      if(callback) this.popup.shrink({duration: 1, beforeStart: callback});
      else{
        this.update("");
        this.popup.shrink({duration: 1});
      }

      this.fondNoir.fade({duration: 1});
    },

    update: function(contenu){
      this.popupContenu.update(contenu);
    }
});