/**************************************************************/
/* Global Taskenstein Javascript (Used Across Multiple Pages) */
/**************************************************************/

jQuery(document).ready(function($) {
  
  /*****************/
  /* Link Behvaior */
  /*****************/
  
  // enable properly classed links to be completely disabled
  $('a.tskn-disabled').click(function(e) { tskn__e_stop(e); });
  
  /*****************/
  /* Form Behvaior */
  /*****************/
  
  // allow a special class to enable enter submission
  $('form.tskn-enter-submits').keypress(function(e) {
    if (e.which == 13) $(this).get(0).submit();
  });
  
  // in order to ensure that enter does 
  // not submit a form unless we want that
  // behavior standardize the times when
  // it should be allowed to submit
  $('form').keypress(function(e) {
    if (e.which != 13) return true;
    if ($(this).hasClass('tskn-enter-submits')) return true;
    if ($(this).find('input[type=submit]').size()) return true;
    return false;
  });
  
  // allow a special class to submit when clicked
  $('form .tskn-click-submits').click(function(e) {
    tskn__e_stop(e);
    $(this).parents('form').get(0).submit();
  });
  
  // enable the tab traversible container to trap
  // input focus within tab indexed elements if
  // the browser supports the detection of tab
  var ttc = $('.tskn-tab-traversible-container');
  ttc.each(function(i, el) { 
    var el = $(el);
    
    // find all tab indexed elements and add code
    el.find('[tabindex]').keypress(function(e) {
      if (e.keyCode != 9) return;
      var inpt = $(this);
      var ttc = inpt.parents('.tskn-tab-traversible-container');

      // locate all inputs and give each a number
      // then determine the number of current element
      var inpts = ttc.find('[tabindex]:visible');
      inpts.each(function(i, el) { $(el).data('index', i); });
      var i = inpt.data('index');
      
      // perform differently
      // based on shift key
      if (e.shiftKey)
      {
        // the only special behavior
        // occurs on first element
        if (i != 0) return true;
        i = inpts.size() - 1;
        e.preventDefault();
        inpt.blur();
        $(inpts.get(i)).focus();
      }
      else
      {
        // the only special behavior occurs 
        // on the last element so check index
        if (i != (inpts.size()-1)) return true;
        i = 0;
        e.preventDefault();
        inpt.blur();        
        $(inpts.get(i)).focus();  
      }
    });
  });
  
  /**************/
  /* Lightboxes */
  /**************/
  
  // enable the links in the footer to open proper lightboxes
  $('.tskn-terms-of-service-lightbox-opener').click(function(e) {
    tskn__e_stop(e);
    plb__lightbox.open($('#tskn-footer .tskn-lightbox.terms-of-service').clone().removeClass('tskn-hidden'));
  });
  $('.tskn-privacy-lightbox-opener').click(function(e) {
    tskn__e_stop(e);
    plb__lightbox.open($('#tskn-footer .tskn-lightbox.privacy-policy').clone().removeClass('tskn-hidden'));
  });  
  $('.tskn-upgrade-lightbox-opener').click(function(e) {
    tskn__e_stop(e);    
    plb__lightbox.open($('#tskn-footer .tskn-lightbox.upgrade-to-premium').clone().removeClass('tskn-hidden'));
  });  
});

/********************/
/* Helper Functions */
/********************/

function tskn__e_stop(e) 
{
  // shortcut for 
  // jquery events
  e.preventDefault();
  e.stopPropagation();
}
