/**
 * Librairie de fonctions javascript pour le scrolling vertical d'une section de texte.
 *
 * @author Anthony Nivet <nivux@free.fr>
 * @package Librairies
 * @subpackage Javascript
 * @license http://www.gnu.org/licenses/gpl.html GNU Public License
 */

var IEDOM=document.all||document.getElementById ;

/**
 * Compteur de conteneur animé
 * @global integer cpt_scroller
 */
var cpt_scroller = 0 ;

/**
 * Constructeur
 *
 * Initialise les propriétés du scroller
 *
 * @author Anthony Nivet <nivux@free.fr>
 * @package Librairies
 * @subpackage Javascript
 *
 * @param string id Identifiant de la section à animer
 * @param string obj Libellé de l'instance scrollText
 * @param integer w Largeur du conteneur
 * @param integer h Hauteur du conteneur (si 0, scroll horizontal)
 * @param string o orientation du scroll - H, V (defaut: "V")
 * @param integer s Vitesse d'animation de 1 à 10 (defaut: 3)
 * @param boolean p TRUE si pour que l'animation se mette en pause au passage de la souris, sinon FALSE (defaut: TRUE)
 * @param boolean autoload TRUE pour afficher automatiquement le scroller, sinon FALSE (defaut: FALSE)
 * @global integer Compteur de conteneur animé
 */
