Event.observe(window, 'load', function(evt) {
  loadLightBoxButtons();
})

function setLightBox(type) {
  setLightBoxExt(type, true);
}

function setLightBoxExt(type, shouldRegister) {
  //generic function to set up button behaviors that lead to lightboxes opening
  if ($('book-' + type))
  {
    Event.observe($('book-' + type), 'click', 
      function(evt) {
        openLightBox(type, shouldRegister);
        Event.stop(evt);
      });
  }
  
}

function openLightBox(type, shouldRegister, customTitle, customMessage, redirect) {
  // TODO: we need a queue mechanism on lighboxes
  if (isLightBoxOpened()) // ignore other lightboxes.
    return;
  clearLightBox();  //close any open lightboxes

  //figure out which behaviors to associate with which elements
  var isUser = isLoggedIn();
  var lightbox;
  //make sure any contextual lightboxes have the proper context applied
  var actionNames = document.getElementsByClassName('action-name');
  for (var i=0; i<actionNames.length; i++) {
    var strName = type.substring(0,1).toUpperCase() + type.substr(1);
    actionNames[i].innerHTML = strName;
  }

  if (isUser || !shouldRegister) {

    switch(type) {
      case 'save':
        setBlockStatus('Saved');
        window.globalAjaxListener.onSaveBlockMetaDataSuccess = function() {
          window.globalAjaxListener.onSaveBlockMetaDataSuccess = null;
          window.location.href = '/blocks/edit/saved?block-id='+blockId;
        }
        return;
      case 'publish':
        openLightBox_NEW('wait', 'Please Wait', 'Your block is being published now');
        publishBlock();
        window.globalAjaxListener.onPublishBlock = function() {
          window.globalAjaxListener.onPublishBlockSuccess = null;
          window.location.href = '/blocks/edit/publish?block-id='+blockId;
        }
        return;
      case 'force-publish':
        openLightBox_NEW('wait', 'Please Wait', 'Your block is being published now');
        publishBlock(true);
        window.globalAjaxListener.onPublishBlock = function() {
          window.globalAjaxListener.onPublishBlockSuccess = null;
          window.location.href = '/blocks/edit/publish?block-id='+blockId;
        }
        return;

      case 'done':
        setBlockStatus('Saved_Complete');
        window.globalAjaxListener.onSaveBlockMetaDataSuccess = function() {
          window.globalAjaxListener.onSaveBlockMetaDataSuccess = null;
          window.location.href = '/blocks/edit/publish?block-id='+blockId;
        }
        return;
      default:
      lightbox = $(type + '-registered');
      if (lightbox) {
        if (typeof(customTitle) != 'undefined' && typeof(customMessage) != 'undefined') {

          // add custom title and message to the lightbox
          $$('#'+type + '-registered h2')[0].innerHTML = customTitle;
          $$('#'+type + '-registered div')[0].innerHTML = customMessage;
        }

        $('lightbox-out').style.display = "block";

        lightbox.style.display = "block";

      }
      else {
        console.error('lightbox '+ type + '-registered' + ' not found');
      }
    }

  }
  else {
    //make submit button pop open the lightbox they originally requested
    $('action-submit').onclick = function() {
      submitRegistrationAction(type);
    };
    lightbox = $('action-login');
    //show registration lightbox to user
    lightbox.style.display = "block";
  }
  $('lightbox-out').style.display = "block";

  if(lightbox) {
    $(lightbox).getElementsBySelector('.close-lightbox').each(function(el){
      el.onclick = function(){
        clearLightBox();
      }
    });
    $(lightbox).getElementsBySelector('.close-lightbox.redirect').each(function(el){
      el.onclick = function(){
        clearLightBox();
        if (redirect) {
          window.location.href = redirect;
        }
      }
    });
  }
}

