/*****************************************************************************
 ** Callsign Object                                                         **
 *****************************************************************************/

/* ----------------------------------------------------------------------------
 * Name: callObject
 * Callsign object for tracking.
 * Parameters:
 *   c		: Callsign to set object to.
 * Returns: n/a
 */
function callObject(c) {
  this.name = c;
} // callObject::Constructor

/* ----------------------------------------------------------------------------
 * Name: Track
 * Track object for APRS station tracking.
 * Parameters:
 *   p		: Pointer to parent class.
 * Returns: n/a
 */
function Track(p) {
  this.on = false;
  this.loading = 0;
  this.running = false;
  this.parent = p;
  this.callMap = new Map();
  this.myPause = true;
} // Track::Constructor

/* ----------------------------------------------------------------------------
 * Name: Track::toggle
 * Toggles tracking functionality on or off.
 * Parameters:
 *   none
 * Returns: True if toggled on, false if toggled off.
 */
Track.prototype.toggle = function() {
  if (!this.isOn()) {
    showWindow('trackOverlay');
    showControl('controlTRACK', 'on');
    windowControl('trackOverlay');
    this.On();
    Drag.init(document.getElementById("trackHandle"), document.getElementById("trackOverlay"), null, null, null, null, true);
    changeById('OpenAPRS:Href:Track', '+ Hide Tracking List');
  } // if  
  else {
    hideWindow('trackOverlay');
    showControl('controlTRACK', 'off');
    this.Off();
    changeById('OpenAPRS:Href:Track', 'View Tracking List');
  } // else
  
  this.checkMiniImage();

  return this.isOn;
} // toggle

/* ----------------------------------------------------------------------------
 * Name: Track::togglePause
 * Toggles tracking pause functionality on or off.
 * Parameters:
 *   none
 * Returns: True if toggled on, false if toggled off.
 */
Track.prototype.togglePause = function() {
  if (this.isPaused())
    this.unpause();
  else
    this.pause();

  return this.isPaused();
} // togglePause

/* ----------------------------------------------------------------------------
 * Name: Track::checkMiniImage
 * Sets the mini icon for tracking.
 * Parameters:
 *   none
 * Returns: True if toggled on, false if toggled off.
 */
Track.prototype.checkMiniImage = function() {
    if (this.isPaused())
      changeById('miniTRACK',
                 '<img src="/images/mini/off/track.png" width="22" height="22" border="0" alt="Tracking List is Paused" />');
    else
      changeById('miniTRACK',
                 '<img src="/images/mini/on/track.png" width="22" height="22" border="0" alt="Tracking List is Active" />');

  return this.isPaused();
} // checkMiniImage

/* ----------------------------------------------------------------------------
 * Name: Track::getTrackList
 * Creates a list of callsigns to be tracked.
 * Parameters:
 *   none
 * Returns: Formatted string of callsigns to track.
 */
Track.prototype.getTrackList = function() {
  var keys = this.callMap.getKeys();
  var ret = "";
  for(var i in keys) {
    if (ret.length > 0)
      ret += ",";

    ret += keys[i];
  } // for

  return ret;
} // Track::getTrackList

/* ----------------------------------------------------------------------------
 * Name: Track::On
 * Turns on the tracking window and starts tracking callsigns.
 * Parameters:
 *   none
 * Returns: n/a
 */
Track.prototype.On = function() {
  if (this.on)
    return;

  //if (!this.running)
  //  this.updateTrackTable();

  this.running = true;

  this.on = true;
} // Track::On

/* ----------------------------------------------------------------------------
 * Name: Track::pause
 * Pauses current tracking efforts.
 * Parameters:
 *   none
 * Returns: n/a
 */
Track.prototype.pause = function() {

  if (this.myPause)
    return false;

  this.myPause = true;
  showControl('controlTRACKPAUSE', 'on');
  changeFieldById('Track:Window:Form:Toggle', 'ON');
  this.checkMiniImage();

  return true;
} // Track::pause

/* ----------------------------------------------------------------------------
 * Name: Track::unpause
 * Pauses current tracking efforts.
 * Parameters:
 *   none
 * Returns: n/a
 */
Track.prototype.unpause = function() {

  if (!this.myPause)
    return false;

  this.myPause = false;
  showControl('controlTRACKPAUSE', 'off');
  changeFieldById('Track:Window:Form:Toggle', 'OFF');
  this.checkMiniImage();
  this.parent.clear();
  this.refresh();

  return true;
} // Track::unpause

/* ----------------------------------------------------------------------------
 * Name: Track::isPaused
 * Returns the current state of this.pause boolean is in.
 * Parameters:
 *   none
 * Returns: True if paused, false if not.
 */
Track.prototype.isPaused = function() {
  return this.myPause;
} // Track::isPaused

/* ----------------------------------------------------------------------------
 * Name: Track::isOn
 * Returns current activation state of tracking system.
 * Parameters:
 *   none
 * Returns: True if on, false if off.
 */
Track.prototype.isOn = function() {
  return this.on;
} // Track::isOn

/* ----------------------------------------------------------------------------
 * Name: Track::Off
 * Turns off tracking system.
 * Parameters:
 *   none
 * Returns: True on success, false if already off.
 */
Track.prototype.Off = function() {
  if (!this.on)
    return false;

  this.on = false;

  return true;
} // Track::Off

/* ----------------------------------------------------------------------------
 * Name: Track::add
 * Adds a callsign to the tracking system.
 * Parameters:
 *   c		: Callsign to add to system.
 * Returns: Number of callsigns added.
 */
