
//
//
// Common java-script for managing the maps
//
//
//

var DIVEFLAG_MARKER = new GIcon();
DIVEFLAG_MARKER.image = "img/iconr_diveflag.png";
DIVEFLAG_MARKER.iconSize = new GSize(24, 24);
DIVEFLAG_MARKER.iconAnchor = new GPoint(12, 23);
DIVEFLAG_MARKER.infoWindowAnchor = new GPoint(12, 0);

var PUSHPIN_SHADOW_MARKER = new GIcon();
PUSHPIN_SHADOW_MARKER.shadow = "img/pushpin_shadow.png";
PUSHPIN_SHADOW_MARKER.shadowSize = new GSize(37, 34);
PUSHPIN_SHADOW_MARKER.iconSize = new GSize(24, 24);
PUSHPIN_SHADOW_MARKER.iconAnchor = new GPoint(12, 23);
PUSHPIN_SHADOW_MARKER.infoWindowAnchor = new GPoint(12, 0);

var GREEN_PUSHPIN_MARKER = new GIcon(PUSHPIN_SHADOW_MARKER);
GREEN_PUSHPIN_MARKER.image = "img/grn-pushpin.png";

var RED_PUSHPIN_MARKER = new GIcon(PUSHPIN_SHADOW_MARKER);
RED_PUSHPIN_MARKER.image = "img/red-pushpin.png";

function popupSiteWindow(site, externalp) {
  var win = window.open(site._site_info_url,
                        "site_detail_window",
                        "width=1024,height=600,scrollbars=yes");
  if (win==null || typeof(win)=="undefined") {
    var infoHTML;

    if(externalp) {
      infoHTML = "<span class=\"ReportText\" style=\"color:#112232; font-weight:bold\">" + site._site_name + 
		 "</span>" +
                 "<hr>" + 
	         "<span class=\"ReportText\" style=\"color:#112232\">" +
                 "Click on site marker for more information" +
                 "</span>";
    } else {
      infoHTML = "Unable to popup site-detail page ("+site._site_info_url+")." +
                 "Please verify that javascript is enabled and popup-blockers are disabled";
    }

    setTimeout(function(){site._site_marker.openInfoWindowHtml(infoHTML);},0);
  } else {
    if(win.focus) { win.focus(); }
    return true;
  }
  return false;
}

function addMarkerPoint(map, mgr, site) {
  var markerpt = new GLatLng(site._x, site._y);
  var marker;

  marker = new GMarker(markerpt, {icon: DIVEFLAG_MARKER, title:site._site_name} );
  GEvent.addListener(marker, 
                     'click', 
                     function() { 
                       popupSiteWindow(site, false);
                     });
  mgr.addMarker(marker, 15, 17);
  site._site_marker = marker;
}


function DiveSite(xcor,ycor,site_info_url,site_name,site_short_name) {
  this._x = xcor;
  this._y = ycor;
  this._site_info_url = site_info_url;
  this._site_name = site_name;
  this._site_short_name = site_short_name;
}



