var isMinNS4 = (document.layers) ? true : false;
var isMinIE4 = (document.all) ? true : false;
var isStd = (document.getElementById) ? true : false;
var OP=(navigator.userAgent.indexOf('Opera')!=-1)?true:false;

var DescScroller;						/* Description du scroller */
var scrollerInterval = 20;	/* intervalle de mise à jour du scroller */

var OPclipLeft;
var OPclipRight;
var OPclipTop;
var OPclipBottom;

var NSorigWidth;
var NSorigHeight;

if (isMinNS4) 
{
  NSorigWidth  = window.innerWidth;
  NSorigHeight = window.innerHeight;
	window.onresize = NSscrollerReload;
}

function Scroller(x, y, width, height, border, padding) 
{
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
  this.border = border;
  this.padding = padding;
  this.items = new Array();
	this.liens = new Array();
  this.created = false;
  this.fgColor = "#000000";
  this.bgColor = "#ffffff";
  this.bdColor = "#000000";
  this.fontFace = "Arial,Helvetica";
  this.fontSize = "2";
  this.speed = 50;
  this.pauseTime = 2000;
  this.setColors = scrollerSetColors;
  this.setFont = scrollerSetFont;
	this.setBehaviour = scrollerSetBehaviour;
  this.setSpeed = scrollerSetSpeed;
  this.setPause = scrollersetPause;
  this.addItem = scrollerAddItem;
  this.create = scrollerCreate;
  this.show = scrollerShow;
  this.hide = scrollerHide;
  this.setzIndex = scrollerSetzIndex;
}

function scrollerSetColors(fgcolor, bgcolor, bdcolor) 
{
  this.fgColor = fgcolor;
  this.bgColor = bgcolor;
	this.bdColor = bdcolor;
}

function scrollerSetFont(face, size) 
{
  this.fontFace = face;
  this.fontSize = size;
}

function scrollerSetBehaviour(arret)
{
	this.arret = arret;
}

function scrollerSetSpeed(pps) 
{
  this.speed = pps;
}

function scrollersetPause(ms) 
{
  this.pauseTime = ms;
}

function scrollerAddItem(str,lien) 
{
  this.items[this.items.length] = str;
	if (lien) this.liens[this.liens.length] = lien;
	else this.liens[this.liens.length] = "";
}

