// ------------------------------resetMap ------------------------------ //
function resetMap() {
	map.setCenter(new GLatLng(38.4038,-96.1804), 2);
}
//--------------------------------- getMarkers --------------------------//
function getMarkers(zoomlvl, bounds) {

	map.clearOverlays();
 markerlist.length = 0;
 markerlistID.length = 0;
	var swlat = bounds.getSouthWest().lat();
	var swlng = bounds.getSouthWest().lng();
	var nelat = bounds.getNorthEast().lat();
	var nelng = bounds.getNorthEast().lng();
	startLoading();
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'getmarkers.php?type=bar&swlat=' + swlat + '&swlng=' + swlng + '&nelat=' + nelat + '&nelng=' + nelng + '';
	document.getElementsByTagName('head')[0].appendChild(script);  
	//map.clearOverlays();
}

//--------------------------------- filterMarkers --------------------------//
function filterMarkers(tags) {

	map.clearOverlays();
 markerlist.length = 0;
 markerlistID.length = 0;
	startLoading();
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'getmarkers.php?filter=yes&type=bar&tags=' + tags + '';
	document.getElementsByTagName('head')[0].appendChild(script);  
	//map.clearOverlays();
}

//--------------------------------- updateTagstring --------------------------//
function updateTagstring(tag) {
  //window.alert(tag);
  tagstring = tagstring + tag + " ";
  //window.alert(tagstring);
  //tagdivhtml = document.getElementById("thetagstring").innerHTML;
  //tagdivhtml = tagdivhtml + tag + "(<a href=\"javascript:void(0);\" onClick=\"javascript:updateTagstringDelete('" + tag + "');\">x</a>) ";
 //document.getElementById("thetagstring").innerHTML = tagdivhtml;
  updateTagstringDivHtml(tagstring);
  filterMarkers(tagstring);
  updateTagsDivHtml(tagstring);
  updateResultsDivHtml(tagstring);

}

//--------------------------------- updateTagstringDelete --------------------------//
function updateTagstringDelete(tag) {
  if (tagstring != '') {
	  tagstring = tagstring.replace(tag + ' ', '');
  }
//document.getElementById("thetagstring").innerHTML = tagstring;
updateTagstringDivHtml(tagstring);
  filterMarkers(tagstring);
  updateTagsDivHtml(tagstring);
  updateResultsDivHtml(tagstring);
}

//--------------------------------------- updateTagstringDivHtml ------------------//
function updateTagstringDivHtml(tags) {
  tagdivhtml = '';
  tagarr = tags.split(" ");
  for(i = 0; i < tagarr.length-1; i++) {
    thetag = tagarr[i];
    tagdivhtml = tagdivhtml + thetag + "(<a href=\"javascript:void(0);\" onClick=\"javascript:updateTagstringDelete('" + thetag + "');\">x</a>) ";
  }
 document.getElementById("thetagstring").innerHTML = tagdivhtml;

}

//--------------------------------------- updateTagsDivHtml ------------------//
function updateTagsDivHtml(tags) {
	var url = 'gettagslist.php?tags=' + tags + '';
	var myAjax = new Ajax.Request(url, {method: 'get', onComplete: function(response) { 

		infoHtml = response.responseText;
		document.getElementById("taglinks").innerHTML = infoHtml;


		}

		});
}

//--------------------------------------- updateResultsDivHtml ------------------//
function updateResultsDivHtml(tags) {
	var url = 'getresults.php?filter=yes&type=bar&tags=' + tags + '';
	var myAjax = new Ajax.Request(url, {method: 'get', onComplete: function(response) { 

		infoHtml = response.responseText;
		document.getElementById("results").innerHTML = infoHtml;


		}

		});
}

