﻿$(document).ready(function(){
		showAnother();
	});
	
	function showAnother(){
		var tourId = 0;
		var maxLength = 150;
		//alert(window.location.pathname.indexOf("tours.asp"));
		if((window.location.pathname.indexOf("tours") > 0) && (getParameterByName('id') != ""))
		{
			tourId = getParameterByName('id');
		}
		
		var cycle = 5; // cycle in seconds
		// Reset the current testemonies
		$("#currentTestemony").html("Loading...");
		var count = 0;
		// count the total number of testemonies
		$("#testemonySlideWrapper .testemonySlide").each(function(){
			count = count + 1;
		});
		// if we have some, show them
		if(count >0)
		{
			var attempt = 0;
			var newText = null;
			var quoter = null;
			var snippit = null;
			var rand_no = 1;
			var specTestLimit = 20;
			while ((newText == null) && (attempt < 25))
			{
				// Only try and find content 10 times, if any more then something is wrong
				attempt ++;
				
				rand_no = Math.floor(count*Math.random());
				// select a random testemony to show
				newText = $("#testemonySlideWrapper .testemonySlide:eq(" + rand_no +") p").html();
				quoter = $("#testemonySlideWrapper .testemonySlide:eq(" + rand_no +") strong").html();
				snippit = $("#testemonySlideWrapper .testemonySlide:eq(" + rand_no +") span").html();

				// force a research if we are on a tour specific page and not the correct tour
				if((tourId == 138) && (newText != null) && (newText.indexOf("Gran Premio") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 143) && (newText != null) && (newText.indexOf("Velka Pardubicka") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 146) && (newText != null) && (newText.indexOf("The Arc (Oct) - Eurostar") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 148) && (newText != null) && (newText.indexOf("The Arc (Oct) - flights") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 147) && (newText != null) && (newText.indexOf("Breakfast with Charlie Mann") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 163) && (newText != null) && (newText.indexOf("Breakfast with Ann and George Duffield") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 164) && (newText != null) && (newText.indexOf("Breakfast with Carl Llewellyn") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
				
				if((tourId == 165) && (newText != null) && (newText.indexOf("Breakfast with Jamie Osborne") == -1) && (attempt < specTestLimit)){
					newText = null;
				}
			}

			if(newText != null)
			{
				if(snippit != null){
					
					// If we have a snippit, use this
					newText = "\"..." + snippit + "...\" <strong>" + quoter + "</strong>";	
				
				} else{
					
					// use body and trim if needs be
					var stringTag = newText.indexOf("<STRONG>");
					// if firefox
					if(stringTag == -1){
						stringTag = newText.indexOf("<strong>");
					}
					
					// Get the quoter
					var secondSection = newText.substring(stringTag, newText.length);
								
					// Trim if it exceeds a certain length
					if((newText.length - (newText.length - stringTag)) > maxLength){
						var firstSection = "\"" + newText.substring(1, maxLength) + "...\" ";
						// reassign the text to show
						newText = firstSection + secondSection;
					}
				}
				
				// if we have some text, show it and start the process off again in 5 seconds
				$("#currentTestemony").html("<p><a href='/testimonials.asp'>" + newText + "</a></p>");
				setTimeout("showAnother()", cycle * 1000);
			}else{
				// if we cant find any after looping 50 times, hide the box
				$("#testemonySlideWrapper").hide();
			}
		}
	}
	
	function getParameterByName( name ) 
	{ 
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	  var regexS = "[\\?&]"+name+"=([^&#]*)"; 
	  var regex = new RegExp( regexS ); 
	  var results = regex.exec( window.location.href ); 
	  if( results == null ) 
		return ""; 
	  else 
		return decodeURIComponent(results[1].replace(/\+/g, " ")); 
	}
