/*
**  veapp.js
**
**  Created by Robin Hamilton-Pennell on 2007-04-15.
**  Copyright (c) 2007 The Timoney Group. All rights reserved.
*/

   var latLong = null;
   var zoomLevel = 9;
 





var lastPin = null, trClass = true, map = null, $J = jQuery.noConflict(),
tabberOptions = {manualStartup:true}
var ua = navigator.userAgent, n = "Firefox/", p = ua.indexOf(n);

// If the browser is Firefox 1.5 or above override the Virtual Earth drawing functions to use SVG
if (p != -1 && parseFloat(ua.substring(p + n.length)) >= 1.5)
  Msn.Drawing.Graphic.CreateGraphic = function(f,b){ return new Msn.Drawing.SVGGraphic(f,b) };

function Log(l) {
  $J('#log').html(l);
};

function addDBPin(which,p) {
  var a = [];
  var icon = null;//'images/pins/' + escape(p.store) + '.png';
  var msg = '';//[p.name].join('<br/>');
  var tbody_tr = $J('<tr/>').appendTo($J('#'+which+'_table tbody'));
  /*var pin = addPin(p.y_coord, p.x_coord, icon, p.opname, msg);
  var row = $J('<tr/>').html('<th><img src="' + icon + '"/></th><td><b>' + p.opname + '</b><br/>' + msg + '</td>').click(function() {
    showPin('#' + pin.ID + '_' + map.GUID, this);
  });
  
  $J('<table/>').append(row).appendTo( $J('#key'));*/
  
  if (trClass = !trClass)
    tbody_tr.addClass('odd');
  
  for (var k in eval(which+'ALLOWED')) {
    var val = p[k] || '';
    tbody_tr.append($J('<td/>').html(val));
    a.push(val);
  }
  
  //$J('#excel')[0].value += '\n' + a.join('\t');
};

function addDBPoly(obj, isCircle) {
   var colors = isCircle ? [new VEColor(255, 100, 0, 1.0), new VEColor(255, 200, 0, 0.5)] : [new VEColor(150, 0, 150, 1.0), new VEColor(150, 0, 150, 0.5)];
   addPoly('poly', $J.map(obj.geom, function(e){ return new VELatLong(e[0], e[1]) }), 3, colors[0], colors[1], customLayer);
  
  if (obj.x && obj.y)
    addPin(obj.x, obj.y, 'images/point.png', obj.name, obj.desc);
};

function addPin(x, y, icon, title, msg, addTo) {
  try {
    var pt = y ? new VELatLong(x, y) : x;
    var pin = new VEShape(VEShapeType.Pushpin, pt);
    pin.SetCustomIcon(icon);
    if (title) pin.SetTitle(title);
    if (msg) pin.SetDescription(msg);
    (addTo || map).AddShape(pin);
    return pin;
  } catch(e) { alert([id,x,y,icon,title,msg]+'\n'+e.message);return null; }
};

function addPoly(type, pts, width, line, fill, addTo) {
   /* var poly = new VEPolygon(id, pts);
   poly.SetOutlineWidth(width || 3);
   poly.SetOutlineColor(line || new VEColor(0, 150, 0, 1.0));
   poly.SetFillColor(fill || new VEColor(0, 150, 0, 0.5));
   map.AddPolygon(poly);
   return poly;*/

   if (type == 'line')
      type = VEShapeType.Polyline;
   else if (type == 'pin')
      type = VEShapeType.Pushpin;
   else
      type = VEShapeType.Polygon;

   var shape = new VEShape(type, pts);
   shape.SetLineWidth(width || 3);
   shape.SetLineColor(line  || new VEColor(0, 150, 0, 1.0));
   shape.SetFillColor(fill  || new VEColor(0, 150, 0, 0.5));
   shape.HideIcon();
   (addTo || map).AddShape(shape);
   return shape;
};

