/*
 General Purpose Mapping Tool v0.3
 Copyright (C) 2007  Simon Neira Dueñas <sneira@mundo-r.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 USA.
 */

 /*
  * Javascript tools for GoogleMaps API Management
  */


/*
 * Define Icons
 */
 
 function initializeIcons()
 {
 
 var iconmatrix = Array();
 
 for(i=1;i<=7;++i)       
    {
     iconmatrix[i]= new GIcon();
     iconmatrix[i].image = "img/point"+i+".png";
     //iconmatrix[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
	 iconmatrix[i].iconSize = new GSize(14, 14);
	 //iconmatrix[i].shadowSize = new GSize(38, 34);
	 iconmatrix[i].iconAnchor = new GPoint(7, 7);
	 iconmatrix[i].infoWindowAnchor = new GPoint(7, 2);       
    }
   
 return iconmatrix;
 }


/*
 * Create a marker object with the point information
 */
function createMarker(point, id, icon, icons)
    {
       
     var mymarker = new GMarker(point,icons[icon]);
      						
     var infoTabs = [
                     new GInfoWindowTab("foto",'<html><iframe style=\"width:380px;height:270px;border:none;\" frameborder=0 marginheight=0 marginwidth=0 src=\"showglobepics.php?pointid='+id+'\"></iframe></html>'),
                     new GInfoWindowTab("info",'<html><iframe style=\"width:380px;height:270px;border:none;\" frameborder=0 marginheight=0 marginwidth=0 src=\"showglobepoint.php?pointid='+id+'\"></iframe></html>')
                     ]; 			
	 
      GEvent.addListener(mymarker, "click", function()
        {        
         mymarker.openInfoWindowTabsHtml(infoTabs);                                   
        });

      return mymarker;
    }

function createZoneMarker(point,name, lat, long, zoom, html)
{		
		var Icon = new GIcon(); 
		Icon.image = "img/zoomme.png"; 
		Icon.shadow = "img/zoomme_shadow.png";
		Icon.iconSize = new GSize(40, 40);
		Icon.iconAnchor = new GPoint(20, 14); 
		Icon.infoWindowAnchor = new GPoint(18, 10); 

        var marker = new GMarker(point, Icon);
        	
        // when the marker is clicked
        GEvent.addListener(marker, "click", function() {   
          loadmessage();  
          centermap(lat, long, parseInt(zoom));
        });

		// when mouse is on the marker
        GEvent.addListener(marker, "mouseover", function() {

           marker.openInfoWindowHtml(html);
		});
		
		// when mouse leaves the marker
		GEvent.addListener(marker, "mouseout", function() {
			marker.closeInfoWindow();
		});

        return marker;
}

/*
 * Create a map on the specified coordinates
 */
function createMap(lat, lon, zoom, small)
{
        var map = new GMap2(document.getElementById("map"));
        
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(lat, lon), zoom);
        
        
        /* Define max and minimun zoom level */
        
        
        
        if(small) 
            {         
        	 map.addControl(new GSmallMapControl());
        	 //map.addTargetMap();
        	 map.setMapType(G_HYBRID_MAP);        	 
        	}
        else { 
        	   map.addControl(new GLargeMapControl()); 
               map.addMapType(G_PHYSICAL_MAP);
               //map.setMapType(G_NORMAL_MAP);
               map.setMapType(G_PHYSICAL_MAP);               
             } 
               

        //map.setMapType(G_NORMAL_MAP);
        //map.setMapType(G_HYBRID_MAP);                                        
        //map.setMapType(G_PHYSICAL_MAP);        
          
         var mt = map.getMapTypes(); 
      	for (var i=0; i<mt.length; i++) {
        	mt[i].getMinimumResolution = function() {return 5;}
        	mt[i].getMaximumResolution = function() {return 19;}
        								} 

 		return(map); 
}

/*
 * Show a cross-target in the middle of the map
 */
 GMap2.prototype.addTargetMap = 
 function()
  	{
 		var layer=this.getContainer();
		if(this.targetMap) this.removeTargetMap();

		var targetMap=document.createElement("img");
		targetMap.src='img/target.png';
		targetMap.style.width='19px';
		targetMap.style.height='19px';
		targetMap.style.border='0';
		targetMap.style.position='absolute';
		targetMap.style.top=((layer.clientHeight-19)/2)+'px';
		targetMap.style.left=((layer.clientWidth-19)/2)+'px';
		targetMap.style.zIndex='999';
		layer.appendChild(targetMap);
		this.targetMap=targetMap;
		return targetMap;
	};

/*
 * Record current map center and zoom on an invisible input form in the filter form
 */
function saveLocationListener(map)
{
 
 var initpoint = map.getCenter();
 document.filterPoints.center_lat.value = initpoint.lat();
 document.filterPoints.center_lon.value = initpoint.lng();
 document.filterPoints.center_zoom.value = map.getZoom();

       GEvent.addListener(map, "moveend", function() {            
            var point = map.getCenter();
            document.filterPoints.center_lat.value = point.lat();
            document.filterPoints.center_lon.value = point.lng();
            document.filterPoints.center_zoom.value = map.getZoom();  
                    
          // Update URL to update position when clicking on a link.
          poslink='&lat='+point.lat()+'&lon='+point.lng()+'&zoom='+map.getZoom();                 
		
        });
}

 
 /*
  * Center map view on a geocoded location
  */
 function geocodeCenter(response) {      
      if (!response || response.Status.code != 200) {
         alert("lugar no encontrado");
      } else {
       place = response.Placemark[0];
       centermap(place.Point.coordinates[1],place.Point.coordinates[0],10);
       }               
    } 
 
/*
 * Center de map on a Geocoded Location
 */ 
function geocodeLocation() {
     var address = document.geocodeForm.pnameq.value + " ,Spain";   
     geocoder.getLocations(address, geocodeCenter);
    }  
 
/*
 * Center Map
 */ 
function centermap(lat, lon, zoom)
{
 map.setCenter(new GLatLng(lat, lon), zoom);
}  
 