﻿// loading the map with defaults then data
function loadMapResults(dest, state) {
    var map = new GMap2($("#map").get(0));
    var washingtonState = new GLatLng(47.33137715779824, -120.706787109375);
    map.setCenter(washingtonState, 6);
    //map.setUIToDefault();

  map.clearOverlays();
  $('#loc_instructions').show();
  $('#loc_title').text('');
  $('#loc_address').text('');
  $('#loc_phone').text('');
  $('#loc_url').text('');
  $('#loc_dir').text('');
  $('#loc_csz').text('');

  var gcg = new GClientGeocoder();
  var firstItem = null;
  var readyForZoom = false;
  var showSeasonalKey = false;
  var showSatelliteKey = false;

  var facilityIcon = null;
  var satelliteIcon = null;
  var seasonalIcon = null;

  var markers = [];

  // create an array of locations by type
  var facilities = [];
  var seasonalClasses = []
  var satelliteClasses = []
  var otherListing = []

  $.ajax({
      type: "GET",
      url: "~/assets/locations/locations.xml",
      dataType: "xml",
      success: function(xml) {
          $(xml).find('state').each(function() {
              if ($(this).find('name').text() == state) {
                  // loop through all locations
                  $(this).find('location').each(function() {
                      // check if 'display' property equals 'true'                      
                      if ($(this).find('display').text() == 'True') {

                          // create formatting for array objects
                          var title = '<span>' + $(this).find('title').text() + '</span>';
                          title += '<br/>' + $(this).find('city').text();

                          // get the location type
                          var locType = '';
                          if ($(this).find('type').text() != '') {
                              locType = $(this).find('type').text();
                          }

                          // get the lat and lng for the locations within (this) state
                          marker = new GLatLng(parseFloat($(this).find('lat').text()), parseFloat($(this).find('lng').text()));

                          // put the list into the markers array
                          markers.push(marker);

                          var xmlLocation = $(this);
                          var point = null;

                          if (($(this).find('lat:first').text() != '') &&
                                ($(this).find('lng:first').text() != '')) {
                              point = new GLatLng(parseFloat($(this).find('lat:first').text()),
                                              parseFloat($(this).find('lng:first').text()));

                              var facilityIcon = new GIcon(G_DEFAULT_ICON);
                              facilityIcon.image = "./assets/images/GoogleMap/Orange.png";

                              var satelliteIcon = new GIcon(G_DEFAULT_ICON);
                              satelliteIcon.image = "./assets/images/GoogleMap/Orange.png";

                              var seasonalIcon = new GIcon(G_DEFAULT_ICON);
                              seasonalIcon.image = "./assets/images/GoogleMap/Green.png";

                              if (locType == '' || locType == 'facility') {
                                  markerOptions = { icon: facilityIcon };
                                  var marker = new GMarker(point, markerOptions);
                                  map.addOverlay(marker);
                              } else if (locType == 'seasonal') {
                                  markerOptions = { icon: seasonalIcon };
                                  var marker = new GMarker(point, markerOptions);
                                  map.addOverlay(marker);
                                  showSeasonalKey = true;
                              } else if (locType == 'satellite') {
                                  markerOptions = { icon: satelliteIcon };
                                  var marker = new GMarker(point, markerOptions);
                                  map.addOverlay(marker);
                                  showSatelliteKey = true;
                              } else {
                                  var marker = new GMarker(point);
                                  map.addOverlay(marker);
                              }
                          }
                      }
                      var addr = $(this).find('address').text() + ' ' + $(this).find('city').text() + ', ' + $(this).find('state').text() + '. ' + $(this).find('zip').text();
                      var newItem = $('<li class="singleLoc">' + title + '</li>')
                                .click(function() {
                                    if (point != null) {
                                        map.checkResize();
                                        if (readyForZoom == true) {
                                            map.setZoom(15);
                                        } else {
                                            map.setZoom(7);
                                        }
                                        map.panTo(point);
                                    }
                                    $('.activeLoc').removeClass('activeLoc');
                                    $(this).addClass('activeLoc');
                                    $('#loc_instructions').hide();
                                    $('#loc_title').text(xmlLocation.find('title').text());
                                    $('#loc_address').text(xmlLocation.find('address').text());
                                    $('#loc_csz').text(xmlLocation.find('city').text() + ', ' + xmlLocation.find('state').text() + '. ' + xmlLocation.find('zip').text());
                                    $('#loc_phone').text(xmlLocation.find('phone').text());
                                    $('#loc_url').html('<a href="' + xmlLocation.find('url').text() + '">Website</a>, <a href="mailto:' + xmlLocation.find('email').text() + '">Email</a>');
                                    if (xmlLocation.find('url').text().length > 0) {
                                        $('#loc_url').html('<a href="' + xmlLocation.find('url').text() + '">Website</a>, <a href="mailto:' + xmlLocation.find('email').text() + '">Email</a>');
                                    } else {
                                        $('#loc_url').html('<a href="mailto:' + xmlLocation.find('email').text() + '">Email</a>');
                                    }
                                    if (xmlLocation.find('register').text().length > 0) {
                                        $('#loc_register').show().html('<a target="_blank" href="' + xmlLocation.find('register').text() + '">Register Today</a>');
                                    } else {
                                        $('#loc_register').hide();
                                    }

                                });

                      if (locType == '' || locType == 'facility') {
                          facilities.push(newItem);
                      } else if (locType == 'seasonal') {
                          seasonalClasses.push(newItem);
                      } else if (locType == 'satellite') {
                          satelliteClasses.push(newItem);
                      }

                  });

                  $('#' + dest).empty();
                  // list out 'classes' in their categories
                  if (facilities.length != 0) {
                      $('#' + dest).append('<div id="facility" class="menu_head">Facilities</div><ul id="facilityList"></ul>');
                      $.each(facilities, function(intIndex, objValue) { $('#facilityList').append(objValue); });
                  }
                  if (seasonalClasses.length != 0) {
                      $('#' + dest).append('<div id="seasonal" class="menu_head">Seasonal Classes</div><ul id="seasonalList"></ul>');
                      $.each(seasonalClasses, function(intIndex, objValue) { $('#seasonalList').append(objValue); });
                  }
                  if (satelliteClasses.length != 0) {
                      $('#' + dest).append('<div id="satellite" class="menu_head">Satellite Classes</div><ul id="satelliteList"></ul>');
                      $.each(satelliteClasses, function(intIndex, objValue) { $('#satelliteList').append(objValue); });
                  }
                  if (otherListing.length != 0) {
                      $('#' + dest).append('<div id="other" class="menu_head">Other Listings</div><ul id="otherList"></ul>');
                      $.each(otherListing, function(intIndex, objValue) { $('#otherList').append(objValue); });
                  }

              }
          });

          if (showSeasonalKey == true || showSatelliteKey == true) {
              $("#key").empty();
              if (showSeasonalKey == true) {
                  $("#key").append("<img src='./Assets/images/googlemap/green.gif' alt='seasonal' height='20px' />&nbsp;=&nbsp;seasonal class</li>");
              }
              if (showSatelliteKey == true) {
                  $("#key").append("<img src='./Assets/images/googlemap/orange.gif' alt='seasonal' height='20px' />&nbsp;=&nbsp;satellite class</li>");
              }
          }
          readyForZoom = true;
          var latlngbounds = new GLatLngBounds();
          for (var i = 0; i < markers.length; i++) {
              latlngbounds.extend(markers[i]);
          }
          // center and zoom to the "bounds"
          map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds));
      }
  });
}