var diveSites = [
  new DiveSite(GPS_GREAT_PINNACLE[0], 
               GPS_GREAT_PINNACLE[1],
               "sites/great-pinnacle/great-pinnacle.html",
               "Great Pinnacle",
               "great-pinnacle"),
  new DiveSite(GPS_MARCOS_PINNACLE[0], 
               GPS_MARCOS_PINNACLE[1],
               "sites/marcos-pinnacle/marcos-pinnacle.html",
               "Marco's Pinnacle",
               "marcos-pinnacle"),
  new DiveSite(GPS_BETOS_REEF[0], 
               GPS_BETOS_REEF[1],
               "sites/betos-reef/betos-reef.html",
               "Beto's Reef",
               "betos-reef"),
  new DiveSite(GPS_TWIN_PEAKS[0], 
               GPS_TWIN_PEAKS[1],
               "sites/twin-peaks/twin-peaks.html",
               "Twin Peaks",
               "twin-peaks"),
  new DiveSite(GPS_3_SISTERS[0], 
               GPS_3_SISTERS[1],
               "sites/3-sisters/3-sisters.html",
               "3 Sisters",
               "3-sisters"),
  new DiveSite(GPS_SHORTCUT_REEF[0], 
               GPS_SHORTCUT_REEF[1],
               "sites/shortcut-reef/shortcut-reef.html",
               "Shortcut Reef",
               "shortcut-reef"),
  new DiveSite(GPS_SEA_MOUNT[0], 
               GPS_SEA_MOUNT[1],
               "sites/sea-mount/sea-mount.html",
               "Sea Mount",
               "sea-mount"),
  new DiveSite(GPS_HOLE_IN_THE_WALL[0], 
               GPS_HOLE_IN_THE_WALL[1],
               "sites/hole-in-the-wall/hole-in-the-wall.html",
               "Hole in the Wall",
               "hole-in-the-wall"),
  new DiveSite(GPS_MONTANA[0], 
               GPS_MONTANA[1],
               "sites/montana/montana.html",
               "Montana",
               "montana"),
  new DiveSite(GPS_ROAD_TO_TWIN_PEAKS[0], 
               GPS_ROAD_TO_TWIN_PEAKS[1],
               "sites/road-to-twin-peaks/road-to-twin-peaks.html",
               "Road to Twin Peaks",
               "road-to-twin-peaks"),
  new DiveSite(GPS_THUMBS_UP[0],
               GPS_THUMBS_UP[1],
               "sites/thumbs-up/thumbs-up.html",
               "Thumbs Up",
               "thumbs-up"),
/*
  new DiveSite(GPS_THE_PIT[0],
               GPS_THE_PIT[1],
               "sites/the-pit/the-pit.html",
               "The Pit",
               "the-pit"),
*/
  new DiveSite(GPS_E3[0],
               GPS_E3[1],
               "sites/e3/e3.html",
               "E3",
               "e3"),
  new DiveSite(GPS_DEEP_E3[0],
               GPS_DEEP_E3[1],
               "sites/deep-e3/deep-e3.html",
               "Deep E3",
               "deep-e3"),
  new DiveSite(GPS_D3[0],
               GPS_D3[1],
               "sites/d3/d3.html",
               "D3 (Kindergarden)",
               "d3"),
  new DiveSite(GPS_CYPRESS_CANYON[0],
               GPS_CYPRESS_CANYON[1],
               "sites/cypress-canyon/cypress-canyon.html",
               "Cypress Canyon",
               "cypress-canyon"),
  new DiveSite(GPS_UNCLE_PHILS_DEEP_TRENCH[0],
               GPS_UNCLE_PHILS_DEEP_TRENCH[1],
               "sites/uncle-phils-deep-trench/uncle-phils-deep-trench.html",
               "Uncle Phil's Deep Trench",
               "uncle-phils-deep-trench"),
  new DiveSite(GPS_PINNACLE_POINT_WALL[0],
               GPS_PINNACLE_POINT_WALL[1],
               "sites/pinnacle-point-wall/pinnacle-point-wall.html",
               "Pinnacle Point Wall",
               "pinnacle-point-wall"),
  new DiveSite(GPS_HONEYMOON_ROCKS[0],
               GPS_HONEYMOON_ROCKS[1],
               "sites/honeymoon-rocks/honeymoon-rocks.html",
               "Honeymoon Rocks",
               "honeymoon-rocks"),
  new DiveSite(GPS_EL_PASO[0],
               GPS_EL_PASO[1],
               "sites/el-paso/el-paso.html",
               "El Paso",
               "el-paso"),
/*
  new DiveSite(GPS_CANNERY_POINT[0],
               GPS_CANNERY_POINT[1],
               "sites/cannery-point/cannery-point.html",
               "Cannery Point",
               "cannery-point"),
*/
  new DiveSite(GPS_GRANITE_POINT_PINNACLES[0],
               GPS_GRANITE_POINT_PINNACLES[1],
               "sites/granite-point-pinnacles/granite-point-pinnacles.html",
               "Granite Point Pinnacles",
               "granite-point-pinnacles"),
/*
  new DiveSite(GPS_CANNERY_POINT_PINNACLES[0],
               GPS_CANNERY_POINT_PINNACLES[1],
               "sites/cannery-point-pinnacles/cannery-point0pinnacles.html",
               "Cannery Point Pinnacles",
               "cannery-point-pinnacles"),
*/
  new DiveSite(GPS_MIDDLE_REEF[0],
               GPS_MIDDLE_REEF[1],
               "sites/middle-reef/middle-reef.html",
               "Middle Reef",
               "middle-reef"),
  new DiveSite(GPS_GRANITE_POINT_WALL[0],
               GPS_GRANITE_POINT_WALL[1],
               "sites/granite-point-wall/granite-point-wall.html",
               "Granite Point Wall",
               "granite-point-wall"),
  new DiveSite(GPS_CROSSROADS_PINNACLE[0],
               GPS_CROSSROADS_PINNACLE[1],
               "sites/crossroads-pinnacle/crossroads-pinnacle.html",
               "Crossroads Pinnacle",
               "crossroads-pinnacle"),
  new DiveSite(GPS_EDS_STAIRCASE[0],
               GPS_EDS_STAIRCASE[1],
               "sites/eds-staircase/eds-staircase.html",
               "Ed's Staircase",
               "eds-staircase"),
  new DiveSite(GPS_G3[0],
               GPS_G3[1],
               "sites/f3-g3/f3-g3.html",
               "G3",
               "f3-g3"),
  new DiveSite(GPS_THE_NEEDLE[0],
               GPS_THE_NEEDLE[1],
               "sites/the-needle/the-needle.html",
               "The Needle",
               "the-needle"),
  new DiveSite(GPS_H3[0],
               GPS_H3[1],
               "sites/h3/h3.html",
               "H3",
               "h3"),
  new DiveSite(GPS_IANS_CASTLE[0],
               GPS_IANS_CASTLE[1],
               "sites/ians-castle/ians-castle.html",
               "Ian's Castle",
               "ians-castle"),
  new DiveSite(GPS_F3[0],
               GPS_F3[1],
               "sites/f3-g3/f3-g3.html",
               "F3",
               "f3-g3"),
  new DiveSite(GPS_ORGAN_PIPES[0],
               GPS_ORGAN_PIPES[1],
               "sites/organ-pipes/organ-pipes.html",
               "Organ Pipes",
               "organ-pipes"),
  new DiveSite(GPS_MIDDLE_COVE_CANYON[0],
               GPS_MIDDLE_COVE_CANYON[1],
               "sites/middle-cove-canyon/middle-cove-canyon.html",
               "Middle Cove Canyon",
               "middle-cove-canyon"),
  new DiveSite(GPS_PAULS_PROMISE[0],
               GPS_PAULS_PROMISE[1],
               "sites/pauls-promise/pauls-promise.html",
               "Paul's Promise",
               "pauls-promise"),
  new DiveSite(GPS_EDWARD_COOPERS_WALL[0],
               GPS_EDWARD_COOPERS_WALL[1],
               "sites/edward-coopers-wall/edward-coopers-wall.html",
               "Edward Cooper's Wall",
               "edward-coopers-wall"),
  new DiveSite(GPS_TEXAS[0],
               GPS_TEXAS[1],
               "sites/texas/texas.html",
               "Texas",
               "texas"),
  new DiveSite(GPS_SAN_DIEGO[0],
               GPS_SAN_DIEGO[1],
               "sites/san-diego/san-diego.html",
               "San Diego",
               "san-diego"),
  new DiveSite(GPS_T0[0],
               GPS_T0[1],
               "sites/t0/t0.html",
               "T0",
               "t0"),
  new DiveSite(GPS_TIJUANA[0],
               GPS_TIJUANA[1],
               "sites/tijuana/tijuana.html",
               "Tijuana",
               "tijuana"),
  new DiveSite(GPS_OUTER_TIJUANA[0],
               GPS_OUTER_TIJUANA[1],
               "sites/outer-tijuana/outer-tijuana.html",
               "Outer Tijuana",
               "outer-tijuana"),
  new DiveSite(GPS_DEEP_TIJUANA[0],
               GPS_DEEP_TIJUANA[1],
               "sites/deep-tijuana/deep-tijuana.html",
               "Deep Tijuana",
               "deep-tijuana"),
  new DiveSite(GPS_T4[0],
               GPS_T4[1],
               "sites/t4/t4.html",
               "T4",
               "t4"),
  new DiveSite(GPS_T5[0],
               GPS_T5[1],
               "sites/t5/t5.html",
               "T5",
               "t5"),
  new DiveSite(GPS_NAIAS_WALL[0],
               GPS_NAIAS_WALL[1],
               "sites/naias-wall/naias-wall.html",
               "Naia's Wall",
               "naias-wall")
];


