//<![CDATA[



var map;



var mgr;



var icons = {};



var markerKML;



var centerPoint =new GLatLng(39.7125, -86.32507);// new GLatLng(39.6446, -86.84486); // East College



var zoom = 11;//4;



var minZoom = 2;



var requestSent = false;



var pixelThreshold = 24;
// was 256 - changed by Drew on 11-13-2009


var centerBeforeMove;



var timeout;







$(document).ready(function(){



	if (GBrowserIsCompatible()) {



		if (readCookie("currentLat") != null && readCookie("currentLat") != null && readCookie("currentZoom") != null) {



			centerPoint = new GLatLng(parseFloat(readCookie("currentLat")), parseFloat(readCookie("currentLng")));



			zoom = parseInt(readCookie("currentZoom"));



		}



		map = new GMap2(document.getElementById("map_canvas"));



		//map.setUIToDefault();



		map.addControl(new GLargeMapControl3D());



		map.addControl(new GOverviewMapControl());



		map.addControl(new GMapTypeControl());



		



		map.setCenter(centerPoint, zoom);



		/*var customUI = map.getDefaultUI();



		customUI.zoom.scrollwheel = false;



		map.setUI(customUI);*/



		



		map.enableDoubleClickZoom();

		map.enableScrollWheelZoom();







		new GScreenPoint(100, 100); 



		centerBeforeMove = map.getCenter();



		GEvent.addListener(map, "moveend", moveEnd);



		GEvent.addListener(map, "zoomend", zoomEnd);



		updateLatLngValue(centerBeforeMove.lat(),centerBeforeMove.lng())



		markerSearch()



	}



	$("#SearchAddress").click(function(){showAddress($("#SearchAddressText").val()); return false;})



});



$(document).unload(function() {GUnload();});











function moveEnd() {



	var proj = map.getCurrentMapType().getProjection();



	var zoom = map.getZoom();



	var centerAfterMove = map.getCenter();



	var beforePx = proj.fromLatLngToPixel(centerBeforeMove, zoom);



	var afterPx = proj.fromLatLngToPixel(centerAfterMove, zoom);



	var d = Math.sqrt(Math.pow((beforePx.x - afterPx.x),2) + Math.pow((beforePx.y - afterPx.y),2));



	if (d < pixelThreshold) { return; }



	updateLatLngValue(centerAfterMove.lat().toFixed(5),centerAfterMove.lng().toFixed(5));



	centerBeforeMove = centerAfterMove;



	markerSearch();



	createCookie("currentLat",centerAfterMove.lat().toFixed(5),30);



	createCookie("currentLng",centerAfterMove.lng().toFixed(5),30);



}; // End moveEnd Function











function zoomEnd(oldZ,zoom) {



	var centerBeforeMove = map.getCenter();



	// Limit the zoomend event firing multiple times when zooming with the mouse wheel.



	if (timeout) { window.clearTimeout(timeout); }



	timeout = window.setTimeout(function(){ markerSearch(); },300);



	createCookie("currentZoom",zoom,30);



}; // End zoomEnd Function







function markerSearch() {



	clearMarkers();



	var bounds = map.getBounds();



	var southWest = bounds.getSouthWest();



	var northEast = bounds.getNorthEast();



	var zoom = map.getZoom();



	if (zoom > minZoom) {



		$.ajax({



			type: "post",



			url: "/main/kml/",



			data: {swlat: southWest.lat(), swlng: southWest.lng(), nelat: northEast.lat(), nelng: northEast.lng(), zoom: zoom},



			dataType: "xml",



			beforeSend: function(){



			 	// Handle the beforeSend event



			 	// $("#map_status").html("Loading...").show("slow")



			},



			success: function(markerKML){



				// Add 10 markers to the map at random locations				



				$("#map_status").html("").hide("slow")



				$("Placemark",markerKML).each(function() {



					var markerID = $(this).find("ExtendedData Data[name='loc_id'] value").text();



					var pointLatLng = $(this).find("coordinates").text().split(",");



					var point = new GLatLng(parseFloat(pointLatLng[0]),parseFloat(pointLatLng[1]));



					var title = $(this).find("name").text();



					var markerIcon = $(this).find("ExtendedData Data[name='icon'] value").text();



					if (markerID == "" || markerID == null) {



						var markerType = new Array("bubble_multi","",false);



					} else {



						if (markerIcon == "" || markerIcon == null) {



							var markerType = new Array("bubble_red","",true);



						} else {



							var markerType = new Array(markerIcon,"",true);



						}



					}



					map.addOverlay(createMarker(point, markerType,markerID,title));



				});



			}//End Success Function



		}); //End ajax Call	



	};//End Min Zoom



}; // End markerSearch Function







function createMarker(point, iconType,id,markerTitle) {



	// Create a lettered icon for this point using our icon class



	// Set up our GMarkerOptions object



	markerOptions = { icon: getIcon(iconType), title : markerTitle, clickable: true };



	var marker = new GMarker(point, markerOptions);



	if (iconType[2]) {



		GEvent.addListener(marker, "click", function() { getMarkerData(id) });



		GEvent.addListener(marker, "mouseover", function() { markerRollover(markerTitle) });



	} else {



		GEvent.addListener(marker, "click", function() { 



			var zoom = map.getZoom();



			var point = marker.getPoint();			



			map.setCenter(point, zoom+1);



		});



	}



	return marker;



}; // End createMarker Function