// TODO: substitute all the old lightboxes with this function
// options: array(title, text, onsubmit, onclose, shouldRegister)
function openLightBox_NEW(type, title, text, options) {
  // TODO: we need a queue mechanism on lighboxes
  if (isLightBoxOpened()) // ignore other lightboxes.
    return;
  clearLightBox();  //close any open lightboxes

  if (!options)
    options = {};

  var onsubmit = options.onsubmit;
  var onclose = options.onclose;
  var shouldRegister = (typeof options.shouldRegister != 'undefined' && options.shouldRegister === true) ? true : false;

  var isUser = isLoggedIn();
  var lightbox;

  if (isUser || !shouldRegister) {
    lightbox = $(type + '-registered');
    if (lightbox) {
      // add custom title and message to the lightbox
      lightbox.getElementsBySelector('.lightbox-title')[0].innerHTML = title;
      lightbox.getElementsBySelector('.lightbox-text')[0].innerHTML = text;
      if (typeof onsubmit == 'function') {
        lightbox.getElementsBySelector('.submit-lightbox')[0].onclick = onsubmit;
      }
      if (typeof onclose == 'function') {
        Event.observe(lightbox.getElementsBySelector('.close-lightbox')[0], 'click', onclose);
      }

      $('lightbox-out').show();
      lightbox.show();
    }
    else {
      console.error('lightbox '+ type + '-registered' + ' not found');
    }
  }
  else {
    //make submit button pop open the lightbox they originally requested
    $('action-submit').onclick = function() {
      submitRegistrationAction(type);
    };
    lightbox = $('action-login');
    //show registration lightbox to user
    lightbox.style.display = "block";
  }
  $('lightbox-out').show();

  if(lightbox) {
    $(lightbox).getElementsBySelector('.close-lightbox').each(function(el){
      Event.observe(el, 'click', function() {
        clearLightBox();
      });
    });
  }
}

function isLightBoxOpened() {
  return $('lightbox-out').style.display != "none";
}


function openWizardBox(type) {
  clearLightBox();
  $('personalize-' + type).style.display = "block";
  $('lightbox-out').style.display = "block";
}

function loadBlockBehaviors() {

  //clear any open lightBoxes
  clearLightBox();

  //load the Block Options Lightboxes
  setLightBox('save');
  setLightBox('forward');
  setLightBox('publish');
  setLightBox('make_public');
  setLightBox('make_private');
  setLightBox('resell');
  
  setLightBoxExt('add_cover', false);
  setLightBoxExt('save-non', false);


/*
  //load the Wizard lightboxes
  Event.observe($('personalize-photo'), 'click', function(evt) {
    openLightBox('');
  });
*/


  loadLightBoxButtons();

/*
  //capture keypress events and use right and left arrows as page navigators
  Event.observe(window, 'keyup', function keypressHandler (event) {
    var key = event.which || event.keyCode;
    switch (key) {
      case Event.KEY_RIGHT:
        nextPage();
        break;
      case Event.KEY_LEFT:
        previousPage();
        break;
    }
  });
*/

}

function loadLightBoxButtons() {
  //make sure all lightbox cancel buttons close the lightbox
  var buttList = document.getElementsByClassName('close-lightbox');
  for (var i=0; i<buttList.length; i++) {
    Event.observe(buttList[i], 'click', function(evt) {
      //clicking cancel closes the lightboxes
      clearLightBox();
    });
  }
}

function validateLightBox(type) {
// TODO: do we need to validate?  
return true;
  //all blocks require a title and description
  if (!( globalBlockData.title && globalBlockData.description )) {
    clearLightBox();
    //show box asking for title and description
    $('lightbox-out').style.display = "block";
    $('no-title-desc').style.display = "block";
    return false;
  }
  return true;
}

function clearLightBox() {
  //close any open lightboxes
  if ($('lightbox-out')) {
    $('lightbox-out').style.display = "none";
  }
  //get a list of all lightboxes, and close them
  var boxes = document.getElementsByClassName('lightbox');
  for (var i=0; i<boxes.length; i++) {
      boxes[i].style.display = "none";
  }
}
window.clearLightBox = clearLightBox;