//----------------------------- createSiteMarker -----------------------------------//
function createSiteMarker(id, lat, lng, type) {
	var point = new GLatLng(lat, lng);
//window.alert(lat);
//window.alert(lng);
	var newIcon = new GIcon();
 if (type == "state") {
	 //FOR BIG MARKER - used with states
	 //window.alert("st");
		newIcon.image = "./images/blue_marker.png"
		newIcon.shadow = "./images/default_shadow50.png";
		newIcon.iconSize = new GSize(20, 34);
		newIcon.shadowSize = new GSize(37, 34);
		newIcon.iconAnchor = new GPoint(9, 33);
		newIcon.shadowAnchor = new GPoint(9, 33);
		newIcon.infoWindowAnchor = new GPoint(5, 1);
}
if (type == "city") {
	// FOR SMALL MARKER
	//window.alert("city");
		newIcon.image = "./images/mm_20_red.png"
		newIcon.shadow = "./images/mm_20_shadow.png";
		newIcon.iconSize = new GSize(12, 20);
		newIcon.shadowSize = new GSize(22, 20);
		newIcon.iconAnchor = new GPoint(6, 20);
		newIcon.shadowAnchor = new GPoint(6, 20);
		newIcon.infoWindowAnchor = new GPoint(5, 1);
}
if (type == "bar") {
	// FOR SMALL MARKER
	//window.alert("bar");
		newIcon.image = "./images/mm_20_bars.png"
		newIcon.shadow = "./images/mm_20_shadow.png";
		//newIcon.iconSize = new GSize(12, 20);
		//newIcon.shadowSize = new GSize(22, 20);
		//newIcon.iconAnchor = new GPoint(6, 20);
		//newIcon.shadowAnchor = new GPoint(6, 20);
		//newIcon.infoWindowAnchor = new GPoint(5, 1);

		newIcon.iconSize = new GSize(14, 22);
		newIcon.shadowSize = new GSize(22, 20);
		newIcon.iconAnchor = new GPoint(7, 21);
		newIcon.shadowAnchor = new GPoint(6, 20);
		newIcon.infoWindowAnchor = new GPoint(5, 1);
}

	var marker = new GMarker(point, newIcon);

	GEvent.addListener(marker, "click", function() {
		markerShowInfo(marker, id, type);
	});
	map.addOverlay(marker);
	markerlistID.push(id);
        markerlist.push(marker);
	
	return marker;

}

//---------------------------------------------- findMarkerByID-----------------------//
function findMarkerByID(id) {
	for(var i = 0; i < markerlistID.length; i++) {
		if(markerlistID[i] == id) {
			return markerlist[i];
		}
	}
	return null;
} // end function findMarkerByID

//--------------------------------------- showInfoByID ----------------------------------------------//
function showInfoByID(id, type) {
	var marker = findMarkerByID(id);
	if (marker != null) {
		markerShowInfo(marker, id, type);
	
	} else {
		window.alert("uh oh...");
	}
} // end function showInfoByID

//---------------------------------------------- markerShowInfo -----------------------//
function markerShowInfo(marker, id, type) {

	//var index = findIndexByID(id);
	var infoHtml = "";
	var infoFloorplan;
	var resArray;
	
	b_windowhasbeenopened = 1;

	var url = 'infowindowtext.php?id=' + id + '&info=' + type + '';
	var myAjax = new Ajax.Request(url, {method: 'get', onComplete: function(response) { 

		currentMarker = marker;
		infoHtml = response.responseText;

			var tabs = new Array();
			var title = "Info";
			var data = infoHtml
				
			tabs.push(new GInfoWindowTab(title, data));
	

			marker.openInfoWindowTabsHtml(tabs);
			if (leftinfo != "null") {
				document.getElementById("info").innerHTML = leftinfo;
			}


		}

		});
	

} // end markerShowInfo



//-------------------------- searchrad ----------------------------//
function searchrad(radius) {
	
	var lat = map.getCenter().lat();
	var lng = map.getCenter().lng();
	
	var url = 'radialsearch.php?radius=' + radius + '&lat=' + lat + '&lng=' + lng + '';;
	var myAjax = new Ajax.Request(url, {method: 'get', onComplete: function(response) { 

		 document.getElementById("radsearchresults").innerHTML = response.responseText;
		}

		});

}

//------------------------ addNewAddr ---------------------//
function addNewAddr() {
	// Geocode address, center, add marker and open info window
	var address = document.getElementById("newaddr").value;
	var title = document.getElementById("newtitle").value;
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point)
						alert("Address \"" + address + "\" not found");
				else {
					map.setCenter(point, 11);
					// FOR BIG MARKER
					var newIcon = new GIcon();
					newIcon.image = "./images/default_marker.png"
					newIcon.shadow = "./images/default_shadow50.png";
					newIcon.iconSize = new GSize(20, 34);
					newIcon.shadowSize = new GSize(37, 34);
					newIcon.iconAnchor = new GPoint(9, 33);
					newIcon.shadowAnchor = new GPoint(9, 33);
					newIcon.infoWindowAnchor = new GPoint(5, 1);
					var marker = new GMarker(point, newIcon);
					map.addOverlay(marker);
					customlist.push(marker);
					var lat = marker.getPoint().lat();
					var lng = marker.getPoint().lng();
					var label = "<b>" + title + "</b><br>" + address + "";
					label += "<p>Draw radius | ";
					label += "<a href=\"javascript:addRadius(" + lat + "," + lng + ", 1);\">1mi</a> | ";
label += "<a href=\"javascript:addRadius(" + lat + "," + lng + ", 2);\">2mi</a> | ";
label += "<a href=\"javascript:addRadius(" + lat + "," + lng + ", 5);\">5mi</a> | ";
label += "<a href=\"javascript:addRadius(" + lat + "," + lng + ", 10);\">10mi</a> | ";
					marker.openInfoWindowHtml(label);
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(label);
					});
			return marker;
				}
			}
		);
	}
} // end function addNewAddr

