var startingPoints = [];
var map = null;
var requestUrl = 'http://www.coldwellbanker.aw/addons/maps/ajax.php?action=get_markers&classes=';

var iconRed = makeIcon("red");
var iconBlue = makeIcon("blue");
var iconGreen = makeIcon("green");
var iconWhite = makeIcon("white");
var iconYellow = makeIcon("yellow");

function getX() {
  var x = false;
  if (window.XMLHttpRequest) {
    try {
      x = new XMLHttpRequest();
    }
    catch (e) {
      x = false;
    }
  }
  else if (window.ActiveXObject) {
    try {
      x = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) {
      try {
        x = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e) {
        x = false;
      }
    }
  }
  return x;
}

function listingRequest() {
	var classes = '';
	if (document.mapform.classes)
 		classes = document.mapform.classes.value;
	xmlhttp = getX();
	if (xmlhttp) {
		xmlhttp.onreadystatechange = listingRequestHandler;
		xmlhttp.open("GET", requestUrl + classes, true);
		xmlhttp.send("");
	}
}

function listingRequestHandler() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			if (xmlhttp.responseText) {
				map.clearOverlays();
				eval(xmlhttp.responseText);
			}
		}
	}
}

function addStartingPoint(name, lat, lon, zoom, gtype) {
	startingPoints[startingPoints.length] = { name: name, lat: lat, lon: lon, zoom: zoom, gtype: gtype }; 
}

function loadMap() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        // map.addControl(new GMapTypeControl());

		var lat = startingPoints[0].lat;
		var lon = startingPoints[0].lon;
		var zoom = startingPoints[0].zoom * 1;
		var gtype = startingPoints[0].gtype;
//		alert(lat + "|" + lon + "|" + zoom + "|" + gtype);
		var pos = new GLatLng(lat, lon);
		switch (gtype) {
			case 'G_SATELLITE_MAP':
				map.setCenter(pos, zoom, G_SATELLITE_MAP);
				break;
			default:
				map.setCenter(pos, zoom, G_SATELLITE_MAP);
				break;
		}				
	    return map;
    }
}

function setMapLocation() {
    where = document.mapform.location.value;
	for (var i = 0;i < startingPoints.length; i++) {
		if (startingPoints[i].name == where) {
			var pos = new GLatLng(startingPoints[i].lat, startingPoints[i].lon);
			var zoom = startingPoints[i].zoom * 1;
			switch (startingPoints[i].gtype) {
				case 'G_SATELLITE_MAP':
					map.setCenter(pos, zoom, G_SATELLITE_MAP);
					break;
				default:
					map.setCenter(pos, zoom, G_SATELLITE_MAP);
			}				
			break;
		}
	}
//    document.mapform.location.selectedIndex = 0;
}

function makeIcon(color) {
    icon = new GIcon();
    icon.image = "http://labs.google.com/ridefinder/images/mm_20_" + color + ".png";
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(12, 20);  
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
    return icon;
}

function addMarker(icon, lat, lon, html) {
    var point = new GLatLng(lat, lon);
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}

function addTabs(icon, lat, lon, tabs) {
    var point = new GLatLng(lat, lon);
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowTabsHtml(tabs); 
    });
    return marker;
}
