// JavaScript Document

	 
//
   function showall() {
     var showall_point = map.getCenter();
	 var showall_zoom = map.getZoom();
	 if (showall_zoom < 12) {radius = 20;}
	 else if (showall_zoom < 15){radius = 4;}
	 else {radius = 1;}
	 showlocalMarker(showall_point,radius);
    }

//
//Show markers around any point
//	
   function showlocalMarker(point, radius) {
	 map.clearOverlays();
//	 radius = 6.0;
	 searchUrl = '/classes/genxml.php?lat=' + point.lat() + '&lng=' + point.lng() + '&radius=' + radius;
//	 alert('lat=' + point.lat() + '&lng=' + point.lng() + '&radius=' + radius);
     GetMarkers(searchUrl);
   }
   

//
//Choose a location and find other nearby markers
//
   function searchLocations() {
     var location = document.getElementById('addressInput').value;
     geocoder.getLatLng(location, function(latlng) {
       if (!latlng) {
		 latlng = map.getCenter();
		 alert(location + 'not found.  Locations shown for current map center ' + latlng);

       } else {
	   	 radius = 2.0;
	     searchUrl = '/classes/genxml.php?lat=' + latlng.lat() + '&lng=' + latlng.lng() + '&radius=' + radius;
         GetMarkers(searchUrl);
//		 alert('latlng is' + latlng);
       }
     });
   }//end function searchLocations
   
   function searchforMarkers(searchfor) {
 //    var searchfor = form.searchfor.value;
	 searchUrl = '/classes/genxml.php?whattodo=searchmarkers&searchfor=' + searchfor;
	 map.clearOverlays();
	 map.setCenter(new GLatLng(10.01894, -84.2134), 9);
     GetMarkers(searchUrl);
   }//end function searchforMarkers()
 	
   function GetPoly(PolyUrl) {
   	 var poly_xml ='';
     GDownloadUrl(PolyUrl, function(data) {
	   poly_xml = GXml.parse(data);
	   var polys = poly_xml.getElementsByTagName('poly');
	   for (var i = 0; i < polys.length; i++) {
	     var poly_type = polys[i].getAttribute('poly_type');
		 var poly_points = polys[i].getAttribute('poly_points');
		 var pts = [];
		 var poly_point = poly_points.split("|");
		 for (var q = 0; q < poly_point.length; q++) {
		   poly_point_temp = poly_point[q];
		   var pt = poly_point_temp.split(",");
		   var lng = parseFloat(pt[0]);
		   var lat = parseFloat(pt[1]);
		   pts[q] = new GLatLng(lat, lng);
           }//end (var q = 0; q < poly_point.length; q++)
		 var poly_color = polys[i].getAttribute('poly_color');
		 var poly_weight = polys[i].getAttribute('poly_weight');
		 var poly_opacity = polys[i].getAttribute('poly_opacity');
//		 var poly_fill = polys[i].getAttribute('poly_fill');
		 if (poly_type == 'line') {
		   map.addOverlay(new GPolyline(pts, poly_color, poly_weight, poly_opacity));
		   }
		 else if (poly_type == 'polygon') {
		   map.addOverlay(new GPolygon(pts, poly_color, poly_weight, 0.8, poly_color, poly_opacity));
		   }
	   }//end for (var i = 0; i < polys.length; i++)
     });//end GDownloadUrl(Url, function(data)
   }//end function GetPoly(Url) 

   function GetMarkers(Url) {
     var feature_count = 0;
	 var feature_this = '';
//     var radius = document.getElementById('radiusSelect').value;
     GDownloadUrl(Url, function(data) {
       var xml = GXml.parse(data);
       var markers = xml.getElementsByTagName('marker');
//       map.clearOverlays();
	   var hotel_sidebar = document.getElementById('hotel_sidebar');
	   hotel_sidebar.innerHTML = '<span style="color: #D79F00; font-size: 14px;"><strong>HOTELS & LODGES</strong></span>';
	   hotel_sidebar.style.display = 'none';
	   var food_sidebar = document.getElementById('food_sidebar');
	   food_sidebar.innerHTML = '<span style="color: #B6CAD8; font-size: 14px;"><strong>RESTAURANT & FOOD</strong></span>';
	   food_sidebar.style.display = 'none';
	   var activ_sidebar = document.getElementById('activ_sidebar');
	   activ_sidebar.innerHTML = '<span style="color: #006D33; font-size: 14px;"><strong>ACTIVITIES & ATTR.</strong></span>';
	   activ_sidebar.style.display = 'none';
	   var shop_sidebar = document.getElementById('shop_sidebar');
	   shop_sidebar.innerHTML = '<span style="color: #CC6766; font-size: 14px;"><strong>SHOP & SERVICES</strong></span>';
	   shop_sidebar.style.display = 'none';
       var landmk_sidebar = document.getElementById('landmk_sidebar');
       landmk_sidebar.innerHTML = '<span style="color: #000; font-size: 14px;"><strong>LOCATIONS</strong></span>';
	   landmk_sidebar.style.display = 'none';
	   var featured = document.getElementById('featured');
       featured.innerHTML = '<h2>Recommended!</h2>';
	   featured.style.display = 'none';

	   
       if (markers.length == 0) {
//         alert ('No existing markers found within ' + radius + ' kilometers of this location.');
//         map.setCenter(new GLatLng(9.940, -84.100), 8);
         return;
       }

       var bounds = new GLatLngBounds();
       for (var i = 0; i < markers.length; i++) {
	     var id = markers[i].getAttribute('id');
         var name = markers[i].getAttribute('name');
		 var mainlinkname = 'Costa-Rica-' + (name.replace(/ /g, '-'));
		 var label = name;
		 var description_safe = markers[i].getAttribute('description_safe');
		 var mainpagebutton = '';
		 if (description_safe == "") { }
		 else {
			 var mainpagebutton = '<a href="/information/' + id + '/1/' + mainlinkname + '.html" alt="' + name + ' Costa Rica information" title="more infomation on ' + name + ' Costa Rica"><img src="/graphics/buttons/mainpage.png" border="0" width="80"  height="24" title="Open the ' + name + ' main page" title="' + name + ' main page link"/></a>';
			 }
		 var code = markers[i].getAttribute('code');
		 var iconimage = "/GoogleSymbols/" + code + ".png";
		 var icon = markers[i].getAttribute('icon');
		 var photo = markers[i].getAttribute('photo');
		 if (photo == '') {}
		   else {
		     photo = '<img src="/MarkerImages/' + photo + '" border="0" width="320" height="80" title="' + name + '" style="padding-top: 5px" />';
			 } 
		 var distance = parseFloat(markers[i].getAttribute('distance'));
         var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));
         var category = markers[i].getAttribute('category');
		 var level = parseFloat(markers[i].getAttribute('level'));
		 if (icon != 'lnd') {
		   var codes = markers[i].getAttribute('codes');
		   if (codes == '') {codes_images = '';}
		   else {
		     var code_descs = markers[i].getAttribute('code_descs');
		     var codes_split = codes.split(",");
		     var code_desc_split = code_descs.split("|");
		     var codes_images = '';
		       for (var x=0; x<codes_split.length; x++) {
		         codes_images = codes_images + '<img src="/SymbolsTransp/' + codes_split[x] + '.png" border="0" height="32" width="24" title="' + code_desc_split[x] + '" />';
		         }
               }
		   
		   var accuracy = markers[i].getAttribute('accuracy');
		   if (accuracy >7) {
		     accuracy_disp = '<img src="/SymbolsTransp/1076_sm.png" border="0" width="11" height="12" title="' + id + ' exact GPS Location" />';
		   }
		   else {
		     accuracy_disp = '<img src="/SymbolsTransp/1076_sm.png" border="0" width="11" height="12" title="' + id + ' APPROXIMATE GPS Location" />';
		   }
		   var owner = markers[i].getAttribute('owner');
		   var refer = '';
		   if (owner == '4') {
		     refer = '<a href="http://mapcr.com/marker/9005/9/Costa-Rica-New-Key-19th-edition-2009.html" target="_blank" title="New Key to Costa Rica Guide Book"><img src="/graphics/buttons/newkey.png" border="0" width="80"  height="24" title="New Key to Costa Rica Guide Book" /></a>';
			 description_safe = '&nbsp;&nbsp;&nbsp;<a href="http://mapcr.com/marker/9005/9/Costa-Rica-New-Key-19th-edition-2009.html" target="_blank" title="description courtesy of Beatrice Blake, author of the New Key to Costa Rica">&copy;</a>&nbsp;' + description_safe;
			 }
           if (owner == '6') {
		     refer = '<a href="http://desafiocostarica.com" target="_blank" title="For Information and Reservations we recommend Desafio Adventure Company-click or call 1-866-210-0052"><img src="/graphics/buttons/desafio.png" border="0" width="80"  height="24" title="For Information and Reservations we recommend Desafio Adventure Company-click or call 1-866-210-0052" /></a>';
			 }
		   
		   var url = markers[i].getAttribute('url');
		   var email = markers[i].getAttribute('email');
		   if  (email == '') {}
			else {email = ' <a href="/user/email.php?contactemail=' + email + '&contactname=' + name + '&contact_id=' + id + '"><img src="/graphics/buttons/contact.png" border="0" width="80"  height="24" title="Contact ' + name + '" /></a> ';
			}
		   if  (url == '') {var weblink = "";}
		   else {var weblink = ' <a href="' + url + '" target="_blank" title="Open the ' + name + ' website"><img src="/graphics/buttons/goto.png" border="0" width="80"  height="24" title="Open the ' + name + ' website" /></a> ';}
		   var price_high = markers[i].getAttribute('price_high');
		   var price_low = markers[i].getAttribute('price_low');
		   var pricing = '';
		   if (category == 'hotel') {
		     if ((price_low > 0) && (price_high > 0)) {
			 pricing = 'Double occupancy from $' + price_low + ' to $' + price_high + '.<br />';
			 }
			 else if (price_high > 0) {
			 pricing = 'Double occupancy $' + price_high + '.<br />';
			 }
		   }
		   var phone = markers[i].getAttribute('phone');
		   var phones = '';
		   if (phone == '') {}
		   else {
		     phones = phones + 'phone: (506)' + phone;
			 } 
		   var phone_other = markers[i].getAttribute('phone_other');
		   if (phone_other == '') {}
		   else {
		     phones = phones + ', ' + phone_other;
			 } 
		   var phone_cell = markers[i].getAttribute('phone_cell');
		   if (phone_cell == '') {}
		   else {
		     phones = phones + ' cell ' + phone_cell;
			 } 
		   var phone_us = markers[i].getAttribute('phone_us');
		   if (phone_us == '') {}
		   else {
		     phones = phones + ' U.S. ' + phone_us;
			 } 
		   var phone_tollfree = markers[i].getAttribute('phone_tollfree');
		   if (phone_tollfree == '') {}
		   else {
		     phones = phones + ' toll free ' + phone_tollfree;
			 } 
		   var skype = markers[i].getAttribute('skype');
		   if (skype == '') {}
		   else {
		     phones = phones + ' skype: ' + skype;
			 } 
		   var fax = markers[i].getAttribute('fax');
		   if (fax == '') {}
		   else {
		     phones = phones + ' fax ' + fax;
			 } 
		   var maximized = markers[i].getAttribute('maximized');
//		   var maximized = 'max_content.html';


		   var add_marker_button = '<a href="javascript: mytrip_add_marker(' + id + ')"  title="Add ' + name + ' to Your Trip map and descriptions"><img src="/graphics/buttons/yourtrip.png" border="0" width="80" height="24"/></a>';
		   var disp =   '<P style="font-size: 16px; width: 330px; text-align: left; font-family: Times New Roman, Times, serif; color: #425438"><strong>' 
				 +    name + '</strong>' + photo + '</p>'
				 +    '<P style="font-size: 12px; width: 330px; text-align: justify; font-family: Times New Roman, Times, serif">' + description_safe + '<br /><br />'
				 +    codes_images + '<br />'
				 +    pricing + phones + '<br /><br />' + add_marker_button + mainpagebutton + email + refer + weblink + accuracy_disp + '<br /></p>';
		 
		   var html = disp;
		   
		   if (feature_count < 15 && level < 15) {
		     feature_count ++;
		     feature_this = 'yes';
             var color = '#FFFFFF';
			 
			 var mainpagelink = '...<a href="/information/' + id + '/1/' + mainlinkname + '.html" alt="' + name + ' Costa Rica information" title="more infomation on ' + name + ' Costa Rica">main page.</a>';
			 var description_nomore = description_safe.replace('...<a href="javascript:map.getInfoWindow().maximize()">more</a>', mainpagelink);
		     var featured_disp =   '<P style="font-size: 16px; text-align: left; font-family: Times New Roman, Times, serif; color: #425438"><strong>' 
				 +    name + '</strong><br />' + photo + '</p>'
				 +    '<P style="font-size: 12px; text-align: justify; font-family: Times New Roman, Times, serif">' + description_nomore + '<br /><br />'
				 +    codes_images + '<br />'
				 +    pricing + phones + '<br /><br />' + add_marker_button + mainpagebutton + email + refer + weblink + accuracy_disp + '<br /></p>';
		   }
		   
	   
           var marker = createMarker(point,label,html,icon,iconimage,maximized);
		 }// end if (icon != "lnd")
		 else {
		   var maximized = 'none';
		   var html = name + '<br /><br />' + description_safe;
		   var marker = createMarker(point,label,html,icon,iconimage,maximized);
		   }//end else
         map.addOverlay(marker);
		 
		 if (feature_this == 'yes') {
		   var feature = createFeaturedEntry(id, marker, featured_disp, color);
		   featured.appendChild(feature);
		   var feature_show_button = createFeatureShow(marker);
		   featured.appendChild(feature_show_button);
		   featured.style.display = 'block';
		   feature_this = '';
		 }
		 
         var sidebarEntry = createSidebarEntry(id, marker, name, code);
//if hotel put entry in hotel sidebar, if restaurant in restaurant sidebar etc.

         if (category == 'hotel') {
		   hotel_sidebar.appendChild(sidebarEntry);
		   hotel_sidebar.style.display = 'block';
		 }
		 else if (category == "food") {
		   food_sidebar.appendChild(sidebarEntry);
		   food_sidebar.style.display = 'block';
		 }
		 else if (category == "activity") {
		   activ_sidebar.appendChild(sidebarEntry);
		   activ_sidebar.style.display = 'block';
		 }
		 else if (category == "shop") {
		   shop_sidebar.appendChild(sidebarEntry);
		   shop_sidebar.style.display = 'block';
		 }
		 else {
		   landmk_sidebar.appendChild(sidebarEntry);
		   landmk_sidebar.style.display = 'block';
		 }
		 
         bounds.extend(point);
       }
//	   map.setCenter(bounds.getCenter());
       var bounds_zoom = map.getBoundsZoomLevel(bounds);
//	   if (bounds_zoom > 13) {bounds_zoom = bounds_zoom -1;}
	   map.setCenter(bounds.getCenter(), bounds_zoom);
     });   
   }// end function GetMarkers(Url)
 
