/* JC Jul 2010
 * Support routines to allow help links to be placed in images and divs of the Peachtree Learning Centre.
 *
 * To add the link text to an image or a div in the learning centre, add the addHelp attribute to the img or div tag.
 * The help link always appears above and to the right of the element it is added to. However, the author
 * may control the text that is rendered in the help message and some small positioning items.
 *
 * The addHelp attribute is a list of comma separated values in a set order:
 * 1/ The first value controls the help text ("image" - default or "video")
 * 2/ The second value controls the relative display of the help text ("up" or "down" - default)
 *
 * Examples are:
 * 1/ addHelp="image,down" (typical for most screen shots and the default)
 * 2/ addHelp="video,up" (typical for most videos so help can be read while video plays) 
 */

var HELP_TYPE = 0;
var HELP_POSITION = 1;

// JC Jul 10: Iterate the collection of all images looking for those to add the help link to
function f0_parseImgForHelp()
{
   var la_allImgTags = document.getElementsByTagName('img');
   for( li_loop=0; li_loop<la_allImgTags.length; li_loop++ )
   {
      if( la_allImgTags[li_loop].getAttribute('addHelp')!=null )
      { 
         f0_addHelpToImg(la_allImgTags[li_loop]);
         li_loop = li_loop + 2;  // Must increment by the 2 images thatt have just been inserted (prior) into the array
      }
   }

   var la_allDivTags = document.getElementsByTagName('div');
   for( li_loop=0; li_loop<la_allDivTags.length; li_loop++ )
   {
      if( la_allDivTags[li_loop].getAttribute('addHelp')!=null )
      { 
         f0_addHelpToImg(la_allDivTags[li_loop]);
         li_loop++;  // Must increment by the 1 div that has just been inserted (prior) into the array
      }
   }
}

// JC Jul 10: Add the parse function once the page is complete (DOM compliant)
if( window.addEventListener!=null )
{
   window.addEventListener('load', f0_parseImgForHelp, true );
}

// Add the parse function once the page is complete (MSIE)
else if( window.attachEvent!=null )
{
   window.attachEvent('onload', f0_parseImgForHelp );
}

// JC Jul 10: Add the help link to a given image or div
function f0_addHelpToImg(po_element)
{
   // Determine position
   var lb_down = (po_element.getAttribute("addHelp").toLowerCase().split(",")[HELP_POSITION]=="up")?false:true;

   // Create help image
   var lo_helpImage = document.createElement('img');
   lo_helpImage.src = '/resources/image/global/helpIcon.gif';
   lo_helpImage.alt ='Help icon';
   lo_helpImage.width = '12';
   lo_helpImage.height = '12';
   lo_helpImage.className = 'imgLeftNoTop';

   // Create help text
   var lo_helpTextNode = document.createTextNode('2011 looks different?'); 

   // Create help link
   var lo_helpLink = document.createElement('a');
   lo_helpLink.href = "#";
   lo_helpLink.onclick = function() { return fb_pchSwitchHelp(this) };
   lo_helpLink.setAttribute('helpId', 'open');
   lo_helpLink.appendChild(lo_helpImage);
   lo_helpLink.appendChild(lo_helpTextNode);

   // Create close image
   var lo_closeImage = document.createElement('img');
   lo_closeImage.src = '/resources/image/global/cancelIcon.gif';
   lo_closeImage.alt ='Close icon';
   lo_closeImage.width = '12';
   lo_closeImage.height = '12';
   lo_closeImage.className = 'imgRightNoTop';
   lo_closeImage.style.position = "absolute";
   lo_closeImage.style.top = "3px";
   lo_closeImage.style.right = "3px";

   // Create close link
   var lo_closeLink = document.createElement('a');
   lo_closeLink.href = "#";
   lo_closeLink.onclick = function() { return fb_pchSwitchHelp(this) };
   lo_closeLink.setAttribute('helpId', 'closeLink');
   lo_closeLink.appendChild(lo_closeImage);

   // Create help container
   var lo_helpContainerNode = document.createElement('div');
   lo_helpContainerNode.appendChild(lo_closeLink);
   lo_helpContainerNode.appendChild(lo_helpLink);
   lo_helpContainerNode.style.position = "absolute";
   lo_helpContainerNode.style.right = "0";
   lo_helpContainerNode.style.border = "2px dotted";
   lo_helpContainerNode.style.width = "155px";
   lo_helpContainerNode.style.height = "35px";
   lo_helpContainerNode.style.padding = "30px 5px 0 5px";
   lo_helpContainerNode.style.marginTop = (lb_down)?"-10px":"-70px";
   lo_helpContainerNode.setAttribute("addHelp",po_element.getAttribute("addHelp"));
   lo_helpContainerNode.className = "themeDark themeLightBg themeDarkBd screenOnly";
   po_element.parentNode.insertBefore(lo_helpContainerNode, po_element);

}

// JC Jul 10: Show or hide the help text on request
function fb_pchSwitchHelp(po_source)
{

   // This is the open switch
   if( po_source.getAttribute('helpId')=='open' )
   {
      // Determine if image or video and position
      var lb_image = (po_source.parentNode.getAttribute("addHelp").toLowerCase().split(",")[HELP_TYPE]=="video")?false:true;
      var lb_down = (po_source.parentNode.getAttribute("addHelp").toLowerCase().split(",")[HELP_POSITION]=="up")?false:true;

      // Create close image
      var lo_closeImage = document.createElement('img');
      lo_closeImage.src = '/resources/image/global/cancelIcon.gif';
      lo_closeImage.alt ='Close icon';
      lo_closeImage.width = '12';
      lo_closeImage.height = '12';
      lo_closeImage.className = 'imgRightNoTop';

      // Create close link
      var lo_closeLink = document.createElement('a');
      lo_closeLink.href = "#";
      lo_closeLink.onclick = function() { return fb_pchSwitchHelp(this) };
      lo_closeLink.setAttribute('helpId', 'closeHelp');
      lo_closeLink.appendChild(lo_closeImage);

      // Create help text
      var lo_helpText = 
         'This ' + ((lb_image)?'screen shot':'video') + ' was taken using the 2009 version of Peachtree Complete Accounting. ' +
         'If you are using the 2011 version, you may notice small differences in icons and colors or ' +
         'the positioning of minor screen elements and menu items. These differences are not material and do not ' +
         'significantly alter the way that you complete tasks using the software. You can still use this ' + 
         ((lb_image)?'image':'video') + ' to help you.';
      var lo_helpTextNode = document.createTextNode(lo_helpText);

      // Create help container
      var lo_helpContainerNode = document.createElement('div');
      lo_helpContainerNode.appendChild(lo_closeLink);
      lo_helpContainerNode.appendChild(lo_helpTextNode);
      lo_helpContainerNode.style.position = "absolute";
      lo_helpContainerNode.style.zIndex = "10";
      lo_helpContainerNode.style.right = "-2px";
      if( lb_down )
      {
         lo_helpContainerNode.style.top = "-2px";
      }
      else
      {
         lo_helpContainerNode.style.bottom = "-2px";
      }
      lo_helpContainerNode.style.border = "2px dotted";
      lo_helpContainerNode.style.width = "250px";
      lo_helpContainerNode.className = "themeDark themeLightBg themeDarkBd pppp screenOnly";
      po_source.parentNode.appendChild(lo_helpContainerNode);

   }

   // This is the close switch
   else 
   {
      po_source.parentNode.parentNode.removeChild(po_source.parentNode);
   }  

   // No return value
   return false;
}

