
// This formats the grey boxes to all be the same size.  Unfortunately
// it must run on window.onload() to work.  (Actually, it just has to run
// at the very end of the document, but there's no clean way to insert the
// code there with this framework).

if(window.addEventListener)
{
   window.addEventListener("load", formatThumbBoxes, false);
}
else if(window.attachEvent)
{
   window.attachEvent("onload", formatThumbBoxes);
}

// This function formats the grey boxes that the thumbs are displayed in
// to all be the same height.  It must be called after the document is rendered.
function formatThumbBoxes()
{
   var img;
   var imgs = document.getElementsByTagName('img');
   var maxHeight = 0, maxWidth = 0;
   for (var n = 0; n < imgs.length; n++)
   {
      img = imgs[n];
      if (img.src != null && img.src.toLowerCase().indexOf("sales_image.php") > -1)
      {
         if (img.clientWidth > maxWidth)
            maxWidth = img.clientWidth;
         if (img.clientHeight > maxHeight)
            maxHeight = img.clientHeight;
      }
   }
   for (var n = 0; n < imgs.length; n++)
   {
      img = imgs[n];
      if (img.src != null && img.src.toLowerCase().indexOf("sales_image.php") > -1)
      {
         // These work on IE but not Safari for Windows.
         //img.parentElement.parentElement.parentElement.style.height = maxHeight;
         //img.parentElement.parentElement.style.width = maxWidth;
         // These work on both.
         img.parentNode.parentNode.setAttribute("height", maxHeight + 5);
         img.parentNode.parentNode.setAttribute("width", maxWidth + 5);
      }
   }
}

function getRequestObject()
{
   if (window.XMLHttpRequest)
      return new XMLHttpRequest();
   else if (window.ActiveXObject)
      return new ActiveXObject("Microsoft.XMLHTTP");
}

function reqSelectCB()
{
   if (4 == reqSelect.readyState)
   {
      if(Handle302(reqSelect))
      {
         return;
      }
      if (200 == reqSelect.status || null == reqSelect.status)
      {
         var rsp = reqSelect.responseText;
         var ok = 'Success: ';
         if (rsp.substring(0, ok.length) == ok)
         {
            var results = rsp.substring(ok.length).split('|');
            var count = parseInt(results[0]);
            setSelectCount(count);
            if (1 == count && 3 == results.length)
               setSelection(results[1], results[2]);
         }
         else
         alert(rsp);
      }
      else if (399 < reqSelect.status)
         alert('HTTP error ' + reqSelect.status + ': ' + reqSelect.statusText);
   }
}

function onSelectOne(id, isSelected)
{
   reqSelect = getRequestObject();
   reqSelect.onreadystatechange = reqSelectCB;
   reqSelect.open("POST", reqSelectUrl + "&id=" + id + "&select=" + isSelected, true);
   reqSelect.send(null);
}

function onSelectAll(isSelected)
{
   reqSelect = getRequestObject();
   reqSelect.onreadystatechange = reqSelectCB;
   reqSelect.open("POST", reqSelectUrl + "&id=all&select=" + isSelected, true);
   reqSelect.send(null);

   tot = document.formu.elements.length;
   for (i = 0; i < tot; ++i)
   {
      if(document.formu.elements[i].type == 'checkbox' &&
       'ckimage' == document.formu.elements[i].name.substring(0, 7))
      {
         if ( true == isSelected )
            document.formu.elements[i].checked = true;
         else
            document.formu.elements[i].checked = false;
      }
   }
}

function goToPage(pageSel, offset)
{
   if (pageSel.options.length < 1)
      return;

   var oldIndex = pageSel.selectedIndex;
   var newIndex = pageSel.selectedIndex + offset;
   if (newIndex < 0)
      pageSel.selectedIndex = 0;
   else if (newIndex > ( pageSel.options.length - 1) )
      pageSel.selectedIndex = pageSel.options.length - 1;
   else
      pageSel.selectedIndex = newIndex;

   if (0 == offset || pageSel.selectedIndex != oldIndex)
   {
      newSearch = 'lbx=' + lbxId
       + GetSearchPageParameters(true) + '&page=' + pageSel.options[pageSel.selectedIndex].value
       + paginationSearchParameters;
      this.location.search = newSearch;
   }
}