function markerRollover(title) {}







function getMarkerData(markerID) {



	if(isFinite(markerID) && markerID != "") {



		$.get("/main/markerData/"+ markerID, function(data){



			$("#map_markerDetail").html(data);
			$("a.addData, a.editData").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300 });
			$("a.deleteData").click(function(){
				if(window.confirm('The data you selected will be delete FOREVER!!!\n. . . There is no going back.\n         Do you want to proceed ?')) {
					$.get($(this).attr("href"));
					$(this).parent().parent().remove();
				};
				return false;
			});


		});



	} else {



		$("#map_markerDetail").html("There are more than one points in this location please zoom in to see details.");



	}



}; //End getMarkerData Function







function clearMarkers(){



	map.clearOverlays();



};// End clearMarkers Function







function getIcon(images) {



	var icon = null;



/*	var iconData = {



	  "plainfield": { width: 24, height: 31 },



	  "indot": { width: 24, height: 31 },



	  "bubble_red": { width: 24, height: 31 },



	  "pin_red": { width: 22, height: 30 },



	  "pin_green": { width: 22, height: 30 },



	  "pin_orange": { width: 22, height: 30 },



	  "pin_orange_multi": { width: 45, height: 30 },



	  "pin_shadow_multi": { width: 45, height: 30 },



	  "bubble_multi": { width: 30, height: 30 },



	  "pin_shadow": { width: 22, height: 30 }



	};



*/	



	var iconData = {


	  "westfield": { width: 24, height: 30 },
	  
	  "columbus": { width: 24, height: 30 },
	  
	  "plainfield": { width: 24, height: 30 },
	  
	  "plainfield2": { width: 24, height: 30 },
	  
	  "avon": { width: 24, height: 30 },

	  "indot": { width: 24, height: 30 },
	  
	   "brownsburg": { width: 24, height: 30 },
	   
	   "hendricks": { width: 24, height: 30 }, 
	  
	  "bubble_red": { width: 24, height: 30 },



	  "pin_red": { width: 22, height: 26 },



	  "pin_green": { width: 22, height: 30 },



	  "pin_orange": { width: 22, height: 30 },



	  "pin_orange_multi": { width: 45, height: 30 },



	  "pin_shadow_multi": { width: 45, height: 30 },



	  "bubble_multi": { width: 30, height: 30 },



	  "pin_shadow": { width: 22, height: 30 },
	  
	  "mpo": { width: 24, height: 30 }



	};







	if (images) {



		if (icons[images[0]]) {



			icon = icons[images[0]];



		} else {



			icon = new GIcon();



			icon.image = "/_images/markers/" + images[0] + ".png";



			var size = iconData[images[0]];



			icon.iconSize = new GSize(size.width, size.height);



			icon.iconAnchor = new GPoint(size.width >> 1, size.height);

			//icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);



			if (images[1] != "") {



				var shadow_size = iconData[images[1]];



				icon.shadow = "/_images/markers/" + images[1] + ".png";



				icon.shadowSize = new GSize(shadow_size.width, shadow_size.height);



				icons[images[0]] = icon;



			}



		}



	}



	return icon;



}; // End getIcon Function







function reloadMarkers() {



	markerSearch();



};// End reloadMarkers Function



	



function showAddress(address) {



	if (geocoder) {



		geocoder.getLatLng( address, function(point) {



			if (!point) {



				alert(address + " not found");



			} else {



				updateLatLngValue( point.lat().toFixed(5),point.lng().toFixed(5));



				map.clearOverlays()



				map.setCenter(point, 16);



				marker = new GMarker(point, {draggable: true});  



				map.addOverlay(marker);	



				GEvent.addListener(marker, "dragend", function() {



					var point =marker.getPoint();



					map.panTo(point);



					updateLatLngValue( point.lat().toFixed(5),point.lng().toFixed(5));



				});



			}



		});



	}



};// End showAddress Function







function showLatLng(lat,lng) {



	var point = new GLatLng(lat,lng);



	updateLatLngValue( point.lat().toFixed(5),point.lng().toFixed(5));



	map.clearOverlays()



	map.setCenter(point, 15);



	marker = new GMarker(point, {draggable: true});  



	map.addOverlay(marker);	



	GEvent.addListener(marker, "dragend", function() {



		var point =marker.getPoint();



		map.panTo(point);



		updateLatLngValue( point.lat().toFixed(5),point.lng().toFixed(5));



	});



			



};// End showLatLng Function







function updateLatLngValue(lat,lng) {



	$("#lat").html(lat);



	$("#lng").html(lng);



};// End updateLatLngValue Function







function createCookie(name,value,days) {



	if (days) {



		var date = new Date();



		date.setTime(date.getTime()+(days*24*60*60*1000));



		var expires = "; expires="+date.toGMTString();



	}



	else var expires = "";



	document.cookie = name+"="+value+expires+"; path=/";



}







function readCookie(name) {



	var nameEQ = name + "=";



	var ca = document.cookie.split(';');



	for(var i=0;i < ca.length;i++) {



		var c = ca[i];



		while (c.charAt(0)==' ') c = c.substring(1,c.length);



		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);



	}



	return null;



}







function eraseCookie(name) {



	createCookie(name,"",-1);



}







//]]>




