// The map object
var map;

/**
/**
 * Initiates searching with the POI web service
 */
function search(dsearchtype,dbusinessname,dphysicalregion,dphysicalsuburb,dphysicalstreetnumber,dphysicalstreet){
	searchtype = dsearchtype;
	businessname = dbusinessname;
	town = dphysicalregion;
	suburb = dphysicalsuburb;
	streetnumber = dphysicalstreetnumber;
	street = dphysicalstreet;
	
	businessdetails = [businessname];
	
	switch(searchtype){
		case 1:
			// Street Number and Street Name
			var params = 'name=' + encodeURIComponent(streetnumber + ' ' + street);
			break;
		case 2:
			// Street Name
			var params = 'name=' + encodeURIComponent(street);
			break;
		case 3:
			// Suburb
			var params = 'locality=' + encodeURIComponent(suburb);
			break;
		case 4:
			// Town
			var params = 'locality=' + encodeURIComponent(town);
			break;
		default:
			var params = 'name=' + encodeURIComponent(streetnumber + ' ' + street);
	}
	
	//var params = 'locality=' + encodeURIComponent(town) + '&suburb=' + encodeURIComponent(suburb) + '&name=' + encodeURIComponent(name);
    
	var url = '/site-profile/resources/geo-smart/proxy.php';
    
    // use the Prototype Ajax.Request class to search with the POI web service
    var req = new Ajax.Request(url, {method: 'get', parameters: params, onComplete: searchComplete});
   	return false;
}

/**
 * Callback function that will be called by the Prototype library when the background request
 * has completed
 */
function searchComplete(response){
	// evaluate the JSON data
    var data = eval('(' + response.responseText + ')');
    
    // get the map's base layer and remove previous search results, if any
    var layer = map.getLayer('base');
    layer.clear();
	
	//alert(data.pois.poi.length);
	
	var datalength = data.pois.poi.length;
	
	var hide = false;
	
	switch(datalength){
		case 0:
			switch(searchtype){
				case 1:
					searchtype = 2;
					break;
				case 2:
					searchtype = 3;
					break;
				case 3:
					searchtype = 4;
					break;
				default:
					var hide = true;
					$('locationmap').hide();
					$('locationmapdiv').hide();
					$('locationadsdiv').show();
			}
			
			if(!hide){
				search(searchtype,businessname,town,suburb,streetnumber,street);
			}
			
			break;
		case 1:
			// add the search results to the map
			layer.addFeaturesJson(data.pois.poi, initFeature, businessdetails);
			
		    map.centerOnLayer('base', function(){
		        // if a single match open the popup for the feature
		        if(searchtype == 1) {
		            var poi = data.pois.poi[0];
		            var feature = GSUtil.getFeatureById(layer.data, poi.id);
		            feature.showInfoWindow();
		        } else{
		        	map.closeInfoWindow();
		    		map.clearLayers();
		        }
		    });
			break;
		default:
			layer.addFeaturesJson(data.pois.poi, initFeature, businessdetails);
			
			map.centerOnLayer('base', function(){
				map.closeInfoWindow();
  				map.clearLayers();
			});
	}
}

/**
 * Callback function, called for each feature added to the map
 */
function initFeature(feature, data, args){
    // build the HTML string to display in the popup window when the feature is clicked
    var html = '';
    html += '<p><strong>' + args[0] + '</strong></p>';
    html += '<p><strong>' + feature.name + '</strong><br />' + feature.suburb + '<br />' + feature.region;
    feature.infoHtml = html;
    
    // attach a 'click' event handler to the feature to show the popup
    feature.addEventHandler('click', function(e) {
        this.showInfoWindow();
        GSUtil.cancelEvent(e);
    });
}

/**
 * Remove search results from the map, re-center on NZ, and then do the
 * default form reset action
 */
function clear() {
    map.closeInfoWindow();
    map.clearLayers();
    map.centerOnNewZealand();
    return true;
}

function showmap(){
	$('locationmap').show();
	$('locationmapdiv').show();
}

function hidemap(){
	$('locationmap').hide();
	$('locationmapdiv').hide();
}

//window.onload = init;