function concentricCircles() {
  drawOnMap({
    log:'<b>Please click the search center.</b>',
    title:'Center',
    msg:'This will be the center of your circle.',
    callback: function(pt, detach) {
      var radius1 = prompt('Enter the radius (in miles) of the outer circle:', '50'),
      radius2 = prompt('Enter the radius (in miles) of the inner circle:', '30');
      detach();
      sendRequest({a:'concentric_circles', lat:pt.Latitude, lng:pt.Longitude, radius1:radius1, radius2:radius2});
    }
  });
};

function deleteItems(which) {
  sendRequest({a:'delete_all_' + which});
};

function deletePin(id) {
  try { return map.DeletePushpin(id); } catch(e) { alert(e.message);return false; }
};

function drawCircle(cb) {
  drawOnMap({
    log:'<b>Please click the circle center.</b>',
    title:'Center',
    msg:'This will be the center of your circle.',
    callback: function(pt, detach) {
      var radius = prompt('Enter the radius (in miles) of the circle:', '25');
      
      if (cb)
        cb(pt, radius);
      else
        sendRequest({a:'newCircle', lat:pt.Latitude, lng:pt.Longitude, radius:radius});
      
      detach('');
    }
  });
};

function drawOnMap(obj) {
   Log(obj.log || '');
   var lastPin = null;
   var cb = function(o) {
      var detach = function(l) {
         if (l) Log(l);
         map.DeleteShape(lastPin);
         map.DetachEvent('onclick', cb);
      };
      var point = map.PixelToLatLong(new VEPixel(o.mapX, o.mapY));
      if (lastPin)
         map.DeleteShape(lastPin);
      lastPin = addPin(point, null, obj.img || 'images/x.gif', obj.title, obj.msg);
      if (obj.callback)
         obj.callback(point, detach);
      return false;
   };

   map.AttachEvent('onclick', cb);
};

function drawPoly(cb) {
   window.polyPoints = [];
   var thisPoly = null, lastPoly = null;

   drawOnMap({
      log:'<b>Click point #<a>1</a> of the polygon.</b><div/>',
      callback: function(pt, detach) {
         try {
            polyPoints.push(pt);
            $J('#log a').html(polyPoints.length + 1);

            if (polyPoints.length > 1)
               thisPoly = addPoly('line', polyPoints, 3, new VEColor(225, 225, 0, 1.0), new VEColor(225, 225, 0, 0.5));

            if (polyPoints.length > 2 && lastPoly)
               map.DeleteShape(lastPoly);

            if (polyPoints.length == 3) {
               $J('<button/>').html('Close Polygon and Finish Drawing').click(function() {
                  Log('');
                  var t, pts = $J.map(polyPoints, function(p){ return p.Longitude + ' ' + p.Latitude });
                  if (thisPoly)
                     map.DeleteShape(thisPoly);
                  pts.push(pts[0]);
                  $J('#pts').val(t = pts.join(','));

                  if (cb) cb(t); else $J('#dialog').show();

                  detach('');
               }).appendTo($J('#log div'));
            }

         lastPoly = thisPoly;
         } catch(e){alert('EXCEPTION: '+e.toString()+'\n'+e.message+'\n'+e.line)}
      }
   });
};

function envelope() {
  var center = map.LatLongToPixel(map.GetCenter());
  var left = 0;//map.GetLeft();
  var top = map.GetTop();
  var right = (center.x - left) * 2 + left;
  var bottom = (center.y - top) * 2 + top;
  
  var tl = map.PixelToLatLong(left, top);
  var br = map.PixelToLatLong(right, bottom);
  
  return [tl.Longitude, tl.Latitude, br.Longitude, br.Latitude];
};
 
function getCenter() {
  var point = map.GetCenter();
  sendRequest({a:'new_point', 'type':'center', lat:point.Latitude, long:point.Longitude});
};

function getEnvelope() {
  var e = envelope();
  sendRequest({a:'new_rectangle', t:e[0], l:e[1], b:e[2], r:e[3]});
};

function getJSON(params, func) {
  Log('<b>Loading...</b>');
  $J.getJSON('request.php', params, func);
};

