var lightboxObjs = new Array();
function RefreshLightbox()
{
   var ihtml = "";
   var tFrame = document.getElementById("ilightbox");
   var doc = tFrame.contentDocument;
   if (doc == undefined || doc == null)
      doc = tFrame.contentWindow.document;
   doc.open();
   for (var i = 0; i < lightboxObjs.length; i++)
   {
      ihtml = ihtml + ("<center>" + "<img src='" + lightboxObjs[i].idsid + "'/>" + "</center><br/>");
   }   
   doc.write(ihtml);
   doc.close();
   document.getElementById("ilightbox_count").innerHTML = lightboxObjs.length;
   document.getElementById("ilightbox").style.visibility = "visible"; 
}
function GetLightbox(data)
{
   lightboxObjs = data;
   RefreshLightbox();
}
function ViewLightbox()
{
   document.forms['collectionsSearchFrm'].elements['viewlightbox'].value = "true";
   document.forms['collectionsSearchFrm'].submit();
   return false;
}
function findLightbox(objectNumber, renditionNumber)
{
   var found = false;
   for (var i = 0; i < lightboxObjs.length; i++)
   {
      if (lightboxObjs[i].objectNumber == objectNumber && lightboxObjs[i].renditionNumber)
      {
         found = true;
         break;
      }
   }
   return found;
}
function RepaintLightbox(data, el)
{
   if (data.objectNumber != "")
   {
      var op = data.objectNumber.substr(0, 1);
      data.objectNumber = data.objectNumber.slice(1);
      switch (op)
      {
         case "+" :
            lightboxObjs.push(data);
            el.innerHTML = "Remove from Lightbox";
            el.onclick = function() {
               return LightBox('remove', data, el);
            };
            break;
         case "-":
            for (var i = 0; i < lightboxObjs.length; i++)
            {
               if (lightboxObjs[i].objectNumber == data.objectNumber && lightboxObjs[i].renditionNumber == data.renditionNumber)
               {
                  break;
               }
            }
            lightboxObjs.splice(i, 1);
            el.innerHTML = "Add to Lightbox";
            el.onclick = function() {
               return LightBox('add', data, el);
            };
            break;
         default:
            ;
      }
      RefreshLightbox();

   } 
}
function EmailCallback(status)
{
   if (status)
   {
      alert("Email sent");
   }
   else
   {
      alert("Email invalid");
   }
}
function LightBox(action, artobject, el)
{
   var thisobj = el;
   var RepaintLightboxClosure = {
      callback : function(data) {
         RepaintLightbox(data, thisobj);
      }
   };
   switch (action) 
   { 
      case "add" : 
         HUAMLightBoxAjax.add(artobject, RepaintLightboxClosure); 
         break; 
      case "get" : 
         HUAMLightBoxAjax.get(GetLightbox); 
         break; 
      case "remove" : 
         HUAMLightBoxAjax.remove(artobject, RepaintLightboxClosure); 
         break; 
      case "clear" :
         HUAMLightBoxAjax.clear(GetLightbox);
         break;
      default : 
         ; 
   }
   return false;
}
function EmailLightbox(to)
{
   HUAMLightBoxAjax.email(to, EmailCallback);
   return false;
}
// RenderViewMoreImagesLink: Creates link "View More Images" on detail page once all images are loaded.
function RenderViewMoreImagesLink()
{
   var viewmore = $('viewmore');
   if (viewmore != null)
   {
      $(viewmore).innerHTML = "<a href=\"javascript:showAdditionalImages();\" title=\"View all available images and their captions\">View More Images</a>";
   }
}

function showClearLightbox()
{
   $('lightboxDialog').removeClass('noActive');
   $('lightboxDialog').setStyles({
      right: '336px',
      visibility: 'visible',
      top: '950px',
      opacity: '1'
   });

}

function closeLightboxDialog()
{
   $('lightboxDialog').addClass('noActive');
   $('lightboxDialog').setStyles({
      visibility: 'hidden',
      top: '-6500px',
      opacity: '0'
   });
}

function confirmClearLightbox()
{
   LightBox('clear');
   closeLightboxDialog();
}

function OnBodyLoad()
{
   RenderViewMoreImagesLink();
   LightBox('get');
}
