/*
var gmarkers = [];
var htmls = [];
var i=0;
var fullHtml;
*/

var map;
var points = [];
var gmarkers = [];
var to_htmls = [];
var from_htmls = [];
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// locations data
	//
	//
	//
http://maps.google.com/maps?daddr=The+Villages,+Florida&geocode=&dirflg=&saddr=15049+Hwy+Uu,+Bowling+Green,+MO+63334&f=d&sll=28.938392,-81.978993&sspn=0.09885,0.141792&ie=UTF8&z=6
var data = [
 { name: "New Location - Lake Sumter Landing - The Villages, FL", address: "1085 Canal Street<br/>The Villages, FL 32162<br/>", phone: "352-674-9300", lat:"28.9074", lng:"-81.97450" },
 { name: "Seminole Towne Center Mall", address: "Sanford, FL 32771<br/>", phone: "407-942-0475", lat:"28.8050", lng:"-81.33820" },
 { name: "Winter Park Village", address: "434 North Orlando Avenue<br/>Winter Park, FL 32789<br/>", phone: "407-942-0475", lat:"28.60160", lng:"-81.36420" },
 { name: "Exciting new location, coming soon", address: "The Villages, FL", phone: "", lat:"28.938392", lng:"-81.978993" }
];
//////////////////////////////////////////////////////////////////////////////////////////////// 
	//
	//
	//
	// addIcon(icon)
	//
	//
	//
function addIcon(icon) {
	icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	icon.iconSize = new GSize(32, 32);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(15, 34);
	icon.infoWindowAnchor = new GPoint(19, 2);
	icon.infoShadowAnchor = new GPoint(18, 25);
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// addClickevent(marker)
	//
	//
	//
function addClickevent(marker) {
	GEvent.addListener(marker, "click", function() {
	 	marker.openInfoWindowHtml(marker.content);
	});
	return marker;
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// buildMap()
	//
	//
	//
function buildMap() {
	if(!$('gmap')) return false;
	if(GBrowserIsCompatible()) {
  		map = new GMap2(document.getElementById("gmap"));
  		map.setCenter(new GLatLng(28.788623, -81.356322), 13);
  		map.addControl(new GLargeMapControl());
  		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
  	
  		// Light blue marker icons
  		var icon = new GIcon();
  		icon.image = "http://www.google.com/intl/en_de/mapfiles/ms/icons/ltblue-dot.png";
  		addIcon(icon);
  	
  		for(var i = 0; i < data.length; i++) {
  	 		points[i] = new GLatLng(parseFloat(data[i].lat), parseFloat(data[i].lng));
  	 		gmarkers[i] = new GMarker(points[i], icon);
  	
  	 		// Store data attributes as property of gmarkers
  	 		var html = "<div class='infowindow'>" +
  	 				   "<strong>"+ data[i].name + "<\/strong><p>" +
  	 					data[i].address + data[i].phone + "<\/p><\/div>";
			
			to_htmls[i] = html + '<b>Get directions:</b> <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
			   '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
			   '<input type="text" size=40 maxlength=80 name="saddr" id="saddr" value="" /><br>' +
			   '<input value="Get Directions" type="submit">' +
			   '<input type="hidden" name="daddr" value="' + data[i].lat + ',' + data[i].lng + 
			   '"/>';
			
			from_htmls[i] = html + '<b>Get directions:</b> <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
			   '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
			   '<input type="text" size=40 maxlength=80 name="daddr" id="daddr" value="" /><br>' +
			   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
			   '<input type="hidden" name="saddr" value="' + data[i].lat + ',' + data[i].lng +
			   '"/>';
			
			html = html + '<b>Get directions:</b> <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
			
  	 		gmarkers[i].content = html;
  	 		addClickevent(gmarkers[i]);
  	 		map.addOverlay(gmarkers[i]);
  		}
  		// Open infowindow of first marker
  		gmarkers[0].openInfoWindowHtml( gmarkers[0].content);
		prepare_loc_links();
 	}
} 
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// tohere();
	//
function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// fromhere
	//
function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// position_map()
	//
	//
	//
function position_map() {
	var cur_loc = this.point;
	map.panTo(points[cur_loc]);
	gmarkers[cur_loc].openInfoWindowHtml( gmarkers[cur_loc].content);
	return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// prepare_loc_links()
	//
	//
	//
function prepare_loc_links() {
	var links = $('store_locations').select('a.map_link');
	for(var i=0; i < links.length; i++) {
		var t = links[i];
		t.point = i;
		t.onclick = position_map;
	}

}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// start it up
	//
	//
	//
addLoadEvent(function() {
	buildMap();
});