//<![CDATA[
//
//  scripts by Darryl--not from Google, as the name suggests...
//

var locationName = '';
var mallName = '';
var address1 = '';
var address2 = '';
var city = '';
var state = '';
var zip = '';
var mapAddress = '';
var latlong;
var map;
var geocoder;
var place;
var point;
var marker;
var strHTML = '';

function loadMap(q) {
	getSearchCriteria(q);
	mapAddress = address1 + ', ' + city + ', ' + state + ', ' + zip;  

	strHTML = '';

	strHTML += '<b>' + decodeChars(locationName) + '</b><br />';
	if (mallName.length > 1) {
		strHTML += mallName + '<br />';
	}
	strHTML += address1 + '<br />' + city + ', ' + state + ' ' + zip + '<br /><a target=\"_new\" href=\"http://maps.google.com/maps?q=' + address1 + ',' + city + ',' + state + '%20' + zip + '\">Get Directions</a>';
  
	if (GBrowserIsCompatible()) {
  
  
  
		map = new GMap2(document.getElementById("map"));
  
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
  
		if (latlong.toString().length > 1 ) {
			var arr = latlong.split(',');
		  
			var fltLat = 25.1336; //parseFloat(arr[0]);
			var fltLong = 55.1865; //parseFloat(arr[1]);
			var point = new GPoint(fltLat,fltLong);
		
			//alert('we have a latlong: ' + point);
			marker = new GMarker(point);
			map.setCenter(new GLatLng(point), 13);
			map.addOverlay(marker);
	        marker.openInfoWindowHtml(strHTML); 
		
		} else {
			// Create new geocoding object
			geocoder = new GClientGeocoder();
	
	
	        if (geocoder) {
	          geocoder.getLatLng(mapAddress, function(point) {
	            if (!point) {
	              alert(mapAddress + " not found");
	            } else {
				 // prompt('LongLat returned by geocoder: ',point);
	              map.setCenter(point, 13);
   	           marker = new GMarker(point);
   	           map.addOverlay(marker);
   	           marker.openInfoWindowHtml(strHTML);
   	         }
   	       }
   	       );
   	     }
	
	
		// Retrieve location information, pass it to addToMap()
		//alert(mapAddress);
		//geocoder.getLocations(mapAddress, addToMap);
		//prompt(mapAddress,geocoder.getLatLng());
		}
  
  

    //map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	



	
	//GEvent.addListener(map, "click", function(marker, point) {  if (marker) {    map.removeOverlay(marker);  } else {    map.addOverlay(new GMarker(point));  }});
	
  }
}

// This function adds the point to the map

function addToMap(response) {
	var strGoogleQuerystring =  '';

	
   // Retrieve the object
   place = response.Placemark[0];

   // Retrieve the latitude and longitude
   point = new GLatLng(place.Point.coordinates[1],
                       place.Point.coordinates[0]);

   // Center the map on this point
   map.setCenter(point, 13);

   // Create a marker
   marker = new GMarker(point);

	//marker.openInfoWindowHtml("<table width='215'><tr><td><a target='_blank' href='http://www.lcls.org/'>Lewis & Clark Library System</a></td></tr><tr><td><img src='http://www.lcls.org/images/galleries/tour/01-BuildingFromLot.JPG' border='0' width='195px' height='95' /></td></tr><tr><td>425 Goshen Road<br />Edwardsville,IL 62025<br />618-656-3216</td></tr></table><br /><a target='_blank' href='http://maps.google.com/maps?q=425 Goshen Road%20Edwardsville,%20IL'>Directions</a>");
   
   
   // Add the marker to map
   map.addOverlay(marker);


   // Add address information to marker
   //marker.openInfoWindowHtml(place.address);
   marker.openInfoWindowHtml(strHTML);
 //   marker.openInfoWindowHtml('<b>' + locationName + '</b><br />' + mallName + '<br />' + address1 + '<br />' + city + ', ' + state + ' ' + zip + '<br /><a target=\"_new\" href=\"http://maps.google.com/maps?q=' + address1 + ',' + city + ',' + state + '%20' + zip + '\">Get Directions</a>');   
}


function getSearchCriteria(q) {
	var divCriteria = document.getElementById('divCriteria');
	locationName = getQueryStringValue('location',q);
	mallName = getQueryStringValue('mall',q);
	address1 = getQueryStringValue('address1',q);
	address2 = getQueryStringValue('address2',q);
	city = getQueryStringValue('city',q);
	state = getQueryStringValue('state',q);
	zip = getQueryStringValue('zip',q);
	latlong = getQueryStringValue('ll',q);
	var strHTML = '';
	
	strHTML += '<b>' + decodeChars(locationName) + '</b>';
	if (mallName.length > 0) {
		strHTML += ', ' + mallName;
	}
	strHTML += ': ' + address1;
	if (address2.length > 0) {
		strHTML += ', ' + address2;
	}
	strHTML += ', ' + city;
	strHTML += ' ' + state;
	strHTML += ' ' + zip;
	
	divCriteria.innerHTML = strHTML;
}

function getQueryStringValue(name,qry)   // returns a named value from the querystring.  if "qry" is not provided, request querystring is used
{
   var tmp = ( location.search.substring(1) );
   
   if (qry.length>0) {
   	  tmp = qry;
   }
   var i   = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");

   if ( i >= 0 )
   {
      tmp = tmp.substring( name.length+i+1 );
      i = tmp.indexOf("&");
      return unescape( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
   }

   return("");
}

function showMap(q) {
	var divResults = document.getElementById('divResults');
	var divSplash = document.getElementById('divSplash');
	
	divSplash.style.display = 'none';
	divResults.style.display = 'block';
	loadMap(q);
}

function encodeChars(s) {
	s = s.replace("'", "|apos");
	s = s.replace("&", "|amp");
	return s;
}

function decodeChars(s) {
	s = s.replace("|apos", "'");
	s = s.replace("|amp", "&");
	return s;
}

//]]>