//-- switch from KML to JSON
// red, blue, violet, cyan, orange, lightblue, green, yellow, pink
var redIconURL13 = 'http://dpdonline.org/images/icons/red-13.png';
var blueIconURL13 = 'http://dpdonline.org/images/icons/blue-13.png';
var violetIconURL13 = 'http://dpdonline.org/images/icons/violet-13.png';
var cyanIconURL13 = 'http://dpdonline.org/images/icons/cyan-13.png';
var orangeIconURL13 = 'http://dpdonline.org/images/icons/orange-13.png';
var lightblueIconURL13 = 'http://dpdonline.org/images/icons/lightblue-13.png';
var greenIconURL13 = 'http://dpdonline.org/images/icons/green-13.png';
var yellowIconURL13 = 'http://dpdonline.org/images/icons/yellow-13.png';
var pinkIconURL13 = 'http://dpdonline.org/images/icons/pink-13.png';

var redIconURL17 = 'http://dpdonline.org/images/icons/red-17.png';
var blueIconURL17 = 'http://dpdonline.org/images/icons/blue-17.png';
var violetIconURL17 = 'http://dpdonline.org/images/icons/violet-17.png';
var cyanIconURL17 = 'http://dpdonline.org/images/icons/cyan-17.png';
var orangeIconURL17 = 'http://dpdonline.org/images/icons/orange-17.png';
var lightblueIconURL17 = 'http://dpdonline.org/images/icons/lightblue-17.png';
var greenIconURL17 = 'http://dpdonline.org/images/icons/green-17.png';
var yellowIconURL17 = 'http://dpdonline.org/images/icons/yellow-17.png';
var pinkIconURL17 = 'http://dpdonline.org/images/icons/pink-17.png';

//
var redIconURL = redIconURL13;
var blueIconURL = blueIconURL13;
var violetIconURL = violetIconURL13;
var cyanIconURL = cyanIconURL13;
var orangeIconURL = orangeIconURL13;
var lightblueIconURL = lightblueIconURL13;
var greenIconURL = greenIconURL13;
var yellowIconURL = yellowIconURL13;
var pinkIconURL = pinkIconURL13;

//
function hitHttpJsonCrimeData(url) {
        if (window.XMLHttpRequest) {
                req = new XMLHttpRequest();
                req.onreadystatechange = processJsonCrimeData;
                req.open("GET", url, true);
                req.send(null);
        } else if (window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
                if (req) {
                        req.onreadystatechange = processJsonCrimeData;
                        req.open("GET", url, true);
                        req.send();
                }
        }
}

function processJsonCrimeData() {
        if (req.readyState == 4) {
                if (req.status == 200) {
                        var ret = req.responseText;
                        // debugLog ('Point results: ' + ret);
                        try {
                                eval( ret );
                        } catch (e) {
                                // statusText (siteText['errorText']);
                                // exceptionHandler (e.message);
                        }
                        getLAPDCrimeData(crimeData);
                }
        }
}

function getLAPDCrimeData(crimeData) {
// this code is specific to the JSON data provided by AJAX response

        var testZoom = map.getZoom();
        // loop by crimes
        for (N in crimeData.crimes) {
                var crimeRecord = crimeData.crimes[N];
                var latitude = crimeRecord.Latitude;
                var longitude = crimeRecord.Longitude;
                var color = crimeRecord.Color;

                var crimePoint = new GLatLng(latitude,longitude);
                var crimeIconURL = '';

                // if zoom level is at close level or search radius circle is 2 miles or less
                // if (testZoom < 14 && radius > 2) {
                if (false) {
                        redIconURL = redIconURL13;
                        blueIconURL = blueIconURL13;
                        violetIconURL = violetIconURL13;
                        cyanIconURL = cyanIconURL13;
                        orangeIconURL = orangeIconURL13;
                        lightblueIconURL = lightblueIconURL13;
                        greenIconURL = greenIconURL13;
                        yellowIconURL = yellowIconURL13;
                        pinkIconURL = pinkIconURL13;
                } else {
                        redIconURL = redIconURL17;
                        blueIconURL = blueIconURL17;
                        violetIconURL = violetIconURL17;
                        cyanIconURL = cyanIconURL17;
                        orangeIconURL = orangeIconURL17;
                        lightblueIconURL = lightblueIconURL17;
                        greenIconURL = greenIconURL17;
                        yellowIconURL = yellowIconURL17;
                        pinkIconURL = pinkIconURL17;
                }
                // red, blue, violet, cyan, orange, lightblue, green, yellow, pink
                switch(color) {
                        case "red":
                          crimeIconURL = redIconURL;
                          break;
                        case "blue":
                          crimeIconURL = blueIconURL;
                          break;
                        case "violet":
                          crimeIconURL = violetIconURL;
                          break;
                        case "cyan":
                          crimeIconURL = cyanIconURL;
                          break;
                        case "orange":
                          crimeIconURL = orangeIconURL;
                          break;
                        case "lightblue":
                          crimeIconURL = lightblueIconURL;
                          break;
                        case "green":
                          crimeIconURL = greenIconURL;
                          break;
                        case "yellow":
                          crimeIconURL = yellowIconURL;
                          break;
                        case "pink":
                          crimeIconURL = pinkIconURL;
                          break;
                }

                if (crimeIconURL > '') {
                  var bubbleText = getCrimeInfoHTML(crimeRecord);
                  // get crime info for popup
                  addCrimeIcon(crimePoint,crimeIconURL,bubbleText);
                }
        }

        // use center of circle to find division
        // another AJAX call after getting crimes on the map
        findLocalStation(circleCenterLatitude, circleCenterLongitude);
}

function getCrimeInfoHTML(crimeRecord) {
        //
        var crimeType = crimeRecord.Crime;
        var DR = crimeRecord.DR;
        var crimeDate = crimeRecord.Date;
        var division = crimeRecord.Division;
        var streetAddress = crimeRecord.streetAddress;
        var distance = crimeRecord.Distance;
        var description = crimeRecord.Description;
        var latitude = crimeRecord.Latitude;
        var longitude = crimeRecord.Longitude;

        // make a form
        var crimeReport = document.createElement("form");
        // crimeReport.setAttribute("action", "");
        // crimeReport.onsubmit = function() {reportIncident(); return false;};

        crimeReport.innerHTML = '<span id="crimetype" style="font-family: Arial,sans-serif; font-size: medium; font-weight: bold;">'
          + crimeType + '</span><br />'
          + '<span id="description" style="font-family: Arial,sans-serif; font-size: small;width:100%">'
          + description + '</span><br />';

        return crimeReport;
}

function addCrimeIcon(point, iconURL, incidentHTML) {
  var icon = new GIcon();
  icon.image = iconURL;
  icon.iconSize = new GSize(17,17);
  icon.iconAnchor = new GPoint(14,25);
  icon.infoWindowAnchor = new GPoint(14,14);
  var marker = new GMarker( point,icon);
  GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(incidentHTML,{ maxWidth:300 }); });
  //
  map.addOverlay(marker);
}

//-- end switch from KML to
