var list; // global list variable cache
var tickerObj; // global tickerObj cache
var hex = 255;
var allInstance = new Array();

function fadeInfo(divId, curRec) {
  curtickerObj = document.getElementById(divId);
  opacityGoingUp = allInstance[divId]["opacityGoingUp"];
  opacityValue = allInstance[divId]["opacityValue"];
  delayLect= allInstance[divId]["delayLect"];
  isOver = allInstance[divId]["isOver"];
  if (curtickerObj)
  { 
	if (isOver==true)
  	{
     		curtickerObj.style.opacity = 100/100;
      		curtickerObj.style.filter =  "alpha(opacity="+100+")";
     		setTimeout("fadeInfo('" + divId + "',"+allInstance[divId]["curRow"]+1+")", 1000);
  	}
	else
	{
		if (opacityValue < 100 && opacityGoingUp) 
		{
			opacityValue+=1;
	      	curtickerObj.style.opacity = opacityValue/100;
	            curtickerObj.style.filter =  "alpha(opacity="+opacityValue+")";
	      	setTimeout("fadeInfo('" + divId + "',"+allInstance[divId]["curRow"]+")", fadeSpeed); 
	    	}
	    	else
	    	{
			if (opacityValue < 101 && opacityGoingUp) 
			{
				opacityValue+=1;
				curtickerObj.style.filter = "";
		      	setTimeout("fadeInfo('" + divId + "',"+allInstance[divId]["curRow"]+")", delayLect*1000); 
			}
			else
			{
				if (opacityValue > 100) opacityGoingUp = false;
		      	if (opacityValue > 0 && !opacityGoingUp ) 
				{
			  		opacityValue-=1;
					curtickerObj.style.opacity = opacityValue/100;
			            curtickerObj.style.filter =  "alpha(opacity="+opacityValue+")";
		        		setTimeout("fadeInfo('" + divId + "',"+allInstance[divId]["curRow"]+")", fadeSpeed); 
		      	} 
				else 
				{
					opacityGoingUp = true;
					curRec2 = allInstance[divId]["curRow"]+1;
					window.setTimeout("run('" + divId + "', " + curRec2+ ")", 10);
				}
			}
	    	}
	}
	  allInstance[divId]["opacityGoingUp"]=opacityGoingUp;
	  allInstance[divId]["opacityValue"]=opacityValue;
  }
}
function goDown(e)
{
	src=getEventSrc(e);
        src2=src.parentNode.previousSibling;
	if (allInstance[src2.id])
	{
	  curtickerObj= document.getElementById(src2.id);
	  list = curtickerObj.childNodes;

	   val = allInstance[src2.id]["curRow"];

	   list[val].style.display = "none";
	   if (val<list.length-1)
		val++;
	   else
	   {
		val= 0;
	   }
	   list[val].style.display = "block";
	   //document.getElementById("info"+src2.id).innerHTML = 	allInstance[src2.id]["curRow"]+" vers " +val;
	   allInstance[src2.id]["curRow"]=val;
	   allInstance[src2.id]["isOver"]=true;
	   document.getElementById(src2.id+"_pause").className="action scrollplay";
	}
}
function goUp(e)
{
	src=getEventSrc(e);
        src2=src.parentNode.previousSibling;
	if (allInstance[src2.id])
	{
	  curtickerObj= document.getElementById(src2.id);
	  list = curtickerObj.childNodes;

	   val = allInstance[src2.id]["curRow"];

	   list[val].style.display = "none";
	   if (val>0)
		val--;
	   else
	   {
		val= list.length-1;
	   }
	   list[val].style.display = "block";
	   //document.getElementById("info"+src2.id).innerHTML = 	allInstance[src2.id]["curRow"]+" vers " +val;
	   allInstance[src2.id]["curRow"]=val;
	   allInstance[src2.id]["isOver"]=true;
	   document.getElementById(src2.id+"_pause").className="action scrollplay";
	}
}
function startStopFading(e)
{
	src=getEventSrc(e);
        src2=src.parentNode.previousSibling;
	if (allInstance[src2.id])
	{
	   if (allInstance[src2.id]["isOver"])
	   {
		allInstance[src2.id]["isOver"]=false;
		document.getElementById(src2.id+"_pause").className="action scrollpause";
	   }
	   else
	   {
	   	allInstance[src2.id]["isOver"]=true;
		document.getElementById(src2.id+"_pause").className="action scrollplay";
	   }
	}
}
function initialiseList(divId, speedLec) {
  delayLect = speedLec;
  curtickerObj= document.getElementById(divId);
  if(!curtickerObj)
    reportError("Could not find a div element with id \"" + divId + "\"");
  list = curtickerObj.childNodes;
  if(list.length <= 0)
    reportError("The div element \"" + divId + "\" does not have any children");
  for (var i=0; i<list.length; i++) {
    var node = list[i];
    if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
    {
              curtickerObj.removeChild(node);
    }
    node.style.display="none";
  }
  //setTimeout("printRun()", 1000); 
  allInstance[divId]=new Array();
  allInstance[divId]["opacityValue"]=0;
  allInstance[divId]["opacityGoingUp"]=true;
  allInstance[divId]["delayLect"]=speedLec;
  allInstance[divId]["isOver"]=false;
  allInstance[divId]["curRow"]=0;

  parcurtickerObj = document.createElement("div");
  parcurtickerObj.id = divId+"_actions";
  parcurtickerObj.className="actiondiv";

  nouveauDiv = document.createElement("input");
  nouveauDiv.type="button";
  nouveauDiv.id = divId+"_moins";
  nouveauDiv.className="action scrollmoins";
  parcurtickerObj.appendChild(nouveauDiv);
  if (nouveauDiv) {
	  addEvent(nouveauDiv, "click", goUp);
  }

  nouveauDiv = document.createElement("input");
  nouveauDiv.type="button";
  nouveauDiv.id = divId+"_pause";
  nouveauDiv.className="action scrollpause";
  parcurtickerObj.appendChild(nouveauDiv);

  if (nouveauDiv) {
	  addEvent(nouveauDiv, "click", startStopFading);
  }

  nouveauDiv = document.createElement("input");
  nouveauDiv.type="button";
  nouveauDiv.id = divId+"_plus";
  nouveauDiv.className="action scrollplus";
  parcurtickerObj.appendChild(nouveauDiv);

  if (nouveauDiv) {
	  addEvent(nouveauDiv, "click", goDown);
  }

  //curtickerObj.parentNode.appendChild(parcurtickerObj);
  //document.body.insertBefore(parcurtickerObj,curtickerObj);

  glocurtickerObj = document.createElement("div");
  glocurtickerObj.id = divId+"_global";
  glocurtickerObj.className="scrollbox";

  curtickerObj.parentNode.appendChild(glocurtickerObj);
  glocurtickerObj.appendChild(curtickerObj);
  glocurtickerObj.appendChild(parcurtickerObj);


  run(divId, 0);
}

