// If it is not IE, we assume that the browser is NS.
var IE = (document.all?true:false);
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) {
  document.captureEvents(Event.MOUSEMOVE)
  document.onmousemove = findMousePosition;
}

xMousePos = 0;
yMousePos = 0;

if (!this.originalHeight)
	this.originalHeight = new Object();

if (!this.currentHeight)
	this.currentHeight = new Object();

if (!this.folded)
	this.folded = new Object();

if (!this.currentDirection)
	this.currentDirection = 'down';

if (!this.tooltip)
	this.tooltip = new Object();

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
//Generic Animation Step Value Generator By www.hesido.com 
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
    return Math.ceil(stepp) 
} 

function doHeightChangeMem(elem,startHeight,endHeight,steps,intervals,powr) { 
//Height changer with Memory by www.hesido.com
	var theId = elem.id;
  if (elem.heightChangeMemInt)
		window.clearInterval(elem.heightChangeMemInt);
  var actStep = 0;
  elem.heightChangeMemInt = window.setInterval(
    function() 
    { 
      this.currentHeight[theId] = easeInOut(startHeight,endHeight,steps,actStep,powr);
      elem.style.height = this.currentHeight[theId] + "px"; 
      actStep++;
      if (actStep > steps) {
        window.clearInterval(elem.heightChangeMemInt);
        return;
      }
    } 
	,intervals)
}

function doOpacityChange(elem,startOpacity,endOpacity,steps,intervals,powr) { 
	var theId = elem.id;
	var opacity = 0;
  if (elem.opacityChangeMemInt)
		window.clearInterval(elem.opacityChangeMemInt);
  var actStep = 0;
  elem.opacityChangeMemInt = window.setInterval(
	function() 
	{ 
	  opacity = easeInOut(startOpacity,endOpacity,steps,actStep,powr);
	  elem.style.opacity = opacity / 100;
	  elem.style.filter = "alpha(opacity=" + opacity + ")";
	  actStep++;
	  if (actStep > steps)
	  {
	    window.clearInterval(elem.opacityChangeMemInt);
      if (elem.style.opacity == 0)
        document.getElementById(theId).style.display = "none";
      return;
	  }
	} 
	,intervals)
}

function setHeight(theId, theHeight)
{
  var elem = document.getElementById(theId);
	if (!elem)
		alert("ERROR: Couldn't find an element with id = " + theId);

	// If original height for the element of this id is not created, create and assign it
	if (!this.originalHeight[theId])
	{
		if (elem.style.display == 'none')
		    elem.style.display = ''
    this.originalHeight[theId] = elem.offsetHeight;
		if (theHeight)
		{
			if (theHeight > 0)
			{
				elem.style.height = theHeight + 'px';
				if (!this.currentHeight[theId])
					this.currentHeight[theId] = theHeight;
			}
			else
				// Resize the element to 1px after storing the value of the element's height.
				// Note: IE doesn't change the height to 0px, thus we set it to 1px.
				elem.style.height = '1px';
		}
		else
			// Resize the element to 1px after storing the value of the element's height.
			// Note: IE doesn't change the height to 0px, thus we set it to 1px.
			elem.style.height = '1px';
	}
	// Now we can unhide the element
    //elem.style.display = '';
}

function expand(theId, theHeight) 
{ 
	this.currentDirection = 'down';
	var elem = document.getElementById(theId);
	if (!elem)
	{
		alert("ERROR: Couldn't find an element with id = " + theId);
		return;
	}
	if (!this.originalHeight[theId])
	{
		alert("Before calling expand you need to set the height of element. Use setHeight(id, optional height)");
		return;
	}
	if (!this.currentHeight[theId])
		currentHeight[theId] = 1;
	if (theHeight)
	{
		if (theHeight > 0 && theHeight > this.currentHeight[theId])
		{
			doHeightChangeMem(elem,this.currentHeight[theId],theHeight,10,10,0.5); 
		}
		else
		{
			doHeightChangeMem(elem,this.currentHeight[theId],this.originalHeight[theId],10,10,0.5);
			return;
		}
	}
	else
	{
		doHeightChangeMem(elem,this.currentHeight[theId],this.originalHeight[theId],10,10,0.5); 
	}
}

