//<!-- 
//<![CDATA[
var mapviewer, geocoder;
var qs, street, city, state, postal_code, country_code_quick, country_code_full, searchbtn, quicksearchbtn;
var geocode_status, message, results_panel;
var results = new Array();
var markers = new Array();
var max_results = 10;
var pan_zoom_widget;
var overview_widget;
var tools_widget;
var map_type_widget;

//function onLoad() {
function callGeocoder( fields ) {
    //Add the map 
    mapviewer = new MultimapViewer( document.getElementById( 'mapviewer' ) );
//    mapviewer.goToPosition( new MMLocation( new MMLatLon( -33.8661, 151.2064 ) ,15 ) );
    mapviewer.goToPosition( new MMLocation( new MMLatLon( 53.49645, -2.22758 ) ,15 ) );
    pan_zoom_widget = new MMPanZoomWidget ();
    overview_widget = new MMOverviewWidget ();
    tools_widget  = new MMToolsWidget ();
	map_type_widget = new MMMapTypeWidget ();
	mapviewer.addWidget( overview_widget  );
	mapviewer.addWidget( pan_zoom_widget  );
	mapviewer.addWidget( map_type_widget  );

    // Setup some references that will be useful later.
    qs = document.getElementById( 'qs' ); 
    street = document.getElementById( 'street' );
    city = document.getElementById( 'city' )
    state = document.getElementById( 'state' );
    postal_code = document.getElementById( 'postal_code' );
    country_code_quick = document.getElementById( 'country_code_quick' );
    country_code_full = document.getElementById( 'country_code_full' );
    searchbtn = document.getElementById( 'searchbtn' );
    quicksearchbtn = document.getElementById( 'quicksearchbtn' ); 
    
    geocode_status = document.getElementById( 'geocodestatus' );

    //Create a new geocoder object specifying a callback function
    geocoder = new MMGeocoder(processResults);
//}

//function callGeocoder( fields ) {
    // create a new address object
    var address = new MMAddress();
    address.street = fields.street;
    address.city = fields.city;
    address.state = fields.state;
    address.postal_code = fields.postal_code;
    address.country_code = fields.country_code;
    address.qs = fields.qs;
    
    // clear any existing items from previous geocoding requests
    cleanUp();

    // inform the user a geocode is occurring
    updateGeocodeStatus('startGeocode');

    // perform the geocode
    geocoder.count = max_results;
    geocoder.geocode(address);
}

function processResults() {
    // callback function registered with the geocoder to handle geocoding results
    // inform the user a geocode has finished
    updateGeocodeStatus('endGeocode');
    // if an error occurred, inform the user
    if (geocoder.error_code && geocoder.error_code != 'MM_GEOCODE_MULTIPLE_MATCHES') {
        return false;
    }
    results = geocoder.result_set;
    var ol = document.createElement('ol');
     
    //loop through the result set
    for (var count=0; count < results.length; count++) {
        var address = results[count].address;
        var li = document.createElement('li');
        //add a link for each result
        var anchor = document.createElement('a');

        anchor.href = '#';
        anchor.result_count = count;
        anchor.onclick = function () { moveToResult(this.result_count); return false; };
        anchor.appendChild(document.createTextNode(address.display_name));
        li.appendChild(anchor);
        ol.appendChild(li);

        //add a marker for each result
        var marker = mapviewer.createMarker(results[count], { 'label' : address.display_name, 'text' : (count+1) });
        //add an infobox for each result
        marker.setInfoBoxContent('<p>' + address.display_name + '<' + '/p>');
        markers.push(marker);
    }


    //if there were multiple matches, display all the results
    if (geocoder.error_code == 'MM_GEOCODE_MULTIPLE_MATCHES') {
    	var location = mapviewer.getAutoScaleLocation( markers );
    	mapviewer.goToPosition( location ); 
    	
    //otherwise show the only match at the optimal zoom factor
    } else {
    	mapviewer.goToPosition( results[0] );
    }
}
             
function cleanUp() {
    // Clean up the HTML containers
    //remove any markers
//    mapviewer.removeAllOverlays();
    markers = new Array();
}
     
function updateGeocodeStatus( type ) {
    if( type == 'startGeocode' ) {
        // if the geocode is starting, disable the form inputs and inform the user
//        geocode_status.style.display = 'block';
        disableFields(true);
    } else {
        // if the geocode is ending, enable the form inputs and inform the user
        geocode_status.style.display = 'none';
        disableFields(false);
    }
}
  
function disableFields( bool ) {
    // disable the user input fields
//    qs.disabled = bool; 
//    street.disabled = bool;
//    city.disabled = bool;
//    state.disabled = bool;
//    postal_code.disabled = bool;
//    country_code_quick.disabled = bool;
//    country_code_full.disabled = bool;
//    searchbtn.disabled = bool; 
//    quicksearchbtn.disabled = bool;  
}

function moveToResult (count) {
    //move the map specified location and open the info box
    mapviewer.goToPosition(results[count]);
    markers[count].openInfoBox();
}

//MMAttachEvent( window, 'load', onLoad );
//]]> 
// -->

