/*  function setActive
 *  parameters :  2
 *      aIndice   Menu item index to set as active
 *      nbrItems  Total number of menu items   
 *  description:  Sets the specified menu item as active. This means setting
 *                its id to "menuactive" 
 *  author  :     Paul Maire
 *  date    :     15.03.2010        
 */ 
function setActive(aIndice, nbrItems)
{
  //alert("coucou");
  for (var i = 0; i < nbrItems; i++)
  {
    var n = i+1;
    var object = null;
    if(n == aIndice)
    {
      
      object = document.getElementById("menu"+ n);
      if(object != null)
        object.id = "menuactive" + n;
    }
    else
    {
        object = document.getElementById("menu"+ n);
        if(object == null)
        {
          object = document.getElementById("menuactive"+ n);
        }
        if(object != null)
          object.id = "menu" + n;
    }
  }
}

/*  function Picked
 *  parameters :  2
 *      aArray    the array to scan
 *      aValue    the value to find
 *  description:  This function returns true if the specified array
 *                already contains the aValue
 *  author  :     Paul Maire
 *  date    :     15.01.2010        
 */ 
function Picked(aArray, aValue)
{
  for (var i = 0; i < aArray.length; i++)
  {
      if(aArray[i] == aValue)
        return true;
  }
  return false;
}

/*  function toggleComments
 *  parameters :  none
 *  description:  Looks for all div's of class 'comment' and sets randomly
 *                three (3) of them visible, the rest are set to hide. 
 *  author  :     Paul Maire
 *  date    :     15.01.2010        
 */ 
function toggleComments()
{
  var nbrOfCommentsPerPage = 3;               //Set number of comments to toggle
  //document.getElementsByClassName('comment');
  var instances = $$(".comment");             //Retrieve all divs of class "comment"
  
  var count = instances.length;               //Count instances
  var selection = [-1,-1,-1];                 //Array which will save indices of selected comments
  var randomnumber = -1;                      //used to store random values
  
  //Randomly choose comments
  for (var i = 0; i < nbrOfCommentsPerPage; i++)
  {
    while(Picked(selection, randomnumber))    //Generate random number as long as it hasn't been picked yet
      randomnumber=Math.floor(Math.random()*(count));
    selection[i] = randomnumber;              //Save the random number to the selected array
  }
  
  var newdiv = document.createElement('div'); //Create a new empty div
  newdiv.innerHTML = '<h1></h1>';             //Set its content to display "Comments"
  //Add selected comments at the begining of the div element 
  for (var i = 0; i < selection.length; i++)
  {
      instances[selection[i]].style.visibility = 'visible';     //Set instance to visible
      newdiv.appendChild(instances[selection[i]]);      //Add it to the new div (clone is to avoid it being deleted from the original array thus making indices wrong)
      newdiv.innerHTML += /*count + " / " + selection + */"<br><br>";                           //Add to empty lines at the end of the comment
  }
  //Add all other comments and hide them
  for (var i = 0; i < instances.length; i++)
  {
      if(!Picked(selection, i))                       //Make sure the item has not been picked
      {
        instances[i].style.visibility = 'hidden';     //Set it to hide
        newdiv.appendChild(instances[i]);     //Add it to the new div (clone is to avoid it being deleted from the original array thus making indices wrong)
      }
  }
  //Set the "comments" div's innerHTML value to our new div innerHTML
  document.getElementById("comments").innerHTML = newdiv.innerHTML;
  //Set call back on this method every 30 seconds
  window.setTimeout(toggleComments, 30000);
}

/*  function OuvrirPopup
 *  parameters :  3
 *       page     link(url) to page to show
 *       nom      name to display
 *       option   window options (comma separated valeus)
 *  description:  Opens the specified page as a popup window
 *  author  :     Paul Maire
 *  date    :     15.03.2010        
 */ 
function OuvrirPopup(page, nom, option)
{
  option = ",scrollbars=yes,location=no";
  var width = screen.availWidth/2;
  var height = screen.availHeight/2;
  var left = parseInt((screen.availWidth/2) - (width/2));
  var top = parseInt((screen.availHeight/2) - (height/2));
  window.open(page, nom, "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + option);
} 