//requires markerclusterer.js http://gmaps-utility-library-dev.googlecode.com/svn/trunk/markerclusterer/markerclusterer.js
(function($){
	$.fn.destinations_map = function(options){
		$.fn.destinations_map.config = $.extend($.fn.destinations_map.config, options);
		$.fn.destinations_map.config.target = $(this).get(0);
		$.fn.destinations_map.initialize();
	};

	$.fn.destinations_map.config = {
		target:{},
		points:[],
		center: {lat: 42.79, long: 24.85},
		zoom: 7
	}

	$.fn.destinations_map.initialize = function()
	{
		google.load('maps', '2', {
			language: $('html').first().attr('lang').substring(0,2),
			callback: function()
			{
						var map = new google.maps.Map2($.fn.destinations_map.config.target);
						map.disableScrollWheelZoom();
						var point  = new GLatLng($.fn.destinations_map.config.center.lat, $.fn.destinations_map.config.center.long);

						map.setCenter(point, $.fn.destinations_map.config.zoom);
						map.setUIToDefault();
						map.setMapType(G_HYBRID_MAP);
						map.checkResize();
						$.fn.destinations_map.drawPointMarkers(map);
			}
		});
	}

	$.fn.destinations_map.drawPointMarkers = function(map)
	{
		var markers = new Array();
		$($.fn.destinations_map.config.points).each(
			function(i,point)
			{
				var pnt  = new GLatLng(point.lat, point.lng);
			 	marker = new GMarker(pnt);

				GEvent.addListener(marker, 'click', function(pnt){
					html='';
					for(var i = 0; i <  $.fn.destinations_map.config.points.length; i++)
					{
						var data = $.fn.destinations_map.config.points[i];
						if(data.lat == pnt.y && pnt.x == data.lng)
						{
							var html = '';

							$(data.links).each(function(index, val){
								html += $.fn.destinations_map.buildLink(val) + '<br>';
							})

							this.openInfoWindowHtml(html);
							return;
						}
					}

				})

				markers.push(marker);

			});

			var clusterer = new MarkerClusterer(map, markers);
	}

	$.fn.destinations_map.buildLink = function(link)
	{
		return '<a href="' + link.url + '">' + link.label + '</a>';
	}

})(jQuery)