function scrollerCreate() 
{
  var start, end;
  var str;
  var i, j;
  var x, y;

  setInterval('scrollerGo()', scrollerInterval);
  this.created = true;

  this.items[this.items.length] = this.items[0];
  this.liens[this.liens.length] = this.liens[0];

  if (isMinNS4) 
	{
  	start  = '<table border=0 cellpadding='+(this.padding + this.border)+' cellspacing=0 width=' + this.width + ' height=' + this.height + '>';
		start += '<tr><td><font style="color:'+this.fgColor+';font-family:'+this.fontFace+';font-size:'+this.fontSize+'px;">';
  	end    = '</font></td></tr></table>';
		
		str  = '<layer name="scroller_baseLayer" left='+this.x+' top='+this.y+' width='+this.width+' height='+this.height+'px bgcolor='+this.bdColor+' overflow=hidden visibility=hidden';
		if (this.arret) str += ' onmouseover="scrollerStop()" onmouseout="scrollerStart()">';
		else str += '>';
		str += '<layer name="scroller_scrollLayer" width='+this.width+' bgcolor='+this.bgColor+'>';
		var hauteur = 0;
    for (j = 0; j < this.items.length; j++) 
		{
      str += '<layer name="scroller_itemLayers' + j + '" top:'+hauteur+' width:' + this.width + 'px; height='+this.height+'px>';
      str += start + this.items[j] +  end
      str += '</layer>';
			hauteur += this.height;
    }
    str += '</layer></layer>';
		document.write(str);
  }
	else
	{
  	start  = '<table border=0 cellpadding='+(this.padding + this.border)+' cellspacing=0 width=' + this.width + ' height=' + this.height + '>';
		start += '<tr><td style="color:'+this.fgColor+';font-family:'+this.fontFace+';font-size:'+this.fontSize+'px;">';
  	end    = '</td></tr></table>';

    str  = '<div id="scroller_baseLayer" style="position:absolute; background-color:'+this.bdColor+'; width:' + this.width + 'px; height:' + this.height + 'px; overflow:hidden; visibility:hidden;"';
		if (this.arret) str += ' onmouseover="scrollerStop()" onmouseout="scrollerStart()">';
		else str += '>';
    str += '<div id="scroller_scrollLayer" style="position:absolute; background-color:'+this.bgColor+'; width:' + this.width + 'px; height:' + (this.height * this.items.length) + 'px; visibility:inherit;">';
    for (j = 0; j < this.items.length; j++) 
		{
      str += '<div id="scroller_itemLayers' + j + '" style="position:absolute; width:'+this.width+'px; height:' + this.height + 'px; visibility:inherit;';
      if (this.liens[j] == "") str+= 'cursor:default">'
			else str += 'cursor:pointer" onclick="document.location.href=\''+this.liens[j]+'\'">'
			str += start + this.items[j] + end
      str += '</div>';
    }
    str += '</div></div>';
		document.write(str);

  }

  this.baseLayer = getLayer("scroller_baseLayer");
  this.scrollLayer = getLayer("scroller_scrollLayer");
  this.itemLayers = new Array();
  for (j = 0; j < this.items.length; j++) 
	{
		this.itemLayers[j] = getLayer("scroller_itemLayers" + j);
	}

  moveLayerTo(this.baseLayer, this.x, this.y);
	clipLayer(this.baseLayer, 0, 0, this.width, this.height);
  moveLayerTo(this.scrollLayer, this.border, this.border);
	clipLayer(this.scrollLayer, 0, 0, this.width - 2 * this.border, this.height - 2 * this.border);
	if (OP)
	{
		OPclipTop = 0;
		OPclipRight = 0;
		OPclipBottom = this.width - 2 * this.border;
		OPclipLeft = this.height - 2 * this.border;
	}

  x = 0;
  y = 0;
  for (i = 0; i < this.items.length; i++) 
	{
    moveLayerTo(this.itemLayers[i], x, y);
		clipLayer(this.itemLayers[i], 0, 0, this.width, this.height);
    y += this.height;
  }

  this.stopped = false;
  this.currentY = 0;
  this.stepY = this.speed / (1000 / scrollerInterval);
  this.stepY = Math.min(this.height, this.stepY);
  this.nextY = this.height;
  this.maxY = this.height * (this.items.length-1);
  this.paused = true;
  this.counter = 0;

	DescScroller = this;
	
	if (isMinNS4)
	{
  	for(var l=0;l<document.layers.length;l++)
  	{
  		if (document.layers[l].name == "scroller_baseLayer")
  		{
  			var lo=document.layers[l].layers[0];
  			for(var sl=0;sl<lo.layers.length;sl++)
  			{
					if (this.liens[sl] != "")
					{ 
  					var slo=lo.layers[sl];
  					slo.document.onmouseup=NSscrollerClick;
  					slo.document.TCode="NSscrollerExecURL('"+this.liens[sl]+"')"
					}
				}
  		}
		}  		
	}
		
	scrollerShow();
}

function scrollerShow() 
{
  if (this.created)
	{
  	if (isMinNS4) this.baseLayer.visibility = "show";
  	else this.baseLayer.style.visibility = "visible";
	}
}

function scrollerHide() 
{
  if (this.created)
	{
  	if (isMinNS4) this.baseLayer.visibility = "hide";
  	else this.baseLayer.style.visibility = "hidden";
	}

}

function scrollerSetzIndex(z) 
{
  if (this.created)
	{
  	if (isMinNS4) this.baseLayer.zIndex = z;
  	else this.baseLayer.style.zIndex = z;
	}
}

function scrollerStart() 
{
  DescScroller.stopped = false;
}

function scrollerStop() 
{
  DescScroller.stopped = true;
}

