//
// - Some common functions 'n' stuff to work with various
//   google based maps that are used around here.
//

/*
// - Would be nice if this worked but it doesn't. It's for Overlays.
//
google.maps.KmlLayer.prototype.hide = function() {
  if (this.div_) {
    this.div_.style.visibility = "hidden";
  }
}

google.maps.KmlLayer.prototype.show = function() {
  if (this.div_) {
    this.div_.style.visibility = "visible";
  }
}

google.maps.KmlLayer.prototype.toggle = function() {
  if (this.div_) {
    if (this.div_.style.visibility == "hidden") {
      this.show();
    } else {
      this.hide();
    }
  }
}
*/

function liToggleMyKML (li) {

  Element.extend(li);

  img_on = li.select('img.legend_on').first();
  img_off = li.select('img.legend_off').first();

  layer = layers[li.id]['layer'];

  if (layer.toggleState == 1) {
    layer.setMap(null);
//    layer.hide();
    layer.toggleState = 0;
    li.removeClassName('map_option_selected');
    li.addClassName('map_option');
    img_on.hide();
    img_off.show();
  }
  else {
    layer.setMap(map);
//    layer.show();
    layer.toggleState = 1;
    li.removeClassName('map_option');
    li.addClassName('map_option_selected');
    img_on.show();
    img_off.hide();
  }

}

/*
 * - Use check boxes to switch layers.
 */
function cbToggleMyKML (checkbox) {

  e = checkbox.parentNode;
  Element.extend(e);

  layer = layers[checkbox.value]['layer'];

  if (layer.toggleState == 1) {
    layer.setMap(null);
    layer.toggleState = 0;
    checkbox.checked = 0;
    e.removeClassName('map_option_selected');
    e.addClassName('map_option');
  }
  else {
    layer.setMap(map);
    layer.toggleState = 1;
    checkbox.checked = 1;
    e.removeClassName('map_option');
    e.addClassName('map_option_selected');
  }

}

/*
 * - Use select boxes to switch layers.
 */
function toggleMyKML (L) {

  layer = layers[L.value]['layer'];

  O = L.options[L.selectedIndex];

  if (layer.toggleState == 1) {
    layer.setMap(null);
    layer.toggleState = 0;
    O.removeClassName('map_option_selected');
    O.addClassName('map_option');
    O.text = layers[L.value]['label'];
  } 
  else {
    layer.setMap(map);
    layer.toggleState = 1;
    O.removeClassName('map_option');
    O.addClassName('map_option_selected');
    O.text = "* " + layers[L.value]['label'];
  }

  L.value = 0;

}


/*
 * - Center and zoom in.
 */

var geocoder = new google.maps.Geocoder();

function zoom_to_address(address) {
 
  geocoder.geocode( {'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
        map.setCenter(results[0].geometry.location);
        map.setZoom(13);
      }
      else {
        alert('Sorry, address no found');
      }
    }
    else {
      alert('Google geocoder not OK. Returned ' + status);
    }
  } ); 

  return false;

}

