function getObj(name)
{
  if (document.getElementById)
    return document.getElementById(name);
  else if (document.all)
    return document.all[name];
  else if (document.layers)
    return getObjNN4(document,name);
  else
    return null;
}
function getStyle(name)
{
  if (document.getElementById)
    return document.getElementById(name).style;
  else if (document.all)
    return document.all[name].style;
  else if (document.layers)
    return getObjNN4(document,name);
  else
    return null;
}      
function getObjNN4(obj,name)
{
  var x = obj.layers;
  var foundLayer;
  for (var i=0;i<x.length;i++)
    {
      if (x[i].id == name)
	foundLayer = x[i];
      else if (x[i].layers.length)
	var tmp = getObjNN4(x[i],name);
      if (tmp) foundLayer = tmp;
    }
  return foundLayer;
}
function setHTML(obj, text)
{
  if (!obj) return;
  if (document.getElementById || document.all) 
    { 
      obj.innerHTML = text; 
    }
  else if (document.layers)
    {
      obj.document.write(text); 
      obj.document.close(); 
    }
}

var isOpera = false;
if (navigator.userAgent.indexOf("Opera")>=0) isOpera = true;

function setVisible(name, state)
{
  var s = getStyle(name);
  if (!s) return;
  s.visibility = state;
}

function hideAppletDiv(name)
{
  var s = getStyle(name);
  // opera can't handle hiding the applet by moving it   
  if (!isOpera)
    {
      var rx = 0-findPosX(getObj(name))-parseInt(s.width)+2;
      var ry = 0-findPosY(getObj(name))-parseInt(s.height)+2;
      s.left = rx+"px";
      s.top =  ry+"px";
    }
  else s.visibility = "hidden";
}

function showAppletDiv(name)
{
   var s = getStyle(name);
  // opera can't handle hiding the applet by moving it   
  if (!isOpera)
    {
      s.left = '0px';
      s.top = '0px';
    }
  else s.visibility = "visible";
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }


function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

// fading images in and out
//global object
var fader_obj = { 'clock' : null, 'count' : 1 }
/*******************************************************/


//crossfade setup function
function crossfade()
{
	//if the timer is not already going
	if(fader_obj.clock == null)
	{
		//copy the image object 
		fader_obj.obj = arguments[0];
		
		//copy the image src argument 
		fader_obj.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof fader_obj.obj.style.opacity != 'undefined')
		{
			fader_obj.type = 'w3c';
		}
		else if(typeof fader_obj.obj.style.MozOpacity != 'undefined')
		{
			fader_obj.type = 'moz';
		}
		else if(typeof fader_obj.obj.style.KhtmlOpacity != 'undefined')
		{
			fader_obj.type = 'khtml';
		}
		else if(typeof fader_obj.obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			fader_obj.type = (fader_obj.obj.filters.length > 0 && typeof fader_obj.obj.filters.alpha == 'object' && typeof fader_obj.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			fader_obj.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			fader_obj.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(fader_obj.type != 'none')
		{
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			fader_obj.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

			//set positioning classname
			fader_obj.newimg.className = 'idupe';
			
			//set src to new image src
			fader_obj.newimg.src = fader_obj.src

			//move it to superimpose original image
			fader_obj.newimg.style.left = findPosX(fader_obj.obj) + 'px';
			fader_obj.newimg.style.top = findPosY(fader_obj.obj) + 'px';
			
			//copy and convert fade duration argument 
			fader_obj.length = parseFloat(arguments[2], 10) * 1000;
			
			//create fade resolution argument as 20 steps per transition
			fader_obj.resolution = parseFloat(arguments[2], 10) * 20;
			
			//start the timer
			fader_obj.clock = setInterval('fader_obj.crossfade()', fader_obj.length/fader_obj.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			fader_obj.obj.src = fader_obj.src;
		}
		
	}
};


//crossfade timer function
fader_obj.crossfade = function()
{
	//decrease the counter on a linear scale
	fader_obj.count -= (1 / fader_obj.resolution);
	
	//if the counter has reached the bottom
	if(fader_obj.count < (1 / fader_obj.resolution))
	{
		//clear the timer
		clearInterval(fader_obj.clock);
		fader_obj.clock = null;
		
		//reset the counter
		fader_obj.count = 1;
		
		//set the original image to the src of the new image
		fader_obj.obj.src = fader_obj.src;
	}
	
	//set new opacity value on both elements
	var bkOpacity = 1; // fader_obj.count?
	var fgOpacity = 1-fader_obj.count;
	//using whatever method is supported
	switch(fader_obj.type)
	{
	  
	case 'ie' :
	  fader_obj.obj.filters.alpha.opacity = (bkOpacity * 100);
	  fader_obj.newimg.filters.alpha.opacity = (fgOpacity * 100);
	  break;
	  
	case 'khtml' :
	  fader_obj.obj.style.KhtmlOpacity = bkOpacity;
	  fader_obj.newimg.style.KhtmlOpacity = fgOpacity;
	  break;
	  
	case 'moz' : 
	  //restrict max opacity to prevent a visual popping effect in firefox
	  fader_obj.obj.style.MozOpacity = (bkOpacity == 1 ? 0.9999999 : bkOpacity);
	  fader_obj.newimg.style.MozOpacity = fgOpacity;
	  break;
			
	default : 
	  //restrict max opacity to prevent a visual popping effect in firefox
	  fader_obj.obj.style.opacity = (bkOpacity == 1 ? 0.9999999 : bkOpacity);
	  fader_obj.newimg.style.opacity = fgOpacity;
	}
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	fader_obj.newimg.style.visibility = 'visible';
	
	//keep new image in position with original image
	//in case text size changes mid transition or something
	fader_obj.newimg.style.left = findPosX(fader_obj.obj) + 'px';
	fader_obj.newimg.style.top = findPosY(fader_obj.obj) + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	if(fader_obj.count == 1)
	{
		//remove the duplicate image
		fader_obj.newimg.parentNode.removeChild(fader_obj.newimg);
	}
};