function changeRowCount ()
{
   var value = "";
   var cname = "IssRowCount";
   var options = document.getElementById('rowcount').options;

   for (var i = 0; i < options.length; i++)
   {
      if (options[i].selected)
      {
         value = options[i].value;
         break;
      }
   }

   if (rowCount != value)
   {
      rowCount = value;
      if (readCookie(cname) != null)
      {
         eraseCookie(cname);
      }

      createCookie(cname, rowCount, 7);
      var pageSel = document.getElementById('pageSel');
      this.location.search = 'lbx=' + lbxId
       + GetSearchPageParameters(true) + '&page=' + pageSel.options[pageSel.selectedIndex].value
       + paginationSearchParameters;
   }
}

function changePageCount ()
{
   var value = "";
   var cname = "IssPageCount";
   var options = document.getElementById('pagecount').options;

   for (var i = 0; i < options.length; i++)
   {
      if (options[i].selected)
      {
         value = options[i].value;
         break;
      }
   }

   if (pageCount != value)
   {
      pageCount = value;
      if (readCookie(cname) != null)
      {
         eraseCookie(cname);
      }

      createCookie(cname, pageCount, 7);
      var pageSel = document.getElementById('pageSel');
      this.location.search = 'lbx=' + lbxId
       + GetSearchPageParameters(true) + '&page=' + pageSel.options[pageSel.selectedIndex].value
       + paginationSearchParameters;
   }
}


//added alockett to handle animation of button panel
var buttonTimerIds = [];
var buttonTimers = [];

if(buttonTimerIds.indexOf == null)
{
   buttonTimerIds.indexOf = indexOf;
}

function openButtonPanel(id)
{
   var index = buttonTimerIds.indexOf(id);
   var panel = document.getElementById(id);

   if(panel != null)
   {
      if (index < 0 || index >= buttonTimerIds.length)
      {
         index = buttonTimerIds.length;
         buttonTimerIds.push(id);
         buttonTimers.push(0);
      }
      clearTimeout(buttonTimers[index]);
      animateButtonPanelOpen(panel, index, 1);
   }
}

function closeButtonPanel(id)
{
   var index = buttonTimerIds.indexOf(id);
   var panel = document.getElementById(id);
   if(panel != null)
   {
      if (index < 0 || index >= buttonTimerIds.length)
      {
         index = buttonTimerIds.length;
         buttonTimerIds.push(id);
         buttonTimers.push(0);
      }
      clearTimeout(buttonTimers[index]);
      animateButtonPanelClose(panel, index, 1);
   }
}

function animateButtonPanelOpen(panel, index, time)
{
   var height = parseInt(panel.style.height.split('px')[0]);
   if(height < 30)
   {
      var newHeight = Math.round(height + 4 - .25*time);
      if(newHeight <= height) { newHeight = height + 1; }
      if(newHeight > 30) { newHeight = 30; }
      panel.style.height = newHeight + 'px';
      panel.style.bottom = newHeight + 'px';
      panel.style.marginBottom= '-' + newHeight + 'px';
      if(newHeight < 30)
      {
         buttonTimers[index] = setTimeout(function() {animateButtonPanelOpen(panel, index, time + 1);}, 20);
      }
   }
}

function animateButtonPanelClose(panel, index, time)
{
   var height = parseInt(panel.style.height.split('px')[0]);
   if(height > 0)
   {
      var newHeight = Math.round(height - 4 + .25*time);
      if(newHeight >= height) { newHeight = height - 1; }
      if(newHeight < 0) { newHeight = 0; }
      panel.style.height = newHeight + 'px';
      panel.style.bottom = newHeight + 'px';
      panel.style.marginBottom = '-' + newHeight + 'px';
      if(newHeight > 0)
      {
         buttonTimers[index] = setTimeout(function() {animateButtonPanelClose(panel, index, time + 1);}, 20);
      }
   }
}

function indexOf(obj)
{
   for(var i = 0;i < this.length; ++i)
   {
      if(this[i] == obj)
      {
         return i;
      }
   }
   return -1;
}

//added alockett to handle display of image info panel
function ShowImageMetadata(id)
{
   var elemId = "metadata_" + id;
   var elem = document.getElementById(elemId);
   if(elem != null)
   {
      elem.style.visibility = "visible";
   }
}

function HideImageMetadata(id)
{ 
   var elemId = "metadata_" + id;
   var elem = document.getElementById(elemId);
   if(elem != null)
   {
      elem.style.visibility = "hidden";
   }
}

