var markers = [];
var markerIds = [];

function openWindow(url, width, height) {
 openNamedWindow(url, width, height, "Frias_Properties");
}

function openNamedWindow(url, width, height,name) {
        height += 50;          
  var sFeatures = "top=50,left=50,height=" + height + ",width=" + width + ",status=yes,scrollbars=yes,resizable=1,menubar=";
  var win = window.open(url,name,sFeatures);
        if (win != null) {
   win.focus();
   //win.resizeTo(width, height);
        }
}

/* Google Maps
 * Nothing here needs to be customized, just drop this code into an external js file.
 */
var map;
var geocoder;

function initGMap2(lat, long, zoom, type) {
/* create a new map centered at lat, long */
  map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng(lat, long), zoom);
  map.setMapType(type);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
}

function initGMap(minLat, minLon, maxLat, maxLon, mapType) {
/* create a new map containing given area */
  map = new GMap2(document.getElementById("map"));
  var midLat = (parseFloat(maxLat) + parseFloat(minLat))/2;
  var midLon = (parseFloat(maxLon) + parseFloat(minLon))/2;
  var zoom = map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(minLat, minLon),new GLatLng(maxLat,maxLon)));
  map.setCenter(new GLatLng(midLat, midLon), zoom);
  map.setMapType(mapType);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
}

function someFn() {
/* create a new map centered at lat, long */
  map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng("0","0"), 8);
  map.setMapType(G_HYBRID_MAP);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
}

function showAddress(address, html, zoom, isHTMLOpen) {
 showAddressMarker(address, html, zoom, isHTMLOpen, '');
}

function showAddressMarker(address, html, zoom, isHTMLOpen, markerId) {
  if (address!="") {
    if (!map) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());    
    } 
  
    if (!geocoder ) {
      geocoder = new GClientGeocoder();
    }
    
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
         if (failed_address == 0) {
           failed_address = 1;
           //alert(address + " not found");           
         }
        } else {
          if (zoom != -1) {
           map.setCenter(point, zoom);
          }
          createMarkerId(point, html, isHTMLOpen, markerId);
        }
      }
    );
  } 
}

var failed_address = 0;

function createMarker(point, html, isHTMLOpen) {
 createMarkerId(point, html, isHTMLOpen, '');
}

function createMarkerId(point, html, isHTMLOpen, markerId) {
     var marker = new GMarker(point);
     if (markerId) {      
      markers[markerId] = marker;
      marker.html_window = html;      
     }
     
     GEvent.addListener(marker, 'click', function() {
       marker.openInfoWindowHtml(html);
     });
     this.map.addOverlay(marker);
     if (isHTMLOpen) {
       marker.openInfoWindowHtml(html);
     }
}

function createMarker2(lat, long, html) {
     var marker = new GMarker(new GLatLng(lat, long));
     GEvent.addListener(marker, 'click', function() {
       marker.openInfoWindowHtml(html);
     });
     map.addOverlay(marker);
     marker.openInfoWindowHtml(html);
}

function addPointToMap(lat, lon, html, zoom, isHTMLOpen) {
 addPointToMapMarker(lat, lon, html, zoom, isHTMLOpen, '');
}

function addPointToMapMarker(lat, lon, html, zoom, isHTMLOpen, markerId) {

  if (!map) {
    map =   initGMap2(lat, lon, zoom, G_HYBRID_MAP); 
  }

    // Old API seems to have zoom constants backwards from new one.
    //zoom = 17 - zoom;

    //var point = new GPoint(lon, lat);
    var point = new GLatLng(lat, lon);
    //map.centerAndZoom(point, zoom);

    // Can't set map type unless set center is called
    //map.setMapType(G_HYBRID_MAP);
    createMarkerId(point, html,isHTMLOpen, markerId);
    //var marker = createMarker(lat, lon, html);
}

/* Greather than or equal to search
 *
 * Step 1: Add this code to an external js file called in the head of your
 * document.
 *
 * Step 2: Build out your form as if it were using the second smc
 * form option - either or logic. Add the field number to the end
 * of the fieldoptionid in the select name: fieldoptionid_103[]. Don't
 * set your select fields to multiple, this defeats the purpose. (If you
 * want to perform an inclusive search using <select multiple> fields, you
 * don't need this js file.) For more information see smc docs:
 * http://help.intrcomm.net/sitepages/pid112.php
 *
 * Step 3: Add a call to your expandSearchOnId to your search form, passing
 * in an array of fields you want to search on:
 * onsubmit="expandSearch([39,91,103])"
 *
 * Additional notes: this script does nothing to sort - you have to do that
 * in your smc preview or search results template. Any manual ordering will
 * break during pagination. This code should not require any customization.
 */

function expandSearch(ids) {
 // Clear out hidden fields to prevent previous searches from interfering.
 clearDynamicForm();

 // Perform search expansion on each id in the array passed to this function. 
 for (var a = 0; a < ids.length; a++) {
  expandSearchOnId(ids[a]);
 }
}

function expandSearchOnId(id) {
 // Look for the form and select box targeted by the id that has been passed in. 
 // We'll perform search expansion based on those options.
 var name = 'fieldoptionid_' + id + '[]';
 
 var forms = document.forms;
 for(var a = 0; a < forms.length; a++) {
  var form = forms[a];
  var elements = form.elements;
  
  for (var b = 0; b < elements.length; b++) {
   var select = elements[b];
   if (select.name ==  name) {
    expandSearchOnSelect(select, form);
   }
  }

  // This forces IE to reprocess form.
  if (form.normalize) {
   form.normalize();
  }   
 }
}


function expandSearchOnSelect(select, form) {
 var selected = false;
 var options = select.options;
 if (options) {
 for (var c = 0; c < options.length; c++) {
  var option = options[c];
  if (option.selected && option.value != "") {
   // If an option is selected, set selected flag so that all options
   // that follow will be added as hidden fields.
   selected = true;
  } else if (selected) {
   // If a selected option was encountered, create a hidden field containing expanded
   // search parameters.
   var input = document.createElement("input");
   input.type = "hidden";
   input.name = select.name;
   input.value = option.value;

   // Set className so that next time this form is submitted we can make sure
   // the last round of hidden fields are removed.
   input.className = "dynamicInput";

   // Add the new hidden field to the form.
   form.appendChild(input);
  }
 }
 }
}
 
function clearDynamicForm() {
 // Clear the form by searching for input fields with a class name of 
 // dynamicInput. This clears previous search parameters from the form.
 var forms = document.forms;
 for (var a = 0; a < forms.length; a++) {
  var form = forms[a];
  var inputs = form.getElementsByTagName('input');
  for (var b = 0; b < inputs.length; b++) {  
   if (inputs[b].className == 'dynamicInput') {
    alert('Removing: ' + inputs[b].name + ":" + inputs[b].value);
    inputs[b].name = "";
    inputs[b].value = "";
    inputs[b].className = "";
   }
  }
 }
}

function open_marker_window(id) {
 var marker = markers[id];
 if (marker) {
  marker.openInfoWindowHtml(marker.html_window);
 }
}