//---------------------------------- addRadius --------------------------------//
function addRadius(lat, lng, radius) {

	//var lat = marker.getPoint().lat();
	//var lng = marker.getPoint().lng();
	
	var radring;
	radring = drawCircle(lat, lng, '#ff0000', radius);
	map.addOverlay(radring);
	radiallist.push(radring);
	
	//radialSet.addOverlay(radring);
	


} // end function addRadius

//---------------------------------------------- drawCircle -----------------------//
function drawCircle(lat,lng,color,radius) {

	//window.alert(lat + ' ' + lng + ' ' + color + ' ' + radius);

   var Cradius = radius;             // mile radius
   var Ccolor = color;    // color blue
   var Cwidth = 4;             // width pixels
   var d2r = Math.PI/180;   // degrees to radians
   var r2d = 180/Math.PI;   // radians to degrees
   var Clat = (Cradius/3963)*r2d;   //  using 3963 as earth's radius 
   
   var Clng = Clat/Math.cos(lat*d2r);
   var Cx, Cy;

   var Cpoints = new Array();
   for (var i=0; i < 41; i++) {
      var theta = Math.PI * (i/20);
      Cx = lng + (Clng * Math.cos(theta));
      Cy = lat + (Clat * Math.sin(theta));
      Cpoints.push(new GLatLng(Cy,Cx));  // changed form GPoint
   };

   return new GPolyline(Cpoints,Ccolor,Cwidth);

} 


// ------------------------removeMyMarkers ----------------------------//
function removeMyMarkers() {

	//map.removeOverlaySet(customSet);
	for(var i = 0; i < customlist.length; i++) {
		map.removeOverlay(customlist[i]);
	}
	customlist.length = 0;

}


//--------------------- updatebounds ------------------------------//
function updatebounds() {
	curbounds = map.getBounds();
	cursw = curbounds.getSouthWest();
	curne = curbounds.getNorthEast();

}

// ---------------------- startLoading() ----------------//
function startLoading() {
	//document.getElementById("loading").innerHTML = "<center><img src=./images/loading.gif><b>Loading...</b></center>";
}

//--------------------- endLoading() -------------------//
function endLoading()
{
	document.getElementById("loading").innerHTML = "";
}

// ------ expand and collapse maps and results  ---- // 
function toggleMap() {

	var obj = document.getElementById ('results')
        if (obj.style.top == '12px') {
                document.getElementById("control").style.top = '370px';
                document.getElementById("control").style.height = '12px';
                document.getElementById("map").style.display = 'block';
                obj.style.top = '392px';
		document.getElementById("mapControl").innerHTML = "Hide Map";
                document.getElementById("results").style.margin = '12px 0 0 0';
		
        } else {
                document.getElementById("control").style.top = '0px';
                document.getElementById("control").style.height = '12px';
                document.getElementById("map").style.display = 'none';
                obj.style.top = '12px';
                document.getElementById("results").style.display = 'block';
                document.getElementById("results").style.margin = '16px 0 0 0';
               
		document.getElementById("mapControl").innerHTML = "Show Map";
        }

}

function toggleResults() {
        var h = document.getElementById("content").offsetHeight;
	var obj = document.getElementById ('map')

        if (obj.style.top == '1px' ) {
                obj.style.height = '370px';
                obj.style.top = '0px';
                document.getElementById("control").style.top = '370px';
                document.getElementById("control").style.height = '12px';
                document.getElementById("results").style.display = 'block';
                
		document.getElementById("resultsControl").innerHTML = "Hide Results";
                document.getElementById("results").style.margin = '6px 0 0 0';
		
        } else {
                obj.style.top = '1px';
                document.getElementById("control").style.top = (h - 21) + 'px';
                document.getElementById("control").style.height = '12px';
                obj.style.height = (h - 30) + 'px';
                document.getElementById("results").style.display = 'none';
                obj.style.border.bottom = 'none';
		document.getElementById("resultsControl").innerHTML = "Show Results";
        }
}