function getLocations(pins) {
  if (map.GetZoomLevel() < RETAIL_ZOOM_THRESHOLD) {
    if (!confirm('Sorry, but you need to zoom closer to show the locations.\n\nWould you like to zoom in automatically?')) return;
    map.SetZoomLevel(RETAIL_ZOOM_THRESHOLD);
  }
  
  var a = envelope();
  var params = {a:'getLocations', MinLong:a[0], MinLat:a[1], MaxLong:a[2], MaxLat:a[3]};
  getJSON(params, function(d){ replaceAllPins(d.pins) });
};

function getLocationsInCircle() {
  drawCircle(function(pt, radius) {
    sendRequest({a:'getLocationsInCircle', lat:pt.Latitude, lng:pt.Longitude, radius:radius});
  });
};

function getLocationsInPoly() {
  drawPoly(function(pts) {
    sendRequest({a:'getLocationsInPoly', points:pts});
  });
};

function velocityCategory(v) {
   v = Number(v);
   if (v < 74) return 'ts';
   if (v <= 95) return '1';
   if (v <= 110) return '2';
   if (v <= 130) return '3';
   if (v <= 155) return '4';
   return '5';
}

function loadMyMap() {
if(map != null)
   {
    map.Dispose();
   }
  map = new VEMap('myMap');

  
  map.SetDashboardSize(VEDashboardSize.Tiny);
  map.onLoadMap = function() {
     $J('#MSVE_navAction_mapStyleCell').empty();
     map.AttachEvent('onendzoom', function() {
        var z = map.GetZoomLevel();
        
        //if (z < 4 || z > 6) {
        //   $J('#showOil, #showGas, #showLayerBtns2').attr('disabled', 'disabled');
        //   map.ShowTileLayer('s3');
        //} else {
        //   $J('#showOil, #showGas').attr('disabled', $J('#showLayerBtns2').attr('disabled', '')[0].checked ? '' : 'disabled');
           //map.HideTileLayer('s3');
        //}
     
       // if (z > 12)
      //     map.ShowTileLayer('detail');
      //  else
      //     map.HideTileLayer('detail');
     });
  
      //var browserName=navigator.appName;
      //if (browserName=="Microsoft Internet Explorer")
      //	{
      //map.SetCenterAndZoom(new VELatLong(10,-90.28), 7);
      //	}else{
      //map.SetCenterAndZoom(new VELatLong(24,-90.28), 7);
      //}


  
  };
  
  

  
  
  
  map.LoadMap(new VELatLong('27.16','-93.9'),'6','h', false, VEMapMode.Mode2D, false);
  
    addPin(31, -95.3, 'images/pins/current_loc.gif', 'Hurricane Ike', '10AM CDT Sat -- 80 mph wind');
 





  	    var center = map.LatLongToPixel(map.GetCenter());
  
  	  var srn_x = parseInt(parseInt(screen.width)*0.6);
  	  var srn_y = parseInt(parseInt(screen.height)*0.6)

 
      
	var shape = new VEShape(VEShapeType.Polygon,[new VELatLong(30.98,-94.69),new VELatLong(30.9,-94.69),new VELatLong(30.77,-94.7),new VELatLong(30.66,-94.73),new VELatLong(30.6,-94.81),new VELatLong(30.56,-94.91),new VELatLong(30.51,-95),new VELatLong(30.49,-95.12),new VELatLong(30.5,-95.25),new VELatLong(30.52,-95.37),new VELatLong(30.56,-95.47),new VELatLong(30.63,-95.54),new VELatLong(30.7,-95.61),new VELatLong(30.78,-95.67),new VELatLong(30.86,-95.72),new VELatLong(30.97,-95.74),new VELatLong(31.1,-95.73),new VELatLong(31.2,-95.69),new VELatLong(31.3,-95.65),new VELatLong(31.41,-95.62),new VELatLong(31.52,-95.59),new VELatLong(31.61,-95.54),new VELatLong(31.7,-95.49),new VELatLong(31.78,-95.43),new VELatLong(31.81,-95.33),new VELatLong(31.83,-95.22),new VELatLong(31.83,-95.1),new VELatLong(31.79,-95),new VELatLong(31.72,-94.93),new VELatLong(31.65,-94.86),new VELatLong(31.56,-94.81),new VELatLong(31.46,-94.77),new VELatLong(31.35,-94.74),new VELatLong(31.24,-94.71),new VELatLong(31.12,-94.69),new VELatLong(30.98,-94.69)]);



            shape.SetLineWidth(2);
            shape.SetLineColor(new VEColor(255,235,0,1.0));
            shape.SetFillColor(new VEColor(165,134,179,0.5));
            shape.SetTitle("Hurricane Winds Swath");
            shape.SetDescription("Area of Gulf Of Mexico Subject to hurricane-strength winds");
            shape.HideIcon();


            
      var shapeCur = new VEShape(VEShapeType.Polygon,[new VELatLong(31.44,-95.22),new VELatLong(31.44,-95.14),new VELatLong(31.42,-95.05),new VELatLong(31.38,-94.96),new VELatLong(31.33,-94.87),new VELatLong(31.27,-94.8),new VELatLong(31.19,-94.73),new VELatLong(31.1,-94.67),new VELatLong(31,-94.62),new VELatLong(30.89,-94.62),new VELatLong(30.78,-94.64),new VELatLong(30.67,-94.69),new VELatLong(30.59,-94.78),new VELatLong(30.51,-94.87),new VELatLong(30.47,-94.98),new VELatLong(30.45,-95.09),new VELatLong(30.46,-95.2),new VELatLong(30.47,-95.3),new VELatLong(30.51,-95.39),new VELatLong(30.56,-95.46),new VELatLong(30.61,-95.54),new VELatLong(30.67,-95.6),new VELatLong(30.74,-95.63),new VELatLong(30.8,-95.68),new VELatLong(30.87,-95.7),new VELatLong(30.93,-95.72),new VELatLong(31,-95.72),new VELatLong(31.06,-95.7),new VELatLong(31.12,-95.66),new VELatLong(31.18,-95.63),new VELatLong(31.22,-95.59),new VELatLong(31.27,-95.54),new VELatLong(31.32,-95.49),new VELatLong(31.36,-95.44),new VELatLong(31.39,-95.37),new VELatLong(31.43,-95.3),new VELatLong(31.44,-95.22)]);

            
            
            shapeCur.SetLineWidth(2);
            shapeCur.SetLineColor(new VEColor(52,71,252,1.0));
            shapeCur.SetFillColor(new VEColor(252,151,52,0.5));
            shapeCur.SetTitle("Hurricane Winds Swath");
            shapeCur.SetDescription("Area of Gulf Of Mexico Subject to hurricane-strength winds");
            shapeCur.HideIcon();
        
        
        
        
        
        
        var shapePast = new VEShape(VEShapeType.Polygon,[new VELatLong(21.58,-52.05),new VELatLong(21.46,-51.97),new VELatLong(21.3,-51.85),new VELatLong(21.2,-51.92),new VELatLong(21.23,-52.21),new VELatLong(21.34,-52.56),new VELatLong(21.42,-52.95),new VELatLong(21.47,-53.38),new VELatLong(21.58,-53.79),new VELatLong(21.73,-54.16),new VELatLong(21.88,-54.53),new VELatLong(22.03,-54.9),new VELatLong(22.17,-55.28),new VELatLong(22.3,-55.67),new VELatLong(22.43,-56.06),new VELatLong(22.56,-56.45),new VELatLong(22.7,-56.83),new VELatLong(22.84,-57.21),new VELatLong(22.96,-57.61),new VELatLong(23.07,-58.02),new VELatLong(23.15,-58.46),new VELatLong(23.2,-58.92),new VELatLong(23.24,-59.39),new VELatLong(23.25,-59.88),new VELatLong(23.22,-60.37),new VELatLong(23.18,-60.85),new VELatLong(23.13,-61.32),new VELatLong(23.02,-61.73),new VELatLong(22.9,-62.13),new VELatLong(22.79,-62.54),new VELatLong(22.67,-62.94),new VELatLong(22.56,-63.35),new VELatLong(22.45,-63.76),new VELatLong(22.36,-64.19),new VELatLong(22.28,-64.63),new VELatLong(22.19,-65.06),new VELatLong(22.1,-65.49),new VELatLong(22.01,-65.92),new VELatLong(21.93,-66.36),new VELatLong(21.87,-66.82),new VELatLong(21.76,-67.23),new VELatLong(21.59,-67.58),new VELatLong(21.46,-67.97),new VELatLong(21.37,-68.4),new VELatLong(21.22,-68.77),new VELatLong(21.04,-69.1),new VELatLong(20.91,-69.49),new VELatLong(20.82,-69.93),new VELatLong(20.74,-70.36),new VELatLong(20.67,-70.8),new VELatLong(20.66,-71.25),new VELatLong(20.63,-71.7),new VELatLong(20.55,-72.14),new VELatLong(20.49,-72.59),new VELatLong(20.46,-73.08),new VELatLong(20.43,-73.55),new VELatLong(20.4,-74.01),new VELatLong(20.44,-74.44),new VELatLong(20.51,-74.89),new VELatLong(20.54,-75.38),new VELatLong(20.57,-75.87),new VELatLong(20.6,-76.36),new VELatLong(20.62,-76.84),new VELatLong(20.6,-77.3),new VELatLong(20.56,-77.77),new VELatLong(20.56,-78.25),new VELatLong(20.61,-78.72),new VELatLong(20.7,-79.15),new VELatLong(20.85,-79.52),new VELatLong(21.07,-79.82),new VELatLong(21.28,-80.13),new VELatLong(21.43,-80.5),new VELatLong(21.51,-80.93),new VELatLong(21.58,-81.38),new VELatLong(21.67,-81.81),new VELatLong(21.82,-82.19),new VELatLong(22.04,-82.49),new VELatLong(22.27,-82.78),new VELatLong(22.47,-83.1),new VELatLong(22.63,-83.46),new VELatLong(22.79,-83.81),new VELatLong(22.99,-84.14),new VELatLong(23.16,-84.49),new VELatLong(23.31,-84.86),new VELatLong(23.5,-85.18),new VELatLong(23.72,-85.48),new VELatLong(23.89,-85.84),new VELatLong(24.01,-86.24),new VELatLong(24.12,-86.65),new VELatLong(24.21,-87.08),new VELatLong(24.27,-87.54),new VELatLong(24.34,-87.98),new VELatLong(24.47,-88.38),new VELatLong(24.65,-88.72),new VELatLong(24.81,-89.08),new VELatLong(24.93,-89.47),new VELatLong(25,-89.92),new VELatLong(25.03,-90.42),new VELatLong(25.09,-90.87),new VELatLong(25.21,-91.27),new VELatLong(25.39,-91.61),new VELatLong(25.6,-91.92),new VELatLong(25.78,-92.26),new VELatLong(25.95,-92.62),new VELatLong(26.13,-92.95),new VELatLong(26.33,-93.28),new VELatLong(26.51,-93.62),new VELatLong(26.74,-93.91),new VELatLong(27.02,-94.14),new VELatLong(27.29,-94.4),new VELatLong(27.52,-94.69),new VELatLong(27.76,-94.97),new VELatLong(28.02,-95.22),new VELatLong(28.31,-95.45),new VELatLong(28.65,-95.63),new VELatLong(29,-95.8),new VELatLong(29.36,-95.96),new VELatLong(29.75,-96.08),new VELatLong(30.13,-96.2),new VELatLong(30.49,-96.28),new VELatLong(30.85,-96.22),new VELatLong(31.18,-96.02),new VELatLong(31.47,-95.8),new VELatLong(31.73,-95.55),new VELatLong(31.91,-95.22),new VELatLong(31.97,-94.81),new VELatLong(31.92,-94.39),new VELatLong(31.75,-94.04),new VELatLong(31.48,-93.78),new VELatLong(31.16,-93.58),new VELatLong(30.81,-93.41),new VELatLong(30.45,-93.26),new VELatLong(30.11,-93.08),new VELatLong(29.79,-92.87),new VELatLong(29.48,-92.66),new VELatLong(29.23,-92.4),new VELatLong(28.98,-92.13),new VELatLong(28.71,-91.87),new VELatLong(28.51,-91.56),new VELatLong(28.32,-91.22),new VELatLong(28.15,-90.87),new VELatLong(28,-90.5),new VELatLong(27.84,-90.14),new VELatLong(27.73,-89.74),new VELatLong(27.62,-89.32),new VELatLong(27.49,-88.93),new VELatLong(27.36,-88.54),new VELatLong(27.23,-88.16),new VELatLong(27.07,-87.79),new VELatLong(26.9,-87.45),new VELatLong(26.73,-87.1),new VELatLong(26.58,-86.73),new VELatLong(26.44,-86.34),new VELatLong(26.29,-85.98),new VELatLong(26.08,-85.67),new VELatLong(25.81,-85.42),new VELatLong(25.49,-85.22),new VELatLong(25.17,-85.02),new VELatLong(24.89,-84.79),new VELatLong(24.59,-84.6),new VELatLong(24.25,-84.51),new VELatLong(23.96,-84.49),new VELatLong(23.79,-84.3),new VELatLong(23.76,-83.93),new VELatLong(23.74,-83.56),new VELatLong(23.57,-83.26),new VELatLong(23.28,-83.03),new VELatLong(23.04,-82.75),new VELatLong(22.88,-82.39),new VELatLong(22.7,-82.04),new VELatLong(22.52,-81.71),new VELatLong(22.39,-81.32),new VELatLong(22.31,-80.88),new VELatLong(22.23,-80.44),new VELatLong(22.14,-80.02),new VELatLong(22.02,-79.64),new VELatLong(21.97,-79.26),new VELatLong(21.99,-78.82),new VELatLong(21.97,-78.37),new VELatLong(21.98,-77.91),new VELatLong(22.02,-77.44),new VELatLong(22.04,-76.95),new VELatLong(22.01,-76.46),new VELatLong(21.98,-75.97),new VELatLong(21.95,-75.48),new VELatLong(21.92,-74.99),new VELatLong(21.91,-74.49),new VELatLong(21.86,-74.04),new VELatLong(21.83,-73.61),new VELatLong(21.82,-73.16),new VELatLong(21.77,-72.74),new VELatLong(21.79,-72.33),new VELatLong(21.85,-71.87),new VELatLong(21.89,-71.42),new VELatLong(21.9,-70.98),new VELatLong(21.91,-70.56),new VELatLong(21.98,-70.15),new VELatLong(22.09,-69.73),new VELatLong(22.23,-69.36),new VELatLong(22.4,-69.01),new VELatLong(22.54,-68.63),new VELatLong(22.63,-68.2),new VELatLong(22.74,-67.79),new VELatLong(22.88,-67.41),new VELatLong(23.01,-67.02),new VELatLong(23.09,-66.58),new VELatLong(23.16,-66.13),new VELatLong(23.23,-65.68),new VELatLong(23.33,-65.26),new VELatLong(23.44,-64.85),new VELatLong(23.53,-64.42),new VELatLong(23.59,-63.96),new VELatLong(23.65,-63.5),new VELatLong(23.71,-63.04),new VELatLong(23.79,-62.6),new VELatLong(23.89,-62.18),new VELatLong(24,-61.77),new VELatLong(24.1,-61.36),new VELatLong(24.16,-60.91),new VELatLong(24.16,-60.44),new VELatLong(24.13,-59.98),new VELatLong(24.11,-59.56),new VELatLong(24.12,-59.14),new VELatLong(24.17,-58.68),new VELatLong(24.19,-58.24),new VELatLong(24.13,-57.82),new VELatLong(24,-57.43),new VELatLong(23.87,-57.04),new VELatLong(23.71,-56.68),new VELatLong(23.53,-56.34),new VELatLong(23.34,-56.01),new VELatLong(23.17,-55.66),new VELatLong(23.02,-55.29),new VELatLong(22.9,-54.89),new VELatLong(22.76,-54.51),new VELatLong(22.61,-54.14),new VELatLong(22.47,-53.76),new VELatLong(22.32,-53.39),new VELatLong(22.17,-53.02),new VELatLong(22.04,-52.63),new VELatLong(21.85,-52.3),new VELatLong(21.58,-52.05)]);
	
       

 
	            
	            shapePast.SetLineWidth(2);
	            shapePast.SetLineColor(new VEColor(252,151,52,1.0));
	            shapePast.SetFillColor(new VEColor(52,179,252,0.5));
	            shapePast.SetTitle("Hurricane Winds Swath");
	            shapePast.SetDescription("Area of Gulf Of Mexico Subject to hurricane-strength winds");
	            shapePast.HideIcon();
         
         
       
         //Add the shape the the map
         map.AddShape(shapePast);
         map.AddShape(shape);
         map.AddShape(shapeCur);


   


  
  //Nexrad tiles
  //nexradTiles = new VETileSourceSpecification("nexrad", 'wms_proxy.php/nexrad/%1%4.png');
  //nexradTiles.ZIndex = 2;
  //map.AddTileLayer(nexradTiles, true);
  
  //GOES tiles
  //goesTiles = new VETileSourceSpecification("goes", 'wms_proxy.php/goes/%1%4.png');
  //goesTiles.ZIndex = 3;
  //map.AddTileLayer(goesTiles, true);
  //map.HideTileLayer('goes');
  
  // No more map detail tile
  detailTiles = new VETileSourceSpecification("detail", 'images/nodetail.png');//wms_proxy.php/detail/%1%4.png');
  detailTiles.ZIndex = 8;
  detailTiles.MinZoom = 13;
  detailTiles.MaxZoom = 15;
  map.AddTileLayer(detailTiles, true);
  map.HideTileLayer('detail');
  
  // Oil tiles
  oilTiles = new VETileSourceSpecification("oil", 'http://s3.amazonaws.com/gulfimpact_oilsum/%4.png');
  oilTiles.ZIndex = 4;
  oilTiles.MinZoom = 4;
  oilTiles.MaxZoom = 6;
  map.AddTileLayer(oilTiles, true);
  map.HideTileLayer('oil');
  
  // Gas tiles
  gasTiles = new VETileSourceSpecification("gas", 'http://s3.amazonaws.com/gulfimpact_gassum/%4.png');
  gasTiles.ZIndex = 5;
  oilTiles.MinZoom = 4;
  oilTiles.MaxZoom = 6;
  map.AddTileLayer(gasTiles, true);
  map.HideTileLayer('gas');
  
  // Main tiles
  s3Tiles = new VETileSourceSpecification("s3", 'http://s3.amazonaws.com/arc2earth1874005980/Gulf08/tiles/%4.png');
  s3Tiles.ZIndex = 1;
  map.AddTileLayer(s3Tiles, true);
  //map.HideTileLayer('s3');
  
 // $J('#showNEXRAD, #showGOES').click(function() {
 //    map.ShowTileLayer(this.id == 'showNEXRAD' ? 'nexrad' : 'goes');
 //    map.HideTileLayer(this.id == 'showNEXRAD' ? 'goes' : 'nexrad');
 // });
  
  $J('#showOil, #showGas').click(function() {
     map.ShowTileLayer(this.id == 'showOil' ? 'oil' : 'gas');
     map.HideTileLayer(this.id == 'showOil' ? 'gas' : 'oil');
  });
  
  //$J('#showLayerBtns1').click(function() {
  //   $J('#layerBtns1 div input').attr('disabled', !this.checked)
  //   if (!this.checked) {
  //      map.HideTileLayer('goes');
  //      map.HideTileLayer('nexrad');
  //   } else
  //      map.ShowTileLayer($J('#showNEXRAD').attr('checked') ? 'nexrad' : 'goes');
 // });
  
  //$J('#showLayerBtns2').click(function() {
 //    $J('#layerBtns2 div input').attr('disabled', !this.checked)
  //   if (!this.checked) {
  //      map.HideTileLayer('oil');
  //      map.HideTileLayer('gas');
  //   } else
  //      map.ShowTileLayer($J('#showOil').attr('checked') ? 'oil' : 'gas');
  //});
  
  // Add shape layers
  var forePosLayer = new VEShapeLayer();
  map.AddShapeLayer(forePosLayer);
  var pastPosLayer = new VEShapeLayer();
  map.AddShapeLayer(pastPosLayer);
  window.customLayer = new VEShapeLayer();
  map.AddShapeLayer(customLayer);
  
  try {
     $J.each(forePosPts, function(i,p) {
        var c = p.coords.split(',');
   
        var shape = new VEShape(VEShapeType.Pushpin, [new VELatLong(c[0],c[1])]);
        shape.SetCustomIcon('images/pins/cat'+velocityCategory(p.windMph)+'_fut.png');
        shape.SetTitle('Forecast Position: ' + p.type);
        shape.SetDescription(p.date + ', ' + p.time + '<br>Wind Speed: '+p.windMph+ ' mph<br>Movement: ' + p.moveMph+' mph');
        forePosLayer.AddShape(shape);
     });
     $J('#forePos').click(function(){ forePosLayer[this.checked ? 'Show' : 'Hide']() });
  
     $J.each(pastPosPts, function(i,p) {
        var c = p.coords.split(',');
        if (c[0] != stormLoc[0] || c[1] != stormLoc[1]) {
           var shape = new VEShape(VEShapeType.Pushpin, [new VELatLong(c[0],c[1])]);
           shape.SetCustomIcon('images/pins/cat'+velocityCategory(p.windMph)+'.png');
           shape.SetTitle('Past Position: ' + p.type);
           shape.SetDescription(p.date + ', ' + p.time + '<br>Wind Speed: '+p.windMph+ ' mph<br>Movement: ' + p.moveMph+' mph');
           pastPosLayer.AddShape(shape);
        }
     });
  } catch(e){alert('EXCEPTION: '+e.toString()+'\n'+e.message+'\n'+e.line)}
  $J('#pastPos').click(function(){ pastPosLayer[this.checked ? 'Show' : 'Hide']() });
  
 

  tabberAutomatic(tabberOptions);
  $J('li a').click(function() {
    VEPushpin.Hide();
    $J('#key tr').css('background-color', '');
    return true;
  });
};



