/*
 * Mazama_databrowser.js
 *
 * Jonathan Callahan
 * http://mazamascience.com
 *
 * Default behavior for Mazama Science databrowsers.
 *
 * 1) Any change to an element in the form should trigger a plot request so that the current state
 *    of the UI always reflects the parameters used to generate the plot.
 * 2) Each successful response contains a url for the plot and one for the pdf version of the plot.
 *
 */


function chooseMineral() {
  if (compatibleVariables()) {
    sendRequest();
  } else {
    var mineral = $('#subset').val();
    alert("No usage data for " + mineral + ".\nPlease select another mineral.\n\nHint:  minerals with a '*' have no usage data");
  }
}

function choosePlottype() {
  if (compatibleVariables()) {
    sendRequest();
  } else {
    var mineral = $('#subset').val();
    alert("No usage data for " + mineral + ".\nPlease select a non-Usage plot.\n\nHint:  minerals with a '*' have no usage data");
  }
}

function compatibleVariables() {
  var mineral = $('#subset').val();
  var plottype = $('#plottype').val();
  if ( plottype in { 'Usage':1, 'UsagePie':1 } ) {
    if ( mineral in { 'barite':1, 'boron':1, 'bromine':1, 'cement':1, 'cesium':1, 'clay':1, 'gypsum':1, 'hafnium':1,
                      'iodine':1, 'ironsteelscrap':1, 'ironsteelslag':1, 'kyanite':1, 'lithium':1, 'mercury':1,
                      'platinum':1, 'potash':1, 'quartzcrystal':1, 'rareearths':1, 'rhenium':1, 'sandgravelindustrial':1,
                      'sodaash':1, 'sodiumsulfate':1, 'stonedimension':1, 'thallium':1, 'thorium':1, 'titaniummineral':1,
                      'vermiculite':1, 'wollastonite':1, 'zirconium':1 } ) {
      return false;
    } else {
      return true;
    }
  } else {
    return true;
  }
}

////////////////////////////////////////////////////////////////////////////////

function handleJSONResponse(JSONResponse) {
// TODO:  check the status for possible errors
  $('#spinner').hide();
  if (JSONResponse.status == 'ERROR') {
    var newContent = '<p class="error_text">' + JSONResponse.error_text + '</p>';
    $('#invitation').children().replaceWith(newContent);
  } else {
    // Always reset to erase any error messages
    // NOTE:  We might only do this if an error is present but it works fine this way.
    var newContent = '<h1>Explore the US Minerals dataset (<a rel="external" href="http://minerals.usgs.gov/ds/2005/140/">USGS DS140</a>)</h1>';
    $('#invitation').children().replaceWith(newContent);
    $('#plot').attr('src',JSONResponse.img_url);
    $('#pdf').attr('href',JSONResponse.pdf_url);
  }
}

function sendRequest() {
  $('#spinner').fadeIn(4000);
  var url = '/cgi-bin/USGS.cgi';
  data = $('#controls_form').serialize();
  $.getJSON(url, data, handleJSONResponse);
}

function sendRequestNoSpinner() {
  var url = '/cgi-bin/USGS.cgi';
  data = $('#controls_form').serialize();
  $.getJSON(url, data, handleJSONResponse);
}

/***********************************************************
 * Initialization
 */
$(function() {
  $('#spinner').hide();
//  TODO:  Restore other selectors when they are used.
  $('#subset').bind('change',chooseMineral);
  $('#plottype').bind('change',choosePlottype);
//  $('#xvar').bind('change',sendRequest);
//  $('#yvar').bind('change',sendRequest);
//  $('#zvar').bind('change',sendRequest);
//  $('#zval').bind('change',sendRequest);

  // Plot Options
  $('#plotsize300').bind('click',sendRequest); // For checkboxes
  $('#plotsize500').bind('click',sendRequest); // For checkboxes
  $('#plotsize600').bind('click',sendRequest); // For checkboxes
  $('#plotsize001').bind('click',sendRequest); // For checkboxes

  // Set up tooltips
  $('span.tooltip').cluetip({width: '500px', attribute: 'id', hoverClass: 'highlight'});
});

