
var home_tabs;

$(document).ready(function() {
  
  map_initialise();
  home_tabs = $("#project_tabs").tabs();
  
  // completion filter radio buttons 
  $('form#pfilter_form input#pfilter_all').checkbox({cls:'jquery-pv-filter-all-checkbox', empty: '/sites/all/themes/yf/images/spacer.gif'});
  $('form#pfilter_form input#pfilter_completed').checkbox({cls:'jquery-pv-filter-completed-checkbox', empty: '/sites/all/themes/yf/images/spacer.gif'});
  $('form#pfilter_form input#pfilter_ongoing').checkbox({cls:'jquery-pv-filter-ongoing-checkbox', empty: '/sites/all/themes/yf/images/spacer.gif'});
  
});

/**
 * Makes sure all markers are ready before we perform any actions on them
 */
function map_initialise() {
  if (GBrowserIsCompatible()) {
    var m = Drupal.gmap.getMap('auto1map');
    if (m.map) {
      map_ready();
    } else {
      m.bind('ready', function(data) {
        map_ready();
      });
    }
  }
}

function map_ready(){

  setMarkers();
  
  // project completion filter

  $("#pfilter_form input[name=pfilter]").click(function(){
    runFilter($("#pfilter_form input[name=pfilter]:checked").val());
  });
  
  $("#project_tabs").bind("tabsshow", function(event, ui){
    // we need to reload and centre the map when the tab is loaded otherwise it goes all over the place
    Drupal.gmap.getMap('auto1map').map.checkResize();
    Drupal.gmap.getMap('auto1map').map.setCenter(new GLatLng(Drupal.settings.map_centre[0], Drupal.settings.map_centre[1]));
  });
  
}

var markers = new Array();

/**
 * Place the markers and set info window content for each marker.
 */
function setMarkers(){
  
  for(var i in Drupal.settings.projects.ongoing){
    
    var project = Drupal.settings.projects.ongoing[i];
    addMarker(project, "ongoing");
    
  }
  
  for(var i in Drupal.settings.projects.completed){

    var project = Drupal.settings.projects.completed[i];
    addMarker(project, "completed");
    
  }
  
}

/**
 * Place individual marker
 */
function addMarker(project, state){
  
  var marker_icon = new GIcon(G_DEFAULT_ICON);
  marker_icon.image = Drupal.settings.basePath + "sites/all/modules/gmap/markers/" + (state == "ongoing" ? "blue.png" : "orange.png");
  var marker = new GMarker(new GLatLng(project.lat, project.lon), {icon:marker_icon});
  
  Drupal.gmap.getMap('auto1map').map.addOverlay(marker);
  setMarkerClickEvent(marker, project);
  markers.push({marker:marker, nid:project.nid});
  
}

function setMarkerClickEvent(marker, project){
  
  GEvent.addListener(marker, "click", function(){

    // load data for project and show in infowindow
    this.openInfoWindowHtml("Loading...");
    
    jQuery.get(Drupal.settings.basePath + "casestudy/details/" + project.nid, function(data){
      marker.openInfoWindowHtml(data, {maxWidth:300});
    });
      
      
  });
  
}

/**
 * Show projects that have completion = state (all, ongoing, completed)
 */
function runFilter(state){
  
  // cycle through each ongoing project
  
  for(var i in Drupal.settings.projects.ongoing){
    
    var project = Drupal.settings.projects.ongoing[i];
    
    switch(state){
      case "all":
      case "ongoing":
        markerVisible(getMarker(project.nid), true);
        break;
      case "completed":
        markerVisible(getMarker(project.nid), false);
        break;
    }
      
  }
  
  // cycle through each completed project
  
  for(var i in Drupal.settings.projects.completed){
    
    var project = Drupal.settings.projects.completed[i];
    
    switch(state){
      case "all":
      case "completed":
        markerVisible(getMarker(project.nid), true);
        break;
      case "ongoing":
        markerVisible(getMarker(project.nid), false);
        break;
    }
      
  }
  
  // filter the project wall
  
  switch(state){
    case "all":
      yfcsp.clearFilter();
      break;
    case "completed":
      yfcsp.filterByComplete();  
      break;
    case "ongoing":
      yfcsp.filterByNotComplete();  
      break;
  }
  
}

function markerVisible(marker, visible){
  if(marker){
    if(visible == true) marker.show();
    else{
		marker.hide();
	}
  }
}

/**
 * Return the GMarker object for the project
 */
function getMarker(nid){

  for(var i in markers){
    
    if(markers[i].nid == nid){

      return markers[i].marker;
      
    }
    
  }
  
  return null;
  
}