function extend(superType) {
    var intermediateConstructor = function() {};
    intermediateConstructor.prototype = superType.prototype;
    return new intermediateConstructor();
}


// We define the function first
function TextToggleControl(initfn) {
  GControl.call();
  this.initfn = initfn;
  TextToggleControl.prototype.initialize = initfn;
  this.controlIndex = TextToggleControl.numControls;
  TextToggleControl.numControls++;
}
TextToggleControl.numControls = 0;

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
TextToggleControl.prototype = extend(GControl);

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.


// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
TextToggleControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7 + (100 * this.controlIndex), 7));
}

// Sets the proper CSS for the given button element.
TextToggleControl.prototype.setButtonStyle_ = function(button) {
  button.style.color = "#6699cc";
  button.style.backgroundColor = "#112232";
  button.style.font = "small Arial, sans-serif";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}


// We define the function first
function TextDisplayControl(initfn) {
  GControl.call();
  this.initfn = initfn;
  TextDisplayControl.prototype.initialize = initfn;
  this.controlIndex = TextDisplayControl.numControls;
  TextDisplayControl.numControls++;  
}
TextDisplayControl.numControls = 1;

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
TextDisplayControl.prototype = extend(GControl);

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.


// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
TextDisplayControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7 + (50 * this.controlIndex)));
}

