/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Firefox download visualization project.
 *
 * The Initial Developer of the Original Code is
 * Alistair MacDonald - hyper-metrix.com
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Alistair MacDonald (Twitter: @F1LT3R) - hyper-metrix.com
 * Anders Holbøll - optimization of SVG; code cleanup
 * Daniel Einspanjer - Mozilla Corporation
 * Dave Dash - Mozilla Corporation
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */


(function(){

  var prevScrollX=-1,prevScrollY=-1,prevZoom=-1,prevFrame=-2;
  var mouseX=0,mouseY=0,scrollX=0,scrollY=0;
  var grabX=0, grabY=0,mouseDown=false;
  var width=640,height=320;
  var zoom=1;

  var wheel={ // Wheel function based on code by Adomas Paltanavicius. Thank you sir!
    event:function(event){ // http://adomas.org/javascript-mouse-wheel/index.html
    	var delta=0;
    	if(!event)event=window.event;
    	if(event.wheelDelta){delta=event.wheelDelta/120;if(window.opera)delta=-delta;}
      else if(event.detail){delta=-event.detail/3;}
    	if(delta){wheel.handle(delta);
        event.preventDefault?event.preventDefault():0;
        event.returnValue=false;
      }
    },
    handle:function(delta){
      if(delta<1){zoom=zoom>1?zoom-=.1:1;}
      else{zoom=zoom<10?zoom+=.1:10;}
    }
  }
  //window.addEventListener('DOMMouseScroll',wheel.event,false);


   var initMap = function(){
      var _ = this;
      worldMap = [[1,2521,1,-39,74,1,110,6,246,3,-56,-4,-110,-11,73,-14,47,-8,-4,-3,-2,-4,-36,-9,35,-2,52,-9,6,-8,-45,-12,-30,-8,-15,-8,-35,-7,51,-3,82,0,18,-1,22,-2,-4,-8,-2,-3,3,-3,60,-9,36,-5,88,-5,259,-9,19,3,11,6,15,-4,14,-2,-8,9,81,2,81,-6,-13,-5,-11,-4,-12,-5,15,0,23,-3,-26,-4,-26,-4,13,-3,86,5,73,-4,4,-6,2,6,9,4,21,-5,7,2,30,6,57,6,21,-2,24,-6,53,6,-5,-6,8,-3,12,5,7,4,94,-16,4,-9,-9,-11,-10,-17,7,-8,12,-12,-4,-12,-4,-4,6,-3,9,-2,18,-12,16,-10,63,-23,18,-5,20,0,-1,1,-8,6,-2,4,-7,-5,-6,1,-15,7,-36,20,-16,5,-7,4,-6,4,-16,20,14,6,14,5,10,4,11,2,-2,1,-4,6,5,8,9,4,-4,12,-9,8,12,0,6,3,6,7,12,6,-13,-1,-13,2,3,3,4,6,-8,6,-5,4,-7,3,-18,4,-65,15,-132,0,-18,-2,22,10,44,10,16,4,-60,2,-26,4,28,5,-15,3,-44,-2,-30,-2,67,12,33,6,66,18,69,4,17,8,12,6,19,4,8,4,8,4,77,-4,153,-9,51,-5,55,-12,52,-4,48,-4,31,-6,-56,-12,-39,-9,203,-41,54,-10,25,-8,22,-4,-7,-4,-13,-4,44,-12,35,-14,23,-6,22,-2,15,-6,9,4,12,4,100,-4,86,-7,111,-9,9,3,20,4,18,1,34,0,11,0,17,2,17,2,39,0,54,-8,22,-11,27,-3,31,7,23,4,13,-6,116,-29,15,0,5,-6,9,-1,13,-4,46,-11,36,10,173,16,16,15,-9,19,4,6,-4,6,-2,4,-5,8,4,4,48,-21,33,-12,20,-3,12,-4,19,-8,111,-24,116,-6,94,-6,82,2,63,1,47,-5,17,6,-3,6,40,3,68,-5,80,2,22,-4,160,2,30,2,13,2,4,4,-5,4,17,1,20,5,32,6,44,2,21,-2,17,1,39,9,49,14,20,7,5,-3,35,2,47,8,27,6,2,4,-7,10,-27,7,-15,3,-14,6,-14,6,-19,6,-16,18,2,11,8,7,7,6,6,4,4,5,7,1,11,1,-34,7,-39,10,-13,9,5,2,14,9,8,9,92,20,5,3,15,4,49,4,67,6,35,3,0,39,0,40,-2560,0,-2560,0,1,-39],
      [4877,2453,-8,1,5,2,3,-3],
      [1790,2428,10,-4,44,-14,8,-12,42,-8,39,5,-7,6,4,3,7,18,-126,10,-21,-4],
      [241,2413,-7,-5,56,2,20,5,-30,0,-39,-2],
      [1542,2412,28,-10,24,-5,-19,12,-33,3],
      [4930,2384,4,-4,6,4,-4,4,-6,-4],
      [4952,2384,2,-4,4,4,-2,4,-4,-4],
      [454,2368,3,-4,15,4,-18,0],
      [784,2336,-24,-7,-10,-4,11,-1,13,4,12,4,12,4,1,4,-15,-4],
      [816,2336,16,-4,14,4,-16,4,-14,-4],
      [857,2334,8,-1,-3,3,-5,-2],
      [1440,2316,10,-4,8,4,-10,4,-8,-4],
      [1524,2312,18,-2,4,-5,-32,0,-23,0,3,-5,16,-2,30,-6,14,-4,0,-4,-3,-4,-12,-18,36,13,-2,37,-49,0],
      [1124,2308,27,-2,48,3,-75,-1],
      [1484,2276,12,0,-9,4,-3,-4],
      [1580,2238,11,-6,1,6,-11,6,-1,-6],
      [1650,2200,6,-4,4,4,-6,4,-4,-4],
      [1567,2065,-22,-6,-3,-4,-2,-3,-2,-4,-1,-4,-6,5,-11,-1,-3,-8,-3,-7,-8,-9,7,-2,-1,-6,-10,-6,-2,-12,-2,-8,-12,-20,13,0,3,-8,-15,-15,5,-7,15,-10,19,-43,-12,0,-8,2,4,-24,6,-19,0,-18,-1,-16,25,-53,2,-31,2,-18,2,-10,4,-24,11,-105,-11,-37,-32,-20,-42,-37,-9,-21,-25,-45,-24,-37,-9,-9,-3,-13,8,-19,5,-13,-2,-31,4,-14,9,-7,10,-17,8,-6,10,-8,-4,-57,-6,-13,-8,-11,-16,2,-6,11,-14,3,-17,-8,-19,-12,-16,-11,-12,-10,-34,-39,-66,-26,-42,-10,-29,-4,-23,-8,-24,-10,-23,-10,-21,-13,-16,-21,-3,-23,-48,-48,-6,-8,-42,-40,-2,-2,-5,-18,-17,-19,-11,8,14,25,14,17,8,12,20,37,6,5,10,18,-7,-3,-15,-14,-12,-14,-1,-10,-19,-13,-12,-15,-5,-17,-27,-37,-47,-39,-27,-32,-4,-4,-4,-5,-10,-15,-10,-17,-4,-7,2,-81,-4,-23,-6,-12,12,0,16,7,4,-3,-20,-23,-13,-8,-7,-3,13,15,-46,-14,-13,-8,13,3,11,-2,-8,-4,-6,-3,-10,-14,-8,-18,-3,-2,-15,-7,-3,-10,-26,-23,-6,-2,-2,8,9,5,3,6,-9,1,-12,-14,-6,-5,-4,-2,8,-4,3,-8,-8,-8,-1,4,-2,6,-6,-5,-8,-16,-4,7,-6,-4,-4,-4,-2,4,-27,-5,-19,-11,-34,-4,-48,-7,-43,0,-6,7,-22,6,-19,2,1,-4,0,-8,14,-8,9,-4,-40,14,-16,17,-94,47,-20,5,-10,4,1,-5,15,-7,56,-31,5,-9,-13,-1,-26,-2,-17,0,-9,0,-17,-16,-29,-11,-4,-5,0,-17,35,-13,25,-13,-36,-6,-40,-4,-11,-7,17,-12,30,0,23,2,20,-2,7,-5,-10,-1,-10,-1,-11,-4,-27,-9,-21,-8,-5,-4,19,-6,29,-10,44,-14,45,-8,11,1,21,2,25,3,122,10,92,14,28,0,6,-4,16,0,30,-6,35,-3,16,-1,25,3,21,1,10,0,17,2,31,-1,73,12,24,5,-9,4,1,5,39,1,58,12,6,1,-2,-8,8,-10,13,-5,-18,2,4,-6,16,1,3,2,7,1,66,10,36,-7,1,-7,-3,-12,33,6,-6,9,-4,5,7,6,6,6,5,-5,3,-5,22,-10,-6,-1,-4,-4,-12,-9,-17,-13,3,-9,16,-16,-4,-4,2,-18,66,1,-33,16,-15,5,-2,8,16,10,15,10,-3,6,4,6,18,11,5,11,7,-10,18,-2,6,10,0,8,15,4,4,-7,2,-4,18,-16,30,-5,18,16,-9,0,10,8,8,9,-34,9,-4,2,-19,2,-15,2,-3,6,-9,8,-32,16,-8,2,-11,4,-6,3,-4,9,-34,59,16,13,19,9,32,5,76,22,23,20,38,35,8,-31,-7,-14,20,-10,3,-45,-10,-3,9,-10,4,-17,-1,-10,-2,-8,32,-9,55,14,11,4,15,3,0,30,9,1,15,2,9,4,29,-15,6,-9,31,20,19,28,5,8,11,8,31,10,7,6,2,4,12,5,14,16,-21,16,-30,13,-58,6,-52,5,-15,11,-26,19,-14,14,21,-14,73,-15,-20,9,-2,2,8,5,7,9,53,10,12,-2,-6,9,-6,4,-24,10,-27,11,-19,0,24,-15,3,-3,-5,-5,-13,2,-50,20,-26,27,-3,9,-32,10,-6,4,-7,3,-10,17,-5,7,-13,1,-4,7,4,17,2,8,2,8,-17,17,-16,10,-25,16,-13,56,6,51,-30,-38,-28,-29,-16,-1,-37,-5,-22,8,-5,6,-14,0,-57,3,-10,6,-25,25,4,10,-3,16,0,52,61,44,18,-1,20,-21,29,-20,23,4,-5,10,-5,14,-5,14,-4,12,-6,20,36,5,29,4,6,4,4,15,7,62,39,9,41,10,7,5,7,-11,12,-19,20,-13,26,-11,13,-3,-5,7,11,8,10,-7,2,-6,4,4,8,6,10,6,16,6,24,4,15,1,27,-5,14,10,14,12,6,6,25,18,25,18,61,14,21,24,10,20,-3,19,-11,11,12,0,6,-4,-1,-4,7,4,10,4,0,16,-5,8,11,-8,41,0,16,16,6,4,27,0,22,4,33,14,28,16,21,35,-31,50,-11,17,-8,8,-9,45,-5,28,-4,12,-10,22,-10,18,-33,19,-23,6,-15,8,-24,14,-13,32,-6,21,-14,19,-13,19,1,-4,-3,-9,-12,20,-34,41,-52,-5,2,8,8,10,5,10,-4,17,-61,21,-11,14,-8,16,-19,-2,-11,10,-1,25,-3,9,-15,15,-5,26,0,21,-14,13,-12,12,-6,21,-3,9,1,5,11,9,24,15,9,10,-32,3,-23,-2],
      [1568,2052,0,-8,-1,-4,-3,-6,1,-6,-14,15,4,6,3,2,7,5,3,-4],
      [1546,2036,-10,0,1,4,9,-4],
      [1533,2029,-8,1,5,2,3,-3],
      [1528,2020,-4,-2,-4,4,4,2,4,-4],
      [1584,1514,-17,-12,4,8,13,4],
      [1548,1144,-10,-8,6,16,4,-8],
      [1413,686,7,-12,-29,3,-6,4,-2,3,-7,4,37,-2],
      [1332,682,2,-14,8,-24,7,-6,21,0,3,14,2,6,10,4,16,-6,13,-10,2,-12,-21,-4,-5,-2,-4,-4,-72,16,-3,6,3,14,-1,22,19,0],
      [1474,663,-9,-9,-22,4,-15,6,46,-1],
      [1295,612,11,0,39,7,10,-10,-9,-9,-10,-6,-55,6,-26,15,40,-3],
      [1723,515,-5,-2,-10,3,-4,4,19,-5],
      [834,354,12,-6,-4,6,11,0,21,-7,9,-6,-13,-3,-14,-2,5,-4,2,-2,-67,10,19,1,19,-1,-14,7,-6,10,20,-3],
      [1298,348,-14,-4,0,4,14,4,0,-4],
      [789,341,-8,1,5,2,3,-3],
      [1177,317,-8,1,5,2,3,-3],
      [2038,2056,-1,-4,9,4,-8,0],
      [1700,2018,21,-5,-9,9,-12,-4],
      [3540,1982,6,-2,6,6,-6,2,-6,-6],
      [4944,1941,-13,-5,36,-32,41,-39,6,-5,6,4,8,4,-2,20,-8,12,-8,6,-18,16,-48,19],
      [1504,1922,4,-4,4,6,-4,4,-4,-6],
      [4633,1895,-14,-32,11,1,24,1,15,-2,-2,13,-34,19],
      [1646,1889,2,-7,5,6,-7,1],
      [5050,1862,-8,-15,-5,-9,-6,-45,-1,-13,12,14,14,13,6,4,24,9,10,4,-10,12,-10,9,-22,27,-4,-10],
      [4592,1830,-16,-4,-29,-23,-10,-13,-8,-8,-9,-7,-9,4,11,-31,-17,14,-19,7,-80,-36,-83,25,-29,6,-40,8,-46,1,-9,-13,-13,-78,-5,-25,-2,-17,-3,-20,3,-15,3,-7,20,-11,47,-17,41,-21,8,-17,5,-9,8,0,9,-3,6,-6,6,-5,6,-11,4,-5,13,-8,22,2,27,4,30,-35,12,-5,28,1,27,3,3,2,-6,10,-5,32,26,15,23,14,12,4,20,-48,13,-46,15,39,8,9,18,24,6,15,4,11,21,25,22,23,13,16,9,6,18,22,15,28,4,20,-16,68,-30,58,-18,17,-22,7,-28,-1,-14,0,-20,5],
      [4505,1790,8,-1,-3,3,-5,-2],
      [2827,1765,-9,-18,-29,-64,-19,-55,-10,-36,-32,-68,21,-63,5,-22,-4,-27,-9,-33,-9,-23,-22,-28,-20,-35,2,-69,-27,-6,-29,-11,-14,-16,-65,12,-48,6,-42,4,-51,-16,-44,-40,-16,-23,-14,-19,-6,-3,-13,-23,-3,-10,3,-19,1,-74,4,-27,15,-23,5,-12,10,-14,14,-18,15,-10,35,-37,30,-41,20,-20,14,-9,24,6,41,-7,80,-17,37,-1,38,0,7,1,3,2,-5,7,0,4,-1,20,-6,6,47,20,31,14,18,10,20,6,26,-12,6,-14,36,-1,45,14,57,5,22,-2,37,-3,22,-54,2,-14,-30,0,-29,-3,-20,0,-20,0,-15,-6,-10,-8,-9,-8,-1,-3,-2,-16,16,-13,18,-9,-17,1,-31,1,-23,3,-10,2,-6,8,3,9,4,7,9,1,1,7,-10,2,-12,-3,1,3,2,20,-17,-17,-14,-21,-14,-26,-17,-20,-24,-12,-24,-19,-10,-5,-6,-5,-11,-2,-10,11,6,11,19,18,22,14,6,1,18,12,4,8,-13,2,3,10,-5,15,-11,6,4,-7,-12,-25,-31,-17,-34,-25,-41,-9,-40,8,-25,11,-16,16,-32,32,-16,23,-7,5,-21,6,-26,6,-17,-5,-16,-7,-18,-9,-5,-11,0,-15,0,-44,33,-12,70,2,9,-16,-7,-29,-25,-17,-14,-10,15,-3,25,-7,10,-4,15,-2,11,-4,10,-8,30,-11,10,-13,8,-9,6,5,1,-6,5,-6,36,-6,7,-13,-6,-12,1,-20,16,-7,12,7,-7,15,-2,13,10,7,7,4,14,-1,25,3,12,3,51,-10,25,-3,10,-4,7,-14,7,-19,19,-1,15,4,0,-18,0,-14,29,-2,39,-5,16,-4,-14,-4,-49,1,-47,0,-10,-11,-2,-15,17,-20,30,-16,0,-15,-47,13,-37,24,-23,29,12,9,6,14,-12,8,-14,22,-22,18,-10,4,-21,-1,-7,6,-12,11,0,-4,1,-4,8,-11,13,-5,-9,-9,-9,-14,-4,-5,-4,-4,-6,-11,-6,-1,-51,18,-13,-10,4,-4,-6,-3,-6,-1,4,-4,0,-6,-6,-16,15,-15,15,-8,6,-2,10,-3,9,-4,10,-3,21,-12,22,-19,23,-18,17,-10,6,-2,6,-2,-6,-4,-8,-3,14,-2,13,-3,8,-6,13,-6,9,-1,15,2,24,-3,18,-5,18,-8,9,4,9,0,10,-1,9,-1,10,-1,13,3,19,1,-3,6,4,4,22,1,10,2,28,6,95,29,-95,8,-28,-4,25,12,5,6,22,22,21,4,-5,-10,-8,-4,9,-3,27,5,11,-5,15,-15,32,-3,12,-2,-2,-9,-1,-11,15,-9,7,10,-3,8,33,0,7,-6,25,-6,51,-7,15,3,25,-2,22,-4,16,0,16,1,13,-2,-9,-8,-16,-7,-8,-4,15,0,33,6,81,19,13,-1,-4,-11,-15,-5,-5,-12,9,-14,15,-12,12,-10,6,-4,9,-4,4,4,11,6,10,14,-1,9,4,19,5,16,3,11,-39,18,-6,3,41,-2,25,-23,10,-8,26,11,3,7,5,-8,-39,-15,-15,-16,-4,-11,6,-11,14,-8,4,-2,1,10,12,9,13,-2,-8,-3,-4,-4,13,-2,6,-1,-5,-5,48,7,25,1,-15,-9,-9,-8,14,-7,46,-3,27,-7,17,-12,114,-14,71,-10,62,-6,10,4,10,3,36,2,43,1,11,4,5,7,-16,11,-69,23,36,-7,21,-1,24,5,16,0,39,1,55,6,37,-5,17,-4,34,4,26,4,1,14,10,13,32,-1,17,-5,92,-1,-3,-6,7,-4,14,-3,47,0,76,11,13,6,21,6,47,1,65,11,3,6,111,0,12,1,11,11,17,-8,-1,-7,107,7,25,35,-1,29,-17,1,-10,9,19,24,-15,1,-97,25,-25,5,-22,-1,-27,4,-14,2,-13,0,-21,13,-4,19,5,8,2,8,-6,9,-11,11,-11,10,-17,14,-8,6,-12,10,-24,19,-16,-68,35,-31,19,-10,20,-12,16,-10,12,-5,17,-13,8,-10,1,-4,-22,5,-20,14,-20,-6,-22,-3,-45,17,-15,17,-6,4,-28,2,-9,-2,-13,-6,-23,0,-49,4,-44,0,-32,21,-52,32,-9,12,11,8,6,4,6,1,8,4,8,-4,26,2,5,33,-7,30,-90,89,-20,0,-42,25,-18,15,-8,17,21,42,-15,15,-19,5,-5,0,1,-33,-11,-12,-11,-10,2,-12,-39,0,-7,-11,-1,-10,-14,2,-15,9,-9,8,-11,5,-2,14,9,7,18,2,15,-2,8,11,-25,15,-14,12,8,7,16,20,10,13,3,10,-11,13,-10,2,10,3,-6,38,-14,19,-62,49,-18,4,-7,3,-25,9,-18,13,-3,4,-5,-8,-30,-5,-28,32,22,32,27,66,-16,18,-29,21,-14,14,-1,-13,-8,-15,-22,-18,-21,-15,-6,-6,-5,-6,-19,61,4,1,12,25,10,9,33,41,7,24,-18,5,-31,-46,-18,-33,-13,-20,5,-26,-17,-80,-6,-13,-11,10,-22,8,-4,-17,-5,-22,-6,-12,-10,-10,-13,-16,-6,-16,-13,-1,-28,11,-27,14,-13,15,-23,16,-45,36,-13,26,-23,72,-23,11,-12,-24,-11,-25,-8,-16,-10,-26,-19,-79,-7,-22,-10,13,-22,-3,-3,-17,-2,-5,-15,-7,-10,-6,-6,-5,-20,-20,-92,1,-36,-15,-23,-5,-35,-6,-32,-26,-26,-16,-8,6,-2,14,16,20,10,16,6,14,6,-1,5,-9,2,12,25,16,28,-14,14,-14,4,13,17,17,29,23,-21,25,-6,10,-7,12,-10,8,-10,8,-8,5,-19,9,-24,11,-25,15,-24,10,-70,21,-16,-47,-10,-22,-10,-15,-16,-19,-16,-28,-12,-25,-12,-15,-18,-27,-18,-32,-5,4,-20,-3,-10,-12,7,17,36,71,10,15,14,31,14,29,15,25,15,21,41,44,7,13,16,10,41,-10,45,-10,4,14,-51,98,-35,30,-37,33,-22,25,-19,29,-7,30,7,29,8,24,6,38,-27,60,-35,25,-23,21,7,12,6,29,-19,26,-20,26,-12,30,-18,21,-101,52,-22,2,-21,5,-24,-8],
      [3057,1465,-9,-43,2,54,7,-11],
      [2998,1391,-10,-17,-4,-13,-8,-29,10,53,7,15,3,4,2,-13],
      [3038,1313,3,-7,3,-11,7,-7,-4,-6,-32,4,-3,18,13,14,13,-5],
      [3328,746,-13,-34,-5,-20,-7,-20,-16,-12,-5,-12,7,-6,13,-6,10,-6,0,-9,-35,-4,-13,5,-34,19,-1,12,7,12,18,25,14,18,-12,30,7,10,15,6,29,6,21,-14],
      [3147,694,5,-14,-50,-31,-14,-12,19,-16,4,-11,-51,24,4,8,-27,4,-4,-8,-2,-6,5,-4,-18,-6,-28,5,-7,10,-7,7,-8,12,-4,8,-8,7,4,16,57,3,50,-1,19,6,10,3,51,-4],
      [3069,638,8,-1,-3,3,-5,-2],
      [3668,680,-12,-5,-6,5,18,0],
      [4072,542,17,-8,32,-33,-8,-7,-10,14,-58,36,-3,4,30,-6],
      [3027,421,-29,-17,-3,16,32,1],
      [2718,376,-4,-2,-2,6,6,-4],
      [3746,284,3,-4,0,-4,-2,-10,-9,0,-1,8,2,3,8,11,-1,-4],
      [3188,1636,-6,-16,-2,-10,4,-31,5,-30,18,-39,35,-30,6,-6,8,-10,11,1,6,42,-7,12,-37,118,-18,5,-23,-6],
      [4915,1590,-17,-18,36,25,-19,-7],
      [5085,1535,7,-5,7,5,-7,5,-7,-5],
      [5100,1522,8,-6,8,-1,-8,6,-8,1],
      [4930,1497,2,-2,3,7,-5,-5],
      [4416,1448,3,-4,7,4,-10,0],
      [4858,1432,2,-4,6,4,-2,4,-6,-4],
      [4677,1427,-28,-16,-42,-16,-10,11,-14,5,-25,-7,-24,-6,-6,-9,3,-13,-39,-31,-29,-11,-7,2,-10,-6,8,-13,12,-4,-15,-1,-15,-4,-9,-6,2,-9,25,-3,14,13,27,17,11,-8,6,-5,50,11,69,34,18,13,10,10,3,20,10,13,4,4,15,10,-14,5],
      [4260,1422,-3,-6,19,8,-16,-2],
      [4319,1422,38,-20,9,2,-8,4,-20,10,-19,4],
      [4836,1420,1,-4,7,4,-8,0],
      [4222,1408,-6,-2,-4,-1,22,-1,-4,5,-8,-1],
      [4846,1408,-1,-6,4,4,-3,2],
      [4239,1402,5,-3,2,9,-7,-6],
      [4267,1403,18,0,13,4,-18,1,-13,-5],
      [4156,1400,-92,-28,39,4,31,0,11,1,23,4,5,2,3,9,12,6,-5,5,-27,-3],
      [4194,1400,6,-4,4,4,-6,4,-4,-4],
      [4822,1396,4,-2,2,6,-6,-4],
      [4424,1392,4,-6,4,2,-4,6,-4,-2],
      [4468,1372,4,-10,4,6,-4,10,-4,-6],
      [4766,1372,-5,-10,9,6,5,10,-9,-6],
      [4680,1367,10,-7,24,-11,10,-5,0,8,-2,4,-11,8,-31,3],
      [4025,1345,-33,-41,-18,-26,-6,-8,-39,-47,-13,-18,5,-2,14,5,24,13,24,22,17,9,8,4,8,6,8,8,5,5,4,10,5,11,6,5,11,15,10,28,-40,1],
      [4260,1350,-6,-21,-3,-10,9,-23,42,-29,28,-2,6,-5,-1,8,-36,8,-31,8,27,10,19,-2,-2,4,-14,6,-2,20,11,19,-2,19,0,-13,-2,-3,-8,5,-5,-4,-6,-9,-4,-8,-4,-8,-4,20,-6,21,-6,-11],
      [4730,1341,2,-2,3,7,-5,-5],
      [4187,1334,-7,-4,-6,-2,-12,0,-10,0,-12,-4,-14,-14,-6,-12,-8,-38,15,-4,11,3,17,-18,21,-15,21,-14,6,-2,10,-15,17,-5,17,12,2,8,-7,6,-4,4,-4,17,10,24,-1,8,-10,11,-9,18,-9,23,-7,14,-13,3,-8,-4],
      [4354,1332,2,-6,12,6,-14,0],
      [4412,1332,-20,-4,6,-5,19,4,-5,5],
      [4090,1324,4,-4,2,6,-6,-2],
      [4063,1316,-3,-10,10,5,-7,5],
      [3966,1304,-1,-6,4,4,-3,2],
      [4372,1304,4,-4,4,4,-4,4,-4,-4],
      [1261,1287,3,-3,1,12,-4,-9],
      [4374,1276,4,-19,0,5,3,3,8,0,-2,8,-2,3,-5,6,-3,7,-3,-13],
      [3947,1269,-3,-6,5,0,2,7,-4,-1],
      [4333,1196,-8,-12,-2,-11,-24,4,-3,0,24,-15,24,-13,13,19,-6,13,-5,9,-13,6],
      [3698,1190,2,-40,11,4,9,36,-22,0],
      [4230,1156,11,-9,1,4,-12,5],
      [4304,1146,-1,-6,6,-8,-5,-4,-8,-4,16,-2,1,18,-9,6],
      [4319,1141,0,-8,8,3,-1,8,-7,-3],
      [4252,1136,4,-6,4,2,-4,6,-4,-2],
      [1684,1134,4,-4,4,2,-4,4,-4,-2],
      [4333,1128,-1,-10,-1,-9,4,-5,6,25,-8,-1],
      [4316,1108,-1,-4,7,4,-6,0],
      [3322,1105,4,-4,4,4,-4,3,-4,-3],
      [4277,1097,2,-7,7,2,2,6,-11,-1],
      [4313,1092,-13,-5,-2,-2,23,7,-8,0],
      [4278,1084,0,-9,-1,-1,-9,-9,0,-12,4,-15,18,-18,4,32,-5,20,-11,12],
      [1450,1024,22,0,-7,4,-15,-4],
      [1538,1025,-16,-2,-16,-3,10,-1,11,-5,-5,-11,51,6,17,9,-18,1,-22,5,-12,1],
      [1610,1024,3,-4,9,4,-3,4,-9,-4],
      [4106,1016,19,-18,11,4,-30,14],
      [343,1006,9,-8,-2,11,-7,-3],
      [1460,993,-3,-5,-13,-8,-7,-7,-40,-10,-32,4,-1,-4,25,-9,52,12,38,14,5,4,8,4,7,8,-39,-3],
      [4272,959,3,-22,9,17,-7,16,-5,-11],
      [1449,934,0,-8,4,6,-4,2],
      [4420,836,-4,-2,-2,-10,-1,-10,-7,-2,22,-8,2,22,-10,10],
      [4447,813,8,-14,17,3,-9,3,-9,4,-7,4],
      [4485,795,-16,-5,-24,3,-5,3,-8,2,6,-13,36,-9,22,-6,4,-6,4,-6,4,-8,2,4,17,-2,25,-40,10,-13,18,19,-8,13,-8,16,-12,37,-2,-4,-4,-2,-6,6,-18,2,-18,3,-16,15,-3,-9],
      [3022,784,13,-4,5,4,-18,0],
      [2911,781,12,0,-5,2,-7,-2],
      [2766,756,-14,-6,7,-10,18,10,-11,6],
      [2679,714,7,-16,10,3,-13,27,-4,-14],
      [2597,718,8,-1,-3,3,-5,-2],
      [2687,689,3,-13,3,11,-6,2],
      [4551,684,14,-16,9,-16,16,-8,20,10,7,16,-16,9,-12,2,-33,-1,5,5,-2,4,-8,-5],
      [1657,624,3,-5,12,5,-15,0],
      [4580,626,0,-7,-1,-69,-2,-15,10,-14,9,11,8,29,8,18,-9,2,-9,32,1,6,-11,4,-4,3],
      [1771,611,-26,-7,-24,-6,7,-8,30,-40,2,11,-2,11,7,2,20,6,11,4,-1,7,1,9,-4,3,-13,6,-8,2],
      [1797,613,1,-9,6,0,4,2,2,4,-13,3],
      [1658,578,-4,-6,18,6,-14,0],
      [2496,559,15,-5,7,-2,-25,-5,4,-8,3,-9,5,-6,8,-14,-3,-6,-9,-3,-10,-9,-5,-4,-5,-3,-3,-6,2,-15,23,-16,15,0,-10,8,-10,7,17,-2,8,15,-2,8,16,11,19,19,8,12,8,3,6,14,-3,9,-37,8,-27,3,-13,3,-11,0,9,-7],
      [2419,545,4,-10,2,-10,-5,-10,5,-7,18,-5,33,-2,0,9,-5,16,-19,16,-33,3],
      [675,524,1,-12,9,10,-10,2],
      [1400,528,1,-4,9,4,-10,0],
      [218,504,11,-4,1,4,-12,0],
      [4516,500,2,-4,4,4,-2,4,-4,-4],
      [2702,496,4,-4,6,4,-4,4,-6,-4],
      [631,468,1,-13,11,23,-12,-10],
      [362,468,3,-4,16,-8,11,-9,-1,9,-29,12],
      [2820,464,4,-4,4,4,-4,4,-4,-4],
      [2460,456,6,-4,6,2,-6,3,-6,-1],
      [2876,450,8,-4,-4,7,-4,-3],
      [4891,443,10,-2,-5,4,-5,-2],
      [186,428,5,-4,11,4,-16,0],
      [1920,424,-6,-4,-6,-4,-11,-1,-52,-26,-1,-26,-4,-3,-15,5,-5,-3,-13,-16,-5,-10,0,-7,4,-15,21,-1,3,6,4,-17,-12,-19,-24,-3,16,-2,18,-5,-17,-6,-6,-2,-15,-2,-18,1,-10,-4,10,0,5,-4,-1,-6,-34,-34,-11,-6,-105,-12,-48,-2,-18,-7,8,-5,7,-6,-51,-10,85,-12,17,-2,-11,-10,-13,-3,66,-13,20,-6,18,-5,18,-2,34,-3,34,4,6,4,4,-4,7,-2,25,4,6,-4,-10,-6,12,0,44,6,36,2,5,-5,-12,0,-22,-7,13,-4,-2,4,-10,3,11,1,11,-3,60,-5,28,-1,152,-3,66,7,-8,9,-14,4,32,1,23,2,-15,7,-14,6,19,-3,139,-5,-16,4,-62,17,-12,3,-2,-4,-7,-4,-15,4,4,4,-3,10,-23,19,6,-1,9,-1,19,8,16,4,-24,1,-32,1,-2,3,13,4,27,13,-6,1,-11,1,9,7,5,6,-17,8,-37,4,-15,0,16,6,14,7,-30,-4,-7,-1,14,10,18,10,7,2,8,11,-35,-4,-20,-4,-9,10,-10,5,12,-2,38,7,-107,21,-18,3,-4,3,-17,10,-51,20,-9,-5,-2,0,-16,7,-12,4,-9,4,-5,3,-20,29,-2,6,-25,34,-11,-4],
      [1844,288,-4,-2,-4,4,4,2,4,-4],
      [2166,276,-2,-4,-6,4,2,4,6,-4],
      [2170,260,-7,-4,1,4,6,0],
      [2252,228,-4,-4,-2,4,4,4,2,-4],
      [1616,180,-7,-4,-1,4,8,0],
      [2193,121,-8,1,5,2,3,-3],
      [2115,117,-14,0,7,2,7,-2],
      [2175,113,-32,0,15,2,17,-2],
      [1989,105,-10,0,5,2,5,-2],
      [1420,400,6,-4,4,4,-6,4,-4,-4],
      [1588,395,-22,-5,-24,-10,-14,-8,-44,-6,-32,-2,36,-12,24,-4,-5,-4,10,-9,15,-11,-34,-22,-16,-6,-20,-8,-22,-9,-3,1,-36,6,-107,-9,10,-5,14,-3,-17,0,-17,-17,49,-20,17,0,-11,6,4,26,13,0,-4,-2,-6,-2,5,-4,0,-3,30,-19,32,19,3,4,20,0,7,-2,15,-8,-10,-2,-36,-9,21,-2,37,10,5,4,24,13,31,3,14,4,21,7,16,7,12,3,3,4,-10,11,45,16,23,8,18,6,11,2,1,4,-9,0,-2,5,-8,6,-7,5,-28,-6,-30,-6,-2,4,1,4,11,8,10,4,16,10,5,10,-5,6,-22,-3,-13,4,19,11,-32,-3],
      [1370,392,13,-4,5,4,-18,0],
      [136,380,6,-2,8,6,-14,-4],
      [1344,380,-11,-4,-7,-3,9,-15,13,-13,5,2,17,9,25,11,12,5,6,4,-21,-2,-28,2,-20,4],
      [2265,375,-17,-8,-9,-7,-2,-4,7,-4,-12,-3,-11,-3,11,-3,6,-3,14,4,20,-1,6,1,8,0,15,-2,22,-2,41,14,-40,18,-59,3],
      [78,362,-21,-9,-21,-3,-14,-4,-11,-6,-6,6,-1,8,-4,-26,5,-26,34,10,33,16,16,4,38,2,-4,14,-11,6,-5,6,-6,3,-22,-1],
      [1464,318,23,-7,-9,13,-14,-6],
      [946,302,-22,-6,-26,-6,14,-6,40,0,12,-4,-46,-4,-36,-4,18,-5,-9,-2,-23,-4,11,-10,11,-8,-11,3,-31,13,-47,6,-19,-7,4,-14,8,-16,40,-5,34,3,48,10,12,4,10,4,15,1,21,3,12,4,8,-5,25,9,9,-4,12,-11,9,-3,6,-6,11,11,2,12,6,13,39,14,11,6,-7,1,-17,3,-1,2,11,2,-39,4,-45,0,-47,6,-33,-4],
      [905,241,-10,0,5,2,5,-2],
      [1473,302,8,-1,-3,3,-5,-2],
      [3248,300,17,-6,3,6,-20,0],
      [1593,294,8,-1,-3,3,-5,-2],
      [1787,293,4,-9,27,8,-31,1],
      [3323,270,-17,-6,-5,-9,8,-6,11,-7,10,-6,10,-7,12,-5,26,-15,80,-12,35,-5,45,0,-71,14,-50,9,-69,39,20,19,-19,3,-26,-6],
      [5104,272,8,-5,8,1,-8,5,-8,-1],
      [0,268,14,-4,16,4,-14,4,-16,-4],
      [1134,259,-19,-13,19,-2,-6,-4,-6,-3,51,-6,-1,7,3,8,-2,17,-17,5,-22,-9],
      [4566,236,14,-4,18,4,-14,4,-18,-4],
      [4153,225,15,-1,-9,4,-6,-3],
      [940,220,13,-4,11,-2,-42,-1,-22,-1,8,-11,26,-5,13,1,19,5,31,6,16,0,-9,-8,0,-10,16,6,24,6,16,1,-38,13,-44,4,-38,0],
      [1311,221,-32,-2,-29,-8,-30,-15,-28,-9,88,13,79,7,68,2,-53,11,-53,2,-10,-1],
      [1194,216,6,-7,31,7,-37,0],
      [4518,215,49,-13,53,4,-35,9,-43,3,-24,-3],
      [4657,216,0,-6,39,6,-39,0],
      [1072,212,6,-4,6,4,-6,4,-6,-4],
      [1134,212,3,-6,-13,0,-45,-13,18,5,17,2,2,-6,6,2,11,4,1,-4,13,-3,21,17,-34,2],
      [2292,212,6,-1,6,3,-6,2,-6,-4],
      [870,204,8,-4,4,4,-8,4,-4,-4],
      [832,201,-18,-2,29,-9,52,-9,18,2,-3,5,-11,6,-14,-2,-10,1,-12,5,-5,3,-6,2,-20,-2],
      [1410,196,-60,-2,-45,-9,9,-6,18,-1,21,-1,-3,-4,-13,-3,-22,-6,-12,-1,-66,0,-11,-4,12,-3,-11,-4,-32,-4,4,-12,59,-6,24,6,11,3,12,3,19,4,6,-2,29,-4,86,-9,-12,-1,-97,2,-4,-1,-49,-6,-12,-7,32,-3,57,-3,31,-5,263,-2,-13,14,-25,5,2,4,-38,8,-98,16,-16,4,20,4,4,5,-21,6,-16,5,-9,4,-1,9,-10,5,-23,2],
      [1405,181,-8,1,5,2,3,-3],
      [1354,160,20,0,-13,-6,-15,-2,-9,4,-17,4,4,2,30,-2],
      [1276,192,4,-4,4,2,-4,4,-4,-2],
      [2778,186,-8,-7,-4,-3,10,-6,13,-6,-26,2,-35,-6,-6,-4,-6,-4,68,4,5,0,9,-12,14,4,5,3,4,-6,28,-4,34,-2,6,0,30,1,23,4,-70,8,-46,0,16,5,56,19,-18,3,-18,-2,-2,-8,-35,3,-13,8,-7,6,-27,0],
      [1058,180,10,0,-1,4,-9,-4],
      [1264,180,16,0,-5,4,-11,-4],
      [952,176,22,-4,20,4,-22,4,-20,-4],
      [1194,176,-7,-4,-23,-9,19,-1,19,11,12,3,20,3,-19,0,-21,-3],
      [929,174,8,-1,-3,3,-5,-2],
      [1128,172,-34,-4,-16,-4,12,-3,-12,-1,-15,0,-3,-5,19,-1,35,4,34,15,-20,-1],
      [3990,164,33,-9,13,4,20,6,-40,4,-26,-5],
      [977,164,20,0,-11,3,-9,-3],
      [3922,158,-15,-4,-4,-8,-7,-2,-26,4,-12,-1,12,-3,8,-4,37,-12,29,9,22,8,14,7,-27,8,-31,-2],
      [3244,140,6,-4,6,2,-6,3,-6,-1],
      [3361,142,8,-1,-3,3,-5,-2],
      [3376,136,-8,-8,-2,4,-5,1,2,-6,17,4,4,11,-8,-6],
      [3262,136,15,-4,-4,5,-11,-1],
      [3408,136,30,-4,0,4,-30,0],
      [3460,132,18,-4,2,4,-18,4,-2,-4]];
      var seav=document.getElementById('sea');
      var seac=seav.getContext('2d');
      var landv=document.getElementById('land');
      var landc=landv.getContext('2d');
      var v=document.getElementById('vdu');
      var c=v.getContext('2d');

      var f=function(n){
        return {
          x:parseFloat(n[0][0]),
          y:n[1]?parseFloat(n[1][0]):0
        }
      }

      $(v).tooltip({bodyHandler:function(){return'Use the mousewheel to zoom in and then click and drag to pan.'}});
      v.addEventListener('DOMMouseScroll',wheel.event,false);

      v.addEventListener("mousedown",function(e){
        origX=scrollX;origY=scrollY;
        grabX=mouseX;grabY=mouseY;
        mouseDown=true;
      },false);
      v.addEventListener("mouseup",function(e){
        mouseDown=false;
      },false);

      v.addEventListener("mousemove",function(e){
        try{
          var scrolX=window.scrollX!=null?window.scrollX:window.pageXOffset;
          var scrolY=window.scrolY!=null?window.scrollY:window.pageYOffset;
          mouseX=(e.clientX-v.offsetLeft+scrolX);
          mouseY=(e.clientY-v.offsetTop+scrolY);

          mouseY>0&&mouseY<height&&mouseX>0&&mouseX<width?document.body.style.cursor="pointer":document.body.style.cursor="auto";

          if(mouseDown){
            scrollX=origX+(mouseX-grabX)/zoom/zoom;
            scrollY=origY+(mouseY-grabY)/zoom/zoom;
            draw();
          }
        }catch(e){}
      },false);

      for (var i = 0; i < worldMap.length; i++) {
        var m = worldMap[i];
        for (var j = 2; j < m.length; j++) m[j] += m[j-2];
        for (var j = 0; j < m.length; j++) m[j] /= 4;
      }

      img=function(c){
        var wm = worldMap;
        c.beginPath();
        for (var i = 0; i < wm.length; i++) {
          var m = wm[i];
          c.moveTo(m[0], m[1]);
          for (var j = 2; j < m.length; j += 2) {
            c.lineTo(m[j], m[j+1]);
          }
        }
        c.closePath();
        c.fill();
      }

      var grad={
        perl:[[320,120,0,320,120,640],'#e2fcff','#6ccef1','#023384'],
        land:[[0,0,0,620],'#082d6b','#2d6faf',],
        fire:[[0,0,0,620],'#870801','#da650d','#fef751'],
        grey:[[320,120,0,320,120,640],'#454545','#3c3c3c','#232323'],
        red:[[0,0,0,620],'#000','#d22','#daa'],
        gold:[[0,0,0,620],'#fb2','#fb2','#000']
      }

      var make=function(a){
        var g,t=a[0].length,l=a.length;
        if(t>4){g=c.createRadialGradient(a[0][0],a[0][1],a[0][2],a[0][3],a[0][4],a[0][5]);}
        else{g=c.createLinearGradient(a[0][0],a[0][1],a[0][2],a[0][3]);}
        for(var i in a){i>0?g.addColorStop(1/(l-2)*(i-1),a[i]):0;}
        return g;
      }

      var sea =make(grad.perl);
      // var sea = make(grad.grey);
      var land=make(grad.land);
      // var land=make(grad.red);
      // var fire=make(grad.fire);
      var fire=make(grad.gold);

      var ll=function(lat,lon){
        return {
          x:(180+lon)*width/360,
          y:(90-lat)*height/180
        }
      }

      var startTime=-1;
      //////////////////////////////////////////////////////////////////////////
      var drawSea=function(c){
        c.shadowBlur=0;
        c.fillStyle=sea;
        c.globalAlpha=.7;
        c.fillRect(0,0,640,320);
      }

      var drawLand=function(c, scrollX, scrollY, zoom){
        c.clearRect(0,0,640,320);
        c.save();
        tran(c, scrollX, scrollY, zoom);
        c.fillStyle=land;
        c.scale(.5,.5);
        c.globalAlpha=1;
        c.shadowOffsetX=0;
        c.shadowOffsetY=3;
        c.shadowBlur=3;
        c.shadowColor='rgba(0,0,0,.5)';
        img(c);
        c.strokeStyle="#048";
        c.lineWidth=.5;
        c.stroke();
        c.restore();
      }

      var drawData=function(c, scrollX, scrollY, zoom, frame){
        c.clearRect(0,0,640,320);
        c.save();
        tran(c, scrollX, scrollY, zoom);
        if(data){
          c.globalAlpha=1;
          c.fillStyle="#fa0;";
          c.strokeStyle="#840"
          c.lineWidth=.4;
          try{
            c.shadowOffsetX=0;
            c.shadowOffsetY=5;
            c.shadowBlur=3;
            c.shadowColor='rgba(255,255,255,.25)';
            // Sort such that pins are drawn from the north, so a pins are covered by pins slightly to the south
            var loc = data.seconds[frame].locations.sort(function(a,b) {return b.latitude-a.latitude});
            for(var i = 0; i < loc.length; i++){
              pin=loc[i];
              p=ll(pin.latitude,pin.longitude); // perhaps we should skip through this array quicker when things get heavy?
              c.save();
              c.translate(p.x,p.y-3);
              c.beginPath();
              c.moveTo(0,0);
              c.lineTo(2,-2);
              c.lineTo(1,-2);
              c.lineTo(1,-4);
              c.lineTo(-1,-4);
              c.lineTo(-1,-2);
              c.lineTo(-2,-2);
              c.closePath();
              c.stroke();
              c.fill();
              c.restore();
            }
          }catch(e){}
          c.shadowBlur=0;
        }
        c.restore();
      }

      var tran=function(c, scrollX, scrollY, zoom){
        var s = zoom*zoom;
        c.translate(310*(1-s) + s*scrollX, 155*(1-s) + s*scrollY);
        c.scale(s, s);
      }

      seac.clearRect(0,0,640,320);
      drawSea(seac);

      var draw=function(){
        var sX=scrollX, sY=scrollY, z=zoom, f=Math.floor(((new Date()).getTime()-startTime)/1000);
        if (prevScrollX!=sX || prevScrollY!=sY || prevZoom!=z) {
          drawLand(landc, sX, sY, z);
        }
        if (prevScrollX!=sX || prevScrollY!=sY || prevZoom!=z || prevFrame!=f) {
          if (data && data.seconds && data.seconds[f]) {
            drawData(c, sX, sY, z, f);
          }
        }
        prevScrollX=sX;
        prevScrollY=sY;
        prevZoom=z;
        prevFrame=f;
        setTimeout(draw, 100);
      }
      //////////////////////////////////////////////////////////////////////////

      var getData = function() {
        $.getJSON("data/latlng_report.json", function(json) {data=json; startTime = (new Date()).getTime();});
      }
      window.setInterval(getData, 60000);
      getData();

      draw();
    }

  addEventListener("DOMContentLoaded",initMap,!true);

  var data = {};
})();
