/*********************************************************************************************\
*       COPYRIGHT © 2009 ENVISION INFORMATION TECHNOLOGIES, LLC.    ALL RIGHTS RESERVED       *
*       DISTRIBUTION, UNAUTHORIZED USE AND MODIFICATION IS STRICTLY PROHIBITED                *
*       ENVISION IT, MADISON, WI    http://www.envisionitllc.com   info@envisionitllc.com     *
\*********************************************************************************************/

var displayedPlantID = null;

function getTop(srcObject)
{
  var topPosition = srcObject.offsetTop;
  var objectReference = srcObject;
  
  while (objectReference = objectReference.offsetParent)
  {
    topPosition += objectReference.offsetTop;
  };
  
  return topPosition;
}; // end getTop

function getLeft(srcObject)
{
  var leftPosition = srcObject.offsetLeft;
  var objectReference = srcObject;
  
  while (objectReference = objectReference.offsetParent)
  {
    leftPosition += objectReference.offsetLeft;
  };
  
  return leftPosition;
}; // end getLeft

function showPlant(id, link)
{
  if (displayedPlantID == id)
  {
    if (hideTimer != null)
    {
      clearTimeout(hideTimer);
      hideTimer = null;
    };
    
    return;
  }
  else if (displayedPlantID != null)
  {
    if (hideTimer != null)
    {
      clearTimeout(hideTimer);
      hideTimer = null;
    };
    
    reallyHidePlant();
  };
  
  var container = document.createElement('DIV');
  container.style.width = '500px';
  container.id = 'plantContainer';
  container.style.padding = '10px';
  
  if (allPlantsById[id]['image'] != '')
    container.innerHTML = "<img src='/encyclopedia/images/" + allPlantsById[id]['image'] + "' style='float:left;margin:0px 10px 10px 0px;' />";
  else
    container.innerHTML = "";
  
  container.innerHTML += "<div style='width:300px;color:white;font-size:15px;font-family:Times New Roman;float:left;'>" + allPlantsById[id]['description'];
  if (allPlantsById[id]['link'] != "")
  {
    if (allPlantsById[id]['linkname'] == '')
      allPlantsById[id]['linkname'] = 'Click here for more information';
    
    container.innerHTML += "<br/><a href='" + allPlantsById[id]['link'] + "'>" + allPlantsById[id]['linkname'] + "</a>";
  };
  
  container.innerHTML += "</div><div style='clear:both;'></div>";
  
  container.style.position = 'absolute';
  
  document.body.appendChild(container);
  
  container.style.top = (getTop(link) - container.offsetHeight - 15) + 'px';

  var contentLeft = getLeft(document.getElementById("content"));
  var lft = getLeft(link);
  if(lft < contentLeft + 350)
    container.style.left = lft + 'px';
  else
    container.style.left = lft - 400 + 'px';
  
  container.style.backgroundColor = '#758536';
  container.style.border = '5px solid #FFFFFF';
  
  container.onmouseover = function()
  {
    if (hideTimer != null)
      clearTimeout(hideTimer);
  };
  
  container.onmouseout = function()
  {
    hidePlant();
  };
  
  displayedPlantID = id;
};

var hideTimer = null;
function hidePlant()
{
  if (hideTimer != null)
    clearTimeout(hideTimer);
  
  hideTimer = setTimeout('reallyHidePlant()', 300);
};

function reallyHidePlant()
{
  document.body.removeChild(document.getElementById('plantContainer'));
  
  displayedPlantID = null;
  
  clearTimeout(hideTimer);
  hideTimer = null;
};

function processPlantBodyText()
{
  var allP = document.getElementsByTagName('P');
  var allLi = document.getElementsByTagName('LI');
  var allH1 = document.getElementsByTagName('H1');
  var allH2 = document.getElementsByTagName('H2');
  var allH3 = document.getElementsByTagName('H3');
  var allH4 = document.getElementsByTagName('H4');
  var allH5 = document.getElementsByTagName('H5');
  var allH6 = document.getElementsByTagName('H6');
  
  for (var a=0; a<allP.length; a++)
  {
    if (allP[a].innerHTML.toLowerCase().indexOf('<object') == -1)
      processPlantBodyTextElm(allP[a]);
  };
  
  for (var a=0; a<allLi.length; a++)
    processPlantBodyTextElm(allLi[a]);
  
  for (var a=0; a<allH1.length; a++)
    processPlantBodyTextElm(allH1[a]);
  
  for (var a=0; a<allH2.length; a++)
    processPlantBodyTextElm(allH2[a]);
  
  for (var a=0; a<allH3.length; a++)
    processPlantBodyTextElm(allH3[a]);
  
  for (var a=0; a<allH4.length; a++)
    processPlantBodyTextElm(allH4[a]);
  
  for (var a=0; a<allH5.length; a++)
    processPlantBodyTextElm(allH5[a]);
  
  for (var a=0; a<allH6.length; a++)
    processPlantBodyTextElm(allH6[a]);
  
  var end = true;
};

function processPlantBodyTextElm(elm)
{
  var content = elm.innerHTML.replace(/^\s+|\s+$/g,"");
  
  // array with the start position and end position of every piece of text on the site, not within HTML
  var starts = new Array();
  var ends = new Array();
  
  var inTag = false;
  var previouslyInTag = true;
  var strLength = 0;
  for (var a=0;a < content.length; a++)
  {
    if (inTag == false && content[a] == '<')
    {
      inTag = true;
      
      if (previouslyInTag == true)
        previouslyInTag = false;
      else if (strLength != 0)
        ends.push(a);
    }
    else if (inTag == true && content[a] == '>')
    {
      inTag = false;
      previouslyInTag = true;
    }
    else if (inTag == false)
    {
      if (previouslyInTag == true)
      {
        starts.push(a);
        previouslyInTag = false;
        strLength = 1;
      }
      else
        strLength += 1;
    };
  };
  
  if (starts.length > ends.length)
    ends.push(content.length);
  
  // make new content by combining everything we previously processed
  var newContent = '';
  
  if (starts.length == 0)
    newContent = content;
  
  for (var a=0; a < starts.length; a++)
  {
    if (a == 0 && starts[a] != 0)
      newContent += content.substr(0, starts[a]);
    
    // This is the text to check!
    var text = content.substr(starts[a], ends[a]-starts[a]);
    
    for (var b=0; b < highlightPlants.length; b++)
    {
      var textRegExp = new RegExp("((^| )" + allPlantsById[highlightPlants[b]]['name'] + ")", "gi");
      text = text.replace(textRegExp, "<span class='plantEncyclopediaWord' onmouseover='showPlant(" + highlightPlants[b] + ", this);' onmouseout='hidePlant();' >$1</span>");
    };
    
    newContent += text;
    
    if (a != starts.length-1)
      newContent += content.substr(ends[a], starts[a+1]-ends[a]);
    else
      newContent += content.substr(ends[a], content.length - ends[a]);
  };
  
  elm.innerHTML = newContent;
  var end = true;
};