
var KGMap = Class.create({
     COLOR_RED:'red',
     COLOR_BLUE:'blue',
     COLOR_GREEN:'green',
     COLOR_YELLOW:'yellow',
     COLOR_BLACK:'black',
      
     initialize: function (mapElement) //tested
     {
          this.mapElement=$(mapElement);
          this.map=new GMap2(this.mapElement);
          this.icons=new Array();
          this.iconColors=$A();
          this.geocoder=false;
          this.addedMarkers=$A();
          
          // private util variables
          this._waitingForData=false;
          this._wdata=false;
          this.tooltip = document.createElement("div");
          this.mapElement.appendChild(this.tooltip);
          this.tooltip.style.visibility="hidden";

     },
     getColoredIcon: function (color) //tested
     {
          if(this.iconColors.indexOf(color)!=-1){
              
return this.icons[color];
	}

          this.icons[color]= new GIcon();
          this.icons[color].image = "http://labs.google.com/ridefinder/images/mm_20_"+ color +".png";
          this.icons[color].shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
          this.icons[color].iconSize = new GSize(14.4, 24); //12, 20
          this.icons[color].shadowSize = new GSize(26.4, 24); // 22, 20
          this.icons[color].iconAnchor = new GPoint(7.2, 24); // 6, 20
          this.icons[color].infoWindowAnchor = new GPoint(5, 1); // 5 ,1
          //this.icons[color].imageMap = [4,0,0,4,0,7,3,11,4,19,7,19,8,11,11,7,11,4,7,0];
          this.icons[color].transparent = "mapIcons/mm_20_transparent.png";
          this.iconColors[this.iconColors.length]=color;
          return this.icons[color];
     },
     getGeocoder: function () //tested
     {
         if(!this.geocoder)
               this.geocoder=new GClientGeocoder();
          return this.geocoder;
     },
     setCenter: function (latLng, zoom) // tested
     {
          zoom=zoom || 8;
          this.map.setCenter(latLng, zoom);
          return this;
     },
     addControlByKey: function (key) //tested
     {
         switch(key)
          {
               case 'zoom':
                   this.map.addControl(new GLargeMapControl());
                    break;
               case 'type':
                   this.map.addControl(new GMapTypeControl());
                    break;
               case 'scale':
                this.map.addControl(new GScaleControl());
                break;                    
          }
          return this;
     },
     getMapElement: function () //tested
     {
          return this.mapElement;
     },
     getMap: function () //tested
     {
         return this.map;
     },
     addMarker: function (latLng, description, color) //tested
     {
          var desc= description || '';
          color=color || 'red';
          var marker=new GMarker(latLng , this.getColoredIcon(color));
          
          if(desc!='')
          {
               GEvent.addListener(marker , 'click' , function (){
                     marker.openInfoWindowTabsHtml(desc);
               });
          }
          this.map.addOverlay(marker);
          this.addedMarkers[this.addedMarkers.length]=marker;
          return marker;
     },
     addMarkerMO: function (latLng, description, color , mohtml) //tested
     {
          var desc= description || '';
          color=color || 'red';
          var marker=new GMarker(latLng , {'icon' : this.getColoredIcon(color)} );
          
          if(desc!='')
          {
               GEvent.addListener(marker , 'click' , function (){
                     marker.openInfoWindowTabsHtml(desc);
               });
          }
          marker.tooltip = '<div class="tooltip">'+mohtml+'<\/div>';
          var kgm=this;
          GEvent.addListener(marker , 'mouseover' , function (p)
          {
               kgm.showMarkerToolTip(marker);
               

          });
          GEvent.addListener(marker , 'mouseout' , function (p)
          {
              kgm.hideMarkerToolTip(marker);
          });
          this.map.addOverlay(marker);
          this.addedMarkers[this.addedMarkers.length]=marker;
          
          return marker;
     },
     showMarkerToolTip: function (marker)
     {
          this.tooltip.innerHTML = marker.tooltip;
          var map=this.getMap();
          var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
          var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
          var anchor=marker.getIcon().iconAnchor;
          var width=marker.getIcon().iconSize.width;
          var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
          pos.apply(this.tooltip);
          this.tooltip.style.visibility="visible";
          marker.hide();
          marker.show();

     },
     hideMarkerToolTip: function (marker)
     {
        this.tooltip.style.visibility="hidden";
        
     },
     setDragging: function (boolValue) //tested
     {
          if(boolValue)
               this.map.enableDragging();
          else
               this.map.disableDragging();
          return this;
     },
     createLatLng: function (lat , lng) //tested
     {
        return new GLatLng(lat, lng);
     },
     setCenterByAddress: function(address, callback) //tested
     {
          var gc=this.getGeocoder();
                var m=this.getMap(); 
                
          gc.getLatLng(address , function (p)
          {
               if(p)
               {
                   m.setCenter(p , 8);     
               }
                        if(Object.isFunction(callback))
                        {
                           callback(p);
                        }
               
          });
                
         
     },
     addressToLatLng: function(address, callback)
     {
          var gc=this.getGeocoder();
          var m=this.getMap(); 
          gc.getLatLng(address , callback);   
     },
     latLngToAddress: function(latLng, callback)
     {
         alert('NOT IMLPEMENTED');
     },
     showAllMarkers: function (markers) //tested
     {
          markers=markers || this.addedMarkers;
          this.map.setCenter(new GLatLng(0,0),0);
          var bounds = new GLatLngBounds();

          for(var i=0; i<markers.length; i++)
          {
               var marker=markers[i];
               bounds.extend(marker.getLatLng());
                           
          }
          this.map.setZoom(this.map.getBoundsZoomLevel(bounds));
          this.map.setCenter(bounds.getCenter());
          
     }
    
});