function scrollerGo() 
{
  if (DescScroller.stopped);
  else if (DescScroller.paused) 
	{
    DescScroller.counter += scrollerInterval;
    if (DescScroller.counter > DescScroller.pauseTime) DescScroller.paused = false;
  }
  else 
	{
    DescScroller.currentY += DescScroller.stepY;

    if (DescScroller.currentY >= DescScroller.nextY) 
		{
      DescScroller.paused = true;
      DescScroller.counter = 0;
      DescScroller.currentY = DescScroller.nextY;
      DescScroller.nextY += DescScroller.height;
    }

    else if (DescScroller.currentY >= DescScroller.maxY) 
		{
      DescScroller.currentY = 0;
      DescScroller.nextY = DescScroller.height;
    }
		var layer = DescScroller.scrollLayer
  	var dx = getClipLeft(layer);
		var dy = getClipTop(layer) - Math.round(DescScroller.currentY);
  	
  	var cl = getClipLeft(layer);
  	var ct = getClipTop(layer);
  	var cr = getClipRight(layer);
  	var cb = getClipBottom(layer);

  	clipLayer(layer, cl - dx, ct - dy, cr - dx, cb - dy);
		if (isMinNS4) layer.moveBy(dx, dy);
  	else if (isMinIE4)
		{
    	layer.style.pixelLeft += dx;
    	layer.style.pixelTop  += dy;
  	}
		else
		{
    	layer.style.left = (parseInt(layer.style.Left) + dx) + "px";
    	layer.style.top  = (parseInt(layer.style.top) + dy) + "px";
  	}
  }
}

function NSscrollerReload() 
{
  if ((NSorigWidth != window.innerWidth) || (NSorigHeight != window.innerHeight))
  	window.location.href = window.location.href;
}

function moveLayerTo(layer, x, y) 
{
	if (isStd)
	{
    layer.style.left = x + "px";
    layer.style.top  = y + "px";		
	}
  else if (isMinNS4) layer.moveTo(x, y);
  else if (isMinIE4) 
	{
    layer.style.left = x;
    layer.style.top  = y;
  }
}

function NSfindLayer(name, doc) 
{
  var i, layer;

  for (i = 0; i < doc.layers.length; i++) 
	{
    layer = doc.layers[i];
    if (layer.name == name) return layer;
    if (layer.document.layers.length > 0) 
		{
      layer = NSfindLayer(name, layer.document);
      if (layer != null) return layer;
    }
  }

  return null;
}

function getLayer(name) 
{
	if (isStd) return eval('document.getElementById("'+name+'")')
  else if (isMinNS4) return NSfindLayer(name, document);
  else if (isMinIE4) return eval(name);
  return null;
}

function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) 
{
  if (isMinNS4) 
	{
    layer.clip.left   = clipleft;
    layer.clip.top    = cliptop;
    layer.clip.right  = clipright;
    layer.clip.bottom = clipbottom;
  }
  else 
	{
		layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
		if (OP)
		{
			OPclipTop = cliptop;
			OPclipRight = clipright;
			OPclipBottom = clipbottom;
			OpclipLeft = clipleft;
		}
	}
}

function getClipLeft(layer) 
{
	if (OP) return(OPclipLeft);
  else if (isMinNS4) return(layer.clip.left);
  else 
	{
    var str =  layer.style.clip;
    if (!str) return(0);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[3]);
  }
  return(-1);
}

function getClipTop(layer) 
{
	if (OP) return(OPclipTop);
  else if (isMinNS4) return(layer.clip.top);
  else 
	{
    var str =  layer.style.clip;
    if (!str) return(0);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[0]);
  }
  return(-1);
}

function getClipRight(layer) 
{
 	if (OP) return(OPclipRight);
  else if (isMinNS4) return(layer.clip.right);
  else 
	{
    var str =  layer.style.clip;
    if (!str) return(layer.style.pixelWidth);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1]);
  }
  return(-1);
}

function getClipBottom(layer) 
{
	if (OP) return(OPclipBottom);
  else if (isMinNS4) return(layer.clip.bottom);
  else 
	{
    var str =  layer.style.clip;
    if (!str) return(layer.style.pixelHeight);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2]);
  }
  return(-1);
}

// Donne les différentes valeurs du clip pour IE
function getIEClipValues(str) {

  var clip = new Array();
  var i;

  i = str.indexOf("(");
  clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  return(clip);
}

// fonction appelée sur click dans un menu
function NSscrollerClick(e)
{
	eval(this.TCode);
}

function NSscrollerExecURL(url)
{
	document.location.href=url;
}

