var map;
var initLL, dalstonLL, haggerstonLL;
var dalstonMarker, dalstonInfo, haggerstonMarker, hanggerstonInfo;

function initMap() {
  initLL = new google.maps.LatLng (51.5518, -0.0746);
  dalstonLL = new google.maps.LatLng (51.5470, -0.0746);
  haggerstonLL = new google.maps.LatLng (51.5351, -0.0696);

  var myOptions = {
    zoom: 14,
    center: initLL,
    disableDefaultUI:true,
    draggable:false,
    scrollwheel:false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  
  map = new google.maps.Map(document.getElementById("map"), myOptions);
  // map.fitBounds(new google.maps.LatLngBounds(dalstonLL, haggerstonLL));
  
  // setTimeout("$('#map').slideDown(2000, bustAMove)",2000);
  $('#map').slideDown(800, showDalston);
}

function showDalston() {
  // fade out old address details
  $('.contact').css({height:$('.contact').height()+'px'});
  $('.old-details').fadeOut(1200, function() {
    $('.new-details').fadeIn(1000);
  });
  
  
  google.maps.event.trigger(map, 'resize');
  map.setCenter( dalstonLL );
  map.setZoom( map.getZoom() );

  $.scrollTo('.contact', {
    axis:'y',
    duration:800,
    offset: {top: -36},
    onAfter:function() {
      map.panTo(dalstonLL);
      
      dalstonMarker = new google.maps.Marker({
        map:map,
        clickable:false,
        draggable:false,
        animation: google.maps.Animation.DROP,
        position: dalstonLL
      });
      
      setTimeout("showDalstonBubble()", 1500);
    }
  });
}

function showDalstonBubble() {
  var dalstonMessage = 'After nearly three years at Fitzroy House, we’re on the move. It’s been fun, and we will miss the dogs and the friends we made but we definitely won’t miss the draft.';
  dalstonMessage += '<br /><a href="javascript:nextBubble();">So …</a>';
  
  dalstonInfo = new google.maps.InfoWindow({
    content:dalstonMessage,
    maxWidth: 200
  });
  
  dalstonInfo.open(map, dalstonMarker);
}

function nextBubble() {
  var dalstonMessage = '… We’ve found ourselves a new office, it’s near the next stop on the East London Line.';
  dalstonMessage += '<br /><br /><a href="javascript:panTo();">Come and see!</a>';
  dalstonInfo.setContent(dalstonMessage);
  
  // setTimeout("bustAMove()", 2000);
}

function panTo() {
  dalstonInfo.close();
  
  map.panTo(haggerstonLL);
  
  haggerstonMarker = new google.maps.Marker({
    map:map,
    clickable:false,
    draggable:false,
    animation: google.maps.Animation.DROP,
    position: haggerstonLL
  });
  
  setTimeout("showHaggerstonBubble();", 500);
}

function showHaggerstonBubble() {
  var haggerstonMessage = 'This is our lovely new office, with a view over the park.<br /><br />';
  haggerstonMessage += '<a href="javascript:showAddress();">Here are our new address details</a>';
  
  haggerstonInfo = new google.maps.InfoWindow({
    content:haggerstonMessage,
    maxWidth: 200
  });
  
  haggerstonInfo.open(map, haggerstonMarker);  
}

function showAddress() {
  var addressMessage = '<b>Industry</b><br /> Adelaide Wharf<br /> 21 Whiston Road<br /> London<br /> E2 8EX<br /><br />';
  addressMessage += '020 7749 6942';
  haggerstonInfo.setContent(addressMessage);
  haggerstonInfo.open(map, haggerstonMarker);
}


function loadMap() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&region=GB&callback=initMap";
  document.body.appendChild(script);
}

$(document).ready(function() {
  loadMap();
});

