var mapimage = "includes/imgs/gmap.png";
var ajaxurl = "includes/ajax/ajax_serve.php"; // url of the ajax server
var iconpath = "includes/imgs/icon/"; // url of icon folder (most found at http://labs.google.com/ridefinder/images/
var thumbnailurl = "thumbnail.php"; // url of thumbnail.php file
var homeurl = "index.php"; // path of the main map site
var map = null; // google map object
var timeoutholder = null; // timeouts - wait between keypresses
var timeoutholder2 = null; //timeouts - wait between clicks
var infoWindows = []; // array to hold the contents of the markers
var gmarkers = []; // array to hold markers
var glines = []; // array to hold polylines
var pointclosest = null; // holds points that user clicked on map
var groundOverlay = null; // holds image that is overlayed over the tiles
function onLoad() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
GEvent.addListener(map, "load", function() {document.getElementById("loading").style.visibility = (document.all) ? 'hidden' : 'collapse';});
var pointCenter = new GLatLng(32.2829422129357, -106.747756004334);
map.setCenter(pointCenter, 17, G_NORMAL_MAP);
var pointSW = new GLatLng(32.268519,-106.764268);
var pointNE = new GLatLng(32.286316,-106.736587);
var mapBounds = new GLatLngBounds(pointSW, pointNE);
groundOverlay = new GGroundOverlay(mapimage, mapBounds);
map.addOverlay(groundOverlay);
// To improve performance for panning, we want to hide the NMSU image and then show it again when we stop.
GEvent.addListener(map,"dragstart", function(){layerToggle(false)});
GEvent.addListener(map,"movestart", function(){layerToggle(false)});
GEvent.addListener(map,"zoomstart", function(){layerToggle(false)});
GEvent.addListener(map,"dragend", function(){layerToggle(true)});
GEvent.addListener(map,"moveend", function(){layerToggle(true)});
GEvent.addListener(map,"zoomend", function(){layerToggle(true)});
map.addControl(new GMapTypeControl());
map.addControl(new GSmallMapControl());
map.addControl(new GOverviewMapControl());
// add additional zoom options
map.enableDoubleClickZoom();
map.enableContinuousZoom();
map.enableScrollWheelZoom();
// add classes to the map
// check to see if user even has classes first
if (document.getElementById('classes-results')) {
getClasses();
checkMarkers('classes', true);
}
// get Permalink
getPermalink();
// uncheck all checkboxes
document.getElementById('COMPUTERLABS-check').checked = false;
document.getElementById('FOOD-check').checked = false;
document.getElementById('BUS-check').checked = false;
// put focus in search box
document.getElementById('txtSearch').focus();
// clear search box on page load
document.getElementById('txtSearch').value = "";
// add listener for when user clicks the map
GEvent.addListener(map, 'click', getreadytofind);
}
else
document.getElementById("map").innerHTML = "We are sorry, but your browser is not compatible with NMSU maps.";
}
// Shows/hides the NMSU map image when panning is involved.
function layerToggle(show) {
if (show == true)
groundOverlay.show();
else
groundOverlay.hide();
}
// Sets a timeout waiting to search but cancels it if another key is pressed within 0.4 secs
function getreadytolook() {
var search = document.getElementById("txtSearch").value;
if(timeoutholder != null)
window.clearTimeout(timeoutholder);
timeoutholder = window.setTimeout("getSearch(\'"+escape(search)+"\');", 400);
}
// Sets a timeout waiting to look up closest marker but cancels it if map is clicked in 0.3 secs
function getreadytofind(overlay, point) {
pointclosest = point; // write global variable
if(timeoutholder2 != null)
window.clearTimeout(timeoutholder2);
timeoutholder2 = window.setTimeout('getClosest()', 300);
}
// search, ajax call, parse results, add to map
function getSearch(search) {
var results = "";
map.closeInfoWindow();
// clear all previously searched overlays
var overlays = getOverlays();
for (var i in overlays) {
if (overlays[i].type == 'build' || overlays[i].type == 'dept' || overlays[i].type == 'places')
removeMarker(overlays[i]);
}
if (search.length == 0)
document.getElementById("results").innerHTML = "";
else if (search.length == 1)
document.getElementById("results").innerHTML = "You must type at least 2 characters to search.";
else {
document.forms.searchform.txtSearch.className = 'throbbing';
var urlstr = ajaxurl + "?search="+search;
GDownloadUrl(urlstr, function (response) {
document.forms.searchform.txtSearch.className = 'form-autocomplete';
var xmlDoc = GXml.parse(response);
buildings = xmlDoc.documentElement.getElementsByTagName("building");
if (buildings.length) {
results = results + "Buildings:
';
if (img != "")
contents = contents + '

';
if (info != "")
contents = contents + info;
contents = contents + '
PermaLink (Copy/Paste Link) ';
tab = new GInfoWindowTab('Bulding Info', contents);
this.tabs.push(tab);
};
this.addDeptTab = function (name, phone, url) {
var contents = '';
if (img != "")
contents = contents + '

';
if (info != "")
contents = contents + info;
tab = new GInfoWindowTab('Info', contents);
this.tabs.push(tab);
};
this.addClassTab = function (inside) {
var contents = '
';
contents = contents + '
' + inside + '
';
tab = new GInfoWindowTab('Class Info', contents);
this.tabs.push(tab);
};
this.addGenericTab = function (inside) {
var contents = '
';
contents = contents + '
' + inside + '
';
tab = new GInfoWindowTab('Info', contents);
this.tabs.push(tab);
};
this.writeMarker = function() {
var index = this.id;
if (this.tabs.length) {
infoWindows[index] = this.tabs;
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowTabsHtml(infoWindows[index]);
}
);
}
marker.type = this.type; // classes, build, perma, etc
marker.id = index; // build0, bulid1, etc
map.addOverlay(marker);
gmarkers[index] = marker;
};
}
// remove marker from the map
function removeMarker(overlay) {
infoWindows[overlay.id] = null;
gmarkers[overlay.id] = ''; // BUGBUG: using null totally breaks the app, no idea why
GEvent.clearInstanceListeners(overlay);
map.removeOverlay(overlay);
}
// return all markers on the map
function getOverlays() {
return gmarkers;
}
// return all polylines on the map
function getPolylines() {
return glines;
}
// opens the info window when the sidebar link is clicked
function openWindow(id) {
var overlays = getOverlays();
overlays[id].openInfoWindowTabsHtml(infoWindows[id]);
}
// hide or show markers when the checkbox is clicked
function checkMarkers(color, collapse){
var checked = color + '-check';
var results = color + '-results';
map.closeInfoWindow();
var overlays = getOverlays();
// hide markers by default
if (collapse == true) {
for (var i in overlays) {
if (overlays[i].type == color)
overlays[i].hide();
}
document.getElementById(results).style.visibility = (document.all) ? 'hidden' : 'collapse';
document.getElementById(results).style.height = '0px';
// make sure checkboxes are not checked, they have a habit of checking themselves
document.getElementById(checked).checked = false;
}
else {
// hide the marker
if (document.getElementById(checked).checked == false) {
for (var i in overlays) {
if (overlays[i].type == color)
overlays[i].hide();
}
document.getElementById(results).style.visibility = (document.all) ? 'hidden' : 'collapse';
document.getElementById(results).style.height = '0px';
}
// show the marker
else {
for (var i in overlays) {
if (overlays[i].type == color)
overlays[i].show();
}
document.getElementById(results).style.visibility = 'visible';
document.getElementById(results).style.height = '';
}
}
}
// hide or show markers when the checkbox is clicked
function checkMarkersAjax(color, icon){
var checked = color + '-check';
var results = color + '-results';
map.closeInfoWindow();
var overlays = getOverlays();
// if this is the first time showing the markers, get the information from teh database using ajax
if (document.getElementById(results).innerHTML == '') {
document.getElementById(results).innerHTML = '
';
var urlstr = ajaxurl + "?checkboxes=" + color;
GDownloadUrl(urlstr, function (response) {
var xmlDoc = GXml.parse(response);
// add markers to the map
locations = xmlDoc.documentElement.getElementsByTagName("location");
if (locations.length) {
for (var i = 0; i < locations.length; i++) {
var cm = new createMarker(color + locations[i].getAttribute("id"), color, locations[i].getAttribute("lat"), locations[i].getAttribute("lng"), icon);
cm.addGenericTab(locations[i].getAttribute("inside"));
cm.addBuildTab(locations[i].getAttribute("buildname"), locations[i].getAttribute("code"), locations[i].getAttribute("code"), locations[i].getAttribute("buildimg"), locations[i].getAttribute("buildinfo"));
cm.writeMarker();
}
}
// left sidebar (seperate from adding markers because two links might go to the same place.
individuals = xmlDoc.documentElement.getElementsByTagName("individual");
if (individuals.length) {
var resultsTxt = '
';
document.getElementById(results).innerHTML = resultsTxt;
}
});
}
// if information has been retrieved from the database previously, just hide/show the information
else {
// show the markers
if (document.getElementById(checked).checked == true) {
for (var i in overlays) {
if (overlays[i].type == color)
overlays[i].show();
}
document.getElementById(results).style.visibility = 'visible';
document.getElementById(results).style.height = '';
}
// hide the markers
else {
for (var i in overlays) {
if (overlays[i].type == color)
overlays[i].hide();
}
document.getElementById(results).style.visibility = (document.all) ? 'hidden' : 'collapse';
document.getElementById(results).style.height = '0px';
}
}
}
// get Bus Route
function busRoute(route, color, icon) {
var checked = route + '-check';
var results = route + '-results';
var overlays = getOverlays();
var polylines = getPolylines();
// show the route
if (document.getElementById(checked).checked == true) {
// if this is the first time showing the route, get information from the database using ajax
if (polylines[route] == undefined) {
document.getElementById(results).innerHTML = '
';
var points = [];
var urlstr = ajaxurl + "?busroute=" + escape(route);
GDownloadUrl(urlstr, function (response) {
var xmlDoc = GXml.parse(response);
// get lat/lng of the polyline segments and add to the map
routes = xmlDoc.documentElement.getElementsByTagName("route");
if (routes.length) {
// added parseFloat to coordinates to fix bug with polylines not displaying properly
for (var i = 0; i < routes.length; i++)
points.push(new GLatLng(parseFloat(routes[i].getAttribute("lat")), parseFloat(routes[i].getAttribute("lng"))));
}
var polyline = new GPolyline(points, color, 5);
glines[route] = polyline;
map.addOverlay(polyline);
// add bus stops to the map
stops = xmlDoc.documentElement.getElementsByTagName("busstop");
if (stops.length) {
for (var i = 0; i < stops.length; i++) {
var cm = new createMarker(route+i, route, stops[i].getAttribute("lat"), stops[i].getAttribute("lng"), icon);
cm.writeMarker();
}
}
// clear loading image
document.getElementById(results).style.visibility = (document.all) ? 'hidden' : 'collapse';
document.getElementById(results).style.height = '0px';
});
}
// if information has been retreived from the database previously, just show the information
else {
polylines[route].show();
for (var i in overlays) {
if (overlays[i].type == route)
overlays[i].show();
}
}
}
// hide the route
else {
polylines[route].hide();
for (var i in overlays) {
if (overlays[i].type == route)
overlays[i].hide();
}
}
}
// hide or show individual route checkboxes when the checkbox is clicked
function toggleBusRoutes(color) {
var checked = color + '-check';
var results = color + '-results';
var overlays = getOverlays();
var polylines = getPolylines();
// if first time getting info, inject html into the div, from now on will just hide and show it.
if (document.getElementById(results).innerHTML == '') {
document.getElementById(results).innerHTML = '
';
}
else {
// show the div
if (document.getElementById(checked).checked == true) {
document.getElementById(results).style.visibility = 'visible';
document.getElementById(results).style.height = '';
}
// hide the div and hide any individual bus routes that may have been displayed
else {
document.getElementById(results).style.visibility = (document.all) ? 'hidden' : 'collapse';
document.getElementById(results).style.height = '0px';
document.getElementById('Route1-check').checked = false;
document.getElementById('Route2-check').checked = false;
document.getElementById('Route3-check').checked = false;
for (var i in polylines)
polylines[i].hide();
for (var i in overlays) {
if (overlays[i].type == 'Route1' || overlays[i].type == 'Route2' || overlays[i].type == 'Route3')
overlays[i].hide();
}
}
}
}
// clear search clears the search box, removes searched results when the red X is clicked on.
function clearSearch() {
document.getElementById("txtSearch").value = "";
map.closeInfoWindow();
// clear all previously searched overlays
var overlays = getOverlays();
for (var i in overlays) {
if (overlays[i].type == 'build' || overlays[i].type == 'dept' || overlays[i].type == 'places')
removeMarker(overlays[i]);
}
document.getElementById("results").innerHTML = "";
}
// Try to free up some memory when the page exits
function onUnload() {
var overlays = getOverlays();
for (var i in overlays)
removeMarker(overlays[i]);
GEvent.clearInstanceListeners(map); // may be redundant, GUnload might already do this
GUnload();
}