//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='img/open.gif';
  if(addPoint.x!=0) {
   map.removeOverlay(addMarker); 
   GEvent.removeListener(addMarkerEvent);
   GEvent.removeListener(addMarkerClickEvent);
   addPoint.x=0;
  }
 } else { 
    document.getElementById(gName+"_block").style.display='';
    if(gName=="Directions"){ 
     document.getElementById("Add_marker_block").style.display='none';
     document.getElementById("Add_marker_state").src='img/open.gif';
     if(addPoint.x!=0) {
      map.removeOverlay(addMarker); 
      GEvent.removeListener(addMarkerEvent);
      GEvent.removeListener(addMarkerClickEvent);
      addPoint.x=0;
     }
    } else {
       document.getElementById("Directions_block").style.display='none';
       document.getElementById("Directions_state").src='img/open.gif';
       addMarkerEvent = GEvent.addListener(map, 'click', function(overlay, point) {
        if(!point) return;
        if(addPoint.x!=0) { 
         map.removeOverlay(addMarker); 
         GEvent.removeListener(addMarkerClickEvent);
         addPoint.x=0;
        }
        addPoint.x = point.x;
        addPoint.y = point.y;
        addMarker = new GMarker(addPoint, iconAddMarker);
        addMarkerClickEvent = GEvent.addListener(addMarker, 'click', function() {
         addMarker.openInfoWindowHtml("<div id=s>Fill in form and press &quot;Add&quot; button<br>to save this marker.</div");
        });
       map.addOverlay(addMarker);
      });
     }
    document.getElementById(gName+"_state").src='img/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(currentImage==""){ alert("Please upload an image"); return;}
 if(document.addMarkerForm.mTitle.value==""){ alert("Please enter a value for the \"Title\" field."); return;}
 var sCategory = "";
 for(i=1;i<document.addMarkerForm.mCategory.length;i++){
  if(document.addMarkerForm.mCategory.options[i].selected)
   sCategory = sCategory+document.addMarkerForm.mCategory.options[i].value+",";
 }
 if(sCategory==""){ alert("Please select a category from the \"Category\" list."); return;}
 
 var sTitle = document.addMarkerForm.mTitle.value;
 var sDescription = document.addMarkerForm.mDescription.value;
 var sName = document.addMarkerForm.mName.value;
 var sCity = document.addMarkerForm.mCity.value;
 
 tA = sTitle.split("&"); sTitle = tA.join(" ");
 tA = sCategory.split("&"); sCategory = tA.join(" ");
 tA = sDescription.split("&"); sDescription = tA.join(" ");
 tA = sName.split("&"); sName = tA.join(" ");
 tA = sCity.split("&"); sCity = tA.join(" ");
 tA = sTitle.split("#"); sTitle = tA.join(" ");
 tA = sCategory.split("#"); sCategory = tA.join(" ");
 tA = sDescription.split("#"); sDescription = tA.join(" ");
 tA = sDescription.split("\n"); sDescription = tA.join("<br>");
 tA = sName.split("#"); sName = tA.join(" ");
 tA = sCity.split("#"); sCity = tA.join(" ");
  
 var request;
 request = GXmlHttp.create();
 request.open("GET", 
              "data.php?add&mX="+addPoint.x+"&mY="+addPoint.y+"&mTitle="+sTitle+"&mCategory="+sCategory+
              "&mImage="+currentImage+"&mName="+sName+"&mCity="+sCity+"&mDescription="+sDescription, true);
 request.send(null);
 
 changeGroupView("Directions");
 document.addMarkerForm.mTitle.value="";
 document.addMarkerForm.mDescription.value="";
 document.getElementById("mCurrentImage").innerHTML="<br>&nbsp;<br>&nbsp;<br><center>Please, upload an Image first.</center><br>&nbsp;<br>&nbsp;";
 currentImage="";
 changeDetail();
}

map = new GMap(document.getElementById("map"));
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());
map.enableScrollWheelZoom();
directionsPanel = document.getElementById("mDirectionsTmp");
directions = new GDirections(map,directionsPanel);

GEvent.addListener(directions, "load", checkDirections);
GEvent.addListener(directions, "error", checkDirections);
GEvent.addListener(directions, "addoverlay", getDirections);

GEvent.addListener(map, "moveend",
 function() {
  var zoom= map.getZoomLevel();
  var bounds = map.getBoundsLatLng();
  if(map.getInfoWindow().isHidden()){
   startRequest(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, zoom, 1);
  }
  lastzoom=zoom;
});

map.centerAndZoom(new GPoint(centerx,centery), zoom);

document.getElementById("QCWithMe").innerHTML="Contact us: <a href="+"\"ma"+"il"+"to"+":t"+"ed@simpati"+"go.com\">t"+"ed@simpati"+"go.com</a>.";

