function create_map_icon(opts) {
  var width = opts.width || 32;
  var height = opts.height || 32;
  var primaryColor = opts.primaryColor || "#ff0000";
  var strokeColor = opts.strokeColor || "#000000";
  var cornerColor = opts.cornerColor || "#ffffff";
   
  var baseUrl = "http://chart.apis.google.com/chart?cht=mm";
  var iconUrl = baseUrl + "&chs=" + width + "x" + height + 
      "&chco=" + cornerColor.replace("#", "") + "," + primaryColor.replace("#", "") + "," + strokeColor.replace("#", "") + "&ext=.png";
  var icon = new GIcon(G_DEFAULT_ICON);
  icon.image = iconUrl;
  icon.iconSize = new GSize(width, height);
  icon.shadowSize = new GSize(Math.floor(width*1.6), height);
  icon.iconAnchor = new GPoint(width/2, height);
  icon.infoWindowAnchor = new GPoint(width/2, Math.floor(height/12));
  icon.printImage = iconUrl + "&chof=gif";
  icon.mozPrintImage = iconUrl + "&chf=bg,s,ECECD8" + "&chof=gif";
  var iconUrl = baseUrl + "&chs=" + width + "x" + height + 
      "&chco=" + cornerColor.replace("#", "") + "," + primaryColor.replace("#", "") + "," + strokeColor.replace("#", "");
  icon.transparent = iconUrl + "&chf=a,s,ffffff11&ext=.png";

  icon.imageMap = [
      width/2, height,
      (7/16)*width, (5/8)*height,
      (5/16)*width, (7/16)*height,
      (7/32)*width, (5/16)*height,
      (5/16)*width, (1/8)*height,
      (1/2)*width, 0,
      (11/16)*width, (1/8)*height,
      (25/32)*width, (5/16)*height,
      (11/16)*width, (7/16)*height,
      (9/16)*width, (5/8)*height
  ];
  for (var i = 0; i < icon.imageMap.length; i++) {
    icon.imageMap[i] = parseInt(icon.imageMap[i]);
  }

  return icon;
}


