var showing;
$(document).ready(function() {

  var tstm="";
  $("#rotating_items").contents().remove();
  $.each(testimonials, function(index,t) {

    if (t.Content!=""){
     tstm = "<div class=\"rotating_item\">";
     tstm+= "<p><q>";
	 if (t.Content.length>200){
		 tstm+= t.Content_Plain.substring(0,200) + "..."; 
		 tstm+= "<a href=\"" + rootURL + "aboutus/testimonials.html#" + t.ID + "\"> read more</a>";
	 }else{
		 tstm+= t.Content;
	 }
	  
	  tstm+= "</q></p>";
     tstm+= "<p class=\"testimonial-h-sig-sub\">" + t.Name ;
	 if ((t.Name!="") && (t.Company!="")){
	 	tstm+= ", ";
	 }
	 
	 if (t.URL!=""){
	     tstm+= "<a href=\"" +  t.URL + "\">" 
	 }
	 
	 tstm+= t.Company;
	 
	 if (t.URL!=""){
	     tstm+= "</a>" ;
	 }
     tstm+= "</p>";
     tstm+= "</div>";

     $("#rotating_items").append(tstm);

   }// end if

  } // end of function
  );  // each




showing = $('#rotating_items div.rotating_item:first'); // Initiate the 'showing' variable as the first rotating_item
showing.siblings('div').hide(); // Hide all other rotating_items
setInterval("show_next_rotating_item(showing)", 13000); // Set the rotate time to 13 seconds
	
$('#rotating_items').show();


/*
// When a link in the rotating_item is clicked
// we want to instead treat it as if the whole
// box was clicked.
$('#rotating_items a').click(function(){
		$('#rotating_items').click();
		return false;
	});
*/

});





// Below is the code that picks an item at random to display
jQuery.jQueryRandom = 0;  
jQuery.extend(jQuery.expr[":"],  
{  
    random: function(a, i, m, r) {  
        if (i == 0) {  
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);  
        };  
        return i == jQuery.jQueryRandom;  
    }  
}); 


// The below function repeatedly gets called, to do the rotating
function show_next_rotating_item(t){
	jQuery(t).fadeOut('slow');

	var next_rotating_item = jQuery(t).siblings('.rotating_item:random');
	if(!next_rotating_item.attr('class')){
		next_rotating_item = jQuery('#rotating_items div.rotating_item:first');
	}
	next_rotating_item.fadeIn('slow');
	
	showing = next_rotating_item;	
}


// The below function repeatedly gets called, to do the rotating
function show_next_rotating_item(t){
	$(t).fadeOut('slow');

	var next_rotating_item = $(t).siblings('.rotating_item:random');
	if(!next_rotating_item.attr('class')){
		next_rotating_item = $('#rotating_items div.rotating_item:first');
	}
	next_rotating_item.fadeIn('slow');
	
	showing = next_rotating_item;	
}


function loadTestimonialsArchive(){

  var tstm="";
  $('#testimonials_archive').contents().remove();

 
 $.each(testimonials, function(index,t) {

    if (t.Content!=""){
	 tstm= "<a name=\"" + t.ID + "\" href=\"#" + t.ID + "\"></a>";
     tstm+= "<div class=\"testimonial-archive\">";
     tstm+= "<p><q>" + t.Content + "</q></p>";
     tstm+= "<p class=\"testimonial-h-sig-sub\">" + t.Name ;
	 
     if ((t.Name!="") && (t.Company!="")){
	 	tstm+= ", ";
	 }
	 
	 if (t.URL!=""){
	     tstm+= "<a href=\"" +  t.URL + "\" target=\"_blank\">" 
	 }
	 
	 tstm+= t.Company;
	 
	 if (t.URL!=""){
	     tstm+= "</a>" ;
	 }
     tstm+= "</p>";
     tstm+= "<p style=\"color:#15397F;\">We'd Like to hear from you! <a href=\"http://www.smithwarren.com/contactus.html?s=Comment%2FFeedback\">Click Here</a> to send us your comments and feedback</p>"
     tstm+= "</div>";

     $("#testimonials_archive").append(tstm);

   }// end if

  } // end of function
  );  // each

}