function replaceAllPins(which,pins) {
  Log('');//'<small>' + pins.length + ' locations found.</small>');
  
  var a = [];
  var tr = $J('#'+which+' table thead tr').empty();
  $J('#'+which+' table tbody').empty();
  
  map.DeleteAllPushpins();
  //map.DeleteAllPolygons();
  trClass = true;
  
  $J.each(eval(which+"ALLOWED"), function(k,v) {
    $J('<th class="sortable"/>').html(v).appendTo(tr);
    a.push(v);
  });
  
  //$J('#excel').val(a.join('\t'));
  $J.each(pins, function(k,v){ addDBPin(which,v) });
  fdTableSort.init(which+"_table");
};

function sendRequest(params) {
  Log('<b>Sending...</b>');
  $J.get('request.php', params, function(d){ Log('');/*$J('#mapLegend').html(d);*/eval(d); });
};

function showPin(p, e) {
  lastPin = p;
  $J('a[@title=Map]').click();
  $J(p).mouseover();
  $J('#key tr').css('background-color', '');
  $J(e).css('background-color', '#ccf');
};

$J(function() {
  $J('#dialog form').submit(function() {
    var params = Form.serialize(this);
    //Log(params)
    sendRequest(params);
    $J('#dialog').hide();
    return false;
  });
});

var idStack = {};
function poly(id, pts, show) {
   if (!(id in idStack))
      idStack[id] = addPoly('polygon', pts);
   //alert('id:'+id+', show:'+show+', idStack[id]:'+idStack[id])
   idStack[id][show ? 'Show' : 'Hide']();
}