//<![CDATA[


var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
var intCounter = 1;

// A function to create the marker and set up the event window
function createMarker(point,name,html,addr,map) {
	// The info window version with the "to here" form open
	to_htmls[i] = '<div id="' + 'popupWindow' + intCounter + '">' + html + '<br />Directions: <strong>To here</strong> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
	   '<br />Start address (Street, City, State, Zip):<form action="http://maps.google.com/maps" method="get" target="_blank">' +
	   '<input type="text" size="30" maxlength="48" name="saddr" id="saddr" value="" /><br />' +
	   '<input value="Get Directions" type="submit" /><br /><br />' +
	   '<input type="hidden" name="daddr" value="' + addr + '"/>' +
	   '</div>';

	// The info window version with the "to here" form open
	from_htmls[i] = '<div id="' + 'popupWindow' + intCounter + '">' + html + '<br />Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <strong>From here</strong>' +
	   '<br />End address (Street, City, State, Zip):<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
	   '<input type="text" size="30" maxlength="48" name="daddr" id="daddr" value="" /><br />' +
	   '<input value="Get Directions" type="submit" /><br /><br />' +
	   '<input type="hidden" name="saddr" value="' + addr + '"/>' +
	   '</div>';
   
	// The inactive version of the direction info
	html = '<div id="' + 'popupWindow' + intCounter + '">' + html + '<br /><strong>Get Directions:</strong> <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></div>';

	// Create the Marker
	var marker = new google.maps.Marker({
                         position: point,
                         title: name,
                         map: map
				 });
				 
	// Attach the Info Window
	var objInfoWindow = new google.maps.InfoWindow({});

	google.maps.event.addListener(marker, 'click', (function(markerArg,mapArg,intPoint) {
			return function() {
				objInfoWindow.setContent(html);
				objInfoWindow.open(mapArg, markerArg);
				if (intPoint == 1){
					mapArg.setCenter(new google.maps.LatLng(41.602900, -93.6400197));
				}
				else if (intPoint == 2){
					mapArg.setCenter(new google.maps.LatLng(40.829595, -96.711032));
				}
			};
	})(marker,map,intCounter));

	gmarkers[i] = objInfoWindow;
	htmls[i] = html;
	i++;
	var strContainerName = 'popupWindow' + intCounter;
	
	// Hide the scroll bar inside info window
	google.maps.event.addListener(objInfoWindow, 'domready', function() {
		document.getElementById(strContainerName).parentNode.style.overflow='';
		document.getElementById(strContainerName).parentNode.parentNode.style.overflow='';
	});
	
	//
	google.maps.event.addListener(objInfoWindow, 'closeclick', (function(mapArg,objPoint) {
		return function() {
				mapArg.setCenter(objPoint);
		};
	})(map,point));

	// Re center the map after closing the info Window
	// I know is not the best way of doing it, but it works in this case
	google.maps.event.addListener(map, 'click', (function(mapArg,objPoint) {
			return function() {
				objInfoWindow.close();
				mapArg.setCenter(objPoint);
			};
	})(map,point));

	intCounter++;
		
	return marker;
}

// Functions that open the directions forms
function tohere(i) {
	gmarkers[i].setContent(to_htmls[i]);
}

function fromhere(i) {
	gmarkers[i].setContent(from_htmls[i]);
}

// Display the maps, with some controls and set the initial location
function load() {
	var point1 = new google.maps.LatLng(41.586889, -93.640097);
	var point2 = new google.maps.LatLng(40.813595, -96.711032);
	
	var map1 = new google.maps.Map(document.getElementById("map1"), {
                      zoom: 13,
                      scrollwheel: false,
					  center: point1,
                      mapTypeId: google.maps.MapTypeId.ROADMAP,
					  mapTypeControl: false,
					  streetViewControl: false
               });
	
	var map2 = new google.maps.Map(document.getElementById("map2"), {
					  zoom: 13,
					  scrollwheel: false,
					  center: point2,
					  mapTypeId: google.maps.MapTypeId.ROADMAP,
					  mapTypeControl: false,
					  streetViewControl: false
               });
					
	// Set up markers with info windows		   
	var addr1 = "1707+High+St,+Des+Moines,+IA+50309";
	var marker1 = createMarker(point1,'Des Moines Office','<strong>Des Moines Office</strong><br />1707 High Street<br />Des Moines, IA 50309-3313',addr1,map1);
		
	var addr2 = "720+O+St,+Suite+D+Lincoln,+NE+68508";
	var marker2 = createMarker(point2,'Lincoln Office','<strong>Lincoln Office</strong><br />720 "O" Street, Suite D<br />Lincoln, NE 68508-1322',addr2,map2);
}
//]]>
