/* search refinement javascript */
var MAX_LENGTH = 28;
var SEARCH_ACCOMODATION = "ACCO";
var SEARCH_SEEANDDO = "ACTI%2CATTR%2CCATE%2CENTE%2CRETA%2CTOUR";
var SEARCH_SEEANDDO_WALKING = "ACTI";
var SEARCH_EVENTS = "EVEN";
var SEARCH_TOURISTINFO = "SERV"

var g_allLocations;
var WALKING_DISTANCES = "9101,9111,9121";
var WALKING_GRADES = "9071,9081,9091";
var WALKING_TIMES = "9021,9031,9041,9051,9061";
var WALIKING_ROUTES_TYPE = "1939";
var CYCLING_ROUTES_TYPE = "3929";
var CYCLING_DISTANCES = "9101,9111,9121";
var CYCLING_GRADES = "9071,9081,9091,9361";
var CYCLING_TIMES = "9021,9031,9041,9051,9061,9291,9301";
var g_arrSeeAndDo = new Object();
//The following values refer to the See & Do Type options. They must be exactly the same as on the form as to this script to work.
var SAD_TOP_TYPES = "Activities,Attractions,Food and Drink,Entertainment,Shopping,Tours and Guided Tours";

$(document).ready(function() 
{
	// #### Refinement Fixes for Product List Pages without URL params ###### 
	var strHRef = window.location.href;
	var strFix = "";

	if (strHRef.indexOf("//what-to-see-and-do/tours-and-guided-tours/boat-trips") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI%2CATTR%2CCATE%2CENTE%2CRETA%2CTOUR&refined=1&refine-category=on&src_category=3129&refine-polygon=on&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451"}

	if (strHRef.indexOf("//what-to-see-and-do/tours-and-guided-tours/city-tours") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI%2CATTR%2CCATE%2CENTE%2CRETA%2CTOUR&refined=1&refine-category=on&src_category=3939%2C6319&refine-polygon=on&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451"}

	if (strHRef.indexOf("//what-to-see-and-do/tours-and-guided-tours/countryside-tours") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI%2CATTR%2CCATE%2CENTE%2CRETA%2CTOUR&refined=1&refine-category=on&src_category=3939%2C3949%2C3959%2C6309%2C3979%2C3989%2C4009&refine-polygon=on&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451"}

	if (strHRef.indexOf("//what-to-see-and-do/activities/bird-watching") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI%2CATTR%2CCATE%2CENTE%2CRETA%2CTOUR&refined=1&src_name=&refine-category=on&src_category=3889&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451&submit=Search"}

	if (strHRef.indexOf("//what-to-see-and-do/activities/equestrian") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI%2CATTR%2CCATE%2CENTE%2CRETA%2CTOUR&refined=1&src_name=&refine-category=on&src_category=639&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451&submit=Search"}

	if (strHRef.indexOf("//discover-north-east-england/visitor-information/tourist-information-centres") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=SERV&refined=1&refine-category=on&src_category=439&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451&refine-name=on&src_name=&submit.x=94&submit.y=11"}

	if (strHRef.indexOf("/what-to-see-and-do/activities/leisure-cycling/cycle-hire") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI&refined=1&refine-category=on&src_category=4479&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451&refine-name=on&src_name=&submit.x=94&submit.y=11"}

	if (strHRef.indexOf("/what-to-see-and-do/activities/leisure-cycling/cycling-holidays") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACTI&refined=1&refine-category=on&src_category=4559&src_polygon=151%2C91%2C361%2C491%2C501%2C511%2C381%2C411%2C291%2C461%2C231%2C541%2C331%2C341%2C431%2C421%2C351%2C401%2C371%2C301%2C451&refine-name=on&src_name=&submit.x=94&submit.y=11"}

	if (strHRef.indexOf("/what-to-see-and-do/activities/leisure-cycling/cyclists-welcome-accommodation") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACCO&refined=1&src_name=&refine-category=on&src_category=&src_polygon=&submit=Search"}

	if (strHRef.indexOf("/what-to-see-and-do/activities/walking/walkers-welcome-accommodation") != -1 && strHRef.indexOf("?") == -1)
	{strFix = "?prodtypes=ACCO&refined=1&src_name=&refine-category=on&src_category=&src_polygon=&submit=Search"}

	var mySearch = new Search(strHRef + strFix);

	// Hide Advanced Search Link on Irrelevant Pages
	if ( strHRef.indexOf("//what-to-see-and-do/search-results") == -1 && strHRef.indexOf("//accommodation/search-results") == -1 && strHRef.indexOf("//what-to-see-and-do/events/search-results") == -1 )
	{ $("#advancedSearch").hide(); }	

	BuildArea(mySearch);
	BuildSubArea(mySearch);
	
	if( mySearch.GetSearchType() == SEARCH_ACCOMODATION )
	{
		BuildType(mySearch);
		BuildGrading(mySearch);
		BuildPrice(mySearch);
		BuildOffers(mySearch);
		BuildDate(mySearch);
		// BuildAccessable(mySearch);
	}
	
	if( mySearch.GetSearchType() == SEARCH_SEEANDDO || mySearch.GetSearchType() == SEARCH_SEEANDDO_WALKING)
	{
	    //The following statement is simply pulling all values for each option on the See & Do Types.
	    //It will be used to refine the search, as it contains the IDs of the Types.
	    $("#colright #refineBySearchForm #src_category option").each(function() {
	        g_arrSeeAndDo[$(this).html()] = $(this).attr("value");
	    });
		
		BuildSeeAndDoType(mySearch);
		BuildOffers(mySearch);
		
		// process any walking refinements
		if( mySearch.GetType() == WALIKING_ROUTES_TYPE)
			BuildWalking(mySearch);
		else if( mySearch.GetType() == CYCLING_ROUTES_TYPE)
			BuildCycling(mySearch);
	}
	
	if( mySearch.GetSearchType() == SEARCH_EVENTS )
	{
		BuildType(mySearch);
		BuildEventsDate(mySearch);
	}
	
	if( $("#results .content").html() == "" )
		$("#results").hide();

	$("#searchRefinement").show();
	
});

function BuildAccessable(mySearch)
{
	if( $("#searchRefinementSearchBox .refine-tfa .field input").length == 1 )
	{
		if( mySearch.HasAccessable() )
		{
			$("#results .content").append("Tourism for all <a href='"+mySearch.GetSearchQueryString({refineAccessable:false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
		}
		else
		{
			$("#accessable .content").append("<a href='"+ mySearch.GetSearchQueryString({refineAccessable:true}) +"' title='Click for venues providing access information.'>Tourism for all</a><br/>");
			$("#accessable").show();
		}
	}
}

/* builds the types for see and do refinements */
function BuildSeeAndDoType(mySearch)
{
	var title;
	var text;
	var value;
	var arrTypes;
	var currentType;
	var selected;
	var searchType = mySearch.GetType();

	if (searchType == "-1") searchType = "";

	if (searchType) {
	    for (var t in g_arrSeeAndDo) {
	        if (unescapeValue(searchType) == g_arrSeeAndDo[t]) {
	            selected = t;
	            break;
	        }
	    }
		
		if( selected )
		{
			if( SAD_TOP_TYPES.indexOf(selected) == -1 )
			{
				// not a top level type
				var currentTop;
				for(var t in g_arrSeeAndDo)
				{
					if( SAD_TOP_TYPES.indexOf(t) != -1 )
						currentTop = t;
						
					if( t == selected ) break;
				}
				
				if( currentTop )
				{
					$("#results .content").append(currentTop.toLowerCase() + " <a href='"+mySearch.GetSearchQueryString({refineCategory:false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
					$("#results .content").append(selected.toLowerCase() + " <a href='"+mySearch.GetSearchQueryString({refineCategory:true,type:g_arrSeeAndDo[currentTop]})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");																				
	
					// Ensure Type is retained in Right Col Searches for See and Do
					$("#colright select#src_category option").each(function()
					{
						value = $(this).attr("value");
							
						if (value == g_arrSeeAndDo[currentTop])
						{$(this).attr("selected","selected");}
					});
				}
				else
				{
					// if no currenTop then this a specific search results page so we don't show refinedBy option
				}
			}
			else { //IF SELECTED ITEM IS A TOP LEVEL TYPE
				
				for(var t in g_arrSeeAndDo)
				{						
					if( t == selected ) 
					{
					    //THE FOLLOWING PIECE OF CODE IS SIMPLY SELECTING ON THE SEE & DO FORM (ON THE RIGHT), 
						//THE TYPE CURRENTLY CHOSEN.
						$("#colright select#src_category option").each(function()
						{
							value = $(this).attr("value");
								
							if (value == g_arrSeeAndDo[t])
							{$(this).attr("selected","selected");}
						});				
						
					}
				}
				
				//OUTPUTTING THE CHOSEN TYPE AS THE CURRENT SEARCH CRITERIA ALONG WITH A BUTTON TO REMOVE IT
				$("#results .content").append(selected.toLowerCase() + " <a href='"+mySearch.GetSearchQueryString({refineCategory:false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
										  
										  
		}
	}
	else
	{
		$("#type").show();
		
		// output the top level types
		arrTypes = SAD_TOP_TYPES.split(",");
		for(var i=0 ; i<arrTypes.length ; i++ )
		{
			value = g_arrSeeAndDo[arrTypes[i]];
			text = arrTypes[i].toLowerCase();
			title = "Click to refine your search results by "+ text + ".";
			
			$("#type .content").append("<a href='"+ mySearch.GetSearchQueryString({refineCategory:true,type: value}) +"' title='"+title+"'>" + text + "</a><br/>");
		}
	}
}

function BuildDate(mySearch)
{
	if( mySearch.HasAvailability() )
	{
		var text = mySearch.GetDay() + "/" + mySearch.GetMonth() + "/" + mySearch.GetYear();
		text += " for " + mySearch.GetNights() + " night";
		
		if( mySearch.GetNights() > 1 )
			text += "s";
		text += ".";
		
		$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({refineAvailability:false})+"'><img src='/images/search/remove.png' alt='Remove.'/></a>");
		
		// DisplayMessageBox("messageBox", "Because you searched for a given date the search results below only include accommodation <em>this website can verify availability for</em>. <a href='"+mySearch.GetSearchQueryString({refineAvailability:false})+"'>Click here</a> to include accommodation that may be excluded from the availability search.");
	}
}

function BuildEventsDate(mySearch)
{
	if( mySearch.hasDates() && !mySearch.SearchAnyDate() )
	{
		var text = mySearch.GetDay() + "/" + mySearch.GetMonth() + "/" + mySearch.GetYear();
		text += " to ";
		text += mySearch.GetEndDay() + "/" + mySearch.GetEndMonth() + "/" + mySearch.GetEndYear();
		
		$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({refineDates:false})+"'><img src='/images/search/remove.png' alt='Remove.'/></a>");
	}
}

function BuildType(mySearch)
{
	var count = 0;
	var searchFor = null;
	var value;
	var anyValue;
	var text;
	
	$("#type").show();
	
	$("#searchRefinementSearchBox #src_category option").each(function()
	{
		value = $(this).attr("value");
		text = $(this).html();
		title = "Click to refine your search results by "+ text + ".";
		
		if( text.length > MAX_LENGTH )
			text = text.substr(0, MAX_LENGTH) + "...";
		
		if( count > 0 )
		{	
			$("#type .content").append("<a href='"+ mySearch.GetSearchQueryString({refineCategory:true,type: value}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( escapeValue(value) == mySearch.GetType())
				searchFor = $(this).html();
		}
		else
		{
			anyValue = value;
		}

		count++;
	});
	
	if( searchFor != null )
	{
		$("#type").hide();
		$("#results .content").append(searchFor + " <a href='"+mySearch.GetSearchQueryString({refineCategory:false})+"' title='Click to remove.' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
	}
	
}

function BuildArea(mySearch)
{
	var count = 0;
	var searchFor = null;
	var value;
	var anyValue;
	
	$("#area").show();
	
	$("#colright #refineBySearchForm .refine-polygon option").each(function()
	{
		value = $(this).attr("value");
		text = $(this).html().toLowerCase();
		if (text == "newcastlegateshead area") {text = "newcastleGateshead area";}
		title = "Click to refine your search results by "+ text + ".";
		
		if( value.indexOf(",") != -1  )
		{		
			if( text.length > MAX_LENGTH )
				text = text.substr(0, MAX_LENGTH) + "...";
				
			if( count > 0 )
			{				
				$("#area .content").append("<a href='"+ mySearch.GetSearchQueryString({area: value}) +"' title='"+title+"'>" + text + "</a><br/>");
				
				if( escapeValue(value) == mySearch.GetArea())
					searchFor = text;
			}
			else
			{
				anyValue = value;
			}
		}
		
		if( count == 0 )
			g_allLocations = value;
		
		count++;
	});
	
	if( searchFor != null )
	{
		$("#area").hide();
		$("#results .content").append(searchFor + " <a href='"+mySearch.GetSearchQueryString({area: anyValue})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
	}
}

function BuildSubArea(mySearch)
{
	var count = 0;
	var searchFor = null;
	var value;
	var anyValue;
	var anyText;
	var currentArea;
	
	if( mySearch.GetArea() != null && mySearch.GetArea().indexOf("%2C") != -1 && mySearch.GetArea().replace(/%2C/g, ",") != g_allLocations )
	{
		$("#subArea").show();
	}
	
	currentArea = "%2C" + mySearch.GetArea() + "%2C";
	
	$("#colright #refineBySearchForm .refine-polygon option").each(function()
	{
		value = $(this).attr("value");
		text = $(this).html();
		title = "Click to refine your search results by "+ text + ".";
		
		if( value.indexOf(",") == -1 && (currentArea.indexOf("%2C"+value+"%2C") != -1 || currentArea.indexOf("%2C"+value+"%2C") != -1 ) )
		{		
			if( text.length > MAX_LENGTH )
				text = text.substr(0, MAX_LENGTH) + "...";
				
							
			$("#subArea .content").append("<a href='"+ mySearch.GetSearchQueryString({area: value}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( escapeValue(value) == mySearch.GetArea())
				searchFor = $(this).html();
		}
		
		if( value.indexOf(",") != -1 && value.indexOf(mySearch.GetArea()) != -1)
		{
			anyValue = value;
			anyText = text.toLowerCase();
		}
		
		count++;
	});
	
	if( searchFor != null )
	{
		$("#area").hide();
		$("#subArea").hide();
		$("#results .content").append(anyText + " <a href='"+mySearch.GetSearchQueryString({area: g_allLocations})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
		$("#results .content").append(searchFor + " <a href='"+mySearch.GetSearchQueryString({area: anyValue})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
	}
	
}

function BuildGrading(mySearch)
{
	$("#grading").show();
	
	if( !mySearch.HasGrading())
	{
		for(var i=1 ; i<=5 ; i++)
		{
			title = "Click to refine your search results by "+ i + " Star grading.";
			$("#grading .content").append("<a href='"+ mySearch.GetSearchQueryString({grading: i }) +"' title='"+title+"'>" + i + " Star" + "</a><br/>");
		}
	}
	else
	{
		$("#grading").hide();
		$("#results .content").append(mySearch.GetGrading() + " Star" + " <a href='"+mySearch.GetSearchQueryString({refineGrading: false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
	}
}

function BuildPrice(mySearch)
{
	$("#price").show();
	
	if( !mySearch.HasPricing() )
	{
		$("#price .content").append("<a href='"+ mySearch.GetSearchQueryString({minimumPrice:0,maximumPrice:25}) +"' title='Refine your search result by up to &pound;25 pppn.'>Up to &pound;25 pppn</a><br/>");
		$("#price .content").append("<a href='"+ mySearch.GetSearchQueryString({minimumPrice:25,maximumPrice:50}) +"' title='Refine your search result by &pound;25 - &pound;50 pppn.'>&pound;25 - &pound;50 pppn</a><br/>");
		$("#price .content").append("<a href='"+ mySearch.GetSearchQueryString({minimumPrice:50,maximumPrice:75}) +"' title='Refine your search result by &pound;50 - &pound;75 pppn.'>&pound;50 - &pound;75 pppn</a><br/>");
		$("#price .content").append("<a href='"+ mySearch.GetSearchQueryString({minimumPrice:75,maximumPrice:100}) +"' title='Refine your search result by &pound;75 - &pound;100 pppn.'>&pound;75 - &pound;100 pppn</a><br/>");
		$("#price .content").append("<a href='"+ mySearch.GetSearchQueryString({minimumPrice:100,maximumPrice:1000}) +"' title='Refine your search result by more than &pound;100 pppn.'>More than &pound;100 pppn</a><br/>");
	}
	else
	{
		$("#price").hide();
		
		if( mySearch.GetMinimumPrice() == 0 )
		{
			$("#results .content").append("Up to &pound;" + mySearch.GetMaximumPrice() + " <a href='"+mySearch.GetSearchQueryString({refinePricing: false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
		}
		else if ( mySearch.GetMaximumPrice() == 1000 )
		{
			$("#results .content").append("More than &pound;" +  mySearch.GetMinimumPrice() + " <a href='"+mySearch.GetSearchQueryString({refinePricing: false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
		}	
		else
		{
			$("#results .content").append("&pound;" + mySearch.GetMinimumPrice() + " - " + "&pound;" + mySearch.GetMaximumPrice() + " <a href='"+mySearch.GetSearchQueryString({refinePricing: false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
		}
	}
}

function BuildOffers(mySearch)
{
	var count = 0;
	var searchFor = null;
	var value;
	var anyValue;
	var hasOffers = false;
	
	$("#searchRefinementSearchBox #src_specoff option").each(function()
	{
		value = $(this).attr("value");
		text = $(this).html();
		title = "Click to refine your search results by "+ text + " special offer.";
		
		if( text.length > MAX_LENGTH )
			text = text.substr(0, MAX_LENGTH) + "...";
		
		if( mySearch.GetSearchType() == SEARCH_ACCOMODATION)
		{
			if( count > 0 )
			{	
				 if( value != "211" )
				 {
					$("#specialOffers .content").append("<a href='"+ mySearch.GetSearchQueryString({specialOffer: value}) +"' title='"+title+"'>" + text + "</a><br/>");
					hasOffers = true;
				 }
					
			}
			else
			{
				anyValue = value;
			}
		}
		else
		{
			$("#specialOffers .content").append("<a href='"+ mySearch.GetSearchQueryString({specialOffer: value}) +"' title='"+title+"'>" + text + "</a><br/>");
			hasOffers = true;
		}
		
		
		if( escapeValue(value) == mySearch.GetSpecialOffer() && value != anyValue )
			searchFor = $(this).html();

		count++;
	});
	
	if( searchFor != null )
	{
		$("#specialOffers").hide();
		$("#results .content").append(searchFor + " <a href='"+mySearch.GetSearchQueryString({refineSpecialOffers: false})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
	}
	else
	{
		if( hasOffers ) $("#specialOffers").show();	
	}
}

function BuildWalking(mySearch)
{
	var arrGrades;
	var text;
	var title;
	
	$("#walkingGrades").show();
	$("#walkingDistance").show();
	$("#walkingDuration").show();
	
	if( $("#searchRefinementSearchBox ul.facility").length == 1)
	{
		arrGrades = WALKING_GRADES.split(",");
		for(var i=0 ; i<arrGrades.length ; i=i+1)
		{
			text = $("#searchRefinementSearchBox ul.facility input[value='"+arrGrades[i]+"']").siblings("label").html();
			title = "Click to refine your search results by "+ text;
			$("#walkingGrades .content").append("<a href='"+ mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"add"}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( mySearch.GetFacility().indexOf(arrGrades[i]) != -1 )
			{
				$("#walkingGrades").hide();	
				$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"remove"})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
			
		}
		
		arrGrades = WALKING_DISTANCES.split(",");
		for(var i=0 ; i<arrGrades.length ; i=i+1)
		{
			text = $("#searchRefinementSearchBox ul.facility input[value='"+arrGrades[i]+"']").siblings("label").html();
			title = "Click to refine your search results by "+ text;
			$("#walkingDistance .content").append("<a href='"+ mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"add"}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( mySearch.GetFacility().indexOf(arrGrades[i]) != -1 )
			{
				$("#walkingDistance").hide();	
				$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"remove"})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
		}
		
		arrGrades = WALKING_TIMES.split(",");
		for(var i=0 ; i<arrGrades.length ; i=i+1)
		{
			text = $("#searchRefinementSearchBox ul.facility input[value='"+arrGrades[i]+"']").siblings("label").html();
			title = "Click to refine your search results by "+ text;
			$("#walkingDuration .content").append("<a href='"+ mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"add"}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( mySearch.GetFacility().indexOf(arrGrades[i]) != -1 )
			{
				$("#walkingDuration").hide();	
				$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"remove"})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
		}
	}
}

/* outputs the cycling specific refine options */
function BuildCycling(mySearch)
{
	var arrGrades;
	var text;
	var title;
	
	$("#walkingGrades").show();
	$("#walkingDistance").show();
	$("#walkingDuration").show();
	
	if( $("#searchRefinementSearchBox ul.facility").length > 0)
	{
		arrGrades = CYCLING_GRADES.split(",");
		for(var i=0 ; i<arrGrades.length ; i=i+1)
		{
			text = $("#searchRefinementSearchBox ul.facility input[value='"+arrGrades[i]+"']").siblings("label").html();
			title = "Click to refine your search results by "+ text;
			$("#walkingGrades .content").append("<a href='"+ mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"add"}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( mySearch.GetFacility().indexOf(arrGrades[i]) != -1 )
			{
				$("#walkingGrades").hide();	
				$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"remove"})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
			
		}
		
		arrGrades = CYCLING_DISTANCES.split(",");
		for(var i=0 ; i<arrGrades.length ; i=i+1)
		{
			text = $("#searchRefinementSearchBox ul.facility input[value='"+arrGrades[i]+"']").siblings("label").html();
			title = "Click to refine your search results by "+ text;
			$("#walkingDistance .content").append("<a href='"+ mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"add"}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( mySearch.GetFacility().indexOf(arrGrades[i]) != -1 )
			{
				$("#walkingDistance").hide();	
				$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"remove"})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
		}
		
		arrGrades = CYCLING_TIMES.split(",");
		for(var i=0 ; i<arrGrades.length ; i=i+1)
		{
			text = $("#searchRefinementSearchBox ul.facility input[value='"+arrGrades[i]+"']").siblings("label").html();
			title = "Click to refine your search results by "+ text;
			$("#walkingDuration .content").append("<a href='"+ mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"add"}) +"' title='"+title+"'>" + text + "</a><br/>");
			
			if( mySearch.GetFacility().indexOf(arrGrades[i]) != -1 )
			{
				$("#walkingDuration").hide();	
				$("#results .content").append(text + " <a href='"+mySearch.GetSearchQueryString({facility:arrGrades[i],facilityMode:"remove"})+"' title='Click to remove.'><img src='/images/search/remove.png' alt='Remove.'/></a><br/>");
			}
		}
	}
}

function escapeValue(value)
{
	if( value )
		return escape(value.replace(/ /g,"+"));
	
	return null;
}

function unescapeValue(value)
{
	if( value )
		return unescape(value.replaceAll("+"," "));;
		
	return null;
}

/**
 * Class used to represent the search query string
 **/
function Search(url) 
{
	this._refineAvailability = false;
	this._refineGrading = false;
	this._refineFacility = false;
	this._facility = "";
	this._accessable = false;
	
	this.Parse(url);
}

Search.prototype.GetSearchType = function()
{
	return this._productType;	
}

Search.prototype._type;
Search.prototype.GetType = function() { return this._type; }
Search.prototype.SetType = function(type) { this._type = type; }

Search.prototype._area;
Search.prototype.GetArea = function() { return this._area; }
Search.prototype.SetArea = function(type) { this._area = area; }

Search.prototype._grading;
Search.prototype.GetGrading = function() { return this._grading; }
Search.prototype.HasGrading = function() { return this._refineGrading; }

Search.prototype.HasPricing = function() { return this._refinePricing; }
Search.prototype.GetMaximumPrice = function() { return this._maximumPrice; }
Search.prototype.GetMinimumPrice = function() { return this._minimumPrice; }

Search.prototype.GetSpecialOffer = function() { return this._specialOffer; }
Search.prototype.HasAvailability = function() { return this._refineAvailability;  }
Search.prototype.GetDay = function() { return this._startDay;  }
Search.prototype.GetMonth = function() { return this._startMonth; }
Search.prototype.GetYear = function() { return this._startYear; }
Search.prototype.GetNights = function() { return this._nights; }

Search.prototype.hasDates = function() { return this._refineDates; }
Search.prototype.SearchAnyDate = function() { return this._anyDate == "yes"; }
Search.prototype.GetEndDay = function() { return this._endDay;  }
Search.prototype.GetEndMonth = function() { return this._endMonth; }
Search.prototype.GetEndYear = function() { return this._endYear; }

Search.prototype.HasFacility = function() { return this._refineFacility; }
Search.prototype.GetFacility = function() { return this._facility; }

Search.prototype.HasAccessable = function() { return this._accessable; }

Search.prototype.Parse = function(url)
{
	this._url = url;
	
	var querystring = url.substring( url.indexOf("?") + 1 );
	var params = querystring.split("&");
	var otherParams = new Array();
	
	for(i=0 ; i<params.length ; i++ )
	{
		var values = params[i].split("=");
		
		switch(values[0])
		{
			case "src_category":
				this._type = values[1];
				break;
			case "prodtypes":
				this._productType = values[1];
				break;
			case "refined":
				this._refined = values[1];
				break;
			case "refine-category":
				this._refineCategory = true;
				break;
			case "submit":
				this._submit = values[1];
				break;
			case "refine-polygon":
				this._refinePoloygon = true;
				break;
			case "src_polygon":
				this._area = values[1];
				break;
			case "staydate_day":
				this._startDay = values[1];
				break;
			case "staydate_month":
				this._startMonth = values[1];
				break;
			case "staydate_year":
				this._startYear = values[1];
				break;
			case "nights":
				this._nights = values[1];
				break;
			case "NumRoomReqs":
				this._numberOfRooms = values[1];		
				this._guestsPerRoom = new Array();
				break;
			case "refine-availability":
				this._refineAvailability = values[1];
				break;
			case "submit":
				this._submit = values[1];
			case "refine-grading":
				this._refineGrading = true;
				break;
			case "src_grading":
				this._grading = values[1];
				break;
			case "refine-price":
				this._refinePricing = true;
				break;
			case "src_minprice":
				this._minimumPrice = values[1];
				break;
			case "src_maxprice":
				this._maximumPrice = values[1];
				break;
			case "src_specoff":
				this._specialOffer = values[1];
				break;
			case "refine-date":
				this._refineDates = values[1];
				break;
			case "start_day":
				this._startDay = values[1];
				break;
			case "start_month":
				this._startMonth = values[1];
				break;
			case "start_year":
				this._startYear = values[1];
				break;
			case "end_day":
				this._endDay = values[1];
				break;
			case "end_month":
				this._endMonth = values[1];
				break;
			case "end_year":
				this._endYear = values[1];
				break;
			case "src_anydate":
				this._anyDate = values[1];
				break;
			case "isostartdate":
				var arr = values[1].split("%2F");
				if( arr.length == 3)
				{
					this._startDay = arr[0];
					this._startMonth = arr[1];
					this._startYear = arr[2];
				}
				break;
			case "isoenddate":
				var arr = values[1].split("%2F");
				if( arr.length == 3)
				{
					this._endDay = arr[0];
					this._endMonth = arr[1];
					this._endYear = arr[2];
				}
				break;
			case "refine-facility":
				this._refineFacility = true;
				break;
			case "src_facility":
				if( this._facility.length > 0 )
					this._facility += ",";
				this._facility += values[1];
				
				break;
			case "refine-tfa":
				this._accessable = true;
				break;
			default:
				otherParams[values[0]] = values[1];
				break;
		}
	}
	
	for(i=0 ; i<this._numberOfRooms ; i++)
	{
		this._guestsPerRoom[i] = otherParams["roomReq_" + (i+1)];
	}

}

Search.prototype.GetSearchQueryString = function(override)
{
	var searchUrl;
	var arrString;
	var tempString;
	
	searchUrl = this.GetSearchUrl(override);
	
	searchUrl += "prodtypes=" + this._productType;
	searchUrl += "&refined=" +this. _refined;
	searchUrl += "&submit=" + this._submit;
	
	
	if( override.refineCategory != null )
	{
		if( override.refineCategory )
		{
			searchUrl += "&refine-category=on";
			searchUrl += "&src_category=" + escapeValue(override.type);
		}
	}
	else if(this._refineCategory)
	{
		searchUrl += "&refine-category=on";
		
		if( override.type ) 
		{
			searchUrl += "&src_category=" + escapeValue(override.type);
		}
		else
		{
			searchUrl += "&src_category=" + this._type;
		}
	}
	
			
	searchUrl += "&refine-polygon=" + this._refinePoloygon;
	
	if( override.area )
		searchUrl += "&src_polygon=" + escapeValue(override.area);
	else
		searchUrl += "&src_polygon=" + this._area;
	
	// #### FIX TO HANDLE UNDEFINED No. Rooms ####
	if (this._numberOfRooms)
	{
		searchUrl += "&NumRoomReqs=" + this._numberOfRooms;
		for(i=0 ; i<this._numberOfRooms; i++ )
		{
			searchUrl += "&roomReq_" + (i+1) + "=" + this._guestsPerRoom[i];
		}
	}
	
	if( override.refineGrading)
	{
		searchUrl += "&refine-grading=on";
		searchUrl += "&src_grading=" + this._grading;
	}
	else if(override.refineGrading != null && !override.refineGrading)
	{
		// do nothing 
	}
	else if(override.grading)
	{
		searchUrl += "&refine-grading=on";
		searchUrl += "&src_grading=" + override.grading;
	}
	else if( this._refineGrading )
	{
		searchUrl += "&refine-grading=on";
		searchUrl += "&src_grading=" + this._grading;
	}
	
	if( override.refinePricing != null )
	{
		if( override.refinePricing )
		{
			searchUrl += "&refine-price=on";
			
			if( override.minimumPricing )
				searchUrl += "&src_minprice=" + override.minimumPrice;
			else
				searchUrl += "&src_minprice=" + this._minimumPrice;
				
			if( override.maximumPricing )
				searchUrl += "&src_maxprice=" + override.maximumPrice;
			else
				searchUrl += "&src_maxprice=" + this._maximumPrice;
		}
	}
	else
	{
		if( this._refinePricing || this._minimumPrice || this._maximumPrice || override.maximumPrice || override.minimumPrice ) searchUrl += "&refine-price=on";

		if( override.minimumPrice >= 0 )
			searchUrl += "&src_minprice=" + override.minimumPrice;
		else if( this._minimumPrice >=0  ) 
			searchUrl += "&src_minprice=" + this._minimumPrice;
		
		if( override.maximumPrice )
			searchUrl += "&src_maxprice=" + override.maximumPrice;
		else if( this._maximumPrice ) 
			searchUrl += "&src_maxprice=" + this._maximumPrice;
	}
	
	if( override.refineSpecialOffers != null  )
	{
		if( override.refineSpecialOffers )
		{
			searchUrl += "&refine-specialoffers=on";
			
			if( override.specialOffer )
				searchUrl += "&src_specoff=" + override.specialOffer;
			else
				searchUrl += "&src_specoff=" + this._specialOffer;
		}
	}
	else if (override.specialOffer)
	{	
		searchUrl += "&refine-specialoffers=on";
		searchUrl += "&src_specoff=" + override.specialOffer;
	}
	else if( this._specialOffer )
	{
		searchUrl += "&refine-specialoffers=on";
		searchUrl += "&src_specoff=" + this._specialOffer;
	}
	
	if( override.refineAvailability != null )
	{
		if( override.refineAvailability )
		{
			searchUrl += "&refine-availability=on";
			searchUrl += "&nights=" + this._nights;
			searchUrl += "&staydate_day=" + this._startDay;
			searchUrl += "&staydate_month=" + this._startMonth;
			searchUrl += "&staydate_year=" + this._startYear;
		}
	}
	else if( this._refineAvailability )
	{
		searchUrl += "&refine-availability=on";
		searchUrl += "&nights=" + this._nights;
		searchUrl += "&staydate_day=" + this._startDay;
		searchUrl += "&staydate_month=" + this._startMonth;
		searchUrl += "&staydate_year=" + this._startYear;
	}
	
	if( override.refineDates != null )
	{
		if( override.refineDates )
		{
			if( override.startDay )
				searchUrl += "&start_day=" + override.startDay;
			else
				searchUrl += "&start_day=" + this._startDay;
			
			if( override.startMonth )
				searchUrl += "&start_month=" + override.startMonth;
			else
				searchUrl += "&start_Month=" + this._startMonth;
				
			if( override.startYear )
				searchUrl += "&start_year=" + override.startYear;
			else
				searchUrl += "&start_year=" + this._startYear;
				
			if( override.endDay )
				searchUrl += "&end_day=" + override.endDay;
			else
				searchUrl += "&end_day=" + this._endDay;
			
			if( override.endMonth )
				searchUrl += "&end_month=" + override.endMonth;
			else
				searchUrl += "&end_Month=" + this._endMonth;
				
			if( override.endYear )
				searchUrl += "&end_year=" + override.endYear;
			else
				searchUrl += "&end_year=" + this._endYear;
		}
	}
	else if ( this._refineDates )
	{
		searchUrl += "&refine-date=on";
		
		if( override.startDay )
			searchUrl += "&start_day=" + override.startDay;
		else
			searchUrl += "&start_day=" + this._startDay;
		
		if( override.startMonth )
			searchUrl += "&start_month=" + override.startMonth;
		else
			searchUrl += "&start_month=" + this._startMonth;
			
		if( override.startYear )
			searchUrl += "&start_year=" + override.startYear;
		else
			searchUrl += "&start_year=" + this._startYear;
			
		if( override.endDay )
			searchUrl += "&end_day=" + override.endDay;
		else
			searchUrl += "&end_day=" + this._endDay;
		
		if( override.endMonth )
			searchUrl += "&end_month=" + override.endMonth;
		else
			searchUrl += "&end_month=" + this._endMonth;
			
		if( override.endYear )
			searchUrl += "&end_year=" + override.endYear;
		else
			searchUrl += "&end_year=" + this._endYear;
	}
	
	if( this._anyDate )
		searchUrl += "&src_anydate=yes";
		
	if( override.facility != null )
	{
		if( override.facilityMode == "add" )
		{
			searchUrl += "&refine-facility=on";
			
			if( this._facility.length > 0 )
				searchUrl += "&src_facility=" + this._facility;
			searchUrl += "&src_facility=" + override.facility;
		}
		else if(override.facilityMode == "remove")
		{
			tempString = "," + this._facility + ",";
			tempString = tempString.replace(override.facility, "");
			tempString = tempString.replace(",,", ",");
			
			if( tempString.indexOf(",") == 0 ) tempString = tempString.substr(1);
			if( tempString.lastIndexOf(",") == tempString.length-1 ) tempString = tempString.substr(0, tempString.length-1);
			
			searchUrl += "&refine-facility=on";
			searchUrl += "&src_facility=" + tempString;
		}
	}
	else
	{
		if( this._refineFacility )
		{
			searchUrl += "&refine-facility=on";
			searchUrl += "&src_facility=" + this._facility;
		}
	}
	
	if( override.refineAccessable != null )
	{
		if( override.refineAccessable )
			searchUrl += "&refine-tfa=on&tfa=-1";	
	}
	else
	{
		if( this.HasAccessable() )
			searchUrl += "&refine-tfa=on&tfa=-1";
	}
	
	
	return searchUrl;
}

/* Get the search url based on the specified override */
Search.prototype.GetSearchUrl = function(override)
{
	var url;
	
	url = "?";
	
	if( override.type && override.type == "1939" )
	{
		url = "//what-to-see-and-do/activities/walking/searchresults?";
	}
	else if( override.type && override.type == "3929" )
	{
		url = "//what-to-see-and-do/activities/leisure-cycling/searchresults?";
	}
	
	return url;
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
	strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
    )
{
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );
     
    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1){
    // Relace out the current instance.
    strText = strText.replace( strTarget, strSubString )
     
    // Get the index of any next matching substring.
    intIndexOfMatch = strText.indexOf( strTarget );
    }
     
    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
  }
