COLOURS = ["#FF3D3D","#FF850A","#FFA347","#FFFF47","#C8FF47","#6CFF47","#47FF7E","#47FFDA","#47C8FF","#008FCC","#336699"];
DOTS = [];
SAVED = [];
HOVERTIMER = null;
ZOOMSCALE = false;
INITIALISED = false;

function LL(lat, lng) {
  return new google.maps.LatLng(lat, lng);
}
function initialise() {
  window.onresize=adjustListSize;
  adjustListSize();
  var latlng = LL(-43.6,172.42);
  var myOptions = {
    zoom: 9,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.HYBRID,
    minZoom: 4
  };
  try{
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
  }catch(x){} // for some reason, IE8 throws an error but renders the map correctly
  window.map = map;

  var rb = document.createElement('button');
  rb.innerHTML='Reset map';
  rb.style.marginRight = '5px';
  rb.onclick=function() {
    map.setCenter(LL(-43.6,172.42));
    map.setZoom(9);
    map.fitBounds(new google.maps.LatLngBounds(LL(-44.1,171.65),LL(-43.2,173.3)));
  }
  map.controls[google.maps.ControlPosition.RIGHT].push(rb);
  
  google.maps.event.addListenerOnce(map, "tilesloaded", processQuakes);
  preload=new Image();
  for(var i=0;i<11;i++){
    preload.src='/images/dots/'+i+'.png';
    DOTS[i] = new google.maps.MarkerImage('/images/dots/'+i+'.png', new google.maps.Size(9, 9), new google.maps.Point(0,0), new google.maps.Point(5, 5));
  }
}

function adjustListSize() {
  var main=document.getElementById('main');
  var list=document.getElementById('sidebar-inner');
  var head=document.getElementById('sidebar-header');
  list.style.height=parseInt(main.offsetHeight)-parseInt(head.offsetHeight)-10+"px";
  document.getElementById('main').className=(document.body.offsetWidth<900)?'narrow':'';
}

function zoomScale() {
  if(ZOOMSCALE) return;
  for(var i=0;i<quakes.length;i++) {
    quakes[i].circle.setRadius(quakes[i].radius*(Math.pow(2,8-map.getZoom())));
  }
}

function p(q) {
  for(var l=q.length,k=q[0],r=Array(((l-k-1)/k)||0),i=1+k,j=0,ki,o;i<l;)
    for(r[j++]=(o={}),ki=0;ki<k;o[q[++ki]]=q[i++]);
  return r;
}
function processQuakes() {
  var newList = "";
  quakes=p(quakes);
  map.fitBounds(new google.maps.LatLngBounds(LL(-44.1,171.65),LL(-43.2,173.3)));
  for(var i=0;i<quakes.length;i++) {
    quakes[i].m = quakes[i].m / 100;
    quakes[i].radius = quakes[i].m*quakes[i].m*quakes[i].m*250;
    quakes[i].point = new google.maps.LatLng(quakes[i].lat,quakes[i].lon);
    quakes[i].colour = COLOURS[quakeDepth(quakes[i].d)];
    quakes[i].circle = new google.maps.Circle({radius: quakes[i].radius*(Math.pow(2,8-map.getZoom())), center: quakes[i].point, strokeColor: quakes[i].colour, fillColor: quakes[i].colour, fillOpacity: 0.6, strokeOpacity: 1.0, zIndex: i});
    quakes[i].dot = new google.maps.Marker({position: quakes[i].point, map: map, icon: DOTS[quakeDepth(quakes[i].d)], zIndex: quakes.length+i+2});
    var qdate = quakeTime(quakes[i].t).split(' ');
    var qtime = qdate[1];
    qdate = qdate[0];
  }
  quakes[24].circle.setMap(map);
  INITIALISED=true;
  adjustListSize();
  zoomScale();
  google.maps.event.addListener(map, "zoom_changed", zoomScale);
}

function quakeClick(i) {
  if(!INITIALISED) return;
  map.panTo(quakes[i].point);
  map.setZoom(14);
}

function quakeHoverOn(hovered,el) {
  el.style.backgroundColor="#C2E0FF";
  if(!INITIALISED) return;
  try{window.clearTimeout(HOVERTIMER);}catch(x){}
  quakes[hovered].circle.setMap(map);
  if(hovered<24) quakes[24].circle.setMap(null);
}

function quakeHoverOff(out,el) {
  el.style.backgroundColor="";
  if(!INITIALISED) return;
  if(quakes[out].circle.getMap()!=null) quakes[out].circle.setMap(null);
  HOVERTIMER = window.setTimeout(restoreFirstQuake, 500);
}

function restoreFirstQuake() {
  quakes[24].circle.setMap(map);
}

function quakeDepth(depth) {
  if(depth < 2.5) return 0;
  if(depth < 5) return 1;
  if(depth < 7.5) return 2;
  if(depth < 10) return 3;
  if(depth < 15) return 4;
  if(depth < 20) return 5;
  if(depth < 25) return 6;
  if(depth < 30) return 7;
  if(depth < 40) return 8;
  if(depth < 50) return 9;
  return 10;
}

function pad(i) {
  if(i<10) return '0' + i;
  return i;
}

function magnitude(f) {
  return Math.round(f*10)/10;
}

Date.prototype.toUTCDateString = function() {
  var utc = new Date();
  utc.setFullYear(this.getUTCFullYear());
  utc.setDate(this.getUTCDate());
  utc.setMonth(this.getUTCMonth());
  return utc.toDateString();
}

function quakeTime(t) {
  var nzoffset = 12 * 60 * 60; // NZ's offset in seconds
  var d = new Date();
  if(t>=1285423200 && t<1301752800) { // 2010-2011 NZDT period
    nzoffset += (60*60);
  }
  d.setTime((t+nzoffset)*1000);
  return d.getUTCDate()+'/'+(d.getUTCMonth()+1)+'/'+d.getUTCFullYear()+' '+pad(d.getUTCHours())+':'+pad(d.getUTCMinutes());
}