Track.prototype.add = function(c) {
  var num_rows = 0;

  if (c == undefined)
    return 0;

  var s = c.split(',');

  for(var i in s) {
    c = s[i].trim().toUpperCase();

    if (c.length < 1)
      continue;

    var co = new callObject(c);

    if (this.callMap.add(c, co))
      num_rows++;
  } // for

  this.updateTrackTable();
  changeById('trackSTATUS', 'Added '+num_rows +' callsign'+(num_rows == 0 || num_rows > 1 ? 's' : '')+'.');

  return true;
} // Track::add

/* ----------------------------------------------------------------------------
 * Name: Track::remove
 * Removes a station from the tracking list.
 * Parameters:
 *   c		: Callsign to remove from tracking list.
 * Returns: True if successful, false if not found.
 */
Track.prototype.remove = function(c) {
  if (c == undefined || c.length < 1)
    return false;

  if (!this.callMap.isKey(c))
    // Could not find.
    return false;

  this.callMap.remove(c);
  this.updateTrackTable();
  this.parent.removeAprs(c);

  changeById('trackSTATUS', 'Removed 1 callsign.');

  return true;
} // Track::remove

/* ----------------------------------------------------------------------------
 * Name: Track::updateTrackTable
 * Updates the current tracking table display.
 * Parameters:
 *   none
 * Returns: True if successful, false if not found.
 */
Track.prototype.updateTrackTable = function() {
  var num_rows = 0;			// Number of rows created.

  if (!this.callMap.length)
    return 0;

  clearRows('trackTable');

  var keys = this.callMap.getKeys();
  for(var i in keys) {
    var k = keys[i];
    var cellArray = [];   
    var img = '<img id="Track:Names:Image:'+k+'" src="/images/friends/offline.png" width="12" height="12" alt="'+k+' is Offline" border="0" />'
    var link = '<span id="Track:Names:Link:'+k+'" style="display: none"><a id="Track:Link:'+k+'" onMouseOut="UnTip()" onMouseOver="showBalloon(\'Snap Map to Location\', \'Moves the map to the location of this tracked station.\')" href="javascript:void(0)" onClick="gOpenAPRS.snapToAprs(\''+k+'\')">'+k+'</a></span>';
    var nolink = '<span id="Track:Names:Nolink:'+k+'">'+k+'</span>';
    var rem = '<a href="javascript:void(0)" onClick="gOpenAPRS.track.remove(\''+k+'\')"><img src="/images/friends/remove.png" width="12" height="12" border="0" alt="Remove '+k+'." /></a>'

    var aCell = new cellStruct;
    aCell.text = img;
    cellArray.push(aCell);

    var aCell = new cellStruct;
    aCell.text = nolink+link;
    cellArray.push(aCell);

    var aCell = new cellStruct;
    aCell.text = rem;
    cellArray.push(aCell);

    addRow2('trackTable', cellArray.reverse());

    num_rows++;
  } // for

  this.parent.checkTrackLinks();

  return num_rows;
} // Track::updateTrackTable

/* ----------------------------------------------------------------------------
 * Name: Track::refresh
 * Runs current refresh function stored on creation.
 * Parameters:
 *   none
 * Returns: n/a
 */
Track.prototype.refresh = function() {
  this.parent.getTrackLastpositionsFromServer(map);
} // Track::refresh

/* ----------------------------------------------------------------------------
 * Name: Track::star
 * Star a callsign.
 * Parameters:
 *   n          : Name of station to unstar.
 * Returns: n/a
 */
Track.prototype.star = function(n) {

  changeImageById('Track:Names:Image:'+n, '/images/stars/yellow.png');

  return true;
} // Track::star

/* ----------------------------------------------------------------------------
 * Name: Track::unstar
 * Unstar a callsign.
 * Parameters:
 *   n          : Name of station to unstar.
 * Returns: n/a
 */
Track.prototype.unstar = function(n) {

  changeImageById('Track:Names:Image:'+n, '/images/windows/offline.png');

  return true;
} // Track::unstar

/* ----------------------------------------------------------------------------
 * Name: Track::link
 * Unlink station name.
 * Parameters:   
 *   n          : Name of station to unlink.
 * Returns: n/a
 */
Track.prototype.link = function(n) {

  showWindow('Track:Names:Link:'+n);
  hideWindow('Track:Names:Nolink:'+n);

  return true;
} // Track::link

/* ----------------------------------------------------------------------------
 * Name: Track::unlink
 * Unlink station name.
 * Parameters:   
 *   n          : Name of station to unlink.
 * Returns: n/a
 */
Track.prototype.unlink = function(n) {

  hideWindow('Track:Names:Link:'+n);
  showWindow('Track:Names:Nolink:'+n);

  return true;
} // Track::unlink

/* ----------------------------------------------------------------------------
 * Name: Track::save
 * Unlink station name.
 * Parameters:   
 *   none
 * Returns: n/a
 */
Track.prototype.save = function() {
  var cl = this.getTrackList();

  if (cl.length < 1) {
    ereaseCookie("myTrack");
    changeById('trackSTATUS', 'Track list cleared.');
    log('Tracking: Tracking list cleared.');
    return false;
  } // if

  createCookie("myTrack", cl, 365);
  changeById('trackSTATUS', 'Track list saved.');
  log('Tracking: Tracking list saved.');

  return true;
} // Track::save

/* ----------------------------------------------------------------------------
 * Name: Track::restore
 * Restore tracking list.
 * Parameters:   
 *   none
 * Returns: n/a
 */
Track.prototype.restore = function() {
  var ret = readCookie("myTrack");

  if (ret < 1)
    return false;

  this.add(ret);

  changeById('trackSTATUS', 'Track list restored.');
  log('Tracking: Restored tracking list.');

  return true;
} // Track::restore