function getEventSrc(e) {
 // get a reference to the IE/windows event object
 if (!e) e = window.event;

 // DOM-compliant name of event source property
 if (e.target)
   return e. target;
 // IE/windows name of event source property
 else if (e.srcElement)
   return e.srcElement;
}
function addEvent(objObject, strEventName, fnHandler) {
 // DOM-compliant way to add an event listener
 if (objObject.addEventListener)
   objObject.addEventListener(strEventName, fnHandler, false);
 // IE/windows way to add an event listener
 else if (objObject.attachEvent)
   objObject.attachEvent("on" + strEventName, fnHandler);
}
function run(divId, count) {

  curtickerObj= document.getElementById(divId);
  list = curtickerObj.childNodes;

  if(count == list.length)
    count = 0;

  //document.getElementById("info"+divId).innerHTML = 	allInstance[divId]["curRow"]+" vers " +count;
  allInstance[divId]["curRow"]=count;
  list[count].style.display = "block";
  if(count > 0)
    list[count-1].style.display = "none";
  else
    list[list.length-1].style.display = "none";
  count++;
  if(count == list.length)
    count = 0;
  fadeInfo(divId,count);
}
function reportError(error) {
  alert("The script could not run because you have errors:\n\n" + error);
  return false;
}

var fadeSpeed = 15; // fade speed, the lower the speed the faster the fade.  40 is normal.


function xstooltip_findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}


function xstooltip_findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


function xstooltip_show(tooltipId, parentId, posX, posY)
{
    it = document.getElementById(tooltipId);
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        //it.style.width = it.offsetWidth + 'px';
        //it.style.height = it.offsetHeight + 'px';
        
        img = parentId; 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0; 
        
        x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    it.style.position = 'absolute';
    it.style.display = ''; 
}
function xstooltip_hide(id)
{
    it = document.getElementById(id); 
    it.style.display = 'none'; 
}
