/*
 * Google API Required - Wrappers for Maps and Geocoding
 *
 * You may not copy/use this code without permission.
 * 
 * @author Justin Carlson <justin.carlson@gmail.com>
 * @version 0.0.1
 *
 */

// define global vars
var map; // google maps
var gdir; // google directions
var geocoder; // google geocoder
var previousEntry = null;

// speed trap ( limit number of active queries )
var busy = false;

// base model for markers (all markers are based on this one)
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = base_url + "/images/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

// init called by the browser onload event, setup various api objects
function gInitialize() {

	if (GBrowserIsCompatible()) {

        // create geocoder for location lookups
        geocoder = new GClientGeocoder();

    }

}

// used to draw a map on user's page
function usermapload() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2($('#map')[0]);
        map.setCenter(new GLatLng(glon,glat), 8);
      }
    }

// handle users adding to map
function addtomap() {
	alert('Disabled');
}

// geocodes profile page
function getProfileCoord() {

    $('#lat')[0].value = '';
    $('#lon')[0].value = '';

    var location = '';
    var privacy = $('#addressprivacy').val();

    if(privacy<2) {
        return;
    } else {

        if(privacy==3) {
            if($('#line1').val().length>0) location+=$('#line1').val();
            if($('#city').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#city').val();
            }
            if($('#state').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#state').val();
            }
            if($('#postal').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#postal').val();
            }
            if($('#country').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#country').val();
            }

        }
        if(privacy==2) {
            if($('#city').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#city').val();
            }
            if($('#state').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#state').val();
            }
            if($('#postal').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#postal').val();
            }
            if($('#country').val().length>0){
              if(location.length>0) location+=', ';
              location+=$('#country').val();
            }
        }
    }

    $('#locationresult').html('<span class="101">Mapping this location...</span>');
	if (geocoder) {
	        geocoder.getLocations(
	          location,
	          function(response) {
	            if (!response) {
	              $('#locationresult').html('<span class="404">This location could not be mapped.</span>');
	            } else {
	            	if( typeof response.Placemark != "undefined" ) {

	            		if (response.Placemark.length>1) {

	            			$('#locationresult').html('<span class="404">That location is too ambiguous, please be more specific.</span>');

		            	} else {

                            $('#locationresult').html('<span class="200">This location was mapped successfully.</span>');
                            $('#lat')[0].value = response.Placemark[0].Point.coordinates[0];
                            $('#lon')[0].value = response.Placemark[0].Point.coordinates[1];
                            setTimeout("$('#locationresult').fadeOut();",2000);

		            	}
			         } else {
			  	          $('#locationresult').html('<span class="404">This location could not be mapped.</span>');
			         }
	            }
	          }
	        );
	}
    setTimeout("$('#locationresult').fadeOut();",6000);

}

// validates and adds an origin
function getLocationCoord(location) {

    $('#locationcoord')[0].value = '';
    $('#locationresult').html('<span class="101">Mapping this location...</span>');
	if (geocoder) {
	        geocoder.getLocations(
	          location,
	          function(response) {
	            if (!response) {
	              $('#locationresult').html('<span class="404">This location could not be mapped.</span>');
	            } else {
	            	if( typeof response.Placemark != "undefined" ) {

	            		if (response.Placemark.length>1) {

	            			$('#locationresult').html('<span class="404">That location is too ambiguous, please be more specific.</span>');

		            	} else {

                            $('#location')[0].value = response.Placemark[0].address;
                            $('#locationresult').html('<span class="200">This location was mapped successfully.</span>');
                            $('#locationcoord')[0].value = response.Placemark[0].Point.coordinates;
                            setTimeout("$('#locationresult').fadeOut();",2000);

		            	}
			         } else {
			  	          $('#locationresult').html('<span class="404">This location could not be mapped.</span>');
			         }
	            }
	          }
	        );
	}
    setTimeout("$('#locationresult').fadeOut();",6000);
}

gInitialize();

