var pict_ids = null;
var randomize_index_pictures_timer = null;


function randomize_index_pictures()
{
  if(!is_ns4 && !pict_ids)
  {
    var nb_pict = 0;
    pict_ids = new Array();
  // constructs current value of pict array
    
    while (document.getElementById("INDEXPICTURE" + nb_pict))
    {
      pict_ids[nb_pict] = nb_pict;
      nb_pict++;
    }
    
    if(nb_pict > 1)
    {
      // randomizes pict array
      
      var nb_changes = nb_pict * 100; 
      
      for(var i=0; i<nb_changes; i++)
      {
  	var pict1 = Math.floor(get_random() * (nb_pict - 1));
  	var pict2 = Math.floor(get_random() * (nb_pict - 1));
  	var temp = pict_ids[pict1];
  	pict_ids[pict1] = pict_ids[pict2];
  	pict_ids[pict2] = temp;
      }
  
//      my_alert("pict_ids=" + pict_ids + ", nb_pict=" + nb_pict);
    
      // updates pict array
      update_pictures(0);
    }
  }
}

function randomize_index_pictures_once()
{
  if(!is_ns4 && !pict_ids) 
    randomize_index_pictures(); 
}

function update_pictures(i)
{
  if(i<pict_ids.length)
  {
      var pict1 = document.getElementById("INDEXPICTURE" + i);
      var pict2 = document.getElementById("INDEXPICTURE" + pict_ids[i]);

//      my_alert("exchange #" + i + " between " + pict1.id + " and " + pict2.id);
      
      if(pict1 != pict2)
      {
        document.getElementById("sync").style.backgroundColor = "red" ;
	exchange_pictures(pict1, pict2);
        window.setTimeout("update_pictures(" + (i+1) + ")", 70)
      }
      else
        window.setTimeout("update_pictures(" + (i+1) + ")", 10)
  }
  else
  {
    document.getElementById("sync").style.backgroundColor = "white";
    delete pict_ids;
    pict_ids = null;
  }
}

function exchange_pictures(pict1, pict2)
{
   if(pict1 != pict2)
   {
     var temp1 = pict1.cloneNode(true);
     var temp2 = pict2.cloneNode(true);
     pict1.parentNode.replaceChild(temp2, pict1);
     pict2.parentNode.replaceChild(temp1, pict2);
   }
}

function randomize_index_pictures_settimer(rand_now)
{
  if(!is_ns4)
  {
    randomize_index_pictures_removetimer(false);
    document.getElementById("stop").style.border = "none" ;
    document.getElementById("stop").style.paddingTop = "0px" ;
    document.getElementById("stop").style.paddingBottom = "0px" ;

    document.getElementById("sync").style.border = "solid thin red" ;

    if (rand_now) randomize_index_pictures();
    randomize_index_pictures_timer = window.setInterval("randomize_index_pictures()", 4000);
  }
}

function randomize_index_pictures_removetimer(display)
{
  if(!is_ns4)
  {
    if(randomize_index_pictures_timer) window.clearInterval(randomize_index_pictures_timer);
  //  if(display) alert("Pictures randomization stopped.");
    delete pict_ids;
    pict_ids = null;
    if(document.getElementById("stop"))
    {
      document.getElementById("stop").style.border = "solid thin red" ;
      document.getElementById("stop").style.paddingTop = "2px" ;
      document.getElementById("stop").style.paddingBottom = "2px" ;
      document.getElementById("sync").style.border = "none" ;
    }
  }
}

