$(document).ready(function() {

		var displaying = -1;

		var urlTo = "../testimonials.xml";

		var toFade = $("#testimonial-output > p");

		

		if(test_page == 'home'){

			urlTo = "http://www.easywebcontent.com/testimonials.xml";

			toFade = $("#testimonial-output");

		}

        $.ajax({type:"GET", 

			   url: urlTo, 

			   dataType:"xml",

			   cache:'false',

			   success: generateRandom

		});



	var global_xml;

	var max_char = 150 + 9; //<![CDATA[ plus actual limit number

	function generateRandom(xml){

		//Create timer

		global_xml = xml;

		displayRand();

		setInterval ( displayRand, 10000);

	}

	

	function displayRand(){		

		xml = global_xml;		

		//Generating random XML

		var len = $(global_xml).find("saying").length;

		var rand = displaying;

		while(rand == displaying)

			rand = Math.floor(Math.random() * len);

		var slit = $(global_xml).find("detail").eq(rand).text();

		if(slit.length > max_char)

		 	slit = slit.slice(0, max_char) + "...";

		slit = slit.replace(/&(lt|gt);/g, function (strMatch, p1){

		 		 	return (p1 == "lt")? "<" : ">";

 		});

					

		slit = slit.replace(/<\/?[^>]+(>|$)/g, "");

		

		//Display	

		if(displaying != -1){

			toFade.fadeOut('slow', function(){	 				

				if(test_page =='home'){

					$("#testimonial-output").html("<p>&quot;"+slit+"&quot;</p>"+"<p class='right-review'> &mdash; "+$(global_xml).find("name").eq(rand).text()+"</p>");

				}

				else{

					$("#testimonial-output > p").html("<p>&quot;"+slit+"&quot;</p>");

					$("#testimonial-output > p").append("<span class='name-testimonials'>"+$(global_xml).find("name").eq(rand).text()+"</span>");	

					

				}

				toFade.fadeIn();

			});

		}

		else{

				if(test_page =='home'){

					$("#testimonial-output").html("<p>&quot;"+slit+"&quot;</p>"+"<p class='right-review'> &mdash; "+$(global_xml).find("name").eq(rand).text()+"</p>");

				}

				else{

					$("#testimonial-output > p").html("<p>&quot;"+slit+"&quot;</p>");

					$("#testimonial-output > p").append("<span class='name-testimonials'>"+$(global_xml).find("name").eq(rand).text()+"</span>");	

					

				}

		}

			

		displaying = rand;

	}

	

});
