window.onload = function() { TBVL_loadSponsors(); }

var TBVL_sponsors;

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id)
{
  if ( document.getElementById(id) )
  {    
    var object = document.getElementById(id).style;
    object.display = (opacity == 0 ? "none" : "block");
    if ( opacity > 0 )
    {
      object.opacity = (opacity / 100); 
      object.MozOpacity = (opacity / 100); 
      object.KhtmlOpacity = (opacity / 100); 
      object.filter = "alpha(opacity=" + opacity + ")";
    }
  } 
} 

function TBVL_sponsorFade(id) {
  var next_id = (id >= TBVL_sponsors.length ? 1 : id + 1);
  opacity("sponsor_" + id, 99, 0, 300);
  setTimeout("opacity('sponsor_" + next_id + "', 0, 99, 300)", 300);
  setTimeout("TBVL_sponsorFade(" + next_id + ")", 5000);
}

function TBVL_loadSponsors()
{
  var number = 1;
  TBVL_sponsors = document.getElementById("sponsors").getElementsByTagName("DIV");
  for ( i in TBVL_sponsors )
  {
    if ( TBVL_sponsors[i].tagName == "DIV" )
    {
      TBVL_sponsors[i].id = "sponsor_" + number;
      changeOpac(0, "sponsor_" + number);
      number++;
    }
  }

  // show random sponsor as first
  var first = Math.round(Math.random()*(number-1));
  first = (first < 1 ? 1 : first);
  changeOpac(100, "sponsor_"+first);

  // start fading
  setTimeout("TBVL_sponsorFade("+first+")", 3500);
}

function setColor(loc, col) {
	loc.style.background = col;
}