if(typeof console === "undefined") {
    console = { log: function() { } };
}

//var MarkerData = {
//    latlng : "",
//    color: "",
//    content: "", 
//    icon: {}
//};


var NpfMap = {
  onload : function(mapCenter, mapZoom) {
 	NpfMap.initializeMap(mapCenter, mapZoom);
 	NpfMap.setupMarkers();
  },
  
  initialize : function() {
    NpfMap.markerDataByLocation = {};
    NpfMap.markerData = [];
    NpfMap.markers = [];
    NpfMap.initializeIcons();
  },
  
  load : function(callback) {
    NpfMap.onload = callback;
  },
  
  initializeMap : function(center, zoom) {
    NpfMap.map = new GMap2(document.getElementById("map"));

    NpfMap.map.setCenter(center, 2);
    NpfMap.map.setUIToDefault();
    NpfMap.map.setMapType(G_PHYSICAL_MAP);
		NpfMap.map.setZoom(zoom);
  },
  
  initializeIcons : function() {
    NpfMap.baseIcon = new GIcon(G_DEFAULT_ICON);
    // NpfMap.baseIcon.image = ".http://s75551.gridserver.com/images/site/home/icon-restaurant.png";
    //     NpfMap.baseIcon.shadow = "";
    NpfMap.baseIcon.iconSize = new GSize(23, 20); //GSize(15, 23);
    NpfMap.baseIcon.shadowSize = new GSize(0, 0); //GSize(18, 23);
    NpfMap.baseIcon.iconAnchor = new GPoint(0, 23);
    NpfMap.baseIcon.infoWindowAnchor = new GPoint(12, 1);
    
    NpfMap.defaultIcon = new GIcon(G_DEFAULT_ICON);
    
    NpfMap.blueIcon = new GIcon(NpfMap.baseIcon);
    NpfMap.blueIcon.image = "http://s75551.gridserver.com/images/site/home/icon-restaurant.png";

    NpfMap.cyanIcon = new GIcon(NpfMap.baseIcon);
    NpfMap.cyanIcon.image = "http://s75551.gridserver.com/images/site/home/icon-museum.png";

    NpfMap.redIcon = new GIcon(NpfMap.baseIcon);
    NpfMap.redIcon.image = "http://s75551.gridserver.com/images/site/home/icon-bar.png";

    NpfMap.yellowIcon = new GIcon(NpfMap.baseIcon);
    NpfMap.yellowIcon.image = "http://s75551.gridserver.com/images/site/home/icon-hotel.png";
  },
  
  getIcon : function(name) {
    switch(name) {
      case 'cyan':    return NpfMap.cyanIcon;
      case 'red':     return NpfMap.redIcon;
      case 'yellow':  return NpfMap.yellowIcon;
      default:        return NpfMap.blueIcon;
    }
  },
  randomizeData : function(count, content) {
    var randomColor = function() {
      switch(Math.round(Math.random() * 4)) {
        case 1:  return 'cyan';
        case 2:  return 'red';
        case 3:  return 'yellow';
        default: return 'blue';
      }
    };
    
    // Add some markers to the map at random locations
    var bounds = NpfMap.map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    for (var i = 0; i < count; i++) {
      NpfMap.addMarker(
        southWest.lat() + latSpan * Math.random(),
        southWest.lng() + lngSpan * Math.random(),
        randomColor(),
        content
      );
    }
  },
  
  addMarkerData : function(latlng, category, entry_id,  content) {
	
	//no geo data to plot
	if (latlng === undefined || latlng.length < 5)
		return;
	
  	// //convert the category into color
  	// switch(category) {
  	//   case 'Museum': 
  	//   	var data = new Object();
  	//   	data.color = "cyan";
  	//   	data.category = category;
  	//   	data.icon = NpfMap.cyanIcon;
  	//     break; 
  	//      
  	//   case 'Bar':     
  	//   	var data = new Object();
  	//   	data.color = "red";
  	//   	data.category = category;
  	//   	data.icon = NpfMap.redIcon;
  	//   	break;
  	//   	
  	//   case 'Hotel':  
  	// 	  	var data = new Object();
  	// 	  	data.color = "yellow";
  	//         data.category = category;
  	// 	  	data.icon = NpfMap.yellowIcon;
  	//   	break;
  	//   case 'Restaurant':
  	//     var data = new Object();
  	//     data.color = "blue";
  	//         data.category = category;
  	//     data.icon = NpfMap.blueIcon;
  	//     break;
  	//   
  	//   default:
  	//     var data = new Object();
  	//     data.category = category;
  	//     data.icon = NpfMap.defaultIcon;
  	//     break;
  	// }

			var data = new Object();
	    data.category = category;
	    data.icon = NpfMap.defaultIcon;
	  	data.content = content;
	  	data.latlng = latlng;
	  	data.entry_id = entry_id;
           
      if (NpfMap.markerDataByLocation[latlng] !== undefined)
      {
          //there is already a key
          NpfMap.markerDataByLocation[latlng].push(data);
      }
      else
      {
          //there is not that key yet
          NpfMap.markerDataByLocation[latlng] = [];
          NpfMap.markerDataByLocation[latlng].push(data);
      }
  },
  
  
  setupMarkers : function()
  {  		
		
		for(var latLng in NpfMap.markerDataByLocation)
		{
	  		var markersData = NpfMap.markerDataByLocation[latLng];
	  		
	  		var markersCount = markersData.length;
	  		var markersIndex = 0;
			
			//loop thru and create the makers
	  		for(; markersIndex < markersCount; markersIndex++)
	  		{	
	  			//get the marker data
	  			var data = markersData[markersIndex];
	  			var entry_id = data.entry_id;
					var latlngString = data.latlng;
					
	  			///covert the latlng from a string into a point
	  			var latlng = GLatLng.fromUrlValue(latlngString);
	  			
	
	  			//if we have more than one marker for this location we need to spread them out a little
	  			if (markersIndex > 0)
	  			{
		  			var lat = latlng.lat() + (Math.random() * .5) / 10;
		  			var lng = latlng.lng() + (Math.random() * .5) / 10;
	  				latlng = new GLatLng(lat, lng);
	  			}
	  			
		  		var marker = new GMarker(latlng, { icon:data.icon });
		
					GEvent.addListener(marker, "click", function() {
								NpfMap.openInfoWindow(latlngString, entry_id);
					});
					
		  		marker.bindInfoWindowHtml(data.content);
		  		var markerCount = NpfMap.markers.push(marker);
		  		NpfMap.map.addOverlay(marker);
		  		data.markerIndex = markerCount - 1;
	  		}
		  }
  },
  
  openInfoWindow : function(latlng, entry_id) 
  {
  	//the marker data for that location
  	var markersData = NpfMap.markerDataByLocation[latlng];
  	
  	var markersCount = markersData.length;
		var markersIndex = 0;
	
	//loop thru
	for(; markersIndex < markersCount; markersIndex++)
	{
		var data = markersData[markersIndex];
		
		//if the markers entry_id is the same as the requested one the open that markers popup
  		if (data.entry_id == entry_id)
  		{
					NpfMap.map.setZoom(8);
					var marker = NpfMap.markers[data.markerIndex];
					marker.openInfoWindowHtml(data.content);
					$.scrollTo($('div.content'), 600);
  		}
  	}
  }, 
  
  resetMarkers : function(keepCategory)
  {
			console.log('called resetMarkers....');
			
        if (keepCategory !== undefined)
        {
            //we need to keep some of the marker data 
            NpfMap.filterMarkerData(keepCategory);
        }
        else
        {
            //clear the marker data object
            NpfMap.markerDataByLocation = {};
        }
	
        var markersCount = NpfMap.markers.length;
        var markersIndex = 0;

        //loop thru the markers and remove them from the map
        for(; markersIndex < markersCount; markersIndex++)
        {
            NpfMap.map.removeOverlay(NpfMap.markers[markersIndex]);
        }

        //reset the markers array
        NpfMap.markers = [];
  },
  
  filterMarkerData: function(keepCategory)
  {
        var newMarkerData = {};
      
        for(var latLng in NpfMap.markerDataByLocation)
    	{
      		var markersData = NpfMap.markerDataByLocation[latLng];
		
      		var markersCount = markersData.length;
      		var markersIndex = 0;
	
    		//loop thru and create the makers
      		for(; markersIndex < markersCount; markersIndex++)
      		{	
		      			//get the marker data
		      			var data = markersData[markersIndex];
			    
					    //we are keeping some...
		          if (data.category == keepCategory)
		          {
		              if (newMarkerData[latLng] !== undefined)
		              {
		                //there is already a key
		                newMarkerData[latLng].push(data);
		              }
		              else
		              {
		                //there is not that key yet
		                newMarkerData[latLng] = [];
		                newMarkerData[latLng].push(data);
		              }
		          }
      		}		
    	  }
    	  
    	  NpfMap.markerDataByLocation = newMarkerData;
  }
  
};



NpfMap.initialize();


