var t;
var delay = 50;
	
function setupAds()
{
	var div1 = document.getElementById("ads1");
	var div2 = document.getElementById("ads2");
	div1.style.top = "0px";	
	// div2.style.top = "408px";	

	moveDivs();
}

function moveDivs()
{	
	var div1 = document.getElementById("ads1");
	var div2 = document.getElementById("ads2");
	
	if(div1.style.top.substr(0, div1.style.top.length-2) < -408)
	{		
		div1.style.top = 408 + "px";
	}
	else
	{
		var mytop = div1.style.top.substr(0, div1.style.top.length-2);		
		div1.style.top = parseInt(mytop - 5) + "px";
	}

	if(div2.style.top.substr(0, div2.style.top.length-2) < -408)
	{		
		div2.style.top = 408 + "px";
	}
	else
	{
		var mytop = div2.style.top.substr(0, div2.style.top.length-2);
		div2.style.top = parseInt(mytop - 5) + "px";
	}
	
	setTimeout("moveDivs();", delay);	
}


function set_opacity(obj, perc)
{
  if (obj.style) 
    {
      if (obj.style.MozOpacity!=null) 
        {
          obj.style.MozOpacity = (perc/100) - .001;
        }
      else if (obj.style.opacity!=null) 
        {
          obj.style.opacity = (perc/100) - .001;
        } 
      else if (obj.style.filter!=null) 
        {
          obj.style.filter = "alpha(opacity="+perc+")";
        }
    }
}


function swap_ads(loc,from_ad,to_ad,last_ad,perc)
{
  var i;
  var doo;
  var dio;
  var oi;

  // Page of ads that is fading out
  oi = 'ad_'+loc+'_'+from_ad;
  doo = document.getElementById(oi);

  // Page of ads that is fading in
  oi = 'ad_'+loc+'_'+to_ad;
  dio = document.getElementById(oi);

  if(perc == 0)
    {
      // First run through of the function so set things ups accordingly
      set_opacity(dio, 0);
      doo.style.zIndex=0;
      doo.style.display='block';
      dio.style.zIndex=10;
      dio.style.display='block';
      timeouts[loc] = setTimeout('swap_ads('+loc+','+from_ad+','+to_ad+','+last_ad+', 10);', 25);
      return;
    }

  if(perc < 100)
    {
//      dio.style.top = '-'+(100-perc)+'px';
//      dio.style.left = '-'+(100-perc)+'px';
      set_opacity(dio, perc);
      perc += 10;
      timeouts[loc] = setTimeout('swap_ads('+loc+','+from_ad+','+to_ad+','+last_ad+', '+perc+');', 25);
      return;
    }

  doo.style.display='none';
  dio.style.display='block';
  oi = 'ad_'+loc+'_button_'+to_ad;
  d = document.getElementById(oi)
  d.style.color='red';
  oi = 'ad_'+loc+'_button_'+from_ad;
  d = document.getElementById(oi)
  d.style.color='gray';

  from_ad = from_ad + 1;
  if(from_ad > last_ad)
    from_ad = 1;

  next_ad = from_ad + 6;
  if(next_ad > last_ad) next_ad -= last_ad;

  func = 'swap_ads('+loc+','+from_ad+','+next_ad+','+last_ad+',0);';
  timeouts[loc] = setTimeout(func, 2000);

  return;

  if(to_ad == last_ad)
    {
      timeouts[loc] = setTimeout('swap_ads('+loc+','+to_ad+',1,'+last_ad+', 0);', 6000);
    }
  else
    {
      new_to_ad = to_ad+1;;
      timeouts[loc] = setTimeout('swap_ads('+loc+','+to_ad+','+new_to_ad+','+last_ad+', 0);', 2000);
    }
}

var timeouts = new Array();

function init_adverts(loc,first_ad,last_ad)
{
  var i;
  var j;
  var d;
  var oi;

  if(first_ad == 0)
    return;

  if(last_ad > 6)
    {
      next_ad = first_ad + 6;
      if(next_ad > last_ad) next_ad -= last_ad;

      func = 'swap_ads('+loc+','+first_ad+','+next_ad+','+last_ad+',0);';
      timeouts[loc] = setTimeout(func, 2000);
    }

  for(i=1; i<=last_ad; i++)
    {
      oi = 'ad_'+loc+'_'+i;
      d = document.getElementById(oi);
      d.style.display='none';
      oi = 'ad_'+loc+'_button_'+i;
      d = document.getElementById(oi)
      d.style.color='gray';
    }

  i=first_ad;
  for(j=0; j < 6; j++)
    {
      oi = 'ad_'+loc+'_'+i;
      d = document.getElementById(oi);
      d.style.display='block';
      oi = 'ad_'+loc+'_button_'+i;
      d = document.getElementById(oi)
      d.style.color='red';
      i++;
      if(i>last_ad) i = 1;
    }
  return;
}

function jump_adverts(loc,page,last_page)
{
  clearTimeout(timeouts[loc]);
  init_adverts(loc,page,last_page);
}
