/**
 *  default functions for onetouchdiabetes.com
 */

/**
 *  Initialized explicitly.
 */
window.hasFlash = false;

window.onunload = function ()
{
  hideAll();
}

function doMouseEvent(id, img)
{
  var targetImg;

  if (document.getElementById) {
    targetImg = document.getElementById(id);
  }
  else if (document.all) {
    targetImg = document.all(id);
  }

  targetImg.src = img.src;
}

function doPopup(imgId, newImg, elId)
{
  var targetImg = document.getElementById(imgId);

  stopHideAllTimer();
  hideAll();

  targetImg.src = newImg.src;
  showPopup(elId);
}

/**
 *  Hides all popup divs. Uses class attr to determine which divs to hide. Also
 *  changes 'monitor' nav image to out state.
 *
 *  @type   void
 */
function hideAll()
{
  var elements;         // list of divs to hide
  var navImg;           // sole image to change
  var imgId = "mon_img" // id of that image

  if (document.getElementById) {
    elements = document.getElementsByTagName("div");
    navImg = document.getElementById(imgId);
  }
  else if (document.all) {
    elements = document.all.tags("div");
    navImg = document.images(imgId);
  }

  if (elements) {
    for (var i = 0; i < elements.length; i++) {
      if (elements.item(i).className == "popup_wrapper") {
        hidePopup(elements.item(i).id);
      }
    }
  }

  if (navImg && navImg.className != "no_out") {
    navImg.src = navbarMonOut.src;
  }
}

/**
 *  Global vars for timer functions
 */
var timerID = null;
var timecount = 200;  // milleseconds

/**
 *  Starts a timer. Calls hideAll() at end of time.
 *
 *  @type   void
 */
function startHideAllTimer()
{
  if (!timerID) {
    timerID = setTimeout("hideAll()", timecount);
  }
}

/**
 *  Stops the timer---hideAll() is not called.
 *
 *  @type   void
 */
function stopHideAllTimer()
{
  if (timerID) {
    clearTimeout(timerID);
    timerID = null;
  }
}

/**
 *  Makes element got by id visible.
 *
 *  @param  id
 *  @type   void
 */
function showPopup(id) {
  if (document.getElementById) {
    document.getElementById(id).style.visibility = "visible";
  }
  else if (document.all) {
    document.all[id].style.visibility = "visible";
  }
}

/**
 *  Makes element got by id hidden.
 *
 *  @param  id
 *  @type   void
 */
function hidePopup(id)
{
  if (document.getElementById) {
    document.getElementById(id).style.visibility = "hidden";
  }
  else if (document.all) {
    document.all[id].style.visibility = "hidden";
  }
}

/**
 *  Parses the get request string for the requested key.
 *
 *  @param  {String}  key   key value to search
 *  @return If key exists then its value else blank string
 *  @type   {String}
 */
function parseGet(key)
{
  var getArgs = location.search.substr(1).split("&");
  var arg;

  for (var i = 0; i < getArgs.length; i++) {
    try {
      arg = getArgs[i].split("=");
      
      if (arg[0].indexOf(key) != -1) {
        return arg[1];
      }
    }
    catch (e) {}
  }

  return "";
}

function swapImg(id, img)
{
  if (document.getElementById) {
    document.getElementById(id).src = img.src;
  }
  else if (document.all) {
    document.all[id].src = img.src;
  }
}

