 /******************************************************************************
 *
 *  Copyright 2008 David D. Emory [additional contributors append names here]
 *  
 *  This file is part of Five Points. See <http://www.fpdev.org> for
 *  additional project information and documentation.
 *  
 *  Five Points is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Five Points is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ******************************************************************************/    

/**
 * fp_util.js
 * 
 * Collection of general utility functions used through the 5P/web package.
 */

// FUNCTION DEFINITIONS

/*
 * fp_dbg(text)
 * 
 * Writes text to debugging output console.
 */
function fp_dbg(text) {
  //var dbg = document.getElementById("fp_debug");
  //dbg.innerHTML = text + "<br>"+dbg.innerHTML;
}

/*
 * fp_disableSelection(element)
 * 
 * Disables user selection abilities for the specified DOM element. Useful when
 * working with draggable onscreen widgets. Selection ability remains suspended
 * until fp_enableSelection() is called on the same element.
 */
function fp_disableSelection(element) {
  fp_dbg("disableSelection");
  element.onselectstart = function() {
    return false;
  };
  element.unselectable = "on";
  element.style.MozUserSelect = "none";
  element.style.cursor = "default";
}

/*
 * fp_enableSelection(element)
 * 
 * Enables user selection for the specified DOM element.
 */
function fp_enableSelection(element) {
  fp_dbg("enableSelection");
  element.onselectstart = "";
  element.unselectable = "off";
  element.style.MozUserSelect = "";
  element.style.cursor = "";
}

/**
 * fp_distance(x1, y1, x2, y2)
 * 
 * Standard Cartesian distance computation. All parameters are Numbers.
 */
function fp_distance(x1, y1, x2, y2) {
  return Math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
}

/**
 * fp_distToSegment(px, py, x1, y1, x2, y2)
 * 
 * Computes the shortest distance from a point to a segment. All parameters
 * are Numbers.
 */
function fp_distToSegment(px, py, x1, y1, x2, y2) {
  var r, dx ,dy;
  dx = x2 - x1;
  dy = y2 - y1;
  r = ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy);
  return fp_distance(px, py, (1 - r) * x1 + r * x2, (1 - r) * y1 + r * y2);
}



/**
 * fp_sTimeToStr(stime)
 * 
 * Converts a numeric 5P seconds-after-4AM time to a standard readable string.
 * (e.g. 3660 -> "5:01a")
 */
function fp_sTimeToStr(stime) {
	
  var tmin = stime/60;
  tmin = Math.floor(tmin);
  var hour = tmin/60 % 24;
  hour = Math.floor(hour);
  var min = tmin % 60;
  var ampm = "";
  if(hour <= 7) {
    hour += 4;
    ampm = "a";
  }
  else if(hour == 8) {
    hour = 12;
    ampm = "p";
  }
  else if(hour > 8 && hour <= 19) {
    hour -= 8;
    ampm = "p";
  }
  else if(hour == 20) {
    hour = 12;
    ampm = "a";
  }
  else {
    hour -= 20;
    ampm = "a";
  }
  
  return hour+":"+(min<10 ? "0" : "")+min+ampm;	
} 

/**
 * fp_elapsedTimeStr(sec)
 * 
 * Converts a numeric measure of seconds elapsed to readable "[hr,] min" format. 
 * (e.g. 300 -> "5 min"; 3660 -> "1 hr, 1 min")
 */
function fp_elapsedTimeStr(sec) {
  var tmin = Math.floor(sec/60);
  var hour = Math.floor(tmin/60);
  var min = tmin % 60;
  if(hour>0) return hour+" hr, "+min+" min";
  return min + " min";
}




