<!--
// Librairie de fonctions jQuery d'animation des images.
// Dépend de jQuery 1.2.6+
//
// (c) 2008 Le Petit Atelier de Génie logiciel, Olivier Lange
//
// Consultez http://www.petit-atelier.ch/ pour contacter l'auteur.
//
// Ce script est distribué selon les termes de le GNU General Public License
// (voir http://www.fsf.org/licensing/licenses/gpl.html et la notice qui suit).
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// $Id: jquery.nav.js 205 2009-08-09 16:07:32Z olange@petit-atelier.ch $

// On document ready
$( function(){
  // Constantes et variables locales, qui seront disponibles dans la clôture
  // (closure) des fonctions appelantes qui les référencent

  var nav = new Object();
  // Expression régulières d'analyse de diverses chaînes de caractères
  nav.getBehaviorArgs = /^(.*)\('([^']*)'\)$/i;         // sépare le nom de l'argument d'une chaîne ayant la forme d'un appel de fonction; forme attendue, p.ex.: focusover('#qnSimpleSearch')
  nav.splitNorm = /^(.*)(\.png|\.gif|\.jpg)$/i;         // sépare l'extension du chemin/nom d'une URI
  nav.splitOver = /^(.*)(-over)(\.png|\.gif|\.jpg)$/i;  // idem, mais d'une URI dont le nom contiendrait '-over' juste avant l'extension
  nav.preloadImages = new Array();

  // Méthodes locales, idem; comme elles référencent les variable locales, celles-ci
  // doivent avoir été déclarées (i.e. ne pas utiliser l'idiome 'var nav = { ... }')

  nav.activateNorm = function( imgElt) {
    var m = nav.splitOver.exec( imgElt.src);
    if( m != null)  imgElt.src = m[1] + m[3];           // enlève le '-over' du nom du fichier du pictogramme
  };
  nav.activateOver = function( imgElt) {
    var m = nav.splitNorm.exec( imgElt.src);
    if( m != null)  imgElt.src = m[1] + "-over" + m[2]; // ajoute '-over' à la fin du nom du fichier du pictogramme, avant l'extension
  };
  nav.preloadOver = function( imgElt) {
    var m = nav.splitNorm.exec( imgElt.src);
    if( m != null) {
      var j = nav.preloadImages.length;
      nav.preloadImages[ j] = new Image;
      nav.preloadImages[ j].src = m[1] + "-over" + m[2];
    }
  };
  nav.focusOver = function( srcElt) {
    var behaviorAttr = $( srcElt).attr( "behavior");
    var args = nav.getBehaviorArgs.exec( behaviorAttr);
    var target = args[ 2];
    if( target != null)  $( target).focus();
  };

  // Opérations immédiates sur le document via jQuery

  // Place le focus sur un élément dont le nom est désigné par l'argument
  // Usage: <p behavior="focusclick('#qnNameSimple')">
  $("*[behavior*=focusover]")
    .mouseover( function() {
      nav.focusOver( this);
    });

  $("*[behavior*=focusclick]")
    .click( function() {
      nav.focusOver( this);
    })
    .mouseover( function() {
      nav.focusOver( this);
    });

  // Active la variante «rollover» d'une image en ajoutant '-over' au nom de l'image
  // ou réactive la variante «normale» en supprimant le suffixe '-over' du nom;
  // au passage, commande le préchargement de la variante «rollover»
  $("img[behavior*=rollover]")
    .mouseover( function() {
      nav.activateOver( this)
    })
    .mouseout( function() {
      nav.activateNorm( this)
    })
    .each( function() {
      nav.preloadOver( this)
    });

  $("img[behavior*=activateover]")
    .each( function() {
      nav.activateOver( this)
    });

  $("img[behavior*=fadeover]")
    .css( "opacity", 0)
    .mouseover( function(){ $( this).stop().animate( {opacity: 100}, {duration: 25000, easing: "swing", queue: false}); })
    .mouseout( function(){ $( this).stop().animate( {opacity: 0}, {duration: 1000, easing: "linear", queue: false}); })
  ;
  $("area[behavior*=fadeover]").each( function() {
    // propage les événements mouseover/out sur une image «associée» au hotspot actif,
    // c-à-d. l'image dont l'identificateur correspond à celui de l'attribut idref
    var idref = $(this).attr( "idref");
    $(this)
      .css( "cursor", "default")
      .mouseover( function(){ $( "img#"+idref).mouseover(); })
      .mouseout( function(){ $( "img#"+idref).mouseout(); });
  });

  $("*[effect*=shadow]").wrap("<div class='shadow1'><div class='shadow2'><div class='shadow3'></div></div></div>");;

  // Animation des images qui ont un fadeover: les fait apparaître au hasard, l'une après l'autre
  var fadeoverAnimation = {
    revealAnother: function() {
      var len = this.imgarray.length;
      if( this.imgarray == undefined || len == 0) {
        this.cancel();
      } else {
        var i = ( len == 1) ? 0 : Math.floor( len * Math.random());
        $(this.imgarray[ i]).mouseover();
        this.imgarray.splice( i, 1);
        this.scheduleAnother();
      }
    },
    scheduleAnother: function() {
      this.cancel();
      var outer = this;
      this.timeoutID = window.setTimeout( function() { outer.revealAnother(); }, this.delay);
    },
    run: function() {
      this.imgarray = $("img[behavior*=fadeover]").filter("[behavior*=animate]").get(); // get() retourne un Array
      this.delay = 8000;
      this.scheduleAnother();
    },
    cancel: function() {
      if( typeof this.timeoutID == "number") { window.clearTimeout( this.timeoutID); }
      delete this.timeoutID;
    }
  }; // animation
  fadeoverAnimation.run();
});

// eof -->