  var multiTown = {};

 $(document).ready(function () {

	var localSearch = new GlocalSearch();

	/* Multi Town Click */
	multiTown = function (loc,category,radius,image,adv) {

		if (category != '') {

			$(image).css('visibility','visible');
	
			checkCache(loc);
			//setTimeout("window.open('search.php?search=1&bypass=1&location="+escape(loc)+"&category="+category+"&radius="+escape(radius)+"','_self')",2000);

			// Set 

			$('input#loc').attr('value',decodeURI(loc));
			$('input#locA').attr('value',decodeURI(loc));

			$('input#hidden_bypass1').attr('value',1);
			$('input#hidden_bypass2').attr('value',1);

			$('#hidden_useLocationIdentifier').attr('value',1);
			$('#hidden_useLocationIdentifier2').attr('value',1);

			if (adv == 1) {
				$("form#searchFormAdvanced").submit();
			} else {
				$("form#searchForm").submit();
			}


		} else {
			alert('error');
		}

	};

	/* Check for cached coordinates */
	function checkCache(loc) {
		
		$.ajax({
		type: "GET",
		url: "ajax/check.php",
		data: "loc="+loc,
		async:false,
		success: function(msg) {
			if (msg != 'CACHED') {
				geoCode(loc,cacheCoords);
			}
			return false;
		}

		});
	}

	/* Cache coordinates */
	function cacheCoords(loc,lat,lng) {
		$.ajax({
		type: "POST",
		url: "ajax/cache.php",
		data: "loc="+loc+"&lat="+lat+"&lng="+lng,
		async:false
		});
	}

	/* Geocode location */
	function geoCode(location, callbackFunction) {
		localSearch.setSearchCompleteCallback(null,
    		function() {
				if (localSearch.results[0]) {
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					callbackFunction(location,resultLat,resultLng);
				} else {
					alert("Sorry, your location could not be found.");
				}
			});
		localSearch.execute(location + ", UK");
	}

	/* Location gets the focus */
	$("input#loc").focus(function () {
		if (this.value == 'Location or Postcode') {
			this.value = '';
		}
	});

	/* Location looses the focus */
	$("input#loc").blur(function () {
		if (this.value == '') {
			this.value = 'Location or Postcode';
		} else {
			$("input#loc").removeClass('locError');
		}
	});

	/* Quick Search */
	$("form#searchForm").submit(function () {

		var loc = $("input#loc").attr('value');
		var radius = $("select#radius").attr('value');
		var bypass = $("input#hidden_bypass1").attr('value');
		var useLocationIdentifier = $("input#hidden_useLocationIdentifier").attr('value');
		var category;

		if ($("input#cat1").attr('checked')) { category = 1; }
		if ($("input#cat2").attr('checked')) { category = 2; }
		if ($("input#cat3").attr('checked')) { category = 3; }

		if (category != '') {

			var temp = ""+loc;
	
			if (loc != '' && loc != 'Location or Postcode' && loc.length >= 3) {
	
				$("button#searchButton").html('One moment please...');
				$("button#searchButton").attr('disabled','disabled');
				$("input#loc").removeClass('locError');
	
				while (temp.indexOf('.')>-1) {
					pos = temp.indexOf('.');
					temp = "" + (temp.substring(0,pos)+temp.substring((pos+1), temp.length));		
				}

				/* Location looses the focus 
	
				//while (temp.indexOf(',')>-1) {
					//pos2 = temp.indexOf(',');
					//temp = "" + (temp.substring(0,pos2)+temp.substring((pos2+1), temp.length));		
				//}
	
				//loc = temp;

				*/
	
				checkCache(loc);

				loc = escape(loc);
	
				setTimeout("window.open('search.php?search=1&location="+escape(loc)+"&category="+category+"&radius="+escape(radius)+"&bypass="+escape(bypass)+"&useLocationIdentifier="+escape(useLocationIdentifier)+"','_self')",2000);
	
			} else {
				$("input#loc").addClass('locError');
			}

		}

	});

	/* Accomodation selected */
	$("input#cat1A").click(function () {
		$("div#busTypes1").show();
		$("div#busTypes2").hide();
		$("div#busTypes3").hide();
		$("div#pet1").show();
		$("div#pet2").hide();
		$("div#pet3").hide();
		$("div#p1").show();
		$("div#p2").hide();
		$("div#p3").hide();
		$("input.busbox2").attr('checked',false);
		$("input.busbox3").attr('checked',false);
	});

	/* Places to eat selected */
	$("input#cat2A").click(function () {
		$("div#busTypes1").hide();
		$("div#busTypes2").show();
		$("div#busTypes3").hide();
		$("div#pet1").hide();
		$("div#pet2").show();
		$("div#pet3").hide();
		$("div#p1").hide();
		$("div#p2").show();
		$("div#p3").hide();
		$("input.busbox1").attr('checked',false);
		$("input.busbox3").attr('checked',false);
	});

	/* Days out selected */
	$("input#cat3A").click(function () {
		$("div#busTypes1").hide();
		$("div#busTypes2").hide();
		$("div#busTypes3").show();
		$("div#pet1").hide();
		$("div#pet2").hide();
		$("div#pet3").show();
		$("div#p1").hide();
		$("div#p2").hide();
		$("div#p3").show();
		$("input.busbox1").attr('checked',false);
		$("input.busbox2").attr('checked',false);
	});

	/* Advanced Search */

	$("form#searchFormAdvanced").submit(function () {

		var loc = $("input#locA").attr('value');
		var temp = ""+loc;
	
		if (loc != '' && loc != 'Location or Postcode' && loc.length >= 3) {
	
			$("button#searchButtonA").html('One moment please...');
			$("button#searchButtonA").attr('disabled','disabled');
			$("input#locA").removeClass('locError');
	
			while (temp.indexOf('.')>-1) {
				pos = temp.indexOf('.');
				temp = "" + (temp.substring(0,pos)+temp.substring((pos+1), temp.length));		
			}
	
			while (temp.indexOf(',')>-1) {
				pos2 = temp.indexOf(',');
				temp = "" + (temp.substring(0,pos2)+temp.substring((pos2+1), temp.length));		
			}
	
			loc = temp;
	
			checkCache(loc);
	
			//setTimeout("window.open('search.php?search=1&location="+escape(loc)+"&category="+category+"&radius="+escape(radius)+"','_self')",2000);
			setTimeout("window.open('search.php?"+$("#searchFormAdvanced").serialize()+"','_self')",2000);

		} else {
			$("input#locA").addClass('locError');
		}


	});







 });