//Developed by Vladimir Lebedko

// Open/close tool groupes
function changeGroupView(gName) {
 if(document.getElementById(gName+"_block").style.display!='none') {
  document.getElementById(gName+"_block").style.display='none';
  document.getElementById(gName+"_state").src='open.gif';
  } else { 
     document.getElementById(gName+"_block").style.display='';
     if(gName=="Legend"){ 
      document.getElementById("Add_marker_block").style.display='none';
      document.getElementById("Add_marker_state").src='open.gif';
      if(addPoint.x!=0) {
       addPoint.x=0; 
       GEvent.removeListener(addMarkerClickEvent);
       GEvent.removeListener(addMarkerEvent);
       map.removeOverlay(addMarker); 
      }
     } else {
        document.getElementById("Legend_block").style.display='none';
        document.getElementById("Legend_state").src='open.gif';
        addMarkerEvent = GEvent.addListener(map, 'click', function(overlay, point) {
         if(!point) return;
         if(addPoint.x!=0) { 
          GEvent.removeListener(addMarkerClickEvent);
          map.removeOverlay(addMarker); 
         }
         addPoint.x = point.x;
         addPoint.y = point.y;
         addMarker = new GMarker(addPoint, iconAddMarker);
         addMarkerClickEvent = GEvent.addListener(addMarker, 'click', function() {
	      addMarker.openInfoWindowHtml("Fill in form and press &quot;Add&quot;<br>button to save this marker.");
         });
         map.addOverlay(addMarker);
        });
       }
     document.getElementById(gName+"_state").src='close.gif';
    }
}

//Add marker
function addNewMarker() {
 // Check values in addMarkerForm
 if(addPoint.x==0){ alert("Please click on the map to place marker"); return;}
 if(document.addMarkerForm.mTitle.value==""){ alert("Please enter a value for the \"Title\" field."); return;}
 if(document.addMarkerForm.mCategory.selectedIndex==0){ alert("Please select a category from the \"Category\" list."); return;}
 if((document.addMarkerForm.mEmail.value=="")&&(document.addMarkerForm.mAddress.value=="")){ alert("Please enter a value for the \"E-mail\" or \"Address\" field."); return;}
 
 var sTitle = document.addMarkerForm.mTitle.value;
 var sCategory = document.addMarkerForm.mCategory.options[document.addMarkerForm.mCategory.selectedIndex].value
 var sType = (document.addMarkerForm.mType[0].checked)?document.addMarkerForm.mType[0].value:document.addMarkerForm.mType[1].value;
 var sEmail = document.addMarkerForm.mEmail.value;
 var sAddress = document.addMarkerForm.mAddress.value;
 var sDescription = document.addMarkerForm.mDescription.value
 
 tA = sTitle.split("&"); sTitle = tA.join(" ");
 tA = sCategory.split("&"); sCategory = tA.join(" ");
 tA = sEmail.split("&"); sEmail = tA.join(" ");
 tA = sAddress.split("&"); sAddress = tA.join(" ");
 tA = sDescription.split("&"); sDescription = tA.join(" ");
 tA = sTitle.split("#"); sTitle = tA.join(" ");
 tA = sCategory.split("#"); sCategory = tA.join(" ");
 tA = sEmail.split("#"); sEmail = tA.join(" ");
 tA = sAddress.split("#"); sAddress = tA.join(" ");
 tA = sDescription.split("#"); sDescription = tA.join(" ");
 tA = sDescription.split("\n"); sDescription = tA.join("<br>");
  
 var request;
 request = GXmlHttp.create();
 request.open("GET", 
              "data.php?add&mX="+addPoint.x+"&mY="+addPoint.y+"&mTitle="+sTitle+"&mCategory="+sCategory+
              "&mType="+sType+"&mEmail="+sEmail+"&mAddress="+sAddress+"&mDescription="+sDescription, true);
 request.send(null);

 
 changeGroupView("Legend");
 changeDetail();
}

//Add marker
function sarchMarkers() {
 document.getElementById("curSearch").value=document.getElementById("sText").value;
 changeDetail();
}

map = new GMap(document.getElementById("map"));
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());


GEvent.addListener(map, "moveend", 
 function() {
  var zoom= map.getZoomLevel();
  var bounds = map.getBoundsLatLng();
   
  startRequest(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, zoom, 1);
    
  lastzoom=zoom;
});

map.centerAndZoom(new GPoint(centerx,centery), zoom);

document.getElementById("QCWithMe").innerHTML="Share any questions or comments with me: <a href="+"\"ma"+"il"+"to"+":t"+"ed@shareand"+"grow.com\">t"+"ed@shareand"+"grow.com</a>.";
