$(document).ready(function() {

	//Speed of the slideshow
	var speed = 5000;
  var iHeight

  if ( $('#testimonial-slides li.selected').length == 0 ) {
    $('#testimonial-slides li:first').addClass('selected');
  }

  $('#testimonial-slides li').each(function() {
    if ( !$(this).hasClass("selected") ) {
      $(this).fadeTo('fast',0).addClass("hidden")
    }
  });

  $("#testimonial-slides li.selected").each(function() {
    //lets change the heights in order to allow the height to change for each testimonial
    $('#testimonial-slides li').css("height","auto")
    $('#testimonials div#testimonial-group').css("height","auto")
    $('#testimonials div#testimonial-group').css("height","auto")
    $('#testimonials').css("height","auto")

    //lets change the hight of the ul to accomidate current li
    iHeight = $(this).height() + 30
    $(this).parent("ul").animate({ height: iHeight })
  });

	//Assign a timer, so it will run periodically
	var run = setInterval('list_fader(0,"#testimonial-slides")', speed);	
});


function list_fader(prev,ul_selector) {
	//Get the current selected item (with selected class), if none was found, get the first item
	var current_slide = $(ul_selector + ' li.selected').length ? $(ul_selector + ' li.selected') : $(ul_selector + ' li:first');
  var iHeight

	//if prev is set to 1 (previous item)
	if (prev) {
		//Get previous sibling
		var next_slide = (current_slide.prev().length) ? current_slide.prev() : $(ul_selector + ' li:last');
	
	//if prev is set to 0 (next item)
	} else {
		//Get next sibling
		var next_slide = (current_slide.next().length) ? current_slide.next() : $(ul_selector + ' li:first');
	}

	//clear the selected class
	$(ul_selector + ' li').removeClass('selected');
	
	//reassign the selected class to current items
  if ( next_slide.hasClass("hidden") ) {
    next_slide.removeClass('hidden');
  };
	next_slide.addClass('selected');

  //lets set the next slides height
  iHeight = next_slide.find("div.content-container").height() + 30

  //lets make the animation changes in order to view the next slide
  current_slide.animate({ opacity: 0 })
  next_slide.animate({ opacity: 1 })
  next_slide.parent("ul").animate({ height: iHeight })
}
