/**
 * \file GenUtilities.js
 *
 * Bunch of generic utilities coalesced here.
 */

 /**
  * ChangeText - this is used in the article/directory lists to
  * swap the text in the side window.
  */
 function ChangeText(parentID, itemID)
 {
   var parentElement = document.getElementById(parentID);

   if (parentElement)
   {
     var nodes = parentElement.getElementsByTagName('DIV');
     var ix;
     for (ix = 0; ix < nodes.length; ix++)
       if (nodes[ix].id)
       {
         if (nodes[ix].id == itemID)
           nodes[ix].style.display = 'block';
         else
           nodes[ix].style.display = 'none';
       }
   }
 }

 /**
  * An issue with browsers - The popular browsers parent the
  * <head> and <body> sections under an HTMLEntity object (underneath
  * the document.)  *That* object contains the scroll position.
  *
  * Other browsers still carry it in the body.
  *
  * So, we just take the max of the two, assuming the browser is only
  * going to set one.
  * 
  * Avoids having to check for user agents (and getting it wrong).
  */
 function DocTop()
 {
   return (document.body.parentNode.scrollTop > document.body.scrollTop)?
     document.body.parentNode.scrollTop : document.body.scrollTop;
 }

 function SetDocTop(val)
 {
   window.scroll(0, val);
 }

 function ShowPicOrSB(url)
 {
   if (url.match('TOPLOC'))
     url = url.replace(/TOPLOC/, 'x' + DocTop());
   window.location = url;
 }

 function ShowImageLinkChoice(tEvent, articleURL, galleryURL)
 {
   var picQueryDiv = document.getElementById('PicQueryDiv');
   if (!picQueryDiv)
   {
     picQueryDiv = document.createElement('div');
     picQueryDiv.className = 'navwindow';
     picQueryDiv.id = 'PicQueryDiv';
     picQueryDiv.style.fontFamily = 'arial,helvetica,sans-serif';
     document.body.appendChild(picQueryDiv);
   }

   var divText =
'<div class="navtitle">Take me to:</div>' +
'<ul style="font-size:13px;">' +
'<li style="line-height:1.5em;">' +
  ((articleURL == '')? '(Image not in article)' :
     'The <a href="' + articleURL + '"><em>ARTICLE</em></a>') +
'</li>' +
'<li style="line-height:1.5em;">The <a href="' + galleryURL + '"><em>GALLERY</em></a></li>' +
'</ul>' +
'<div class="small">...that contains this image</div>' +
'<div class="small" style="text-align:right;cursor:pointer;margin-bottom:5px;" onclick="this.parentNode.style.display=\'none\'"><a>(cancel)</a></div>';

   picQueryDiv.innerHTML = divText;
   picQueryDiv.style.display = 'block';
   PositionObject(picQueryDiv, tEvent);
 }

 //
 // position the object to the event x and y.
 // browser dependent.
 //
 function PositionObject(theObject, tEvent)
 {
   var x;
   var y;

   if (window.event)
   {
     x = event.clientX;
     y = event.clientY + DocTop();
   }
   else
   {
     x = tEvent.pageX;
     y = tEvent.pageY;
   }

   theObject.style.left = (x - 
    ((x < theObject.offsetWidth + 50)? 0 : theObject.offsetWidth)) + 'px';
   theObject.style.top = (y -
    ((y < theObject.offsetHeight + 50)? 0 : theObject.offsetHeight)) + 'px';
 }

 function ShowOrHideDescription(event,img)
 {
   descriptionDiv = document.getElementById('indexDescriptionID');
   textspan = img.parentNode.getElementsByTagName('span');
   if (img.src.match('plus.gif'))
   {
     img.src = '/images/minus.gif';
     textspan[0].innerHTML = 'Hide description';
     descriptionDiv.style.display = 'block';
   }

   else
   {
     img.src = '/images/plus.gif';
     textspan[0].innerHTML = 'Show description';
     descriptionDiv.style.display = 'none';
   }
 }

/*
 function DebugOut(item, desc)
 {
   var p = document.createElement('p');
   p.style.marginLeft = '330px';
   p.innerHTML = item + ': ' + desc;
   document.body.appendChild(p);
 }
*/
