var allFeedbacksHTML =  [ "<div class='feedback_box'><p><strong> A J Motors </strong></p><p>14-10-2011</p> <p class='feedback_text'>&#8220;Very satisfied with the work, customer care and overal feeling of a garage well run.&#8221;</p></div>", 
                          "<div class='feedback_box'><p><strong> Bilwood Motors </strong></p><p>25-10-2010</p><p class='feedback_text'>&#8220;An excellent local garage and service centre with good and helpful staff.&#8221;</p></div>",
                          "<div class='feedback_box'><p><strong> Cavalier Garage</strong></p><p>07-05-2011</p><p class='feedback_text'>&#8220;I would use this garage again and recommend it to friends/family.&#8221;</p></div>",
                          "<div class='feedback_box'><p><strong>Abacus Auto Services </strong></p><p>11-07-2011</p><p class='feedback_text'>&#8220;Excellent service and attitude of staff very efficient company.&#8221;</p></div>",
                          "<div class='feedback_box'><p><strong>Moores Motors (Torquay) Ltd</strong></p><p>17-08-2011</p><p class='feedback_text'>&#8220;Obliging and professional as always.&#8221;</p></div>"];
                          
var currentFeedbackIndex = 0;

var windowFocussed = true;
var windowEventsAttached = false;    

var timeout = 0;

function changeFeedback()
{
  attachWindowEvents();
  
  if(windowFocussed )
  {
      var newIndex = randomFromTo(0,4);
      while(newIndex == currentFeedbackIndex )
      {
        newIndex = randomFromTo(0,4);
      }
    
      currentFeedbackIndex = newIndex ;
      var feedbackContainer = $("#feedback_scroller");
      var feedback = allFeedbacksHTML[newIndex];
      
      //feedbackContainer.css("visibilty", "hidden").html(feedback).fadeIn();
      
      feedbackContainer.animate({
        opacity: 0,
      }, 200, "linear" , function(){
          
       feedbackContainer.html(feedback);
        feedbackContainer.animate({
        opacity: 1,
      }, 200 );
      });
    currentCommentIndex = newIndex;                        
  }
  
  timeout = window.setTimeout(changeFeedback,5000);
}
                

function randomFromTo(from, to)
{
  return Math.floor(Math.random() * (to - from + 1) + from);
}

function attachWindowEvents()
{
  if(!windowEventsAttached )
  {
    window.addEventListener('focus', function() {
      windowFocussed = true;
    });
  
    window.addEventListener('blur', function() {
      windowFocussed = false;
    });
    
    windowEventsAttached = true;
  }
}

