var myimages=new Array();
var loadedimg=0;
var opacTimer = new Array();


function preloadImg(addr)
{
   for (i=1; i < preloadImg.arguments.length; i++)
	{
	       myimages[loadedimg] = new Image();
	       myimages[loadedimg].src = addr + preloadImg.arguments[i];
               loadedimg++;
	}
}

function chgImg(id, img)
{
  if( document.images )
     document.images[id].src = img;
}


function chgImgBySelectForm(formId, textId, imgId, beg, end)
{
	form = self.document.forms[formId];
	
	if( form )
	  {
	  	imgName = beg + form.elements[textId].value + end;
		chgImg(imgId, imgName);
	  }
}



function chgImgSize(id, width, height)
{
  if( document.images )
    {
   	  document.images[id].width  = width;
   	  document.images[id].height = height;
    }
}

function clipObj( id, clLeft, clTop, clRight, clBot )
{
    obj  = document.getElementById(id);
    obj.style.clip = "rect(" + clTop + "px " + clRight + "px " + clBot + "px " + clLeft + "px)";    		
}



function setObjVisibility(id, visibility)
{
	obj  = document.getElementById(id);
	obj.style.visibility = visibility;
}


function setObjOpacity(id, opacity)
{
	obj  = document.getElementById(id);
        obj.style.filter = "alpha (opacity=" + opacity +")";
	obj.style.MozOpacity = opacity / 100;
	
	// alert(opacity);
}

function chgObjOpacity(divid, opacity, lastvalue, timeinc, inc)
{
	obj = document.getElementById(divid);
	opacity += inc;

	if( inc <= 0 )
	  {
	      if( opacity < lastvalue	)
		  opacity = lastvalue;
		
	  }
	else if( inc >= 0 )
	  {
	      if( opacity > lastvalue	)
		  opacity = lastvalue;
		
	  }
	else opacity = lastvalue;

	setObjOpacity(divid, opacity);
	
	if( opacity != lastvalue )
	   {
		clearTimeout( opacTimer[divid] );
		opacTimer[divid] = setTimeout( "chgObjOpacity( '" + divid + "', " + opacity + ", " + lastvalue + ", " + timeinc + ", " + inc + ")", 50 );
	   }
}


function setObjPosition(id, x, y)
{
    obj  = document.getElementById(id);
    obj.style.left = x;
    obj.style.top = y;    
}

function setObjClass(id, className)
{
	obj = document.getElementById(id);
	obj.className = className; 
}


function movObj(id, x, y)
{
    obj  = document.getElementById(id);
    obj.style.left = x;
    obj.style.top = y;    
}

var coords = new Array();

function getObjCoords(id)
{
	obj = document.getElementById(id);
	coords['x'] = parseInt(obj.style.left.replace("px", ""));
	coords['y'] = parseInt(obj.style.top.replace("px", ""));
}


// 
var movObjTimeout = new Array();

function movObjStop( id )
{
	if( movObjTimeout[id] )
		clearTimeout(movObjTimeout[id]);	
}

function movObjStart(id, minx, miny, maxx, maxy, incx, incy, timespeed)
{
	if( movObjTimeout[id] )
		clearTimeout(movObjTimeout[id]);

	if( !incx && !incy )
		return;

	getObjCoords(id);
	x = coords['x'];
	y = coords['y'];

	x += incx;
	y += incy;
	
	if( incx < 0 ) 
	  { 	
		if( x <= minx ) { x = minx; incx = 0; }
	  }
	else if( incx > 0 )
	  {
		if( x >= maxx ) { x = maxx; incx = 0; }
	  }

	if( incy < 0 ) 
	  { 	
		if( y <= miny ) { y = miny; incy = 0; }
	  }
	else if( incy > 0 )
	  {
		if( y > maxy ) { y = maxy; incy = 0; }
	  }

	movObj(id, x, y);
	movObjTimeout[id] = setTimeout( "movObjStart('" + id + "'," + minx + "," + miny + "," + maxx + "," + maxy + "," + incx + "," + incy + "," + timespeed + ")", timespeed );
}





function switchImg(id, last)
{
  if( document.images )
    {
       nom = document.images[id].src;
 
       fin = deb = nom.length - 5;
   
       while( deb >= 0 )
         {
            c = nom.charAt(deb);
            if( c < '0' || c > '9' )
                 break;
            deb--;
         }
      
       deb++; 
       
       part1 = nom.substring(0, deb);
       part2 = nom.substring(deb, fin+1);
       part3 = nom.substring(fin+1, nom.length);

       num = parseInt(part2) + 1;
       if( num > last )
          num = 1;
       
       document.images[id].src = part1 + num + part3;
    }
}


function addDiv( num, w, h, PW, line1 )
{
	l = num * w;
	x = Math.floor(l % PW);
	y = (Math.floor(l / PW)) * h;

	line = "<div style='position:absolute;top:" + y + ";left:" + x + ";'>\n" + line1 + "\n</div>\n";

	document.write(line);
}

function waitForImg(img)
{ 
    if( !img.complete )
      {
    	   setTimeout('waitForImg(img)', 2500);
      }
} 


function image_show(id, speed, cur)
{
        if( (count = image_show.arguments.length - 3) == 0 )
           return; 
	
	imglist = "'" + image_show.arguments[3] + "'";
	for( i = 1; i < count; i++ )
	  	imglist = imglist + ", '" + image_show.arguments[3+i] + "'";

	if( cur >= count ) 
	   cur = 1;
	   
	img = image_show.arguments[2 + cur];  // 2 correspond à 3 (arg. min)  - 1
	// Charge l'image puis met a jour...
	tmpimg     = new Image();
	tmpimg.src = img;
        
	chgImg(id, img);

	cur++;
        setTimeout( "image_show('" + id + "', '" + speed + "', " + cur + "," + imglist +  ")", speed );
}
