$(function()
{
	var parseCustomData = function(data)
	{
		return '<div style="width:305px;height:134px;padding:2px;font-family:Helvetica,Arial,Verdana,sans-serif;line-height:19px">' +

			'<img src="' + data.image + '" width="129" height="129" border="0" style="border:1px solid #cacaca;float:left;margin-right:15px" />' +
			'<div style="float:left;width:150px;position:relative">' +
				'<div style="float:left;width:100%;font-weight:bold;color:#e30071">' + data.title + '</div>' +
				'<div style="float:left;width:100%;font-weight:bold;font-size:85%;color:#2b2b2b">' + data.subtitle + '</div>' +
				'<div style="float:left;width:100%;font-size:85%;color:#2b2b2b">' + data.address + '</div>' +
				'<div style="float:left;width:100%;font-size:85%;color:#2b2b2b">' + data.city + ', ' + data.state + ', ' + data.zip + '</div>' +
				'<div style="float:left;width:100%;font-size:85%;color:#2b2b2b">' + data.email + '</div>' +
				'<div style="float:left;width:100%;font-size:85%;color:#2b2b2b">Phone ' + data.phone + '</div>' +
				'<div style="float:left;width:100%;font-size:85%;font-weight:bold"><a style="text-decoration:none;color:#006f84" href="' + data.profile + '">View My HairFlix Profile</a></div>' +
			'</div></div>';
	};

	$('.map').each(function()
	{
		var map = {};
		var self = this;

		eval($('#' + $(this).attr('rel')).html());

		var centerAt = false;

		if (map.init.centerAt)
		{
			centerAt = map.init.centerAt;
			map.init.centerAt = null;
		}

		map.init.mapShowjMapIcon = false;

		try
		{
			$(this).jmap('init', map.init || {}, function(el, options)
			{
				if (map.init && map.init.showScaleControl) el.jmap.addControl(new GScaleControl());

				var element = $(el);

				if (centerAt && centerAt.length)
				{
					try
					{
						element.jmap('searchAddress', {address: centerAt}, function(options, point)
						{
							element.jmap('moveTo', {mapCenter: [point.y, point.x]});
						});
					}
					catch (e){ /* address not found... */ }
				}

				if (map.markers)
				{
					self.markers = [];

					for (var i = 0; i < map.markers.length; i++)
					{
						var marker = $.extend({}, $.jmap.JMarkerDefaults, map.markers[i]);

						if (marker.customData)
						{
							marker.pointHTML = parseCustomData(marker.customData);
							marker.customData = null;
						}

						self.markers.push(new GMarker(new GLatLng(marker.pointLatLng[0], marker.pointLatLng[1]), marker));

						if (marker.pointHTML)
						{
							self.markers[i].pointHTML = marker.pointHTML;

							GEvent.addListener(self.markers[i], 'click', function()
							{
								this.openInfoWindowHtml(this.pointHTML);
							});
						}

						$.jmap.GMap2.addOverlay(self.markers[i]);
					}
				}
			});
		}
		catch (e)
		{
			/* Google Maps not reachable */
			return false;
		}
	});
});
