Slideshow = Class.create();

/**
 * Slideshow class
 *
 * @author Anthony Nivet <anivet@eskape.fr>
 * @date 28-dec-2007
 * @version 20080108
 */
Slideshow.prototype =
{
  // {{{ properties
  _debug: 2,
  _hdScroll: null,
  _width: 0,
  
  image: new Array(),
  scrollStep: 10,
  // }}}
  
  // {{{ initialize
  /**
   * Constructor
   *
   * @param array options
   */
  initialize: function(options)
  {
  }
  //}}}
  ,
  
  // {{{ _log
  /**
   * Display log message
   */
  _log: function( text, level )
  {
    if( ( typeof(level) == 'undefined' ) ) level = 1 ;
    if( this._debug === true ) this._debug = 1 ;
    debug_test = ( ( this._debug !== false ) && ( this._debug >= level ) && ( typeof(console) != 'undefined' ) );
    if( level > 1 ) text = '-'+level+'- '+ text;
    if( debug_test ) console.log( text );
  }
  // }}}
  ,
  
  // {{{ init
  /**
   * Slideshow initialization
   *
   * @param void
   */
  init: function()
  { 
    if( ! $('slideshow') ) return ;
    
    this._log( '>> -- Initialisation --' );
    this._log( 'Scroll Step : '+this.scrollStep );
    
    // Left button slideshow initialization
    if( $('slideshow-left') )
    {
      $('slideshow-left').onmouseover = this.scrollLeft.bind(this);
      $('slideshow-left').onmouseout = this.scrollStop.bind(this);
    }
    
    // Right button slideshow initialization
    if( $('slideshow-right') )
    {
      $('slideshow-right').onmouseover = this.scrollRight.bind(this);
      $('slideshow-right').onmouseout = this.scrollStop.bind(this);
    }
    
    var _images = $('slideshow').getElementsByClassName('img-slideshow') ;
    
    this._log( 'Nb images : '+_images.length );
    this._width = 0;
    for(i=0; i < _images.length; i++)
    {
      var id = _images[i];
      this._log( 'Initialisation : ' + $(id).src, 2 );
      this._width+= $(id).up('.cadre-image').offsetWidth;
      $(id).onmouseover = this.zoomImageMax.bind(this, id);
      $(id).onmouseout = this.zoomImageMin.bind(this, id);
      $(id).onclick = this.openLink.bind(this, id);
    }
    
    this._log( '>> End Initialisation' );
  }
  // }}}
  ,
  
  // {{{ scrollLeft
  /**
   * Slideshow scroll left
   */
  scrollLeft: function()
  {
    this._log( '>> -- Scroll left --' );
    
    if( ! $('images') ) return ;
    
    if( ! $('images').style.left ) $('images').style.left = '0px' ;
    
    var _left = $('images').style.left;
    _left = parseInt(_left.substring(0,_left.length-2));
    
    this._log( 'Left: '+_left, 2 );
    
    if( this._width > $('slideshow').offsetWidth )
    {
      if( _left < 0 ) $('images').style.left = _left+this.scrollStep+'px';
      else
      {
        $('images').style.left = '0px';
        return ;
      }
    }
    
    $('slideshow-left').src = 'IMG/precedent2.gif' ;
        
    this._hdScroll = setTimeout(this.scrollLeft.bind(this), 1);
    
    this._log( '>> End Scroll Left' );
  }
  // }}}
  ,
  
  // {{{ scrollRight
  /**
   * Slideshow scroll right
   */
  scrollRight: function()
  {
    this._log( '>> -- Scroll right --' );
    
    if( ! $('images') ) return ;
    
    if( ! $('images').style.left ) $('images').style.left = '0px' ;
    
    var _left = $('images').style.left;
    _left = parseInt(_left.substring(0,_left.length-2));
    
    _limit = -1 * $('images').getWidth() + $('images').parentNode.offsetWidth;
    
    if( this._width > $('slideshow').offsetWidth )
    {
      this._log( 'Limit right: '+_limit+' (Left:'+_left+')', 2 );
      $('images').style.left = _left-this.scrollStep+'px';
      if( _left > _limit ) $('images').style.left = _left-this.scrollStep+'px';
      else
      {
        $('images').style.left = _limit+'px';
        return ;
      }
    }    
    
    $('slideshow-right').src = 'IMG/suivant2.gif' ;
        
    this._hdScroll = setTimeout(this.scrollRight.bind(this), 1);
    
    this._log( '>> End Scroll Right' );
  }
  // }}}
  ,
  
  // {{{ scrollStop
  /**
   * Stop slideshow scroll
   */
  scrollStop: function()
  {
    this._log( '>> -- Stop slideshow --' );
    if( ! $('images') ) return ;
    
    clearTimeout(this._hdScroll) ;
    this._hdScroll = null ;
    
    $('slideshow-right').src = 'IMG/suivant.gif' ;
    $('slideshow-left').src = 'IMG/precedent.gif' ;
    
    this._log( '>> End Stop slideshow' );
  }
  // }}}
  ,
  
  // {{{ zoomImageMax
  /**
   * Zoom image
   */
  zoomImageMax: function(ele)
  {
    this._log( '>> -- Zoom In --' );
    this._log( 'Image : '+$($(ele).id).src, 2 );
    
    $(ele).style.height = '135px';
    //$(id).next('.titre-overlay').show();
    $(ele).up('.cadre-image').style.backgroundColor = '#ff7200';
    $('title-'+$(ele).id).down('.titre-image').addClassName('puce-titre');
    $('title-'+$(ele).id).style.margin='20px 2px 0px 2px';
    $('title-'+$(ele).id).down('.titre-image').style.fontSize='9pt';
    $('title-'+$(ele).id).down('.titre-image').style.fontWeight='bold';
    
    var _images = $('slideshow').getElementsByClassName('img-slideshow') ;
    
    var _width = 0;
    for(i=0; i < _images.length; i++)
    {
      var _id = _images[i];
      _width+= $(_id).up('.cadre-image').offsetWidth;
      if( $(ele) == $(_id) ) break;
    }
    
    if( ! $('images').style.left ) $('images').style.left = '0px' ;
    var _left = $('images').style.left;
    _left = parseInt(_left.substring(0,_left.length-2));
    
    // Re-calage à droite
    if( ( (_width+_left) > $('slideshow').offsetWidth ) )
    {
      var _newPos =  - (_width - $('slideshow').offsetWidth);
      $('images').style.left = _newPos+'px';
    }
    // Re-calage à gauche
    else if(true)
    {
      _width = _width - $(ele).up('.cadre-image').offsetWidth;
      this._log( 'Left: '+_left+' - Width: '+_width )
      if( (_left + _width) < 0 )
      {
        var _newPos =  -_width;
        $('images').style.left = _newPos+'px';
      }
    }
    
    this._log( '>> End Zoom In' );
  }
  // }}}
  ,
  
  // {{{ zoomImageMin
  /**
   * Zoom image
   */
  zoomImageMin: function(ele)
  {
    this._log( '>> -- Zoom Out --' );
    this._log( 'Image ('+$(ele).id+'): '+$(ele).src, 2 );
    
    //$(id).next('.titre-overlay').hide();
    $('title-'+$(ele).id).down('.titre-image').removeClassName('puce-titre');
    $('title-'+$(ele).id).style.margin='0px';
    $('title-'+$(ele).id).down('.titre-image').style.fontSize='8pt';
    $('title-'+$(ele).id).down('.titre-image').style.fontWeight='normal';
    $('title-'+$(ele).id).up('.cadre-image').style.backgroundColor = '';
    $(ele).style.height = '80px';
    
    if( ! $('images').style.left ) $('images').style.left = '0px' ;
    
    var _left = $('images').style.left;
    _left = parseInt(_left.substring(0,_left.length-2));
    
    _limit = -1 * $('images').getWidth() + $('images').parentNode.offsetWidth;
    if( (this._width > $('slideshow').offsetWidth) && (_left < _limit) )
    {
      $('images').style.left = _limit+'px';
    }
    
    if( this._width < $('slideshow').offsetWidth )
    {
      $('images').style.left = '0px';
    }
    
    this._log( '>> End Zoom Out' );
  },
  // }}}
  
  // {{{ openLink
  /**
   * Open Link
   */
  openLink: function(ele)
  {
    this._log( '>> -- Open Link --' );
    this._log( 'Image  ('+$(ele).id+'): '+$(ele).src, 2 );
    
    var _link = $(ele).up('a');
    
    if( _link.href != '' )
    {
      var _params = $H(_link.href.toQueryParams());
      
      new Ajax.Updater( 'detail-projet', _link.href.split('?').first(),
      {
        method: 'get',
        evalScripts: true,
        parameters: _params.toQueryString()
      });
    }
    this._log( '>> End Open Link' );
    
    // Désactivation du lien href d'origine
    return false;
  }
  // }}}

}