function WAMapRef(mapObj)  {
  this.obj = mapObj;
  this.directions = false;
  this.icons = [];
  this.markers = [];
  this.addresses = [];
  this.points = [];
  this.getPointByAddress = getPointByAddressFunc;
  return this;
}

function waitForGeo()  {
  var startDate = new Date();
  var endDate = new Date();
  while ((endDate-startDate) < 200)  {
    endDate = new Date();
  }
}

function WAMapPoint(theMarker, theAddress, theIcon)  {
  this.icon = theIcon;
  this.marker = theMarker;
  this.address = theAddress;
  return this;
}

function getPointByAddressFunc(value,attname)  {
  if (!attname) attname = "street";
  for (var x=0; x < this.addresses.length; x++) {
    if (eval("this.addresses[x]."+attname) == value)  {
      return WAMapPoint(this.markers[x],this.addresses[x],this.icons[x]);
    }
  }
  return false;
}


function wagmp_map_1(fromAddr) {
  if(GBrowserIsCompatible()) {
    if(!document.getElementById('wagmp_map_1')) return false;
    var map = new GMap2(document.getElementById('wagmp_map_1'));
    wagmp_map_1_obj = new WAMapRef(map);
    map.enableContinuousZoom();
    map.enableDoubleClickZoom();
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var geocoder = new GClientGeocoder();
    
    var fromAddress = {
      enabled: true,
      street: '',
      city: '',
      state: '',
      zip: '',
      country: '',
      full: ''+fromAddr+''
    };

    var icon_0 = new GIcon();
    icon_0.image = 'http://google.webassist.com/google/markers/house/slate.png';
    icon_0.shadow = 'http://google.webassist.com/google/markers/house/shadow.png';
    icon_0.iconSize = new GSize(25,14);
    icon_0.shadowSize = new GSize(25,14);
    icon_0.iconAnchor = new GPoint(9,13);
    icon_0.infoWindowAnchor = new GPoint(9,0);
    icon_0.printImage = 'http://google.webassist.com/google/markers/house/slate.gif';
    icon_0.mozPrintImage = 'http://google.webassist.com/google/markers/house/slate_mozprint.png';
    icon_0.printShadow = 'http://google.webassist.com/google/markers/house/shadow.gif';
    icon_0.transparent = 'http://google.webassist.com/google/markers/house/slate_transparent.png';
    
    var address_0 = {
      street: 'Sainte-Cecile-d\'Andorge',
      city: '30110 Sainte-Cecile-d\'Andorge',
      state: '',
      zip: '30110',
      country: 'France',
      infowindow: 'default',
      infowindowtext: '<span style="font: 12px Verdana, Arial, Helvetica, sans-serif; color: black;"><strong>Mas Pinet<strong><br><br />Sainte Cecile d\'Andorge<br />30110 Sainte-Cecile-d\'Andorge, 30110 France<br /><br><a href="#" onClick="updateInfoWindow()">Get directions here</a></span>',
      full: '44.2505111111,3.98568611111',
      isdefault: true,
      addressType: 'coordinates',
      loop: '',
      latitude: '44.2505111111',
      longitude: '3.98568611111',
      markerStyle: 'Google Traditional (flat)',
      markerColor: 'Pacifica'
     };
    
    geocoder.getLatLng (
      address_0.full,
      function(point) {
        if(point) {
          var marker = new GMarker(point, icon_0);
          GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml(address_0.infowindowtext);
          });
          if(!fromAddress.enabled || 'address_0' != 'address_0') {
            map.setCenter(point, 13);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(address_0.infowindowtext);
          }
          wagmp_map_1_obj.markers.push(marker);
          wagmp_map_1_obj.addresses.push(address_0);
          wagmp_map_1_obj.icons.push(icon_0);
          wagmp_map_1_obj.points.push(point);
        }
        else {
          map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        }
      }
    );
     waitForGeo();

    directions = new GDirections(map, document.getElementById('wagmp_directions_1'));
	GEvent.addListener(directions, "error", function() {
	  map.setCenter(wagmp_map_1_obj.points[0], 13);
      map.addOverlay(wagmp_map_1_obj.markers[0]);
      wagmp_map_1_obj.markers[0].openInfoWindowHtml(address_0.infowindowtext);
	});
    directions.load('from: ' + fromAddress.full + ' to: ' + address_0.full);
    wagmp_map_1_obj.directions = directions;

  }
}function WA_setCookie(cookieobj, cookiename, cookievalue, cookiecheck, settype, expdays)     {
  var theValue = "";
  if (settype == 1)     {
    for (var n=0; n<cookievalue.length; n++)    {
      if (cookievalue[n].checked)     {
        theValue = cookievalue[n].value;
      }
    }
  }
  else if (settype == 2)     {
    for (var n=0; n<cookievalue.options.length; n++)    {
      if (cookievalue.options[n].selected)     {
        if (theValue != "")
          theValue += ", ";
        theValue += cookievalue.options[n].value;
      }
    }
  }
  else      {
    theValue = cookievalue
  }

  if (cookiecheck)     {
	if (expdays)  {
	  cookieobj.expires = daysToExpire(expdays);
	}
	else  {
	  cookieobj.expires = "";
	}
    cookieobj.setValue(cookiename, theValue, expdays);
  }
}

function updateInfoWindow()  {
  wagmp_map_1_obj.markers[0].openInfoWindowHtml('<span style="font: 12px Verdana, Arial, Helvetica, sans-serif; color: black;"><strong>from address:</strong><br><input id="fromaddress" type="text"><br><strong>city, state:</strong><br><input id="fromcity" type="text"><br><input type="button" value="get directions" onClick="wagmp_map_1(document.getElementById(\'fromaddress\').value+\', \'+document.getElementById(\'fromcity\').value)"></span>')
}

