//Developed by Vladimir Lebedko

var iconAddMarker;
var iconMarker;
var iconMarkerNational;
var iconMarkerWildlife;
var lastzoom;
var zoomInOut;
var points = new Array();
var lastID = 0;
var numPoints=0;
var maxPoints=250;
var map;
var addMarker;
var addMarkerEvent;
var addMarkerClickEvent
var addPoint;

addPoint = new GPoint(0, 0);

iconAddMarker = new GIcon();
iconAddMarker.image = "marker.png";
iconAddMarker.iconSize = new GSize(20, 34);
iconAddMarker.shadow = "shadow.png"
iconAddMarker.shadowSize = new GSize(37, 34);
iconAddMarker.iconAnchor = new GPoint(9, 34);
iconAddMarker.infoWindowAnchor = new GPoint(5, 1);

iconMarkerBabyS = new GIcon();
iconMarkerBabyS.image = "marker-red-s.png";
iconMarkerBabyS.iconSize = new GSize(20, 34);
iconMarkerBabyS.shadow = "shadow.png"
iconMarkerBabyS.shadowSize = new GSize(37, 34);
iconMarkerBabyS.iconAnchor = new GPoint(9, 34);
iconMarkerBabyS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerDiscountsS = new GIcon();
iconMarkerDiscountsS.image = "marker-yellow-s.png";
iconMarkerDiscountsS.iconSize = new GSize(20, 34);
iconMarkerDiscountsS.shadow = "shadow.png"
iconMarkerDiscountsS.shadowSize = new GSize(37, 34);
iconMarkerDiscountsS.iconAnchor = new GPoint(9, 34);
iconMarkerDiscountsS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerHomeS = new GIcon();
iconMarkerHomeS.image = "marker-blue-s.png";
iconMarkerHomeS.iconSize = new GSize(20, 34);
iconMarkerHomeS.shadow = "shadow.png"
iconMarkerHomeS.shadowSize = new GSize(37, 34);
iconMarkerHomeS.iconAnchor = new GPoint(9, 34);
iconMarkerHomeS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerSportsS = new GIcon();
iconMarkerSportsS.image = "marker-green-s.png";
iconMarkerSportsS.iconSize = new GSize(20, 34);
iconMarkerSportsS.shadow = "shadow.png"
iconMarkerSportsS.shadowSize = new GSize(37, 34);
iconMarkerSportsS.iconAnchor = new GPoint(9, 34);
iconMarkerSportsS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerTravelS = new GIcon();
iconMarkerTravelS.image = "marker-purple-s.png";
iconMarkerTravelS.iconSize = new GSize(20, 34);
iconMarkerTravelS.shadow = "shadow.png"
iconMarkerTravelS.shadowSize = new GSize(37, 34);
iconMarkerTravelS.iconAnchor = new GPoint(9, 34);
iconMarkerTravelS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerWiFiS = new GIcon();
iconMarkerWiFiS.image = "marker-orange-s.png";
iconMarkerWiFiS.iconSize = new GSize(20, 34);
iconMarkerWiFiS.shadow = "shadow.png"
iconMarkerWiFiS.shadowSize = new GSize(37, 34);
iconMarkerWiFiS.iconAnchor = new GPoint(9, 34);
iconMarkerWiFiS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerOtherS = new GIcon();
iconMarkerOtherS.image = "marker-white-s.png";
iconMarkerOtherS.iconSize = new GSize(20, 34);
iconMarkerOtherS.shadow = "shadow.png"
iconMarkerOtherS.shadowSize = new GSize(37, 34);
iconMarkerOtherS.iconAnchor = new GPoint(9, 34);
iconMarkerOtherS.infoWindowAnchor = new GPoint(5, 1);

iconMarkerBaby = new GIcon();
iconMarkerBaby.image = "marker-red.png";
iconMarkerBaby.iconSize = new GSize(20, 34);
iconMarkerBaby.shadow = "shadow.png"
iconMarkerBaby.shadowSize = new GSize(37, 34);
iconMarkerBaby.iconAnchor = new GPoint(9, 34);
iconMarkerBaby.infoWindowAnchor = new GPoint(5, 1);