function scrollText( id, obj, w, h, o, s, p, autoload ) {

  // Initialisation des propriétés
  this.el = document.getElementById( id ) ;
  this.scroller = {
    width: w ,
    height: h ,
    orientation: ( typeof( o ) != 'undefined' ) ? o : "V" ,
    content: this.el.innerHTML ,
    speed: ( typeof( s ) != 'undefined' ) ? s : 3 ,
    pauseit: ( typeof( p ) != 'undefined' ) ? p : true ,
    css: ( typeof( this.el.className ) != 'undefined' ) ? this.el.className : '' ,
    autoload: ( ( typeof( autoload ) != 'undefined' ) && ( autoload ) )
    } ;
  this.div_id = id ;
  this.obj = obj ;

  // On incremente le scroller (global)
  cpt_scroller++ ;

  this.idScroller = { ie: 'iescroller'+cpt_scroller, ns: 'ns_scroller'+cpt_scroller } ;

  // slow speed down by 1 for NS
  this.scroller.speed = ( IEDOM ) ? this.scroller.speed : Math.max( 1, this.scroller.speed-1 ) ;

  this.copyspeed = this.scroller.speed ;
  this.pausespeed = ( ! this.scroller.pauseit ) ? this.copyspeed : 0 ;

  /**
   * Lancement du scroll
   * @access public
   */
  this.start = function () {

    this.copyspeed = this.scroller.speed ;

  } ;

  /**
   * Arrêt du scroll
   * @access public
   */
  this.stop = function () {

    this.copyspeed = this.pausespeed ;

  } ;

  /**
   * Redimensionne le conteneur
   * @access public
   */
  this.resize = function (w, h) {
    if ( (typeof( w ) != 'undefined') && w ) {
      this.scroller.width = w;
      if ( IEDOM ) {
        this.cross_container.style.width = w + "px";
      } else if (document.layers) {
        this.ns_container.width = w;
      }
    }

    if ( (typeof( h ) != 'undefined') && h ) {
      this.scroller.height = h;
      if ( IEDOM ) {
        this.cross_container.style.height = h + "px";
      } else if (document.layers) {
        this.ns_container.height = h;
      }
    }
  } ;

  /**
   * Génération du bloc de donnée sur lequel est connecté le scroll
   * @access public
   */
  this.createScroller = function () {

    if (IEDOM||document.layers) {

      var _onmouseover = this.obj + '.stop()' ;
      var _onmouseout = this.obj + '.start()' ;

      this.el.style.display = 'none' ;

      if (IEDOM) {

        var style_div = 'position: relative; overflow: hidden;' ;
        style_div += ( ( typeof( this.scroller.width ) != 'undefined' ) && ( this.scroller.width != 0 ) ) ? ' width: '+this.scroller.width+'px;' : ' width: 100%;' ;
        style_div += ( ( typeof( this.scroller.height ) != 'undefined' ) && ( this.scroller.height != 0 ) ) ? ' height: '+this.scroller.height+'px;' : '' ;

        var style_scroller = 'position: absolute; left: 0px; top: '+(IE?'-2px':'0px')+'; padding: 0px; margin: 0px;' + ((this.scroller.orientation == "V")?' width: 100%;':' vertical-align: middle; white-space: nowrap; line-height: '+this.scroller.height+'px;') ;

        document.write( '<div id="' + this.idScroller.ie + '_container" style="' + style_div + '" class="' + this.scroller.css + '" onmouseover="' + _onmouseover + '" onmouseout="' + _onmouseout + '">' ) ;
        var _tag = (this.scroller.orientation == "V") ? 'div' : 'div' ;
        document.write( '  <'+_tag+' id="' + this.idScroller.ie + '" style="' + style_scroller + '" class="">' ) ;
        document.write( '  </'+_tag+'>' ) ;
        document.write( '</div>' ) ;

      } else if (document.layers) {

        var style_ilayer = ( typeof( this.scroller.width ) != 'undefined' ) ? ' width="' + this.scroller.width + '"' : '' ;
        style_ilayer += ( typeof( this.scroller.height ) != 'undefined' ) ? ' height="' + this.scroller.height + '"' : '' ;

        var style_scroller = style_ilayer + ' left="0" top="0"' ;

        document.write( '<ilayer' + style_ilayer + ' name="' + this.idScroller.ns + '">' ) ;
        document.write( '  <layer' + style_scroller + ' class="' + this.scroller.css + '" name="' + this.idScroller.ns + '_2" onmouseover="' + _onmouseover + '" onmouseout="' + _onmouseout + '"></layer>' ) ;
        document.write( '</ilayer>' ) ;

      }

    }

  } ;

  /**
   * Chargement du contenu du scroller depuis une section présente dans la page
   * @access public
   */
  this.loadContent = function () {

    if ( IEDOM ) {

      this.cross_scroller = document.getElementById ? document.getElementById(this.idScroller.ie) : eval( 'document.all.'+this.idScroller.ie ) ;
      if ( this.scroller.orientation == "V" ) {
        this.cross_scroller.style.top = parseInt( this.scroller.height ) + 8 + "px" ;
        this.cross_scroller.innerHTML = this.scroller.content ;
        this.actualheight = this.cross_scroller.offsetHeight ;
      } else {
        this.cross_scroller.style.left = parseInt( this.scroller.width ) + 8 + "px" ;
        this.cross_scroller.innerHTML = this.scroller.content ;
        this.actualwidth = this.cross_scroller.offsetWidth ;
      }

    } else if ( document.layers ) {

      this.ns_scroller = eval( 'document.'+this.idScroller.ns+'.document.'+this.idScroller.ns+'_2' ) ;
      if ( this.scroller.orientation == "V" ) {
        this.ns_scroller.left = parseInt( this.scroller.height ) + 8 ;
        this.ns_scroller.document.write( this.scroller.content ) ;
        this.ns_scroller.document.close() ;
        this.actualheight = this.ns_scroller.document.height ;
      } else {
        this.ns_scroller.top = parseInt( this.scroller.width ) + 8 ;
        this.ns_scroller.document.write( this.scroller.content ) ;
        this.ns_scroller.document.close() ;
        this.actualwidth = this.ns_scroller.document.width ;
      }

    }

    var _fct = ( this.scroller.orientation == "V" ) ? '.animateScrollerV()' : '.animateScrollerH()' ;
    var lefttime = setInterval( this.obj+_fct, 100 ) ;

  } ;

  /**
   * Mise en animation du scroll vertical
   * @access public
   */
  this.animateScrollerV = function () {

    if ( IEDOM ) {

      if ( parseInt( this.cross_scroller.style.top ) > ( this.cross_scroller.offsetHeight * (-1) + 8 ) ) {
        this.cross_scroller.style.top = parseInt( this.cross_scroller.style.top ) - this.copyspeed + "px" ;
      } else {
        this.cross_scroller.style.top = parseInt( this.scroller.height ) + 8 + "px" ;
      }

    } else if ( document.layers ) {
      if ( this.ns_scroller.top > ( this.actualheight * (-1) + 8 ) ) {
        this.ns_scroller.top-= this.copyspeed ;
      } else {
        this.ns_scroller.top = parseInt( this.scroller.height ) + 8 ;
      }
    }

  } ;

  /**
   * Mise en animation du scroll horizontal
   * @access public
   */
  this.animateScrollerH = function () {

    if ( IEDOM ) {

      if ( parseInt( this.cross_scroller.style.left ) > ( this.cross_scroller.offsetWidth * (-1) + 8 ) ) {
        this.cross_scroller.style.left = parseInt( this.cross_scroller.style.left ) - this.copyspeed + "px" ;
      } else {
        this.cross_scroller.style.left = parseInt( this.scroller.width ) + 8 + "px" ;
      }

    } else if ( document.layers ) {

      if ( this.ns_scroller.top > ( this.actualwidth * (-1) + 8 ) ) {
        this.ns_scroller.left-= this.copyspeed ;
      } else {
        this.ns_scroller.left = parseInt( this.scroller.width ) + 8 ;
      }

    }

  } ;


  /**
   * Affichage du scroller
   * @access public
   */
  this.output = function () {
    this.createScroller() ;
    this.loadContent() ;
  } ;

  /**
   * Chargement complet du scroller
   */
  if ( this.scroller.autoload ) {
    this.output() ;
  }

}
