// Locations data
var locations = null;
// Google map
var map = null;
// Mouse coord
var pageX = 0;
var pageY = 0;
// True if mouse is over tooltip
var isMouseOverTooltip = false;
// True if mouse is over investment icon
var isMouseOverInvestment = false;
// Id of tooltip investment if showing
var tooltipId = false;

// Base icon
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(16, 27);
baseIcon.shadowSize = new GSize(0, 0);
baseIcon.iconAnchor = new GPoint(15, 15);
baseIcon.infoWindowAnchor = new GPoint(15, 15);
baseIcon.infoShadowAnchor = new GPoint(15, 15);

function changeLocation(location) {
  map.setCenter(new GLatLng(locations[location].latitude, locations[location].longitude), locations[location].zoom);
}

function addInvestments(investments) {
  $.each(investments, function(number, investment) {
    var icon = new GIcon(baseIcon);
    icon.image = "/images/site/" + investment.type + "-pin.png";
    var marker = new GMarker(new GLatLng(investment.latitude, investment.longitude), icon);
    map.addOverlay(marker);
    GEvent.addListener(marker, 'mouseover', function() {
      if(tooltipId != investment.id) {
        showTooltip(investment.id);
      }
      isMouseOverInvestment = true;
    });
    GEvent.addListener(marker, 'mouseout', function() {
      window.setTimeout(function() {
        if(!isMouseOverTooltip && !isMouseOverInvestment) {
          hideTooltip();
        }
      }, 1000);
      isMouseOverInvestment = false;
    });
    $("#investment_tooltip").mouseover(function(e) {
      isMouseOverTooltip = true;
    });
    $("#investment_tooltip").mouseout(function(e) {
      window.setTimeout(function() {
        if(!isMouseOverTooltip && !isMouseOverInvestment) {
          hideTooltip();
          isTooltipShowing = false;
        }
      }, 1000);
      isMouseOverTooltip = false;
    });
  });
}

$(document).ready(function() {
  if (GBrowserIsCompatible()) {
    // Initialize google maps
    map = new GMap2(document.getElementById("google-map"));
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
    //map.enableScrollWheelZoom();
    map.setUIToDefault();
		
    // Load locations
    $("#status").text("Initializing...");
    //$.ajax({url:"/investment-map-json.html",dataType:'json',success:onInvestmentMapLoaded, error:handleJsonError});

    $.getJSON("/investment-map-json.html", onInvestmentMapLoaded);

    // Track mouse
    $().mousemove(function(e){
      pageX = e.pageX;
      pageY = e.pageY;
    });
  }
});



function handleJsonError(error)
{
  alert('error');
  alert(dump(error));
}

function onInvestmentMapLoaded(json) {

  $("#status").text("");

  locations = json.locations;
	
  changeLocation(json.defaultLocation);
  map.setMapType(G_HYBRID_MAP);
  addInvestments(json.investments);

  // Initialize location drop down
  for(name in locations) {
    if(name == json.defaultLocation) {
      $("#location").append('<option value="' + name + '" selected="selected">' + name + '</option>');
    } else {
      $("#location").append('<option value="' + name + '">' + name + '</option>');
    }
  }

  $("#location").change(function() {
    changeLocation(this.value);
  });
}

function showTooltip(investmentId) {
  $("#investment_tooltip").hide();
  $("#investment_tooltip").css("left", pageX + 0 + "px");
  $("#investment_tooltip").css("top", pageY + 0 + "px");
  $("#investment_tooltip").load("/investment-map-tooltip/"+investmentId+".html", {}, function() {
    $("#investment_tooltip").fadeIn("slow");
  });
  tooltipId = investmentId;
}


function hideTooltip() {
  $("#investment_tooltip").fadeOut("fast");
  tooltipId = false;
}

$(document).unload(GUnload);