jQuery(document).ready(function(){
	
	var testimonialsArray = new Array();
	var intervalId;
	
	// Get a count of the testimonial divs.
	function getTestimonialCount() {
		var testimonialsQuantity = jQuery('.testimonials_manager_widget').size();
		for(count=0;count<testimonialsQuantity;count++) {
			testimonialsArray[count] = count;
			//alert('hello');
		}
		
		/*for(count1=1;count1<testimonialsArray.length;count1++) {
			alert(testimonialsArray[count1]);	
		}*/
	}
	
// Hide all of the divs

	function hideAllTestimonials() {
		jQuery('.testimonials_manager_widget').hide();	
	}

	function hideTestimonials() {
		jQuery('.testimonials_manager_widget:visible').slideUp('500');	
	}
	
// Get a randomCount of the divs and returns a random value in the array.
// If the testimonial array count is empty, it runs getTestimonialCount to repopulate.

	function getRandomArrayNumber() {
		if (testimonialsArray.length < 1) {
			getTestimonialCount();
		}
		randomNum = Math.floor( Math.random() * testimonialsArray.length);
		delete testimonialsArray[randomNum];
		return randomNum;
		
	}

// Show div # randomCount
	function showDiv(number) {
		jQuery('div.testimonials_manager_widget:eq('+number+')').delay(700).slideDown('500');	
	}
	
	function startInterval() {
		if (intervalId.length < 1) {
			intervalId = setInterval(cycleTestimonial, 9000);
		}
	}
	
	function stopInterval() {
		clearInterval(intervalId);
		intervalId = '';
	}
	
// Main function to hide all testimonials, find one in the array of total testimonials, and display it.
	function cycleTestimonial() {
		//alert('run');
		hideTestimonials();
		numberToDisplay = getRandomArrayNumber();
		showDiv(numberToDisplay);
	}
	
// Wait 8 seconds, and hide the old quote, show new testimonial div #randomCount
	hideAllTestimonials();	
	cycleTestimonial();
	intervalId = setInterval(cycleTestimonial, 9000);
	//startInterval();
	// Restarts Interval Process when window is refocued
	window.addEventListener('focus', startInterval);    
	// Stops the interval process when window is blurred
	window.addEventListener('blur', stopInterval);

})
