/* $Id: testimonials.js 10958 2011-04-13 14:14:37Z mmawdsley $ */

function Rotate_Testimonials ()
{
  /*
   * Used for internal referencing
   */
  var that = this;
  /*
   * Container of the testimonial content
   */
  that.container = $("#testimonial-block .block-content").first ();
  /*
   * List of testimonials
   */
  that.testimonials = null;
      
  /*
   * Class constructor
   * @param array testimonials list of testimonials
   */
  that.setup = function (testimonials)
    {
      if (!that.container)
        return;

      that.testimonials = testimonials;
      that.set_container_height ();
          
      setTimeout (that.next, 500);
          
    }

  /*
   * Works out the correct height of the container
   */
  that.set_container_height = function ()
    {
      var i = null;              // Index
      var height = 0;            // New container height
      var current_height = null; // Current contaner height
        
      for (i in that.testimonials)
        {
          testimonial = that.format_testimonial (that.testimonials[i]);
          
          that.container.empty ();
          that.container.append (testimonial);

          if (that.container.height () > height)
            height = that.container.height ();
            
        }

      height += 10;
      that.container.empty ();

      current_height = that.container.height ();

      if (current_height < height)
        {
          that.container.animate ({ height: "+=" + (height - current_height) }, 500);

        }
      else if (current_height > height)
        {
          that.container.animate ({ height: "-=" + (current_height - height) }, 500);
            
        }
      else 
        {
          that.container.css ("height", height + "px");

        }
        
    }
      
  /*
   * Starts showing the next testimonial
   */
  that.next = function ()
    {
      var testimonial = null;           // Testimonial
      var testimonial_container = null; // Formatted testimonial
          
      testimonial = that.testimonials.shift ();
      that.testimonials.push (testimonial);

      testimonial_container = that.format_testimonial (testimonial);
        
      that.container.empty ();
      that.container.html (testimonial_container);

      testimonial_container.hide ();

      if ($.browser.msie
          && ($.browser.version == "6.0" || $.browser.version == "7.0"))
        {
          $("div", that.container).first ().slideDown (400);
            
        }
      else
        {
          $("div", that.container).first ().fadeIn (1000);

        }
        
      setTimeout (that.hide, 7000);
          
    }

  /*
   * Hides the current testimonial
   */
  that.hide = function ()
    {
      if ($.browser.msie
          && ($.browser.version == "6.0" || $.browser.version == "7.0"))
        {
          $("div", that.container).first ().slideUp (400);
            
        }
      else
        {
          $("div", that.container).first ().fadeOut (1000);

        }
        
      setTimeout (that.next, 1000);
        
    }
      
  /*
   * Formats the testimonial and returns it
   * @param array testimonial single testimonial
   * @return element
   */
  that.format_testimonial = function (testimonial)
    {
      var html = "";          // Return value
      var container = null;   // <div> element

      container = $("<div/>");
        
      if (testimonial["rating"] >= 0 && testimonial["rating"] <= 5)
        {
          html += ('<div class="stars"><img src="/images/'
                   + testimonial["rating"] + 'starwhite.png" alt="'
                   + testimonial["rating"] + ' stars"/></div>');
            
        }

      html += '<p class="name">' + testimonial["name"] + " wrote...</p>";
      html += '<p class="content">' + testimonial["content"] + "</p>";

      container.append (html);
        
      return container;
        
    }
      
}