// Sets the proper CSS for the given button element.
TextDisplayControl.prototype.setButtonStyle_ = function(button) {
  button.style.color = "#6699cc";
  button.style.backgroundColor = "#112232";
  button.style.font = "small Arial, sans-serif";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "13em";
  button.style.cursor = "pointer";
}


var textDisplayShowFn;
var textDisplayHide;
var boundaryPolygons;
function drawLobosBoundaries(map) {
  var diveable_pts = [];
  var permit_pts = [];

  for (i = 0; i < GPS_LOBOS_DIVEABLE_PTS.length; i++)
  {
    diveable_pts[i] = new GLatLng(GPS_LOBOS_DIVEABLE_PTS[i][0],
                                  GPS_LOBOS_DIVEABLE_PTS[i][1]);
  }
  diveable_pts[i] = diveable_pts[0];

  for (i = 0; i < GPS_LOBOS_PERMIT_AREA_PTS.length; i++)
  {
    permit_pts[i] = new GLatLng(GPS_LOBOS_PERMIT_AREA_PTS[i][0],
                                  GPS_LOBOS_PERMIT_AREA_PTS[i][1]);
  }
  permit_pts[i] = permit_pts[0];
  
  var diveable_poly = new GPolygon(diveable_pts,
                                   "#0033ff", /*strokeColor*/
                                   3,        /*strokeWeight*/
                                   0.75,    /*strokeOpacity*/
                                   "#0033FF",   /*fillColor*/
                                   0.3);      /*fillOpacity*/
  GEvent.addListener(diveable_poly, "mouseover",
                     function() {
                       textDisplayShowFn("Diveable Area",
                                         "#0033FF");
                     });
  GEvent.addListener(diveable_poly, "mouseout",
                     function() {
                       textDisplayHideFn();
                     });
  
  var permit_poly = new GPolygon(permit_pts,
                                 "#ff0000", /*strokeColor*/
                                 3,        /*strokeWeight*/
                                 0.75,    /*strokeOpacity*/
                                 "#ff0000",   /*fillColor*/
                                 0.3);      /*fillOpacity*/
  GEvent.addListener(permit_poly, "mouseover",
                     function() {
                       textDisplayShowFn("Research Permit Required",
                                         "#FF0000");
                     });
  GEvent.addListener(permit_poly, "mouseout",
                     function() {
                       textDisplayHideFn();
                     });
  
  boundaryPolygons = [diveable_poly,
                      permit_poly];

  for(i = 0; i < boundaryPolygons.length; i++) {
    map.addOverlay(boundaryPolygons[i]);
  }
}
function hideLobosBoundaries(map) {
  for(i = 0; i < boundaryPolygons.length; i++) {
    boundaryPolygons[i].hide();
  }
}
function unhideLobosBoundaries(map) {
  for(i = 0; i < boundaryPolygons.length; i++) {
    boundaryPolygons[i].show();
  }
}

