var map;
var currentCounty;
var currentCountyID = 1;
var centerLatitude = 34.079962;  //,-118.243103
var centerLongitude = -118.243103;
var startZoom = 13;
var existingmarkers = {};
var ac;

/*
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(2);
  ac = new AutoComplete('search', 'suggest.php?m=json&c=1&s=', {
    delay: 0.25,
    resultFormat: AutoComplete.Options.RESULT_FORMAT_JSON
  }
  );
}

function getDetails(id){
  window.open('http://mapping.ire.org/kcbs/detail.php?l=' + id,'Citation_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 pharmacy: ' + name;
  $('loader').hide();
  return;
}


function updateMarkers() {
  $('search').value = '';
  var lacenterLatitude = centerLatitude;
  var lacenterLongitude = centerLongitude;
/*  var browardcenterLatitude = 26.164875;
  var browardcenterLongitude = -80.20395;
  var monroecenterLatitude = 24.559575;
  var monroecenterLongitude = -81.804242;*/
  var ocCenterLatitude = 33.7174708; 
  var ocCenterLongitude = -117.8311428;
  var riversideCenterLatitude = 33.94743;
  var riversideCenterLongitude = -117.400412;
  var sbCenterLatitude = 34.106866;
  var sbCenterLongitude = -117.611303;
  var venturaCenterLatitude = 34.2659;
  var venturaCenterLongitude = -119.258366;

// YOU ARE HERE

  var gc = $$('input:checked[type="radio"][name="services"]').pluck('value');
  var county_num =  Number($$('input:checked[type="radio"][name="counties"]').pluck('value'));
  if (county_num == 2){
    map.setCenter(new GLatLng(ocCenterLatitude, ocCenterLongitude), startZoom);
    $('thisCounty').innerHTML = 'Orange';
  }else if (county_num == 3){
    map.setCenter(new GLatLng(riversideCenterLatitude, riversideCenterLongitude), startZoom);
    $('thisCounty').innerHTML = 'Riverside';
  }else if (county_num == 4){
    map.setCenter(new GLatLng(sbCenterLatitude, sbCenterLongitude), startZoom);
    $('thisCounty').innerHTML = 'San Bernardino';
   }else if (county_num == 5){
     map.setCenter(new GLatLng(venturaCenterLatitude, venturaCenterLongitude), startZoom);
     $('thisCounty').innerHTML = 'Ventura'; 
  }else {
    map.setCenter(new GLatLng(lacenterLatitude, lacenterLongitude), startZoom);
    $('thisCounty').innerHTML = 'Los Angeles';
  }
  if (ac != undefined){
    ac.changeAction(county_num);
  }

  map.clearOverlays();
  existingmarkers = {};
  var request = GXmlHttp.create();
  request.open('GET', 'server.php?cn=' + county_num, 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);
        /*if (points[i].sn > 0){
          points[i].html = points[i].html + "<br/><a href='javascript:getDetails(\"" + points[i].id + "\")'>Get citation 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/kcbs/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 < 1){
    icon.image = "http://mapping.ire.org/kcbs/gicons/green.png";
    return icon;
/*  }else if (sn == 1){
    icon.image = "http://mapping.ire.org/kcbs/gicons/yellow.png";
    return icon;*/
  }else{
    icon.image = "http://mapping.ire.org/kcbs/gicons/red.png";
    return icon;
  }
}


function createMarker(point, html, sn) {
/*  if (html.indexOf('HOLLYWOOD PRESBYTERIAN') > 0){
    alert("Hollywood Presbyterian found. value of sn: " + 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;

