// ***************************************************************************
// **** THIS FILE IS OBSOLETED BY google-map.php, DO NOT INCLUDE THIS FILE
// ***** ONLY FOR LEGACY USE
// ***************************************************************************
// requires: mmmap.php

function GoogleMap(_div, _latitude, _longitude, _zoom) {
  this.div = _div;
    
  this.div.innerHTML = '<div id="_gmap" style="position: relative; visibility: visible; z-index:2;left: 0px; top: 0 px; width: 800px; height: 500px; border: 0px solid red"></div>';
  this.div.innerHTML += '<div id="center_mark_gmap" style="position: absolute; visibility: visible; z-index:500;-moz-user-select: none;"><img src="http://mmmap15.longdo.com/mmmap/images/red-mark.gif" border=0 galleryimg=no oncontextmenu="return false" style="z-index:3;-moz-user-select: none"></div>';
  
  this.gmap_div = document.getElementById('_gmap');
  this.center_mark_div = document.getElementById('center_mark_gmap');
  
  var _resolution = Math.pow(2,_zoom);
  var gresolution = res_m2g(_resolution);
      
  if (GBrowserIsCompatible()) {
    this.gmap_object = new GMap2(this.gmap_div);
    this.gmap_object.setCenter(new GLatLng(_latitude, _longitude), gresolution);
    this.gmap_object.addControl(new GLargeMapControl());
    this.gmap_object.addControl(new GScaleControl());
    this.gmap_object.addControl(new GMapTypeControl());
    //this.gmap_object.addControl(new GOverviewMapControl());
    //this.gmap_object.setMapType(G_SATELLITE_TYPE);
    this.gmap_object.setMapType(G_HYBRID_MAP);
    this.gmap_object.addMapType(G_PHYSICAL_MAP);
    this.gmap_object.addMapType(G_SATELLITE_3D_MAP);
   }
   
   this.setCenter = function (_latitude, _longitude, _resolution) {
     var gresolution = res_m2g(_resolution);
     this.gmap_object.setCenter(new GLatLng(_latitude, _longitude), gresolution);
   }
   
   this.setSize = function (_w, _h) {
   
    var oldcenter = this.gmap_object.getCenter();
    
    var tmpleft = _w/2 - this.center_mark_div.offsetWidth/2;
    var tmptop =  _h/2 - this.center_mark_div.offsetHeight/2;
    
    // HACK, FIXME
    if (browser == "IE") {
      tmptop += 1;
    }

    this.center_mark_div.style.left = tmpleft + "px";
    this.center_mark_div.style.top = tmptop + "px";
    
    this.gmap_div.style.width = _w + "px";
    this.gmap_div.style.height = _h + "px";
    this.gmap_div.style.clip = "rect(0 "+this.gmap_div.style.width+" "+this.gmap_div.style.height+" 0)";
    
    this.gmap_object.checkResize();
    this.gmap_object.setCenter(oldcenter);
   }
   
   this.enableMouseWheel = function () {
    _global_gmap = this; // needed for muteWheel, any better method?
    acceptWheel(this.gmap_div,gmapScrollHandler);
   }
   
   this.showMap = function () {
     this.div.style.visibility ="visible";
     this.gmap_div.style.visibility = "visible";
     this.center_mark_div.style.visibility = "visible";
   }
   
   this.hideMap = function () {
     this.div.style.visibility ="hidden";
     this.gmap_div.style.visibility = "hidden";
     this.center_mark_div.style.visibility = "hidden";
   }

	 this.googlemap_markers = new Array();

	 this.clearMarkers = function () {
		 if (this.googlemap_markers.length > 0) {
			 for(i=0; i<this.googlemap_markers.length; i++) {
					this.gmap_object.removeOverlay(this.googlemap_markers[i]);
			 }
			 this.googlemap_markers = new Array();
		 }
	 }
   
	 this.addMarker = function(mylat, mylong, description) {
		 var marker = new GMarker(new GLatLng(mylat, mylong));

		 GEvent.addListener(marker, 'click',
				 function() {
				 marker.openInfoWindowHtml(description);
				 }
				 );

		 marker.description = description;
		 marker.showInfo = function() {
			 this.openInfoWindowHtml(this.description);
		 }

		 this.gmap_object.addOverlay(marker);
		 this.googlemap_markers.push(marker);

		 return marker;
	 }

	 this.showMarkerPopup = function(marker_id) {
		 if (this.googlemap_markers && this.googlemap_markers[marker_id]) 
			 this.googlemap_markers[marker_id].showInfo();
	 }

   this.enableMouseWheel();
}

function res_m2g(m) {
	if (resolution_steps <= 10 ) {
    return (10 + Math.log(m)/Math.log(2));
	} else {
    return (4 + Math.log(m)/Math.log(2));
	}
}

function res_g2m(g) {
	if (resolution_steps <= 10 ) {
		return Math.pow(2,g - 10);
	} else {
		return Math.pow(2,g - 4);
	}
}

function gmapScrollHandler(event) {
  var wheelDelta;

  if (!event) event = window.event;

  if (event.wheelDelta) { /* IE/Opera. */
      wheelDelta = event.wheelDelta;
      if (window.opera)
        wheelDelta = - wheelDelta;
  } else {
      wheelDelta = event.detail*-120;
  }
    
  muteWheel(_global_gmap.gmap_div,gmapScrollHandler); // until we process everything
  setTimeout('acceptWheel(_global_gmap.gmap_div,gmapScrollHandler)',200);

  if (wheelDelta >= 120) {
      _global_gmap.gmap_object.zoomIn();
  } else if (wheelDelta <= -120) {
      _global_gmap.gmap_object.zoomOut();
  }
  return false;
 }

