var map;
var markers = [];

// Function that creates icons.
function createIcon(icon,iconType) {
  var iconImg;

  iconImg = new GIcon(iconType);
  iconImg.image = icon;

  return iconImg;
}

// Function that creates info windows based on type desired.
function createInfoWindows(marker,markerDataVal,tabbed) {
  var infoWindowTxt;

  // Make tabbed info window.
  if(tabbed) {
    createTabbedMarkerInfoWindow(marker,markerDataVal);
  } else {// Normal info window.
    infoWindowTxt = markerDataVal.descr;
    if(infoWindowTxt=='') {
      infoWindowTxt = markerDataVal.title;
    }
    createMarkerInfoWindow(marker,infoWindowTxt);
  }
}

// Function to create normal info windows.
function createMarkerInfoWindow(marker,infoWindowTxt) {
  GEvent.addListener(marker, "click", function() {
    if(''!=infoWindowTxt) {
      marker.openInfoWindowHtml(infoWindowTxt);
    }
  });
}

// Function to create tabbed info windows.
function createTabbedMarkerInfoWindow(marker,markerDataVal) {
  var label1 = markerDataVal.label1;
  var html1 = markerDataVal.html1;
  var label2 = markerDataVal.label2;
  var html2 = markerDataVal.html2;

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html1), new GInfoWindowTab(label2,html2)]);
  });
}

// Main function called on load.
function initialize() {
  if(GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById('map'));
    //map.addMapType(G_SATELLITE_3D_MAP);

    map.setCenter(new GLatLng(12.996811, 80.258979), 3); // That's my home location
    map.setUIToDefault();
    //detectBrowser();

    //map.addControl(new GLargeMapControl());
    map.setMapType(G_PHYSICAL_MAP);

    // Home.
    makeMarker(data.homeCount,"markers/marker-GREEN-START.png",G_DEFAULT_ICON,data.homeMarker,true);

    // White markers - Old travels (undocumented).
    makeMarker(data.whiteMarkersCount,"markers/marker-WHITE-BLANK.png",G_DEFAULT_ICON,data.whiteMarkers,false);

    // College.
    makeMarker(data.collegeCount,"markers/marker-PURPLE-DOT.png",G_DEFAULT_ICON,data.collegeMarker,true);

    // Offices Worked at.
    makeMarker(data.officesCount,"markers/marker-YELLOW-DOLLAR.png",G_DEFAULT_ICON,data.officeMarkers,false);

    // Rented Homes.
    makeMarker(data.rentedHomesCount,"markers/marker-ORANGE-DOT.png",G_DEFAULT_ICON,data.rentedHomesMarkers,true);

    // Old Trips with Friends (undocumented).
    makeMarker(data.oldFriendsTripsCount,"markers/marker-BLUE-BLANK.png",G_DEFAULT_ICON,data.oldFriendsTripMarkers,false);

    // Recent Trips with Friends.
    makeMarker(data.tripsWithFriendsCount,"markers/marker-GREEN-BLANK.png",G_DEFAULT_ICON,data.tripsWithFriendsMarkers,false);

    // Personal Trips.
    makeMarker(data.personalTripsCount,"markers/marker-BLANK.png",G_DEFAULT_ICON,data.personalTripsMarkers,false);

    // Europe with Friends.
    makeMarker(data.europeWithFriendsCount,"markers/marker-YELLOW-BLANK.png",G_DEFAULT_ICON,data.europeWithFriendsMarkers,false);

    // Middle East Trips.
    makeMarker(data.middleEastTripsCount,"markers/marker-PURPLE-BLANK.png",G_DEFAULT_ICON,data.middleEastTripsMarkers,false);

    // Solo Trips.
    makeMarker(data.soloTripsCount,"markers/marker-GRAY-BLANK.png",G_DEFAULT_ICON,data.soloTripsMarkers,false);

    var markerCluster = new MarkerClusterer(map, markers);
  }
}

// Function that makes a marker.
function makeMarker(count,icon,iconType,markerData,tabbed) {
  var iconImg = createIcon(icon,iconType);
  var latlng;
  var marker;

  for (var i = 0; i < count; i++) {
    latlng = new GLatLng(markerData[i].latitude, markerData[i].longitude);
    marker = new GMarker(latlng, {icon: iconImg});
    //marker.title = '1';

    //map.addOverlay(marker);
    markers.push(marker);

    createInfoWindows(marker,markerData[i],tabbed);
  }
}
