/*
 * Image Fade
 *
 */
var boxes = new Array();
var links;
var str_links = new Array();
var curr_box=-1;
var nxt_box=0;
var brwsr;
var int_id;

function fade_init()
{
  // Get all the grad5 box classes elements to use
  var divs = document.getElementById('prod_fade').getElementsByTagName('div');
	for (j = 0; j < divs.length; j++)
	{
		if (divs[j].className == "grad5box")
		{
			boxes.push(divs[j]);
		}
	}

	links = document.getElementById('prod_fade').getElementsByTagName('a');

  for (l=0; l<links.length; l+=3)
  {
		str_links.push(links[l].href);
  }
  for (l=0; l<links.length; l++)
	{
		links[l].href = str_links[0];
	}

	if (boxes.length > 0)
  {
  
  	if (boxes[0].style.MozOpacity!=null)
  	{
   	 	brwsr = 'moz';
		}
		else if (boxes[0].style.opacity!=null)
  	{
    	brwsr = "css3";
  	}
  	else if (boxes[0].style.filter!=null)
  	{
    	brwsr = "ie";
  	}
  	else
  	{
    	brwsr = "unk";
  	}

    // Make sure the first box is on top and visible, then make all
    // other boxes visible, too
    boxes[0].style.zIndex=1;
    boxes[0].style.display='block';
  	for (i=1; i<boxes.length; i++)
  	{
    	boxes[i].style.zIndex=0;
      boxes[i].style.display='block';
			set_opacity(boxes[i],0);
  	}
  	int_id = setTimeout('fade_start()',show_time);
    if (nxt_box==boxes.length)
    {
      nxt_box = 0;
    }
  }
}

function do_fade(opac)
{
  if (opac<=100)
  {
    set_opacity(boxes[curr_box],100-opac);
    set_opacity(boxes[nxt_box],opac);
    opac=opac+2;
    if (opac==50)
		{
			for (l=0; l<links.length; l++)
			{
				links[l].href = str_links[nxt_box];
			}
      boxes[nxt_box].style.zIndex=1;
      boxes[curr_box].style.zIndex=0;
		}
    setTimeout('do_fade('+opac+')', fade_time);
  }
	else
	{
		setTimeout('fade_start()', lshow_time);
	}
}

function fade_start()
{
  if (nxt_box != curr_box)
  {
  	curr_box++;
  	if (curr_box==boxes.length)
  	{
    	curr_box=0;
  	}
  	nxt_box++;
  	if (nxt_box==boxes.length)
  	{	
    	nxt_box=0;
  	}
  	do_fade(0);
  }
}

function set_opacity(bx,opac)
{
  if (brwsr=="moz")
	{
    bx.style.MozOpacity = opac/100;
	}
	else if (brwsr=="css3")
  {
    bx.style.opacity = opac/100;
  }
  else if (brwsr=="ie")
  {
    bx.style.filter = "alpha(opacity="+opac+")";
  }
  else if (opac<50)
  {
    bx.style.zIndex = 0;
  }
  else
  {
    bx.style.zIndex = 1;
  }
}

window.addEventListener ? window.addEventListener("load", fade_init, false) :
                          window.attachEvent("onload", fade_init);
