var scroller_obj = null;
var scroller_id=1;

function class_scroller(div_id)
{
	this.id = scroller_id++;
	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 = 0;								/* position courante */
	this.scroll_is = 20.0;				/* vitesse initiale */
	this.scroll_cs = 20.0;				/* vitesse courante */
	this.scroll_pa = -0.5;				/* pseudo accélération */
	this.scroll_div_width = 0;			/* largeur du bloc */
	this.scroll_div_out_width = 0;		/* largeur du bloc encadrant */
	this.scroll_is_scrolling = false;	/* variable d'état - scrolling en cours ? */
	this.scroll_timer = null;			/* timer */

	this.autoscrolling = false;			/* scrolling automatique */
}

class_scroller.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_scroller.prototype.scroll_start_auto = function ()
{
	if(this.scroll_div_width==0) this.scroll_div_width = _div_width(this.scroll_div_id_in);
	if(this.scroll_div_out_width==0) this.scroll_div_out_width = _div_width(this.scroll_div_id);

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

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

		//alert(scroll_max_position);

		this.autoscrolling = true;

		scroller_obj = this;

		this.scroll_timer = setInterval( "scroller_obj.scroll_auto()", 30);
	}
}


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

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

	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_width - this.scroll_div_out_width ))
			{
				this.scroll_cs = -1.0;
            }
            
        	this.scroll_position += this.scroll_cs;
            cdiv.scrollLeft=this.scroll_position;

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

/* initialise un scroll */
class_scroller.prototype.scroll_start_left = function ()
{
	if(this.scroll_div_width==0) this.scroll_div_width = div_width(this.scroll_div_id_in);
	if(this.scroll_div_out_width==0) this.scroll_div_out_width = _div_width(this.scroll_div_id);

	if(!this.scroll_is_scrolling && scroller_obj==null)
	{
		this.scroll_cs = this.scroll_is;

		//alert(scroll_max_position);

		this.scroll_is_scrolling = true;

		scroller_obj = this;

		this.scroll_timer = setInterval( "scroller_obj.scroll_left()", 1);
		this.scroll_is_scrolling = true;

	}
}


/* initialise un scroll */
class_scroller.prototype.scroll_start_right = function ()
{

	scroller_a.debug();

	if(this.scroll_div_width==0) this.scroll_div_width = div_width(this.scroll_div_id_in);
	if(this.scroll_div_out_width==0) this.scroll_div_out_width = _div_width(this.scroll_div_id);
	
	//alert("scroll_div_width="+this.scroll_div_width);
	
	this.scroll_cs = this.scroll_is;

	if(!this.scroll_is_scrolling && scroller_obj==null)
	{
		//alert(scroll_max_position);

		this.scroll_is_scrolling = true;

		scroller_obj = this;

		this.scroll_timer = setInterval( "scroller_obj.scroll_right()", 1);
		this.scroll_is_scrolling = true;		
	}
}

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


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

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

	if (document.getElementById) 
    {
       var cdiv = document.getElementById(id);
       if (cdiv) 
       {
			if(this.scroll_cs <= 0)
			{
				//alert("stoppe scroll_right car scroll_cs="+this.scroll_cs);
				this.scroll_stop();
				return;
            }

			if(this.scroll_position >= (this.scroll_div_width - 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;
            
            this.scroll_cs += this.scroll_pa;
            //div_setContent("debug","scroll_cs="+this.scroll_cs);
        }
        else
        {
        	//alert('erreur dans scroll_right : id non défini');
        }
    }
}


/* scroll à gauche */
class_scroller.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_cs <= 0)
            {
            	//alert("stoppe scroll_left car scroll_cs="+this.scroll_cs);
            	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');
        }        
    }
}