iconMarkerDiscounts = new GIcon();
iconMarkerDiscounts.image = "marker-yellow.png";
iconMarkerDiscounts.iconSize = new GSize(20, 34);
iconMarkerDiscounts.shadow = "shadow.png"
iconMarkerDiscounts.shadowSize = new GSize(37, 34);
iconMarkerDiscounts.iconAnchor = new GPoint(9, 34);
iconMarkerDiscounts.infoWindowAnchor = new GPoint(5, 1);

iconMarkerHome = new GIcon();
iconMarkerHome.image = "marker-blue.png";
iconMarkerHome.iconSize = new GSize(20, 34);
iconMarkerHome.shadow = "shadow.png"
iconMarkerHome.shadowSize = new GSize(37, 34);
iconMarkerHome.iconAnchor = new GPoint(9, 34);
iconMarkerHome.infoWindowAnchor = new GPoint(5, 1);

iconMarkerSports = new GIcon();
iconMarkerSports.image = "marker-green.png";
iconMarkerSports.iconSize = new GSize(20, 34);
iconMarkerSports.shadow = "shadow.png"
iconMarkerSports.shadowSize = new GSize(37, 34);
iconMarkerSports.iconAnchor = new GPoint(9, 34);
iconMarkerSports.infoWindowAnchor = new GPoint(5, 1);

iconMarkerTravel = new GIcon();
iconMarkerTravel.image = "marker-purple.png";
iconMarkerTravel.iconSize = new GSize(20, 34);
iconMarkerTravel.shadow = "shadow.png"
iconMarkerTravel.shadowSize = new GSize(37, 34);
iconMarkerTravel.iconAnchor = new GPoint(9, 34);
iconMarkerTravel.infoWindowAnchor = new GPoint(5, 1);

iconMarkerWiFi = new GIcon();
iconMarkerWiFi.image = "marker-orange.png";
iconMarkerWiFi.iconSize = new GSize(20, 34);
iconMarkerWiFi.shadow = "shadow.png"
iconMarkerWiFi.shadowSize = new GSize(37, 34);
iconMarkerWiFi.iconAnchor = new GPoint(9, 34);
iconMarkerWiFi.infoWindowAnchor = new GPoint(5, 1);

iconMarkerOther = new GIcon();
iconMarkerOther.image = "marker-white.png";
iconMarkerOther.iconSize = new GSize(20, 34);
iconMarkerOther.shadow = "shadow.png"
iconMarkerOther.shadowSize = new GSize(37, 34);
iconMarkerOther.iconAnchor = new GPoint(9, 34);
iconMarkerOther.infoWindowAnchor = new GPoint(5, 1);

lastzoom=-1;

function clearmap() {
 for (var i in points)	{
  GEvent.clearListeners(points[i],'click');
  map.removeOverlay(points[i]);
  delete points[i];	
 }
 numPoints=0;
}

function zoomTo(pointx,pointy) {
 if(map.getZoomLevel()==0) {
  map.centerAndZoom(new GPoint(pointx,pointy), zoomInOut);
  } else {
   zoomInOut=map.getZoomLevel();
   map.centerAndZoom(new GPoint(pointx,pointy), 0);
   }
}

