/* $Id$ */

/*
 * Rotates the client logos
 */
function Clients ()
{
  /*
   * Used for internal referencing
   */
  var that = this;
  /*
   * Container element
   */
  that.container = null;
  /*
   * List of client logos
   */
  that.logos = new Array ();
  /*
   * List of displays icon elements
   */
  that.icons = new Array ();
  /*
   * Current icon index
   */
  that.current_icon_index = 0;
  
  /*
   * Sets up the class
   * @param string container_id ID of the container
   */
  that.setup = function (container_id)
    {
      that.container = document.getElementById (container_id);

      if (!that.container)
        return;

      /* Pre-load the images */
      that.preload ();
      
      /*
       * Create the logos and set up the timer for swapping another logo in
       */
      
      that.setup_logos ();

      if (that.logos.length <= 5)
        return;
      
      setTimeout (that.next, 1000);
      
    }

  that.add_logo = function (name, uri)
    {
      var logo = null;          // Logo being added

      logo = new Array ();
      logo["name"] = name;
      logo["icon"] = uri;
      
      that.logos.push (logo);

    }
  
  /*
   * Pre-loads the images
   */
  that.preload = function ()
    {
      var i = null;

      for (i in that.logos)
        $("img").attr ("src", that.logos["icon"]);
      
    }
  
  /*
   * Creates the initial logos
   */
  that.setup_logos = function ()
    {
      var i = null;             // Counter
      var div = null;           // <div> element
      var image = null;         // <img> element
      var max = null;           // Number of clients to add
      var logo = null;          // Logo information
      
      $("div.logo", that.container).remove ();
      
      max = that.logos.length;

      if (max > 5)
        max = 5;

      for (i = 0; i < max; ++i)
        {
          logo = that.get_next_client ();

          div = document.createElement ("div");
          div.className = "logo";

          image = document.createElement ("img");
          image.setAttribute ("src", logo["icon"]);
          image.setAttribute ("alt", logo["name"]);

          div.appendChild (image);
          
          that.container.appendChild (div);
          that.icons[i] = image;
          
        }
      
    }

  /*
   * Returns the next logo in the list
   */
  that.get_next_client = function ()
    {
      var logo = null;          // Return value

      logo = that.logos.shift ();
      that.logos.push (logo);

      return logo;
      
    }
  
  /*
   * Shows the next logo
   */
  that.next = function ()
    {
      var client = null;        // Next client to show
      var icon = null;          // <img> element
      
      client = that.get_next_client ();
      
      /*
       * Pick one of the clients and random and replace it
       */

      if (that.current_icon_index > that.icons.length - 1)
        that.current_icon_index = 0;
      
      icon = that.icons[that.current_icon_index];

      $(icon).fadeOut (1000, function () {

          $(this).attr ("src", client["icon"]);
          $(this).attr ("alt", client["name"]);
          $(this).fadeIn (1000);
          
        });
      
      setTimeout (that.next, 2000);

      ++that.current_icon_index;
      
    }
  
}