/*    
var boundaryPolyLines;
function drawLobosBoundaries(map) {
  boundaryPolyLines = [
                 // park boundaries
                 new GPolyline([new GLatLng(GPS_LOBOS0[0],
                                            GPS_LOBOS0[1]),
                                new GLatLng(GPS_LOBOS1[0],
                                            GPS_LOBOS1[1])],
                               "#ff0000",
                               5, 0.75),
                 new GPolyline([new GLatLng(GPS_LOBOS1[0],
                                            GPS_LOBOS1[1]),
                                new GLatLng(GPS_LOBOS2[0],
                                            GPS_LOBOS2[1])],
                               "#ff0000",
                               5, 0.75),
                 new GPolyline([new GLatLng(GPS_LOBOS2[0],
                                            GPS_LOBOS2[1]),
                                new GLatLng(GPS_LOBOS3[0],
                                            GPS_LOBOS3[1])],
                               "#ff0000",
                               5, 0.75),
                 new GPolyline([new GLatLng(GPS_LOBOS3[0],
                                            GPS_LOBOS3[1]),
                                new GLatLng(GPS_LOBOS4[0],
                                            GPS_LOBOS4[1])],
                               "#ff0000",
                               5, 0.75),
                 new GPolyline([new GLatLng(GPS_LOBOS4[0],
                                            GPS_LOBOS4[1]),
                                new GLatLng(GPS_LOBOS5[0],
                                            GPS_LOBOS5[1])],
                               "#ff0000",
                               5, 0.75),
                 // no-dive boundary
                 new GPolyline([new GLatLng(GPS_LOBOS6[0],
                                            GPS_LOBOS6[1]),
                                new GLatLng(GPS_LOBOS7[0],
                                            GPS_LOBOS7[1])],
                               "#ff0000",
                               3, 0.75)
                ];

  for(i = 0; i < boundaryPolyLines.length; i++) {
    map.addOverlay(boundaryPolyLines[i]);
  }
}
function hideLobosBoundaries(map) {
  for(i = 0; i < boundaryPolyLines.length; i++) {
    boundaryPolyLines[i].hide();
  }
}
function unhideLobosBoundaries(map) {
  for(i = 0; i < boundaryPolyLines.length; i++) {
    boundaryPolyLines[i].show();
  }
}
*/

function drawSiteMarkers(map, mgr) {
  for(i = 0; i < diveSites.length; i++) {
    addMarkerPoint(map, mgr, diveSites[i]);
  }
  mgr.refresh();
}


function drawSpecialMarkers(map, mgr) {
  // draw the marker for the south end of middle reef
  var markerpt;
  var html;
  var marker;
  var marker_title;

  html = "<span class=\"ReportText\" style=\"color:#112232; font-weight:bold\">" +
         "  South End of Middle Reef"+
         "</span>" +
         "<hr>" +
         "<span class=\"ReportText\" style=\"color:#112232\">" +
         "This marks the Southern edge of Middle reef, where the reef breaks up into " +
         "sparse patches of very low-lying rubble and sand " +
         "in about 20' of water.  This area is usually surgy with low visibility, " +
         "but on very flat and clear days can be a good place to look for small critters." +
         "</span>" +
         "";
  markerpt = new GLatLng(GPS_MIDDLE_REEF_SOUTH_END[0], 
		         GPS_MIDDLE_REEF_SOUTH_END[1]);
  marker_title = "South end of Middle Reef";
  marker = new GMarker(markerpt, 
                       {icon: DIVEFLAG_MARKER, title: marker_title} );
  GEvent.addListener(marker, 
                     'click', 
                     function() { 
                         marker.openInfoWindowHtml(html, {maxWidth:300});
                     });
  mgr.addMarker(marker, 15, 17);
}


function TypedTileLayer(gCopyright, minZ, maxZ, overlayName, boundsArray, nFormat) {
  GTileLayer.call(this, gCopyright, minZ, maxZ); 
  this.overlayName = overlayName;
  this.boundsArray = boundsArray;
  this.nFormat     = nFormat;
}
TypedTileLayer.prototype=extend(GTileLayer);
TypedTileLayer.prototype.getTileUrl=function(a,z) {
  if((z <= this.maxResolution() && z >= this.minResolution()) &&
     ((a.x >= this.boundsArray[z][0]) && (a.x <= this.boundsArray[z][1])) &&
     ((a.y >= this.boundsArray[z][2]) && (a.y <= this.boundsArray[z][3]))) {
    var f = this.nFormat == 1 ?
      "gmaps/tiles/"+this.overlayName+"/Z"+z+"/"+a.y+"_"+a.x+".png" :
      "gmaps/tiles/"+this.overlayName+"/Z"+z+"/"+a.y+"/"+a.x+".png";
    
    return f;
  }

  return "gmaps/tiles/empty.png";
}



