(function($) {
$.gmap = function() { ; };
$.gmap.version = "1.0.0-a1";
  
function r(text, expr, val) {
	while(expr.test(text)) {
	text = text.replace(expr, val);
	}
	return text;
};

$.gmap.r = function(text, expr, val)  { return r(text, expr, val); };

$.gmap.data = [];

$.gmap.showInfo = function (i) {
  points[i].marker.openInfoWindowHtml( getInfo ( i )  );
}

function getInfo( i ) {
	return $.gmap.settings.getInfo(i);
}

$.gmap.getInfo = function(i) {
	return getInfo(i); 
}

$.gmap.renderDirectory = function ( data ) {
	$.gmap.data = data;
	var rows = Array("<table>");
	for (i = 0;i<data.length;i++) {
		x = getDirectoryItem( i )
		rows.push( x );
	}
	rows.push("</table>");
	$("#directory").html(rows.join(''));
}

function getDirectoryItem(i) {
	x = $.gmap.settings.getDirectoryItem(i);
	return x;
}

$.gmap.markers = new Array();

$.gmap.renderdata = function (map, data ) {
	$.gmap.markers = [];
	if (GBrowserIsCompatible()) {
		//$.gmap.map = map;
		map.clearOverlays();
		map.bounds = new GLatLngBounds();
	}
	$.gmap.data = data;
	for (i = 0;i<data.length;i++) {
		if (data[i].lat && data[i].lng) {
			point = new GLatLng(data[i].lat, data[i].lng);
			content = getInfo(i);
			holder = {'marker': createMarker(point, content, i), 'content':content};
			$.gmap.markers.push(holder);
			map.addOverlay(holder.marker);
			map.bounds.extend(point);
		}
	}
	// give it a little breathing room.
	var zoomLevel = map.getBoundsZoomLevel(map.bounds);
	if (zoomLevel >12) {
		zoomLevel = 12;
	}
	map.setZoom(zoomLevel +1 );
	map.setCenter(map.bounds.getCenter());
	//$.gmap.renderDirectory( data );
}

// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
function getBaseIcon() {
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	mDim = $.gmap.settings.markersSize;
	baseIcon.iconSize = new GSize(mDim.w, mDim.h);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	return baseIcon;
}
// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, bubbleContent, index) {
	// Create a lettered icon for this point using our icon class
	var letteredIcon = new GIcon(getBaseIcon());
	letteredIcon.image = getIcon(index);

	// Set up our GMarkerOptions object
	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
	//marker.openInfoWindowHtml(markers[index].content);
	marker.openInfoWindowHtml(bubbleContent);
	});
	return marker;
}

function getIcon(index) {
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    return $.gmap.settings.markersBase[0]  + letter + $.gmap.settings.markersBase[1];
}
$.gmap.getIcon = function(index)  { return getIcon(index); };

$.gmap.showDetails = function (item) {
	var markers = $.gmap.markers;
	if (markers[item]!=undefined && markers[item].marker != undefined) {
		markers[item].marker.openInfoWindowHtml(markers[item].content);
	}
}

  
// default settings
$.gmap.settings = {
	markersBase: ["http://www.google.com/mapfiles/marker",".png"],
	markersSize: {"w":20,"h":34},
	markers: ["http://www.google.com/mapfiles/marker"],
	markerMode: "Sequence",
	directoryItemTemplate: "<tr>"
			+	"<td valign=\"top\">%imageTag%</td>"
			+	"<td valign=\"top\" class=\"Normal\"><div class=\"MapLocation\" >"
			+	"<a class=\"Title\" href=\"#\" onclick=\"jQuery.gmap.showDetails(%index%); return false;\" >%title%</a><br />"
			+	"</div></td>"
			+"</tr>",
	infoTemplate: "<div class=\"Normal\" style=\"text-align: left;\">"
			+ "%description%"
			+ "</div>",
	getDirectoryItem: function (i) {
		var html = jQuery.gmap.settings.directoryItemTemplate;
		if(typeof(html) == 'object') html = $(html).html();
		var el = $.gmap.data[i];
		html = r(html, /%title%/, (el.FacilityName?el.FacilityName:''));
		html = r(html, /%index%/, i);
		html = r(html, /%imageTag%/, (['<img src=\'',getIcon(i),'\' alt=\'\' />'].join('')));
		return html;
	}, 
	getInfo: function( i ) {
		var html = jQuery.gmap.settings.infoTemplate;
		if(typeof(html) == 'object') html = $(html).html();
		html = r(html, /%description%/, ($.gmap.data[i].Description?$.gmap.data[i].Description:''));
		return html;
	}
};
})(jQuery);