var gmap_handler = function(gmap_cfg) {
	var _gpub = {
		map: null,
		controls: true,
		lat: 0,
		lon: 0,
		zoom: 17,
		map_div: null,
		pushpins: null,
		pushpins_url: "pushpins.php?loc=",
		callback: "",
		gmap_initialize: function() {
			// Init the map
			_gpub.map = new google.maps.Map2(_gpub.map_div);
			_gpub.map.setCenter(new google.maps.LatLng(_gpub.lat, _gpub.lon), _gpub.zoom);
			
			// Controls
			_gpub.map.setMapType(G_HYBRID_MAP);
			if (_gpub.controls) {
				_gpub.map.enableScrollWheelZoom();
				_gpub.map.addControl(new GLargeMapControl());
				_gpub.map.addControl(new GScaleControl());
				_gpub.map.addControl(new GMapTypeControl());
			}
		
			// Events			
			GEvent.addListener(_gpub.map, "zoomend", function() { _gpub.mapchange(); });
			GEvent.addListener(_gpub.map, "moveend", function() { _gpub.mapchange(); });
			
			// Load Markers
			_gpub.mapchange();
			
			if (_gpub.callback != "") {
				setTimeout(_gpub.callback,500); // 500ms
			}
		},
		mapchange: function() {
			var bounds = _gpub.map.getBounds();
			//document.getElementById("message").innerHTML = bounds.toString();
			
			// Here's the HTTP Request stuff
			var req;
			
			if (window.XMLHttpRequest) { // Non-IE browsers
				req = new XMLHttpRequest();
			} else if (window.ActiveXObject) { // IE
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			if (req) {
				//Define "callback" function
				req.onreadystatechange = function (){
					switch (req.readyState) {
						// usually, these steps happen too fast to see
						case 4:
							if (req.status == 200) { // OK response
							
								// Process the data
								//_gpub.data = req.responseText.split(",");
								//_gpub.update_list();
								
								// The cluster fuck
								var xmlobj = req.responseXML;
								if (xmlobj == null) {
									// Error reading?
								}
								else {
									var root = xmlobj.getElementsByTagName('venues')[0];
									
									// Valid entry?
									if(root.getElementsByTagName("venue").length
										&& root.getElementsByTagName("venue")[0].firstChild){
											
										var items = root.getElementsByTagName("venue");
										
										
										for (var i = 0 ; i < items.length ; i++) {
											var item = items[i];
											var id = item.getElementsByTagName("primary_ID")[0].firstChild.nodeValue;
											var lat = item.getElementsByTagName("latitude")[0].firstChild.nodeValue;
											var lng = item.getElementsByTagName("longitude")[0].firstChild.nodeValue;
											var html = item.getElementsByTagName("html")[0].firstChild.nodeValue;
											var icon_type = item.getElementsByTagName("icon_type")[0].firstChild.nodeValue;
	
											_gpub.add_marker(id, lat, lng, html, icon_type);
										}
										
									}
								}
								
								
								
							}
							break;
					}			
				}
				
				// Lets do it...
				try {
					if (_gpub.pushpins_url != "") {
						req.open("GET", _gpub.pushpins_url + bounds.toString(), true);
						req.send('');
					}
				} catch (e) {
					alert(e);
				}
			}
		},
		add_marker: function (id, lat, lng, html, icon_type) {
			// Check if the marker is already in the array
			if (id != 0) {
				for (i=0;i<_gpub.pushpins.length;i++) {
					if (_gpub.pushpins[i] == "id"+id) {
						return false;
					}
				}
			}
			
			// Now add things
			var point = new GLatLng(lat, lng);
			//var marker = new GMarker(point, _gpub.marker_type(icon_type));
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});
			
			
			_gpub.map.addOverlay(marker);
			
			// Force Open
			marker.openInfoWindowHtml(html);
			
			if (id != 0) _gpub.pushpins.push("id" + id);
			
			return marker;
		},
		marker_type: function (icon_type) { // Custom Alesearch Biz
			switch (icon_type.toLowerCase()) {
				case "store":
					// Store
					var iconOptions = {};
					iconOptions.primaryColor = "#00E000";
					iconOptions.strokeColor = "#000000";
					iconOptions.label = "S";
					iconOptions.labelColor = "#000000";
					iconOptions.addStar = false;
					iconOptions.starPrimaryColor = "#FFFF00";
					iconOptions.starStrokeColor = "#0000FF";
					var icon = create_map_icon(iconOptions);
					break;
					
				case "bar":
					// Bar
					var iconOptions = {};
					iconOptions.primaryColor = "#E00000";
					iconOptions.strokeColor = "#000000";
					iconOptions.label = "B";
					iconOptions.labelColor = "#000000";
					iconOptions.addStar = false;
					iconOptions.starPrimaryColor = "#FFFF00";
					iconOptions.starStrokeColor = "#0000FF";
					var icon = create_map_icon(iconOptions);
					break;
					
				case "brewery":
					// Brewery
					var iconOptions = {};
					iconOptions.primaryColor = "#F6F3DA";
					iconOptions.strokeColor = "#000000";
					iconOptions.label = "br";
					iconOptions.labelColor = "#000000";
					iconOptions.addStar = false;
					iconOptions.starPrimaryColor = "#FFFF00";
					iconOptions.starStrokeColor = "#0000FF";
					var icon = create_map_icon(iconOptions);
					break;
			}
			return icon;
		},
		trigger_marker: function(marker) {
			GEvent.trigger(marker, "click");
			
			// Dont allow to continue....
			return false;
		}
	}
	google.load("maps", "2.x");
	
	_gpub.map_div = gmap_cfg.map_div;
	_gpub.zoom = gmap_cfg.zoom; // Note: 	17 = zoomed in all the way
								// 			0 = out all the way
	
	_gpub.pushpins = new Array();// To initialize the array
	
	_gpub.controls = gmap_cfg.controls;
	_gpub.lat = gmap_cfg.latitude;
	_gpub.lon = gmap_cfg.longitude;
	_gpub.pushpins = new Array();
	if (gmap_cfg.pushpins_url != undefined) _gpub.pushpins_url = gmap_cfg.pushpins_url;
	if (gmap_cfg.callback != undefined) _gpub.callback = gmap_cfg.callback;
	
	google.setOnLoadCallback(_gpub.gmap_initialize);
	
	return _gpub;
}
