// utilities.js
// Copyright (C) 2008 University of California - San Diego

/**
 * Date extension to replace the way UTC strings are displayed.
 */
Date.prototype.toUTCString = function() {
  var Y = this.getUTCFullYear();
  var m = this.getUTCMonth()+1;
  if(m < 10) { m = "0" + m; }

  var d = this.getUTCDate();
  if(d < 10) { d = "0" + d; }

  var H = this.getUTCHours();
  if(H < 10) { H = "0" + H; }

  var M = this.getUTCMinutes();
  if(M < 10) { M = "0" + M; }

  var S = this.getUTCSeconds();
  if(S < 10) { S = "0" + S; }

  var Z = "UTC";

  return (Y+"-"+m+"-"+d+" "+H+":"+M+":"+S+" "+Z);
}


/**
 * Utility function to determin if param exists in array
 */
function in_array(p,a) { 
  for(var k in a) { 
    if(a[k] == p) { 
      return true;
    }
  }
  return false;
}


/*
 * Returns value of URL parameter, null if not found.
 */
function get_url_parameter(r) {
  // querystring
  var q = location.search.substring(1);
  // parameters
  var p = q.split("&");
  // loop through parameters
  for(var i=0,n=p.length; i<n; i++) {
    // key=value
    var s = p[i].split("=");
    // if key == requested key
    if( s[0] == r ) {
      // return value
      return s[1];
    }
  }
  return null;
}





/**
 * SOLO Specific Utilities
 */

function createMenuItem(floatEl) {
  var fid  = floatEl.getAttribute("id");
  var text = ' SOLO_'+fid;

  var a = document.createElement("a");
  a.setAttribute("href","float.php?id="+fid);
  a.appendChild(document.createTextNode(text));

  var li = document.createElement("li");
  li.appendChild(a);
  return li;
}

function createStormMenuItem(stormEl) {
  var sid  = stormEl.getAttribute("id");
  var text = sid;

  var a = document.createElement("a");
  a.setAttribute("href","storm.php?sid="+sid);
  a.appendChild(document.createTextNode(text));

  var li = document.createElement("li");
  li.appendChild(a);
  return li;
}



function init_floats_menu() {
  var menu = document.getElementById("solo_rimpac_2008_menu");
  if( !menu ) {
    return false;
  }
  menu.innerHTML = '';

  if( !cached["listFloats"] ) { 
    alert("Cached listFloats not found -- listen for listFloatsLoaded");
    return false;
  }

  var dom = GXml.parse(cached["listFloats"]);
  var floats = dom.getElementsByTagName("float");
  for(var i=0,n=floats.length;i<n;i++) {
    var item = createMenuItem(floats[i]);
    if( !item ) {
      continue;
    }
    menu.appendChild(item);
  }
}


function init_storms_menu() {
  var menu = document.getElementById("storms_rimpac_2008_menu");
  if( !menu ) {
    return false;
  }
  menu.innerHTML = '';

  if( !cached["listStorms"] ) {
    alert("Cached listStorms not found -- listen for listStormsLoaded");
    return false;
  }

  var dom = GXml.parse(cached["listStorms"]);
  var storms = dom.getElementsByTagName("storm");
  for(var i=0,n=storms.length;i<n;i++) {
    var item = createStormMenuItem(storms[i]);
    if( !item ) {
      continue;
    }
    menu.appendChild(item);
  }
}


