class_hscroller.instances = new Array()

var hscroller_obj = null;
var hscroller_id=1;

function class_hscroller(div_id)
{
	this.id = class_hscroller.instances.length;
	class_hscroller.instances[this.id] = this;
	
	this.debug_id = 0;
	this.scroll_div_id = 'id-'+div_id+'-out';			/* identifiant du bloc */
	this.scroll_div_id_in = 'id-'+div_id+'-in';			/* identifiant du bloc interne */
	
	this.scroll_position_saved = 0;						/* position courante */
	this.scroll_position = 0;							/* position courante */
	this.scroll_ms = 1.0;				/* vitesse minimale */
	this.scroll_is = 1.0;				/* vitesse initiale */
	this.scroll_cs = 0.0;				/* vitesse courante */
	this.scroll_pa = -0.05;				/* pseudo accélération */
	
	this.scroll_div_getWidth = 0;			/* hauteur du bloc */
	this.scroll_div_out_width = 0;		/* hauteur du bloc encadrant */
	this.scroll_is_scrolling = false;	/* variable d'état - scrolling en cours ? */
	this.scroll_timer = null;			/* timer */

	this.autoscrolling = false;
	this.scroll_limit = 0;				/* amplitude maximale du scroll */
}

class_hscroller.prototype.debug = function()
{
	div_setContent('debug',"scroll_div_id_in="+this.scroll_div_id_in+' debug_id='+this.debug_id);
	this.debug_id++;
}

/* initialise un autoscroll */
class_hscroller.prototype.scroll_start_auto = function ()
{
	if(this.scroll_div_getWidth==0) this.scroll_div_getWidth = _div_getWidth(this.scroll_div_id_in);
	if(this.scroll_div_out_width==0) this.scroll_div_out_width = _div_getWidth(this.scroll_div_id);

	if(!this.autoscrolling && hscroller_obj==null)
	{
		this.scroll_cs = 1.0;
		this.autoscrolling = true;

		hscroller_obj = this;

		this.scroll_timer = setInterval( "class_hscroller.instances["+this.id+"].scroll_auto()", 60);
	}
}

/* scroll automatique */
class_hscroller.prototype.scroll_auto = function ()
{	
	var id = this.scroll_div_id;

	if (document.getElementById) 
    {
       var cdiv = document.getElementById(id);
       if (cdiv) 
       {
			if(this.scroll_position <= 0)
			{
				this.scroll_cs = 1.0;
            }

			if(this.scroll_position >= ( this.scroll_div_getWidth - this.scroll_div_out_width ))
			{
				this.scroll_cs = -1.0;
            }
            
        	this.scroll_position += this.scroll_cs;
            cdiv.scrollLeft=this.scroll_position;

        }
        else
        {
        }
    }
}

/* scrolle jusqu'à une position */
class_hscroller.prototype.scroll_to = function (position)
{
	this.scroll_position = position;
	
	if (document.getElementById) 
    {
       	var cdiv = document.getElementById( this.scroll_div_id );
      	if (cdiv) 
       	{
			cdiv.scrollLeft=this.scroll_position;
		}
	}
}

/* initialise un scroll */
class_hscroller.prototype.scroll_start_left = function (limit)
{
	if(this.scroll_div_getWidth==0) this.scroll_div_getWidth = _div_getWidth(this.scroll_div_id_in);
	if(this.scroll_div_out_width==0) this.scroll_div_out_width = _div_getWidth(this.scroll_div_id);

	this.scroll_limit = this.scroll_position - limit;

	if(this.scroll_position <= 0)
	{
		this.scroll_to(this.scroll_div_getWidth - this.scroll_div_out_width);
	}
	else
	{
		if(!this.scroll_is_scrolling)
		{
			this.scroll_position_saved = this.scroll_position;
			this.scroll_cs = this.scroll_is;
	
			//alert(scroll_max_position);
	
			this.scroll_is_scrolling = true;
	
			this.scroll_timer = setInterval( "class_hscroller.instances["+this.id+"].scroll_left()", 20);
			this.scroll_is_scrolling = true;
	
		}
	}
}


