var map;
var centerLatitude = 25.775161;
var centerLongitude = -80.187836;
var startZoom = 13;
var existingmarkers = {};
var ac;
var gc = 1;

function showExplainer(){
  window.open('http://mapping.ire.org/wfor/serious.html','Definition_of_serious_violations', 'height=350, width=500');
}

function showGeoExplainer(){
  window.open('http://mapping.ire.org/wfor/geo.html','Definition_of_serious_violations', 'height=350, width=500');
}



function init() {
  $('loader').hide();
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMenuMapTypeControl);
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
  map.enableScrollWheelZoom();
  updateMarkers();
  ac = new AutoComplete('search', 'suggest.php?m=json&c=43&s=', {
    delay: 0.25,
    resultFormat: AutoComplete.Options.RESULT_FORMAT_JSON
  }
  );
}

function getDetails(id, c){
  if (c == 6){
    window.open('http://mapping.ire.org/wfor_beta/detail_broward.php?d=' + id,'Inspection_details' ,"scrollbars=1, height=350, width=500");
  }else{
    window.open('http://mapping.ire.org/wfor_beta/detail.php?d=' + id,'Inspection_details' ,"scrollbars=1, height=350, width=500");
  }

}


function searchMarkers(){
  var name = $('search').value;
  $('loader').show();
  for (m in existingmarkers){
    if (existingmarkers[m].name == name){      
      map.panTo(existingmarkers[m].marker.getLatLng());
      existingmarkers[m].marker.openInfoWindowHtml(existingmarkers[m].html);
      $('loader').hide();
      return;
    }
  }
  $('result').innerHTML = 'Could not find daycare: ' + name;
  $('loader').hide();
  return;
}

function set_county_lng_lat(cid){
  var request = GXmlHttp.create();
  var center = new Array();
  center[0] = centerLatitude;
  center[1] = centerLongitude;
  request.open('GET', 'county.php?c=' + cid, true);

  request.onreadystatechange = function(){
    if (request.readyState < 4){
      $('loader').show();
    }
    if (request.readyState == 4) {
      $('loader').hide();
      var jscript = request.responseText;
      eval(jscript);
//      alert('panning to : ' + center);
      map.panTo(new GLatLng(center[0], center[1]));
    
    }
  } // onreadystatechange function
  request.send(null);
} // get_county function


function updateMarkers() {
  $('search').value = '';
  var county_num = $('counties').value;
  set_county_lng_lat(county_num);
  if (ac != undefined){
    ac.changeAction(county_num);
  }
  map.clearOverlays();
  existingmarkers = {};
  var request = GXmlHttp.create();
  request.open('GET', 'server.php?cn=' + county_num + '&gc=' + gc , true);
  request.onreadystatechange = function() {
    if (request.readyState < 4){
      $('loader').show();
    }
    if (request.readyState == 4) {
      $('loader').hide();
      var jscript = request.responseText;
      var points;
      eval(jscript);
      for (i in points) {
        var point = new GLatLng(points[i].lat,points[i].lng);
        points[i].html = points[i].html + "<br/><a href='javascript:getDetails(\"" + points[i].id + "\", " + points[i].cid + ")'>Inspection details</a>";
        var marker = createMarker(point,points[i].html, points[i].sn);
        existingmarkers[i] = points[i];
        existingmarkers[i].marker = marker;
        map.addOverlay(marker);
      } // end for
    }
    
  }
  
  request.send(null);
}

function getIcon(sn){
  var icon = new GIcon;
  icon.iconSize = new GSize(26,25);
  icon.shadow = "http://mapping.ire.org/wfor_beta/gicons/shadow.png";
  icon.shadowSize = new GSize(22,20);
  icon.iconAnchor = new GPoint(13,25);
  icon.infoWindowAnchor = new GPoint(13, 1);
  icon.infoShadowAnchor = new GPoint(26, 13);
  if (sn < 6){
    icon.image = "http://mapping.ire.org/wfor_beta/gicons/green.png";
    return icon;
  }else if (sn >= 6 && sn <= 10){
    icon.image = "http://mapping.ire.org/wfor_beta/gicons/yellow.png";
    return icon;
  }else{
    icon.image = "http://mapping.ire.org/wfor_beta/gicons/red.png";
    return icon;
  }
}


function createMarker(point, html, sn) {
  var marker = new GMarker(point, getIcon(sn));
  GEvent.addListener(marker, 'click', function() {
    var markerHTML = html;
    marker.openInfoWindowHtml(markerHTML);
  });
  return marker;
}

window.onload = init;
window.onunload = GUnload;

