//----------------------------------
//- lib.js
//-
//- DOMFS Support Routines
//-   $(ref)                   Returns a DOM element reference given a ref
//-   $append(ref, buf)      * Appends buf to the innerHTML property of a given ref
//-   $delete(ref)             Removes the DOM element of a given ref
//-   $read(ref)               Returns the innerHTML property of a given ref
//-   $write(ref, buf)       * Writes buf to the innerHTML property of a given ref
//-
//-   * If a DIV with ID="StoreArea" exists in the current HTML document, this function will 
//-     autocreate a DIV (within StoreArea) if the passed reference or ID string does not
//-     already exist.
//-
//- External Module Loaders
//-   loadNewScript(url, id)   Loads a new script from url with optional id DOM identifier
//-   loadNewStyle(url, id)    (NOT IMPLEMENTED)
//-
//- Tagging Routines
//-   $getTags(ref)            Returns a list of (CSS class) tags associated with ref
//-   $hasTag(ref, tag)        Returns true if ref has the specified (CSS class) tag
//-   $setTag(ref, tag)        Sets a (CSS class) tag on the associated ref
//-
//----------------------------------


//----------------------------------
//- DOMFS Support Routines
//----------------------------------
function $(ref) {
  // Return ref if it's an HTML element.
  if (ref.nodeType) {
    return ref;

  } else { // Otherwise check to see if an element named by 'ref' exists...
    var tmpEl = document.getElementById(ref+'');
    if (tmpEl) {
      return tmpEl; // ... Returning it if so.
    }
  }

  return undefined; // Return undefined in any other case.
}

function $read(ref) {
  // Read the contents of an HTML element
  var el = $(ref);
  if (el) {
    return el.innerHTML;
  }
}

function $write(ref, buf) {
  // Write the contents of an HTML element
  if (! buf) { return; }

  var el = $(ref);
  if (el) {
    el.innerHTML = buf;

  } else { // Auto-create if element doesn't exist
    el = document.createElement('div');
    el.id = ref+'';
    el.style.display = 'none';
    $('StoreArea').appendChild(el);
    el.innerHTML = buf;
  }
}

function $append(ref, buf) {
  // Append to the contents of an HTML element
  if (! buf) { return; }

  var el = $(ref);
  if (el) {
    el.innerHTML += buf;

  } else { // Auto-create if element doesn't exist
    el = document.createElement('div');
    el.id = ref+'';
    el.style.display = 'none';
    $('StoreArea').appendChild(el);
    el.innerHTML = buf;
  }
}

function $delete(ref) {
  var el = $(ref);
  if (el) {
    var parentEl = el.parentNode;
    if (parentEl) { parentEl.removeChild(el); }
  }
}

//----------------------------------
//- External Module Loaders
//----------------------------------

function loadNewScript(url, id) {
  if (log) {
    log('loadNewScript("' + url + '", "' + id + '") called!<br/>');
    log('loadNewScript(): creating dom script element.<br/>');
  }
  var elScript = document.createElement('script');

  if (log) {
    log('loadNewScript(): setting ID and SRC properties.<br/>');
  }
  elScript.id = (id) ? id : 'CODE_' + (++System.Counters.Scripts);
  elScript.src = url;

  if (log) {
    log('loadNewScript(): appending element to dom document<br/>');
  }
  document.getElementsByTagName('head')[0].appendChild(elScript);

  if (log) { log('loadNewScript(): finished.<p/>'); }
}

function loadNewStyle(url, id) {
}


//----------------------------------
//- Tagging Routines
//----------------------------------
function $getTags(ref) {
  var el = $(ref);
  if (el) {
    var tags = el.className.split(/ /);
    return tags;
  }
}

function $hasTag(ref, tag) {
  var el = $(ref);
  if (el) {
    var tags = $getTags(el);
    for (var i=0; i<tags.length; i++) {
      if (tags[i] === tag) { return true; }
    }
  }
  return false;
}

function $setTag(ref, tag) {
  var el = $(ref);
  if (el) {
    el.className += ' ' + tag;
    el.className = el.className.replace(/^ /, '');
  }
}


//----------------------------------
//- 
//----------------------------------