/* initialise un scroll */
class_hscroller.prototype.scroll_start_right = function (limit)
{
	//alert('scroll_start_right');
	if(this.scroll_div_getWidth==0) this.scroll_div_getWidth = _div_getWidth(this.scroll_div_id_in);
	if(this.scroll_div_out_width==0) this.scroll_div_out_width = _div_getWidth(this.scroll_div_id);

	this.scroll_limit = this.scroll_position + limit;

	if(this.scroll_position >= (this.scroll_div_getWidth - this.scroll_div_out_width))
	{
		this.scroll_to(0);
	}
	else
	{
		//alert("scroll_div_getWidth="+this.scroll_div_getWidth);
		
		this.scroll_cs = this.scroll_is;
		this.scroll_position_saved = this.scroll_position;
	
		if(!this.scroll_is_scrolling)
		{
			//alert(scroll_max_position);
	
			this.scroll_is_scrolling = true;
	
			this.scroll_timer = setInterval( "class_hscroller.instances["+this.id+"].scroll_right()", 20);
			this.scroll_is_scrolling = true;		
		}
		else
		{
			//alert('b');
		}
	}
}

/* arrête le scroll */
class_hscroller.prototype.scroll_stop = function ()
{
	if(this.scroll_position<0) this.scroll_position = 0;
	if(this.scroll_position>this.scroll_div_getWidth) this.scroll_position = this.scroll_div_getWidth;
	clearInterval(this.scroll_timer);
	this.scroll_is_scrolling = false;
	hscroller_obj = null;
}


/* scroll à droite */
class_hscroller.prototype.scroll_right = function ()
{	
	var id = this.scroll_div_id;

	//this.debug();
	//div_setContent("debug","scroll_div_getWidth="+this.scroll_div_getWidth);

	if (document.getElementById) 
    {
       var cdiv = document.getElementById(id);
       if (cdiv) 
       {
       		if(this.scroll_position == this.scroll_limit)
       		{
       			this.scroll_stop();
				return;
       		}
       		
       		if(this.scroll_position > this.scroll_limit)
       		{
       			this.scroll_to(this.scroll_limit);
       			this.scroll_stop();
				return;
       		}
       					
			if(this.scroll_position >= (this.scroll_div_getWidth - this.scroll_div_out_width))
			{
				//alert("stoppe scroll_right car scroll_position="+this.scroll_position);
				this.scroll_stop();
				return;
            }
            

            
        	this.scroll_position += this.scroll_cs;
            cdiv.scrollLeft=this.scroll_position;
  
			if((this.scroll_position - this.scroll_position_saved) > (this.scroll_limit-this.scroll_position_saved)/2)
            {
            	this.scroll_cs -= this.scroll_pa;
            }
            else
            {
            	this.scroll_cs += this.scroll_pa;
            }

			if(this.scroll_cs <= this.scroll_ms)
			{
				this.scroll_cs = this.scroll_ms;
				//this.scroll_stop();
				return;
            }
            
            
            //div_setContent("debug","scroll_cs="+this.scroll_cs);
        }
        else
        {
        	//alert('erreur dans scroll_right : id non défini');
        }
    }
}


/* scroll à gauche */
class_hscroller.prototype.scroll_left = function ()
{
	var id = this.scroll_div_id;

	//this.debug();

	//alert('erreur dans scroll_left');

	if (document.getElementById) 
    {
       var cdiv = document.getElementById(id);
       if (cdiv) 
        {
        
			if(this.scroll_position == this.scroll_limit)
            {
            	
            	this.scroll_stop();
            	return;            
            }
            
            if(this.scroll_position < this.scroll_limit)
            {
            	this.scroll_to(this.scroll_limit);
            	this.scroll_stop();
            	return;            
            }
            
            if(this.scroll_cs <= 0)
            {
        
            	
            	this.scroll_stop();
            	return;
            }
            
            if(this.scroll_position < 0)
            {
            	//alert("stoppe scroll_left car scroll_position="+this.scroll_position);
            	this.scroll_stop();
            	return;            
            }
            
       		this.scroll_position -= this.scroll_cs;
           	cdiv.scrollLeft = this.scroll_position;
            
           	this.scroll_cs += this.scroll_pa;

        }
        else
        {
        	//alert('erreur dans scroll_left : id non défini');
        }        
    }
}