//read list of entry_ids from a static XML file   
   function collectionLocations(coll_form) {
     map.clearOverlays();
     var collection_id = document.getElementById(coll_form).value;
	 var reg_ex = /p\b/;
	 var checkpoly = collection_id.match(reg_ex);
	 if (checkpoly == 'p') {
	   collection_id = collection_id.replace(reg_ex,'');
	   var PolyUrl ='/Collections_Poly/' + collection_id + '.xml';
	   GetPoly(PolyUrl); 
	   }	 
	     collectionUrl = '/Collections/' + collection_id + '.xml';
         GetMarkers(collectionUrl);

//		 alert('Collection is' + collectionUrl);
//       this doesn't seem to do anything...     GEvent.trigger(marker[lastmarker], 'click');
   }//end function collectionLocations
   
   function startingLocations(starting) {
     var collection_id = starting;
//	 alert('Collection is ' + collection_id);
	 var reg_ex = /p\b/;
	 var checkpoly = collection_id.match(reg_ex);
	 if (checkpoly == 'p') {
	   collection_id = collection_id.replace(reg_ex,'');
	   var PolyUrl ='/Collections_Poly/' + collection_id + '.xml';
	   GetPoly(PolyUrl); 
	   }	 
	     collectionUrl = '/Collections/' + collection_id + '.xml';
         GetMarkers(collectionUrl);
//		 alert('Collection is' + collectionUrl);
   }//end function startingLocations(starting)

