var start = 0;
var term = '';
var page = 0;
var locationArray;
var noTimes=false;
	
var responseSuccess = function(o){
/* Please see the Success Case section for more
 * details on the response object's properties.
 * o.tId
 * o.status
 * o.statusText
 * o.getResponseHeader[ ]
 * o.getAllResponseHeaders
 * o.responseText
 * o.responseXML
 * o.argument
 */
document.getElementById('subloc').disabled=false;
	if(o.responseText !== undefined){ 
		showLoc(o.responseText);
	}
};

var responseFailure = function(o){
document.getElementById('location_list').innerHTML = '<p class="spacing">We are currently experiencing a problem on the site.</p><p>Please wait a moment before trying again. If this problem persists, please <a href="/contact_us/">contact us</a>.</p>' ;
document.getElementById('subloc').disabled= false;
}

var callback =
{
  success:responseSuccess,
  failure:responseFailure,
  timeout: 20000 
};

var doLoc = function(){
	start = new Date();
	start = start.getTime();
	value = encodeURI(document.getElementById('txtLocation').value);
	var cObj = YAHOO.util.Connect.asyncRequest('GET', '/ajax/search-google.php?start='+start+'&v='+value, callback); 
}

var showLoc = function(strRet){
	var jsonResponse = eval("(" + strRet + ")");
	 
	if(jsonResponse.Status.code!=200){
		noResults();
	}else{
		if(jsonResponse.Placemark.length==1 && noTimes==false){
			var fullcord = String(jsonResponse.Placemark[0].Point.coordinates);
			var coord = new Array();
			coord = fullcord.split(',');
			recenter(coord[1],coord[0]);
		}else{
			document.getElementById('location_list').innerHTML='<h5 class="link" style="margin-top:5px">Please select a location</h5>';
			var i;
			for (i = 0; i < jsonResponse.Placemark.length; i++){
				var fullcord = String(jsonResponse.Placemark[i].Point.coordinates);
				var coord = new Array();
				coord = fullcord.split(',');
				document.getElementById('location_list').innerHTML += "<p class='link'><a href='javascript:recenter(" +coord[1]+","+coord[0]+");'>"+jsonResponse.Placemark[i].address+"</a></p>";
			}
		}
	}
	document.getElementById('subloc').disabled= false;
}

function setLocation(i){
	recenter(locationArray[i][0],locationArray[i][1]);
}

function callLoc(){
	loading(true);
	doLoc();
}

function loading(dis){
	document.getElementById('location_list').innerHTML = '<p class="spacing">Searching...</p><div id="loading"><p><img style="height:16px;width:16px" src="/imgs/loading.gif" alt="loading"/></p></div>';
	if(dis)	document.getElementById('subloc').disabled= true;
}
function callLocNext(t,p){
	loading();
	term = t;
	page = p;
	doLocNext();
}

function noResults(){
	
	if(noTimes==false){
		if(cat=='h'){
			c='holidays';
		}else if(cat=='w'){
			c='workshops';
		}else{
			c='classes';
		}
		document.getElementById('location_list').innerHTML = '<p style="margin-top:1em">No results found, please check your location.</p><p style="margin-top:1em">Or double click anywhere on the map, to view those pilates '+c+' nearby.</p>';
	}else{
		document.getElementById('location_list').innerHTML = '<p style="margin-top:1em">No results found, please check your location.</p><p style="margin-top:1em">Double click on the map, or click and drag the red marker to specify your pilates location.</p>';		
	}
	document.getElementById('subloc').disabled= false;
}

var doLocNext = function(){
	start = new Date();
	start = start.getTime();
	var cObj = YAHOO.util.Connect.asyncRequest('GET', '/ajax/search.php?start='+start+'&v='+term+'&page='+page, callback); 	
}

// Centers the map on the given coordinates and chooses either
// satellite, hybrid or map type
function recenter(latitude, longitude) {  
  if(noTimes==false) loading();
  map.clearOverlays();
  point = new GLatLng(latitude, longitude);
  marker = new GMarker(point, {draggable:true,bouncy:true});

  map.addOverlay(marker);
  
  if(noTimes==false){
  	doTimetables(latitude,longitude,true);
  }else{
	map.setCenter(point,15);
	map.panTo(point);
	document.locations_form.lat.value = point.lat();
	document.locations_form.lon.value = point.lng();    
	document.locations_form.use_map.checked = true;		  	
  }

  GEvent.addListener(marker,"dragend", function() {
  var point = marker.getPoint();
  map.clearOverlays();
  map.addOverlay(marker);
  
  if(noTimes==false){
  	doTimetables(point.lat(),point.lng(),false);
  }else{
  	map.setCenter(point,15);
	document.locations_form.lat.value = point.lat();
	document.locations_form.lon.value = point.lng();    
	document.locations_form.use_map.checked = true;	
  }
  
  map.panTo(point);
  });
}
