/**
 * @author scott
 */
var containerId = "map_canvas";
var deflat = -33.83209212904;
var deflong = 151.184994183803;
var latlng;
var mapwidth = 680;
var mapheight = 440;
var mapzoom = 12;
var geocoder;
var map;
var marker;
var infowindow;
var address, latitude, longitude, zoom, pname;

function initGMap(addressQ, latitudeQ, longitudeQ, widthQ, heightQ, zoomQ, pnameQ){
  address = addressQ;
  latitude = latitudeQ;
  longitude = longitudeQ;
  width = parseInt(widthQ);
  height = parseInt(heightQ);
  zoom = parseInt(zoomQ);
  pname = pnameQ;
  setTimeout('initialise()',1000);
}

function initialise() {
  if (width)
    mapwidth = width;
  if (height)
    mapheight = height;
  container = document.getElementById(containerId);
  container.style.width = mapwidth+"px";
  container.style.height = mapheight+"px";
  if (zoom)
    mapzoom = zoom;
  var myOptions = {
    zoom: mapzoom,
    center: new google.maps.LatLng(deflat, deflong),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(container, myOptions);
  
  var contentString = '<div id="infowindowContent">'+
        '<h2>' + pname + '</h2>'+
        '<p>' + address + '</p>'+
        '</div>';

  infowindow = new google.maps.InfoWindow({
      content: contentString
  });
  
  if(latitude.length == 0 || longitude.length == 0){
    if (address.length > 0) {
      geocoder = new google.maps.Geocoder();
      if (geocoder) {
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title: pname
            });
          } else {
            //alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }
    }
  } else {
    latlng = new google.maps.LatLng(latitude, longitude);
    map.setCenter(latlng);
    marker = new google.maps.Marker({
        map: map, 
        position: latlng,
        title: pname
    });
  }
  
  setTimeout('addEars()',2000);
  
  
}

function addEars(){
  google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  google.maps.event.addListener(infowindow, 'closeclick', function() {
      map.setCenter(marker.getPosition());
    });
}