//
//Make normal markers and sorted sidebar entries for locations found by search
//
//create an associative array of GIcons()
  var gicons = [];
  gicons["std"] = new GIcon();
  gicons["std"].image = "/GoogleSymbols/0145.png";
  gicons["std"].iconSize = new GSize(18, 24);
  gicons["std"].iconAnchor = new GPoint (6, 12);
  gicons["std"].infoWindowAnchor = new GPoint(6, 12);
  gicons["lnd"] = new GIcon();
  gicons["lnd"].image = "/GoogleSymbols/0145.png";
  gicons["lnd"].iconSize = new GSize(18, 24);
  gicons["lnd"].iconAnchor = new GPoint (6, 12);
  gicons["lnd"].infoWindowAnchor = new GPoint(6, 12);

  
//create the marker and set up the event window
    function createMarker(point,label,html,icon,iconimage,maximized) {
	  var tg_icon = new GIcon(gicons[icon]);
	  tg_icon.image = iconimage;
	  markerOptions = ({ title: label, icon:tg_icon });
	  var marker = new GMarker(point, markerOptions);
	  var maximumhtml = maximized;
	  if (maximumhtml == 'none') {
        GEvent.addListener(marker, "click", function() {
	    lastmarker = marker;
	    marker.openInfoWindowHtml(html);});
		}
      else {
	    GEvent.addListener(marker, 'click', function(maximized) {
        var maxContentDiv = document.createElement('div');
        maxContentDiv.innerHTML = 'Loading...'
        marker.openInfoWindowHtml(html,{maxContent: maxContentDiv, maxTitle: label});
        var iw = map.getInfoWindow();
	    lastmarker = marker;
        GEvent.addListener(iw, "maximizeclick", function() {GDownloadUrl('/max/' + maximumhtml, function(data) {maxContentDiv.innerHTML = data;});});});
		}
	  return marker;
	}

    function createSidebarEntry(id, marker, name, code) {
      var div = document.createElement('div');
//switch the commenting on the next two lines if you want images with the names	  
//      var symbname = "<img src='/SymbolsTransp_sm/" + code + ".png' alt='" + name + "' border='0' /> <strong>" + name + "</strong>" ;
	  var symbname = name;
	  var html = symbname;
      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.style.marginBottom = '5px'; 
      GEvent.addDomListener(div, 'click', function() {
        GEvent.trigger(marker, 'click');
		window.location='#top';
      });
      GEvent.addDomListener(div, 'mouseover', function() {
        div.style.backgroundColor = '#DCE0C3';
      });
      GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#fff';
      });
      return div;
    }//end function createSidebarEntry(id, marker, name, code)
	
	function createFeaturedEntry(id, marker, html, color) {
      var div = document.createElement('div');
	  div.style.backgroundColor = color;
	  div.style.border = 'ridge';	  
      div.innerHTML = html;
      div.style.marginLeft = '20px'; 
	  div.style.marginTop = '20px'; 
	  div.style.marginRight = '20px';
	  div.style.marginBottom = '0px'; 
	  div.style.paddingLeft = '20px';   
	  div.style.paddingTop = '20px';   
	  div.style.paddingRight = '20px';   
	  div.style.paddingBottom = '3px';
      return div;

	}//end function createFeaturedEntry(id, marker, html, color) 
	
	function createFeatureShow(marker) {
	  var div = document.createElement('div');
	  div.style.cursor = 'pointer';
	  div.style.marginTop = '-12px'; 
	  div.style.marginLeft = '520px';  
	  div.style.marginBottom = '20px';  
	  div.innerHTML = '<img src="/graphics/buttons/show-on-map.png" border="0" width="80" height="24" title="show on map" /><br />';
	    GEvent.addDomListener(div, 'click', function() {
        GEvent.trigger(marker, 'click');
		window.location='#top';
      });
	  return div;	
	}//end function createFeatureShow(marker) 


    //]]>
