/////////////////////////////////////////////////////////////////////////////

function gbx_get_fct0( target, fct )
{
  return function() { fct.call(target); };
}

function gbx_get_fct1( target, fct, arg1 )
{
  return function() { fct.call( target, arg1 ); };
}

function gbx_get_time()
{
  return (new Date()).getTime();
}

function gbx_set_element_opacity( el, alpha )
{
  if( alpha < 0 ) alpha= 0; else if( alpha > 1 ) alpha= 1;

  if( typeof el.style.opacity != "undefined" )
    el.style.opacity= alpha;
   else
    {
    if( el.filters )
      {
      // IE
      if( alpha >= 1 )
        el.style.filter= "";
       else
        {
        alpha= Math.floor( 100*alpha + 0.5 );
        el.style.filter= "progid:DXImageTransform.Microsoft.Alpha(opacity="+alpha+")";
        }
      }
     else
      el.style.MozOpacity= alpha;
    }
}

/////////////////////////////////////////////////////////////////////////////

function gbx_html_slide_show( prefix )
{
  this.cur_index= 0;
  this.prev_index= -1;
  this.n_images= 0;

  this.set_prefix(prefix);

  this.timer_id = -1;
  this.timer_start_time= -1;
  this.transition_duration_ms= 200;

  this.auto_timeout_id= -1;
  this.still_duration_ms= 0; // ==0 if no auto sliding

  this.has_index_map= false;
}


gbx_html_slide_show.prototype.set_prefix = function( prefix )
{
  this.map_prefix= prefix + "_map_";
  this.div_prefix= prefix + "_div_";
  this.img_prefix= prefix + "_img_";
}


gbx_html_slide_show.prototype.init = function( width, height, n_images )
{
  this.n_images= n_images;

  var el;

  for( var i = 0; i < n_images; ++i )
    {
    var map= document.getElementById( this.map_prefix + i );
    if( map )
      {
      map.name= this.map_prefix + i;

      if( this.has_index_map )
        {
        var area_count= map.areas.length;
        for( var j = 0; j < area_count; ++j )
          {
          var area= map.areas[j];
          if( j < n_images )
            {
            if( i != j )
              area.onclick= gbx_get_fct1( this, this.onclick, j );
            else
              area.coords= "";
            area.href= "javascript:;";
            }
          }
        }
      }

    el= document.getElementById(this.div_prefix+i);
    el.style.width= width + "px";
    el.style.height= height + "px";
    el.style.display= "none";

    el.style.position="absolute";
    el.style.zIndex= i;

    el= document.getElementById(this.img_prefix+i);
    el.style.width= width + "px";
    el.style.height= height + "px";

    el.useMap= "#" + this.map_prefix +i;
    }

  this.cur_index= 0;
  el= document.getElementById(this.div_prefix+this.cur_index);
  el.style.display="inline";

  if( this.still_duration_ms > 0 )
    {
    this.auto_timeout_id= setTimeout( gbx_get_fct0(this,this.onautoslide),
                                      this.still_duration_ms );
    }
}






gbx_html_slide_show.prototype.complete_timer = function()
{
  if( this.timer_id < 0 )
    return;

  var el= document.getElementById(this.img_prefix+this.cur_index);

  gbx_set_element_opacity( el, 1 );

  clearInterval(this.timer_id);
  this.timer_id= -1;
  this.timer_start_time= -1;

  if( this.prev_index >= 0 )
    {
    el= document.getElementById(this.div_prefix+this.prev_index);
    el.style.display= "none";
//bubu//
    el= document.getElementById(this.img_prefix+this.prev_index);
    gbx_set_element_opacity( el, 0 );

    this.prev_index= -1;
    }
}

gbx_html_slide_show.prototype.on_timer = function()
{
  var el= document.getElementById(this.img_prefix+this.cur_index);

  var t= gbx_get_time() - this.timer_start_time;
  var x= t / this.transition_duration_ms;
  if( x >= 1 )
    {
    this.complete_timer();

    if( this.auto_timeout_id >= 0 )
      {
      clearTimeout(this.auto_timeout_id);
      this.auto_timeout_id= -1;
      }

    if( this.still_duration_ms > 0 )
      {
      this.auto_timeout_id= setTimeout( gbx_get_fct0(this,this.onautoslide),
                                        this.still_duration_ms );
      }

    return;
    }

  gbx_set_element_opacity( el, x );
}


gbx_html_slide_show.prototype.onautoslide= function()
{
  var next_index = this.cur_index + 1;
  if( next_index == this.n_images )
    next_index= 0;

  this.onclick(next_index);
}

gbx_html_slide_show.prototype.onclick= function( index )
{
  if( this.auto_timeout_id >= 0 )
    {
    clearTimeout(this.auto_timeout_id);
    this.auto_timeout_id= -1;
    }

  if( index == this.cur_index )
    return;

  if( this.timer_id >= 0 )
    this.complete_timer();

  var el;

  el= document.getElementById(this.div_prefix+this.cur_index);
  //el.style.display="none";
  el.style.zIndex= this.cur_index;

  el= document.getElementById(this.img_prefix+index);
  gbx_set_element_opacity( el, 0 );

  el= document.getElementById(this.div_prefix+index);
  el.style.display="inline";
  el.style.zIndex= this.n_images;

  this.prev_index= this.cur_index;
  this.cur_index= index;

  this.timer_start_time= gbx_get_time();
  this.timer_id= setInterval( gbx_get_fct0(this,this.on_timer), 1000/30 );
}

/////////////////////////////////////////////////////////////////////////////

