var mapLoaded = false;
var gm_callbackFunction;
var gm_select=false;
var gm_geocoder;
var gm_map;
var new_marker;
var gm_mng ;
var clickListener;

function mapsLoaded() {
  mapInitialize();
  if(gm_callbackFunction != undefined)gm_callbackFunction();
}

function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}


function loadMap(GM_KEY, method,select){
    gm_callbackFunction = method;
	if(!select){gm_select = true}
	else {gm_select = false}
	if(!mapLoaded){
		var script = document.createElement("script");
		script.src = "http://www.google.com/jsapi?key="+GM_KEY+"&callback=loadMaps";
  		script.type = "text/javascript";
  		document.getElementsByTagName("head")[0].appendChild(script);
  		mapLoaded = true;
	} else {
		mapInitialize();
  		if(gm_callbackFunction != undefined)gm_callbackFunction();
	}
}

 function mapInitialize()    
 {    
 
     if(GBrowserIsCompatible())  // sprawdzamy, czy przeglądarka jest kompatybilna    
     {    
        gm_geocoder =  new GClientGeocoder();
        gm_map = new GMap2(document.getElementById("gm_map"));    
        gm_map.setCenter(new GLatLng(50.05134135030049,19.96155166765675),12);    
		gm_mng =  new GMarkerManager(gm_map,{maxZoom: 19});        
        var mapControl = new GMapTypeControl();
		gm_map.addControl(mapControl);
		gm_map.addControl(new GLargeMapControl());
		//gm_map.enableScrollWheelZoom();

     }    
 }  


function showAddress(address) {
      if (gm_geocoder) {
        gm_geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " - nie znaleziono");
            } else {
              gm_map.setCenter(point,15);
            }
          }
        );
      }
}

function addNewMarker(lat,lon,my_ico,txt){


		 	 var marker = new GMarker(new GLatLng(lat,lon),{icon:my_ico});  
             marker.txt=txt;  
             GEvent.addListener(marker,"click",function() {  
                 marker.openInfoWindowHtml(marker.txt);  
             });  
             return marker;  
}

function addAdminClickListener(){

		clickListener = GEvent.addListener(gm_map, "click", function(overlay,point) {     
		
			if(point){
				selectPoint = true;
				document.getElementById("gm_lat").value =point.y
				document.getElementById("gm_lng").value =point.x;
				new_marker.setPoint(point);
			}	
			});
}

function disableAdminClickListener(){
		GEvent.removeListener(clickListener);
}

 function toggleIconCateogry(category,show)  
 {  
     for(var i=0; i<markers.length; i++)  
     {  
         if(markers[i].category==category)  
         {  
             if(show==true)  
                 markers[i].show();  
             else  
                 markers[i].hide();  
         }  
     }  
 }  
   
 function refreshMap(checkbox,category)  
 {         
     if(document.getElementById(checkbox).checked)  
         toggleIconCateogry(category,true);  
     else  
         toggleIconCateogry(category,false);  
 } 
 
 function showPoint(lat,lng) {
     gm_map.setCenter(new GLatLng(lat,lng),14);    
}