var GMAP_map;

/* Customized zoom control */

function GMAP_ZoomControl() {
}

GMAP_ZoomControl.prototype = new GControl();

GMAP_ZoomControl.prototype.initialize = function(map) {
      var container = document.createElement("div");

      var moveUpDiv = document.createElement("div");
      this.setButtonStyle2_(moveUpDiv);
      this.setPosition_(moveUpDiv, 22, 0);
      container.appendChild(moveUpDiv);
      moveUpDiv.appendChild(document.createTextNode("Up"));
      moveUpDiv.innerHTML = "<table cellspacing='0' cellpadding='0'><tr><td valign='center' align='center'><img border='0' src='design/pic/gmap_up.png' /></td></tr></table>";
	  GEvent.addDomListener(moveUpDiv, "click", function() {
        GMAP_map.panDirection(0, 1);
      });

      var moveLeftDiv = document.createElement("div");
      this.setButtonStyle2_(moveLeftDiv);
      this.setPosition_(moveLeftDiv, 0, 41);
      container.appendChild(moveLeftDiv);
      moveLeftDiv.appendChild(document.createTextNode("Left"));
      moveLeftDiv.innerHTML = "<table cellspacing='0' cellpadding='0'><tr><td valign='center' align='center'><img border='0' src='design/pic/gmap_left.png' /></td></tr></table>";
      GEvent.addDomListener(moveLeftDiv, "click", function() {
        GMAP_map.panDirection(1, 0);
      });

      var moveRightDiv = document.createElement("div");
      this.setButtonStyle2_(moveRightDiv);
      this.setPosition_(moveRightDiv, 44, 41);
      container.appendChild(moveRightDiv);
      moveRightDiv.appendChild(document.createTextNode("Right"));
      moveRightDiv.innerHTML = "<table cellspacing='0' cellpadding='0'><tr><td valign='center' align='center'><img border='0' src='design/pic/gmap_right.png' /></td></tr></table>";
      GEvent.addDomListener(moveRightDiv, "click", function() {
        GMAP_map.panDirection(-1, 0);
      });

      var moveDownDiv = document.createElement("div");
      this.setButtonStyle2_(moveDownDiv);
      this.setPosition_(moveDownDiv, 22, 82);
      container.appendChild(moveDownDiv);
      moveDownDiv.appendChild(document.createTextNode("Down"));
      moveDownDiv.innerHTML = "<table cellspacing='0' cellpadding='0'><tr><td valign='center' align='center'><img border='0' src='design/pic/gmap_down.png' /></td></tr></table>";
      GEvent.addDomListener(moveDownDiv, "click", function() {
        GMAP_map.panDirection(0, -1);
      });


      var zoomInDiv = document.createElement("div");
      this.setButtonStyle_(zoomInDiv);
      this.setPosition_(zoomInDiv, 22, 130);
      container.appendChild(zoomInDiv);
      zoomInDiv.appendChild(document.createTextNode("+"));
      zoomInDiv.innerHTML = "<table cellspacing='0' cellpadding='0'><tr><td valign='center' align='center'><img border='0' src='design/pic/gmap_zin.png' /></td></tr></table>";
      GEvent.addDomListener(zoomInDiv, "click", function() {
        GMAP_map.zoomIn();
      });

      var zoomOutDiv = document.createElement("div");
      this.setButtonStyle_(zoomOutDiv);
      this.setPosition_(zoomOutDiv, 22, 170);
      container.appendChild(zoomOutDiv);
      zoomOutDiv.appendChild(document.createTextNode("-"));
      zoomOutDiv.innerHTML = "<table cellspacing='0' cellpadding='0'><tr><td valign='center' align='center'><img border='0' src='design/pic/gmap_zout.png' /></td></tr></table>";
      GEvent.addDomListener(zoomOutDiv, "click", function() {
        GMAP_map.zoomOut();
      });

      GMAP_map.getContainer().appendChild(container);
      return container;
    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
    GMAP_ZoomControl.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
    }

    // Sets the proper CSS for the given button element.
    GMAP_ZoomControl.prototype.setButtonStyle_ = function(button) {
      button.style.textDecoration = "none";
      button.style.color = "#000000";
      button.style.backgroundColor = "transparent";
      button.style.font = "24px Arial";
      button.style.border = "1px solid #888888";
      button.style.padding = "0px";
      button.style.marginBottom = "3px";
      button.style.textAlign = "center";
      button.style.verticalAlign = "middle";
      button.style.width = "36px";
      button.style.height = "36px";
      button.style.cursor = "pointer";
    }

    GMAP_ZoomControl.prototype.setButtonStyle2_ = function(button) {
    	this.setButtonStyle_(button);
		button.style.font = "12px Arial";
//		button.style.paddingTop = "10px";
	}

    GMAP_ZoomControl.prototype.setPosition_ = function(button, x, y) {
    	button.style.position = "absolute";
		button.style.left = x+"px";
		button.style.top =  y+"px";
	}

/* *********************************** */

var geocoder = null;
var show_location = '';
var defPoint = new GLatLng(37.4419, -122.1419);
var defSize = 5;

var lastMapName;

function GmapCreateMap(gmap_name, nocontrols){
	if (GBrowserIsCompatible()) {
		gmap_element = document.getElementById(gmap_name);
		if ( gmap_element ){
	    	var map = new GMap2(gmap_element);
    	    map.setCenter(defPoint, defSize);
    	    if ( ! nocontrols )
    	    {
	        	map.addControl(new GSmallMapControl());
//        	map.addControl(new ZoomControl());
	        	map.addControl(new GMapTypeControl());
    	    }
   			return map;
        }
    }
}

function GmapShowMap(gmap_name, show_location){
	if (GBrowserIsCompatible()) {
		gmap_element = document.getElementById(gmap_name);
		if ( gmap_element ){

			if (  ! geocoder )
				geocoder = new GClientGeocoder();
        	if ( show_location ){
       			geocoder.getLocations(show_location, GmapShowLocation);
        	}
        	else
        	{
                showMaps();
        	}
        }
    }
}

function GmapShowLocation(response) {
	var map = getLastMap();
 	map.clearOverlays();
    if (!response || response.Status.code != 200) {
        //alert("Sorry, we were unable to geocode that address");
    	map.setCenter(defPoint, defSize);
	} else {
    	place = response.Placemark[0];
        size = 5 + place.AddressDetails.Accuracy;
        if ( size > 15 )
        	size = 15;
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        map.setCenter(point, size );
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address);
      }
      showMaps();
}

function GmapLoad(gmap_name) {
	if (GBrowserIsCompatible()) {
		gmap_element = document.getElementById(gmap_name);
		if ( gmap_element ){
	    	var map = new GMap2(gmap_element);
    	    map.setCenter(defPoint, defSize);
        	map.addControl(new GSmallMapControl());
//        	map.addControl(new ZoomControl());
//	        map.addControl(new GMapTypeControl());
    	    geocoder = new GClientGeocoder();
        	if ( show_location ){
        		geocoder.getLocations(show_location, mapShowLocation);
        	}
        }
    }
}

    function GMAP_showLocation(address) {
		show_location = address;
    }

    function mapShowLocation(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        //alert("Sorry, we were unable to geocode that address");
        map.setCenter(defPoint, defSize);
      } else {
        place = response.Placemark[0];
        size = 5 + place.AddressDetails.Accuracy;
        if ( size > 15 )
        	size = 15;
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        map.setCenter(point, size );
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address);
      }
}