function createMarker(rowdata) {

	var marker;
	var aName=rowdata[2];
	var aCategory=rowdata[3];
	var aType=rowdata[4];
	var aEmail=rowdata[5];
	var aAddress=rowdata[6];
	var aDescription=rowdata[7];
	var id="a"+lastID; lastID++;
	var point;
			
	if (points[id]!=null) return;
	
	point=new GPoint(rowdata[0],rowdata[1]);
	
	switch (aCategory) {
     case "Baby":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerBabyS);
           } else {marker = new GMarker(point, iconMarkerBaby);}
          break;
     case "Discounts":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerDiscountsS);
           } else {marker = new GMarker(point, iconMarkerDiscount);}
          break;
     case "Home":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerHomeS);
           } else {marker = new GMarker(point, iconMarkerHome);}
          break;
     case "Sports":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerSportsS);
           } else {marker = new GMarker(point, iconMarkerSports);}
          break;
     case "Travel":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerTravelS);
           } else {marker = new GMarker(point, iconMarkerTravel);}
          break;
     case "WiFi":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerWiFiS);
           } else {marker = new GMarker(point, iconMarkerWiFi);}
          break;
     case "Other":
           if(aType=="offering_share"){
            marker = new GMarker(point, iconMarkerOtherS);
           } else {marker = new GMarker(point, iconMarkerOther);}
          break;
     default: return;
      
    }
		
	map.addOverlay(marker);
  	GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml("<div id=r><b>"+aName+"</b><br>"+
    	"<a href=javascript:zoomTo("+point.x+","+point.y+")><b>Zoom In/Out</b></a><br>"+
    	"<u>E-mail</u>: "+aEmail+"<br>"+
    	"<u>Address</u>: "+aAddress+"<br>"+
    	"<br>"+aDescription+"<br>"+
    	"<center><a href=http://simpatigo.com>Visit SimpatiGo.com travel guide</a></center><br>"+
        "</div>");
    	document.title="San Francisco | Share and Grow | "+aName;
	});
	
	points[id]=marker;
	numPoints++;
}



function startRequest(minx,miny,maxx,maxy,z,clear,minx2,miny2,maxx2,maxy2) {
	var url;
	var request;
	var sText = "";
	
	if(document.getElementById("curSearch").value!=""){
	 sText=document.getElementById("curSearch").value;
     tA = sText.split("&"); sText = tA.join(" ");
     tA = sText.split("#"); sText = tA.join(" ");
    }
	
	request = GXmlHttp.create();
	
    url="data.php?minx="+minx+"&miny="+miny+"&maxx="+maxx+"&maxy="+maxy+
        "&cbBaby="+((document.getElementById("cbBaby").checked)?"y":"n")+
        "&cbDiscounts="+((document.getElementById("cbDiscounts").checked)?"y":"n")+
        "&cbHome="+((document.getElementById("cbHome").checked)?"y":"n")+
        "&cbSports="+((document.getElementById("cbSports").checked)?"y":"n")+
        "&cbTravel="+((document.getElementById("cbTravel").checked)?"y":"n")+
        "&cbWiFi="+((document.getElementById("cbWiFi").checked)?"y":"n")+
        "&cbOther="+((document.getElementById("cbOther").checked)?"y":"n")+
        "&sText="+sText;
	
	request.open("GET", url , true);
	
	request.onreadystatechange = function() {
	 if (request.readyState == 4) {
  	  if (z!=lastzoom) return;
      var xmlDoc = request.responseText;
      var rows=xmlDoc.split("\n");
      if (clear) clearmap();
      
      for (i=0;i<rows.length-1;i++) {
	   rows[i]=rows[i].split("|");
	   if(rows[i].length=8) createMarker(rows[i]);
	  
	  }
  	 }
	}
  
	request.send(null);
}


function changeDetail() {
	var zoom= map.getZoomLevel();
	var bounds = map.getBoundsLatLng();
	startRequest(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, zoom, 1);
}



function enterkey() {
 if (window.event && window.event.keyCode == 13) jumpToState();
 return !(window.event && window.event.keyCode == 13); 
}


function  jumpToNeighborhood() {
	var nb = document.getElementById("neighborhood").value;
	var request = GXmlHttp.create();
	var url = "location.php?neighborhood="+nb;
	
	if(nb=="") {
	 map.centerAndZoom(new GPoint(centerx,centery), 5);
	 return;
	}
	
	request.open("GET", url , true);
	request.onreadystatechange = function() {
	 if (request.readyState == 4) {
 	  var xmlDoc = request.responseText;
  	  var loc=xmlDoc.split("|");
	  if (loc==0 || loc.length!=2) {
	  map.centerAndZoom(new GPoint(centerx,centery), 5);
	 } else {
		if(nb=="Potrero"||nb=="Mission"||nb=="Sunset"||nb=="Visitacion Valley"){
		 map.centerAndZoom(new GPoint(loc[0],loc[1]), 3);
		} else { map.centerAndZoom(new GPoint(loc[0],loc[1]), 2);}
	   }
  	  }
	}
	changeDetail(); 	
	request.send(null);	
}


function changeMarkers(){

}
