// JavaScript Document

    
    var nextTestimonialTimeoutId = null;
        
    var fetchingTestimonial = false;
    
    var previousTestimonials = new Array();
    
    var preloadedImages = new Array();
    
    var currentIndex = -1; 
    
    var currentTestimonialId = 0;
    
    var userIsNotInMiddleOfRating = true;
    
    Event.observe(window, 'load', LoadTestimonial, false);
    
    function NextTestimonial()
    {
      //if called from View Another, we need to clear the running timer
      if (nextTestimonialTimeoutId) {
        clearTimeout(nextTestimonialTimeoutId);
        nextTestimonialTimeoutId = null;
      }
      
      if (userIsNotInMiddleOfRating) {
        currentIndex = (currentIndex < previousTestimonials.length - 1) ? currentIndex + 1 : 0;
        currentTestimonialId = previousTestimonials[currentIndex][0];
        $('displayContainer').style.minHeight = $('displayContainer').offsetHeight + 'px';
        $('displayContainer').style.height = $('displayContainer').offsetHeight + 'px';
        if ($('ratingContainer')) Element.hide('ratingContainer');
        /*
        Position.clone('testimonialDisplay', 'testimonialDisplayPlacemark');
        $('testimonialDisplayPlacemark').className = 'up';
        $('testimonialDisplay').className = 'down';
        */
        Effect.Fade('testimonialDisplay', {afterFinish: ShowCurrentTestimonial});
        while (currentIndex >= trackPastDisplays) {
          previousTestimonials.splice(0, 1);
          currentIndex--;
        }
      }
      LoadIfNeeded();
      nextTestimonialTimeoutId = setTimeout("NextTestimonial()", loadNextTestimonialInterval * 1000);
    }
    
    function ShowCurrentTestimonial()
    {
      $('testimonialDisplay').innerHTML = previousTestimonials[currentIndex][2];
      if (!Element.visible('testimonialDisplay')) Effect.Appear('testimonialDisplay', {afterFinish: function() {
        $('displayContainer').style.minHeight = null;
        $('displayContainer').style.height = 'auto !important';
        Element.show('ratingContainer');
      }});
    }
    
    function LoadTestimonial()
    { 
      if (fetchingTestimonial) return;
      
      fetchingTestimonial = true;
      
  		var url = 'ajax_display_testimonial.php';
  		
      var pieces = new Array();
  		var j = 0;
  		for (var i = previousTestimonials.length - 1; i >= 0; i--) {
  		  if (j >= trackPastDisplays) {
          break;
        }
        pieces[pieces.length] = 'prev['+j+']='+previousTestimonials[i][0]; 
  		  j++;
      }
  		params = pieces.join("&");
  		
  		params += (TestimonialTokenRequested) ? (((params) ? '&' : '')+'tk='+TestimonialTokenRequested) : '';
  		TestimonialTokenRequested = '';
  		
      //alert('loading... '+params);
  		
  		var myAjax = new Ajax.Request(
  			url, 
  			{
  				method: 'get', 
  				parameters: params, 
  				onComplete: function() {fetchingTestimonial = false;}, //only necessary if neither success nor failure happens
  				onSuccess: CacheTestimonial, 
  				onFailure: LoadTestimonialTimer
			});
    }
    
    function CacheTestimonial(request)
    {
      fetchingTestimonial = false;
      testimonialInfo = request.responseText.split("%|%");
      previousTestimonials.push(testimonialInfo);
      if (currentIndex == -1) {
        NextTestimonial();
      } else {
        //let's preload the image
        if (!preloadedImages[testimonialInfo[1]]) {
          var preloadedImageSrc = '/view_photo.php?token='+testimonialInfo[1]+'&size=n';
          var preloadImage = new Image();
          preloadedImages[testimonialInfo[1]] = {image: preloadImage, src: preloadedImageSrc};
          preloadImage.onload = function() {preloadedImages[testimonialInfo[1]].loaded = true;}
          preloadImage.src = preloadedImageSrc;
        }
      }
      LoadIfNeeded();
    }
    
    function LoadIfNeeded()
    {
      if (currentIndex > (previousTestimonials.length - 1) - loadAhead) {
        LoadTestimonial();
      }
    }
    
    function LoadTestimonialTimer(request)
    {
      fetchingTestimonial = false;
      setTimeout("LoadTestimonial()", retryLoadTestimonialTimeout * 1000);
    }
    
    function RateThis(id, rating)
    {
      userIsNotInMiddleOfRating = false;
  		var url = 'ajax_rate_testimonial.php';
  		var params = 'id='+id+'&rating='+rating;
  		
  		var myAjax = new Ajax.Request(
  			url, 
  			{
  				method: 'post', 
  				parameters: params,
  				onComplete: function () {userIsNotInMiddleOfRating = true;},
  				onSuccess: function (request) {DisplayRating(request, id)}, 
  				onFailure: ErrorOnRating
			});
    }
    
    function DisplayRating(request, id)
    {
      if (request.responseText != '1') {
        //some type of error occured
        //alert(request.responseText);
      } else {
        if ($('ratingContainer')) {
      		var url = 'ajax_display_rating.php';
      		var params = 'id='+id;
      		
      		var myAjax = new Ajax.Request( 
      					url, 
      					{
      						method: 'get', 
      						parameters: params, 
      						onFailure: RatingUnavailable,
      						onSuccess: UpdateRatingContainer
      					});
        }
      }
    }
    
    function UpdateRatingContainer(request)
    {
      $('ratingContainer').innerHTML = request.responseText; 
      previousTestimonials[currentIndex][2] = $('testimonialDisplay').innerHTML;
    }
    
    function ErrorOnRating(request)
    {
      //alert('An error occured. Please try again.');
    }
    
    function RatingUnavailable()
    {
      $('ratingContainer').innerHTML = '<span class="red">Rating Unavailable</span>';
    }