function collapse(theId, theHeight) 
{ 
	this.currentDirection = 'up';
	var elem = document.getElementById(theId);
	if (!elem)
	{
		alert("ERROR: Couldn't find an element with id = " + theId);
		return;
	}
	if (!this.originalHeight[theId])
	{
		alert("Before calling collapse you need to set the height of element. Use setHeight(id, optional height)");
		return;
	}
	if (!this.currentHeight[theId])
		this.currentHeight[theId] = this.originalHeight[theId];
	if (theHeight != null)
	{
		if (theHeight > 0 && theHeight < this.originalHeight)
		{
		    doHeightChangeMem(elem,this.currentHeight[theId],theHeight,10,10,0.5); 
		}
		else
		{
			doHeightChangeMem(elem,this.currentHeight[theId],1,10,10,0.5); 
			return;
		}
	}
	else
	{
		doHeightChangeMem(elem,this.currentHeight[theId],1,10,10,0.5); 
	}
}

function slide(theId, startingDirection, minHeight, maxHeight)
{
	if (minHeight != null)
	{
		if (minHeight > 0)
			minHeight = minHeight;
		else
			minHeight = null;
	}
	if (maxHeight != null)
	{
		if (maxHeight > 0)
			maxHeight = maxHeight;
		else
			maxHeight = null;
	}
	if (!this.folded[theId])
	{
		if (startingDirection == 'up')
		{
			collapse(theId, minHeight);
			this.folded[theId] = 0;
		}
		else if (startingDirection == 'down')
		{
			expand(theId, maxHeight);
			this.folded[theId] = 1;
		}
		else
			alert("slide function needs a startingDirection ('up' or 'down')");
	}
	else if(this.folded[theId] == 1)
	{
		collapse(theId, minHeight);
		this.folded[theId] = 0;
	}
	else
	{
		expand(theId, maxHeight);
		this.folded[theId] = 1;
	}
}

function fadeIn(theId)
{
	var elem = document.getElementById(theId);
	doOpacityChange(elem,0,100,10,20,0.5);
}

function fadeOut(theId)
{
	var elem = document.getElementById(theId);
	doOpacityChange(elem,100,0,10,20,0.5);
}

function fade(theId, startingDirection)
{
	if (!this.folded[theId])
	{
		if (startingDirection == 'in')
		{
			fadeIn(theId);
			this.folded[theId] = 0;
		}
		else if (startingDirection == 'out')
		{
			fadeOut(theId);
			this.folded[theId] = 1;
		}
		else
			alert("fade function needs a startingDirection ('in' or 'out')");
	}
	else if(this.folded[theId] == 1)
	{
		fadeIn(theId);
		this.folded[theId] = 0;
	}
	else
	{
		fadeOut(theId);
		this.folded[theId] = 1;
	}
}


// This is a modified script from http://blog.firetree.net/2005/07/04/javascript-find-position/
function findMousePosition(theEvent01)
{
	if (!theEvent01) var theEvent01 = window.event;
	if (theEvent01.pageX || theEvent01.pageY)
	{
		xMousePos = theEvent01.pageX;
		yMousePos = theEvent01.pageY;
	}
	else if (theEvent01.clientX || theEvent01.clientY) 	{
		xMousePos = theEvent01.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		yMousePos = theEvent01.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
}

function showTooltip(theId, tooltipId, method)
{
  //added by Etienne to avoid the flickering of the top menu (replace the findMousePosition function for IE)
  //-----------------
  if (IE) {
	  if (!theEvent01) var theEvent01 = window.event;
		xMousePos = theEvent01.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		yMousePos = theEvent01.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
  //-----------------

	var elem = document.getElementById(theId);
	var tooltip = document.getElementById(tooltipId);
	var left = xMousePos;
	var top = yMousePos;

	tooltip.style.position = "absolute";
	if (theId == 'pcProduct') 
		left = 560;
	//alert('ID: ' + theId + ' Left: ' +  left + ' TOP : ' + top)
	tooltip.style.left = left + "px";
	tooltip.style.top = top + 10 + "px";


	if (method == 'fade')
	{
		tooltip.style.opacity = 0;
		tooltip.style.filter = "alpha(opacity=0)";
		tooltip.style.display = "";
		fadeIn(tooltipId);
	}
    else if (method == 'slide')
    {
		setHeight(tooltipId);
		tooltip.style.display = "";
		slide(tooltipId,'down');
    }
	else
	{
		alert("The method '" + method + "' for showTooltip is not valid");
	}
}

function hideTooltip(tooltipId, method)
{
	if (method == 'fade')
		fadeOut(tooltipId);
	else if(method == 'slide')
		slide(tooltipId,'down');
	else
		alert("The method '" + method + "' for hideTooltip is not valid");
}