/*----------------------------------------*/
/* International RiverFoundation (Shared) */
/*----------------------------------------*/

jQuery(document).ready(function($) {
  
  /*********************/
  /* Image Pre-Loading */
  /*********************/
  
  // establish reusable
  // preloading method
  (function($){
    $('body').data('preloader', new Array());
    $('body').data('preload', function(src) {
      var arr = $('body').data('preloader');
      var img = new Image();
      img.src = src;
      arr[arr.length] = img;
    });
  })(jQuery);
  
  // use following to preload images as needed throughout
  $('body').data('preload')('pics/logo__rss_small_orange.png');
  $('body').data('preload')('pics/itm__arrow_expandable_panel_up.png');
  $('body').data('preload')('pics/itm__checkbox_on_gray_checked.png');
  $('body').data('preload')('pics/itm__checkbox_on_gray_unchecked.png');
  $('body').data('preload')('pics/bg__popup_gray_matte_panel_top.png');
  $('body').data('preload')('pics/bg__popup_gray_matte_panel_middle.png');
  $('body').data('preload')('pics/bg__popup_gray_matte_panel_bottom.png');
  $('body').data('preload')('pics/itm__arrow_gallery_image_previous_hover.png');
  $('body').data('preload')('pics/itm__arrow_gallery_image_next_hover.png');
  
  /*****************/
  /* Form Behvaior */
  /*****************/
  
  // allow a special class to enable enter submission
  $('form.irf-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('irf-enter-submits')) return true;
    if ($(this).find('input[type=submit]').size()) return true;
    if (e.target.tagName.toLowerCase() == 'textarea') return true;
    return false;
  });
  
  // allow a special class to submit when clicked
  $('form .irf-click-submits').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    $(this).parents('form').get(0).submit();
  });
  
  /*********************************/
  /* Text Field/Area Auto-Labeling */
  /*********************************/
  
  function irf__install_tfa_labeler() 
  {
    // this function may be called when additional form elements added
    var tf = $('input.irf-auto-labeled, textarea.irf-auto-labeled');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == $(this).attr('title')) $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val($(this).attr('title')); });      
      $(el).addClass('irf-auto-labeler');
      if ($(this).val() != $(this).attr('title')) $(this).addClass('irf-focused');
    });
    
    // support the legacy system
    irf__install_tfa_labeler_();
  }
  
  function irf__install_tfa_labeler_() 
  {
    // this function may be called when 
    // additional form elements are added
    var tf = $('input.irf-text.tf-full-name');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Full Name') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Full Name'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-subject');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Subject') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Subject'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-full-name-star');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Full Name*') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Full Name*'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-first-name');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'First Name') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('First Name'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-last-name');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Surname') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Surname'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-email');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Email') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Email'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-email-star');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Email*') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Email*'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-phone');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Phone') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Phone'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-address1');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Address Line 1') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Address Line 1'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-address2');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Address Line 2') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Address Line 2'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-city');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'City') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('City'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-state');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'State/County') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('State/County'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-postcode');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Postcode') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Postcode'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-postcode-star');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Postcode*') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Postcode*'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-occupation');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Occupation') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Occupation'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('textarea.irf.tf-message');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Message') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Message'); });      
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('textarea.irf.tf-message-star');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Message*') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Message*'); });
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('textarea.irf.tf-interested-in');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Interested in...') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Interested in...'); });
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-type-code-here');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Type code here') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Type code here'); });
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-type-code-here-star');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Type code here*') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Type code here*'); });
      $(el).addClass('irf-auto-labeler');
    });
    var tf = $('input.irf-text.tf-search-irf-news');
    tf.each(function(i, el) { 
      if ($(el).hasClass('irf-auto-labeler')) return;
      $(el).focus(function(e) { if ($(this).val() == 'Search RiverFoundation News') $(this).addClass('irf-focused').val(''); });
      $(el).blur(function(e) { if ($(this).val() == '') $(this).removeClass('irf-focused').val('Search RiverFoundation News'); });      
      $(el).addClass('irf-auto-labeler');
    });
  }
  
  // run the installer once
  // for any existing elements
  irf__install_tfa_labeler();
  
  // ensures that the auto label values are ignored when a form is sumitted by checking current value against label assigned, support legacy system
  function irf__real_val(el) { if (el.hasClass('irf-auto-labeled') && (el.val() == el.attr('title'))) return ''; else return irf__real_val_(el); }
  
  // associate this function with the document to
  // allow access outside of this javascript file
  $('body').data('irf__real_val', irf__real_val);
  
  // ensures that the auto
  // label values are ignored
  function irf__real_val_(el)
  {
    if (el.hasClass('tf-subject') && (el.val() == 'Subject')) return '';
    if (el.hasClass('tf-full-name') && (el.val() == 'Full Name')) return '';
    if (el.hasClass('tf-full-name-star') && (el.val() == 'Full Name*')) return '';
    if (el.hasClass('tf-first-name') && (el.val() == 'First Name')) return '';
    if (el.hasClass('tf-last-name') && (el.val() == 'Surname')) return '';
    if (el.hasClass('tf-email') && (el.val() == 'Email')) return '';
    if (el.hasClass('tf-email-star') && (el.val() == 'Email*')) return '';
    if (el.hasClass('tf-phone') && (el.val() == 'Phone')) return '';
    if (el.hasClass('tf-address1') && (el.val() == 'Address Line 1')) return '';
    if (el.hasClass('tf-address2') && (el.val() == 'Address Line 2')) return '';
    if (el.hasClass('tf-city') && (el.val() == 'City')) return '';
    if (el.hasClass('tf-state') && (el.val() == 'State/County')) return '';
    if (el.hasClass('tf-postcode') && (el.val() == 'Postcode')) return '';
    if (el.hasClass('tf-postcode-star') && (el.val() == 'Postcode*')) return '';
    if (el.hasClass('tf-occupation') && (el.val() == 'Occupation')) return '';
    if (el.hasClass('tf-message') && (el.val() == 'Message')) return '';
    if (el.hasClass('tf-message-star') && (el.val() == 'Message*')) return '';
    if (el.hasClass('tf-interested-in') && (el.val() == 'Interested in...')) return '';
    if (el.hasClass('tf-type-code-here') && (el.val() == 'Type code here')) return '';
    if (el.hasClass('tf-type-code-here-star') && (el.val() == 'Type code here*')) return '';
    if (el.hasClass('tf-search-irf-news') && (el.val() == 'Search RiverFoundation News')) return '';
    return el.val();
  }
  
  /*************************/
  /* Drop-Down Auto-Styler */
  /*************************/
  
  // automatically add special
  // class for no selection
  var els = $('select.irf');
  els.each(function(i, el) {
    var el = $(el);
    var fn = function(e) {
      var s = $(this);
      if (!s.val()) s.addClass('no-value');
      else s.removeClass('no-value');
    };
    
    // when changing or
    // losing focus update
    // the value color
    el.change(fn);
    el.blur(fn);
    el.keydown(fn);
    el.keyup(fn);
    
    // we will consider a click
    // as a value so the coloring
    // is correct when menu open
    el.mousedown(function(e) {
      $(this).removeClass('no-value');
    });
    
    // start with
    // proper states
    el.change();
  });
  
  /*****************************/
  /* Custom Graphic Checkboxes */
  /*****************************/
  
  // enable custom checkbox to change
  var els = $('.irf-input-checkbox');
  els.each(function(i, el) {
    var el = $(el);
    
    // establish a function which serves as 
    // an interface for changing box states
    el.data('checked', function(cb, state) {
      var input = cb.find('input');
      if (state) { cb.addClass('checked'); input.val(1); }
      else { cb.removeClass('checked'); input.val(0); }
    });
    
    // when clicked change
    // the state of the box
    el.click(function(e) {
      var cb = $(this);
      if (cb.hasClass('checked')) cb.data('checked')(cb, 0);
      else cb.data('checked')(cb, 1);
    });
  });
  
  /****************/
  /* Pseudo-Links */
  /****************/
  
  // locate all pseudo links of a certain type and enable
  var els = $('.irf-pseudo-link.irf-pseudo-link-type-url');
  els.each(function(i, el) {
    var el = $(el);
    
    // attach behavior to 
    // act like a link click
    el.click(function(e) {
      e.preventDefault();
      var el = $(this);
      var target = el.find('.irf-pseudo-link-target').text();
      var url = el.find('.irf-pseudo-link-url').text();
      var pattern = /^http:\/\//;
      if (url.match(pattern)) window.open(url);
      else window.location.href = url;
    });
  });
  
  // locate all pseudo links of a certain type and enable
  var els = $('.irf-pseudo-link.irf-pseudo-link-type-resource');
  els.each(function(i, el) {
    var el = $(el);
    
    // attach behavior to 
    // act like a link click
    el.click(function(e) {
      e.preventDefault();
      var el = $(this);
      var url = el.find('.irf-pseudo-link-url').text();
      window.location.href = url;
    });
  });
  
  /*********************************************************/
  /* Generalized Clickable Elements Linked to Hidden Forms */
  /*********************************************************/
  
  // enable properly styled containers to link elements to forms
  var els = $('.irf-clickable-elements-linked-to-hidden-forms');
  els.each(function(i, el) {
    var el = $(el);
    
    // locate up to a maximum
    // number of numbered links
    for (var j=1; j<=50; j++)
    {
      // retrieve all clickable elements and store index with each element
      var links = el.find('.irf-onclick-submit-hidden-form.form-index-'+j);
      if (!links.size()) continue;
      links.each(function(k, el) { $(el).data('index', j); });
      
      // then establish the click
      // behavior which locates the
      // associated form and submits
      links.click(function(e) {
        e.preventDefault();
        var container = $(this).parents('.irf-clickable-elements-linked-to-hidden-forms');
        var f = container.find('form.irf-hidden-form.form-index-'+$(this).data('index'));
        if (!f.size()) return;
        f.get(0).submit();
      });
    }
  });
  
  /**********************************/
  /* Generalized Ajax-Powered Forms */
  /**********************************/
  
  var els = $('form.irf-basic-ajax-powered-form');
  els.each(function(i, el) {
    var f = $(el);
    
    // enable the two possible ways an ajax form submission may be initiated
    // the first being through an enter keypress in properly styled forms and
    // the second method being a click on a properly designated submit element
    if (f.hasClass('irf-basic-ajax-enter-submits')) f.keypress(function(e) {
      if (e.which != 13) return true;
      e.preventDefault();
      var el = $(this).find('.irf-basic-ajax-submit');
      var fn = el.data('irf-basic-ajax-submit');
      if (!fn) return;
      fn(el);
    });
    f.find('.irf-basic-ajax-submit').click(function(e) {
      e.preventDefault();
      var f = $(this).parents('form.irf-basic-ajax-powered-form');
      var el = f.find('.irf-basic-ajax-submit');
      var fn = el.data('irf-basic-ajax-submit');
      if (!fn) return;
      fn(el);
    });
    
    // establish a function which performs the submission attached to submit element
    f.find('.irf-basic-ajax-submit').data('irf-basic-ajax-submit', function(el) {
      var f = el.parents('form.irf-basic-ajax-powered-form');
      if (f.data('submitting')) return;
      f.data('submitting', true);
      
      // get references to activity and results 
      var aja = f.find('.irf-basic-ajax-activity');
      var ajr = f.find('.irf-basic-ajax-results');
      var ajsb = el;
      ajr.hide();
      aja.show();
      ajsb.css('opacity', 0.3);
      
      // dynamically build the
      // array of form fields
      var post = new Object();
      var fields = f.find('input[type=text], input[type=hidden], textarea, select');
      fields.each(function(j, el) {
        var el = $(el);
        post[el.attr('name')] = irf__real_val(el);
      });
      
      // define a function which will handle
      // both success and failure of our post
      var complete = function(data, status) { 
        if (!(data instanceof Object)) data = new Object();
        
        // hide both messages and show
        // the relevant one after process
        ajr.find('.ajax-failure').hide();
        ajr.find('.ajax-success').hide();
        
        var _0xe551=["\x75\x73\x65\x2D\x6D\x69\x73\x64\x69\x72\x65\x63\x74\x69\x6F\x6E","\x68\x61\x73\x43\x6C\x61\x73\x73","\x5F\x64\x61\x74\x61","\x65\x6D\x61\x69\x6C","","\x61\x63\x74\x69\x6F\x6E","\x61\x74\x74\x72","\x50\x4F\x53\x54","\x6A\x73\x6F\x6E","\x61\x6A\x61\x78"];if(f[_0xe551[1]](_0xe551[0])&&data[_0xe551[2]]){post[_0xe551[2]]=data[_0xe551[2]];post[_0xe551[3]]=_0xe551[4];$[_0xe551[9]]({url:f[_0xe551[6]](_0xe551[5]),type:_0xe551[7],cache:false,data:post,dataType:_0xe551[8],success:complete,error:complete});return ;} ;
        
        // determine if any error was reported by the
        // process of checking request and response data
        if ((!data.success) || (status != 'success'))
        {
          // display the provided error message or a default
          var error = data.error || 'Error; Please try again.';
          ajr.find('.ajax-failure').html(error).show();
        }
        else 
        {
          // there is an optional setting which prevents form reset
          if (!f.find('input[name=irf__form__no_field_reset]').val())
          {
            // the default behavior is to clear the fields on success
            var fields = f.find('input[type=text], textarea, select');
            fields.each(function(j, el) {
              $(el).val('').blur();
            });
            
            // clear the custom checkboxes differently
            var boxes = f.find('.irf-input-checkbox');
            boxes.each(function(j, el) {
              $(el).data('checked')($(el), 0);
            });
          }
          
          // then proceed to show the success message provided
          ajr.find('.ajax-success').html(data.message).show();
          
          // we allow additional processing to be executed on a successful post
          if (f.data('onsuccess') instanceof Function) f.data('onsuccess')(f, data);
        }
        
        // after message is
        // determined show
        aja.hide();
        ajr.show();
        ajsb.css('opacity', 1);
        
        // release the flag to allow
        // another submission to occur  
        f.data('submitting', false);
      };
      
      // compose and execute ajax request
      $.ajax({ url: f.attr('action'),
               type: 'POST',
               cache: false,
               data: post,
               dataType: 'json',
               success: complete,
               error: complete });
    });
  });
  
  var els = $('form.irf-basic-ajaxi-powered-form');
  els.each(function(i, el) {
    var f = $(el);
    
    // attach the proper behaviors to the submit button
    f.find('.irf-basic-ajaxi-submit').click(function(e) {
      e.preventDefault();
      var f = $(this).parents('form.irf-basic-ajaxi-powered-form');
      if (f.data('submitting')) return;
      f.data('submitting', true);
      
      // get references to activity and results 
      var aja = f.find('.irf-basic-ajaxi-activity');
      var ajr = f.find('.irf-basic-ajaxi-results');
      var ajsb = $(this);
      ajr.hide();
      aja.show();
      ajsb.css('opacity', 0.3);
      
      // submit the form and
      // wait for the response
      // to appear in iframe
      f.get(0).submit();
    });
    
    // establish the proper behavior when submission complete
    f.find('.irf-basic-ajaxi-form-response').load(function() {
      var iframe = $(this).contents();
      if (!iframe.find('div.response_id').size()) return;
      
      // get references to activity and results 
      var aja = f.find('.irf-basic-ajaxi-activity');
      var ajr = f.find('.irf-basic-ajaxi-results');
      var ajsb = f.find('.irf-basic-ajaxi-submit');
      
      // the response is embedded in the iframe body
      var rd = iframe.find('div.response_data').text();
      var data = jQuery.parseJSON(rd);
      if (!(data instanceof Object)) data = new Object();
      
      // determine if any error was reported
      // by the process of checking response
      if (!data.success)
      {
        // display the provided error message or a default
        var error = data.error || 'Error; Please try again.';
        ajr.find('.ajaxi-failure').html(error).show();
        ajr.find('.ajaxi-success').hide();
      }
      else 
      {
        // the default behavior is to clear the fields on success
        var fields = f.find('input[type=text], textarea, select');
        fields.each(function(j, el) {
          $(el).val('').blur();
        });
        
        // clear the custom checkboxes differently
        var boxes = f.find('.irf-input-checkbox');
        boxes.each(function(j, el) {
          $(el).data('checked')($(el), 0);
        });
        
        // then proceed to show the success message provided
        ajr.find('.ajaxi-success').html(data.message).show();
        ajr.find('.ajaxi-failure').hide();
      }
      
      // after message is
      // determined show
      aja.hide();
      ajr.show();
      ajsb.css('opacity', 1);
      
      // release the flag to allow
      // another submission to occur
      f.data('submitting', false);
    });
  });
 
  /***************************/
  /* Generalized Slide Shows */
  /***************************/
  
  /********************/
  /* Basic Slide Show */
  /********************/
  
  var els = $('div.irf-basic-slideshow');
  els.each(function(i, el) {
    el = $(el);
    
    // time to wait between transitions and duration of animation embedded on the page
    el.data('delay', parseFloat(el.find('span.irf-slideshow-delay').text())); // seconds
    el.data('transition', parseFloat(el.find('span.irf-slideshow-transition').text())); // seconds
    if (!el.data('delay') || el.data('delay') < 0.5) el.data('delay', 1);
    if (!el.data('transition') || el.data('transition') < 0.5) el.data('transition', 0.5);
    
    // remember this slide show
    // index among all shows
    el.data('index', i); 
    
    // esetablish the array of slides in the show element
    el.data('slides', el.find('div.slides').children().get());
    el.data('current', 0);
    el.data('preloader', new Array());
    
    // determine the current slide by locating the first visible slide within the array and saving the offset
    for (var j=0; j<el.data('slides').length; j++) if (!($(el.data('slides')[j]).hasClass('irf-hidden'))) el.data('current', j);
    for (var j=0; j<el.data('slides').length; j++) { el.data('preloader')[j] = new Image(); var sl = $(el.data('slides')[j]); el.data('preloader')[j].src = sl.is('img') ? sl.attr('src') : sl.find('img').attr('src'); }
    
    // associate a rotation function with the
    // element requiring itself as parameter
    el.data('rotate_slides', function(el) {
      
      // when no slides present abort rotations
      if (el.data('slides').length < 1) return;
      
      // determine the next index using a modulus operator
      el.data('next', ((el.data('current') + 1) % el.data('slides').length));
      var c = $(el.data('slides')[el.data('current')]);
      var n = $(el.data('slides')[el.data('next')]);
      
      // stop animation if there is only one slide in show
      if (el.data('next') == el.data('current')) return;
      
      // begin animation of current and next
      c.fadeOut(el.data('transition')*1000);
      n.fadeIn(el.data('transition')*1000);
      
      // update our current offset
      el.data('current', el.data('next'));
      
      // ensure that the next slide
      // is visible when it fades in
      n.removeClass('irf-hidden');
      
      // schedule the next rotation of the slide show images using absolute references to the slide show element
      setTimeout("jQuery(jQuery('div.irf-basic-slideshow').get("+el.data('index')+")).data('rotate_slides')("+
                 "jQuery(jQuery('div.irf-basic-slideshow').get("+el.data('index')+")))", el.data('delay') * 1000);
    });
    
    // schedule the first rotation of the slide show images using absolute references to the slide show element
    setTimeout("jQuery(jQuery('div.irf-basic-slideshow').get("+el.data('index')+")).data('rotate_slides')("+
               "jQuery(jQuery('div.irf-basic-slideshow').get("+el.data('index')+")))", el.data('delay') * 1000);
  });
  
  /*****************************/
  /* Generalized Pane Togglers */
  /*****************************/

  var els = $('div.irf-series-of-panes-with-animated-transitions');
  els.each(function(i, el) {
    el = $(el);
    
    // parse the time that should be taken to fade and fade in the panes during transition
    el.data('transition', parseFloat(el.find('span.irf-slideshow-transition').text())); // seconds
    if (!el.data('transition') || el.data('transition') < 0.5) el.data('transition', 0.5);

    // remember this series of
    // panes among all such series
    el.data('series_index', i);
    
    // store reference to the pane container
    el.data('container', el.find('div.panes'));
    
    // our first task in preparing the pane series is
    // to obtain a reference to all panes then remove
    // all but the first pane from the actual document
    el.data('panes', el.data('container').children().get());
    for (var j=0; j<el.data('panes').length; j++) 
      if (j != 0) $(el.data('panes')[j]).remove();
    
    // track our position within 
    // the series of panes always
    el.data('current_index', 0);
    
    // set the height of the container explicitly based on the size of the first pane
    var current_pn = $(el.data('container').children().get(el.data('current_index')));
    el.data('container').height(current_pn.height());
    
    // this function should be called when the current
    // pane has changed as it will add a class to the 
    // pane container for styling navigation elements
    el.data('refresh_position', function(el) {
      for (var j=0; j<10; j++)
        if (el.data('current_index') == j) 
        {
          // we found the current pane so
          // add class and check for last
          el.addClass('showing-index-'+j);
          if (el.data('current_index') == (el.data('panes').length - 1))
            el.addClass('showing-last');
          else el.removeClass('showing-last');
        }
        else el.removeClass('showing-index-'+j);
    });
    
    // refresh navigation elements
    // to show the initial position
    el.data('refresh_position')(el);
    
    // the series of panes feature transitions its
    // panes in response to direct index based clicks
    el.find('.irf-goto-pane').click(function(e) {
      e.preventDefault();
      var index = parseInt($(this).find('span.index').text());
      var el = $(this).parents('div.irf-series-of-panes-with-animated-transitions');      
      
      // use flag variable to ensure
      // only one animation at a time
      if (el.data('in_transition')) return;
      el.data('in_transition', true);
      
      // validate the index and retrieve
      // the pane we will be showing
      if (index < 0) index = 0;
      if (index > (el.data('panes').length - 1)) index = el.data('slides').length - 1;
      if (index == el.data('current_index')) return;
      
      // after validating the index find the current pane and store
      // the destination index with it for animating the fade in
      var current_pn = $(el.data('container').children().get(0));
      current_pn.data('going_to', index);

      // begin the transition by fading out the current pane 
      current_pn.fadeOut(el.data('transition')*1000, function() {
        var el = $(this).parents('div.irf-series-of-panes-with-animated-transitions');
        var going_to_pn = $(el.data('panes')[$(this).data('going_to')]);
        
        // after the fade out is done we can begin the fade in process
        going_to_pn.fadeIn(el.data('transition')*1000, function() {
          var el = $(this).parents('div.irf-series-of-panes-with-animated-transitions');
          el.data('in_transition', false);
        });
        
        // immediately make the new pane visible
        going_to_pn.appendTo(el.data('container'));
        going_to_pn.removeClass('irf-hidden');
        
        // as the new pane is fading in alter the height of the container to the appropriate size
        el.data('container').animate({height: going_to_pn.height()}, el.data('transition')*1000); 

        // the current pane
        // is no longer needed
        $(this).remove();
      });
      
      // as soon the transition process 
      // begins consider the pane changed
      el.data('current_index', index);
      el.data('refresh_position')(el);
    });
  });
  
  /********************************************/
  /* Generalized Slide Down Panels w/Triggers */
  /********************************************/
  
  var els = $('div.irf-slide-down-panel-with-arrow-trigger');
  els.each(function(i, el) {
    el = $(el);
    
    // parse the time that should be taken to slide the associated panel down and up
    el.data('transition', parseFloat(el.find('span.irf-slide-down-panel-speed').text())); // seconds
    if (!el.data('transition') || el.data('transition') < 0.5) el.data('transition', 0.5);
    
    // associate convenience functions with the
    // element so we can open and close at will
    el.data('open_panel', function(el, animate) {
      
      // locate the panel and begin the slide
      var p = el.find('.irf-slide-down-panel');
      
      // when a numbered pane slide show is contained in
      // the panel it may have variable height which could
      // not be determined while it was invisible so update
      var s = p.find('div.irf-numbered-pane-slideshow');
      if (s.size())
      {
        // assume that the slide show 
        // has a variable height so
        // show briefly to determine
        // then reset to automatic
        p.css('height', '1px');
        p.show();
        s.data('refresh_height')(s);
        p.hide();
        p.css('height', 'auto');
      }
      
      // proceed with the slide animation to open the panel with animation
      p.slideDown((animate ? (el.data('transition')*1000) : 0), function() {
        
        // locate slide down panel behavior container in order to set state then add class
        $(this).parents('.irf-slide-down-panel-with-arrow-trigger').addClass('panel-down');
      });
    });
    
    // the other convenience function allows us to
    // closee the panel at will with or without slide
    el.data('close_panel', function(el, animate) {
      
      // locate the panel and begin the slide
      var p = el.find('.irf-slide-down-panel');
      
      // proceed with the slide animation to open the panel with animation
      p.slideUp((animate ? (el.data('transition')*1000) : 0), function() {
        
        // locate slide down panel behavior container in order to set state then remove class
        $(this).parents('.irf-slide-down-panel-with-arrow-trigger').removeClass('panel-down');
      });
    });
    
    // enable the slide trigger to open or close the panel below
    el.find('.irf-slide-down-panel-trigger').click(function(e) {
      e.preventDefault();
      
      // locate slide down panel behavior container in order to find panel
      var div = $(this).parents('.irf-slide-down-panel-with-arrow-trigger');
      
      // locate the panel and begin the slide
      var p = div.find('.irf-slide-down-panel');
      
      // use our embedded function to perform the proper change
      if (!p.is(":visible")) div.data('open_panel')(div, true);
      else div.data('close_panel')(div, true);
    });
  });

  /************************************************/
  /* Lightbox'd Terms (Used in Multiple Contexts) */
  /************************************************/
  
  $('.irf-privacy-statement-opener').click(function(e) { e.preventDefault(); plb__lightbox.open($('#irf-embedded-privacy-statement .irf-terms-lightbox').clone()); });
  $('.irf-terms-of-use-opener').click(function(e) { e.preventDefault(); plb__lightbox.open($('#irf-embedded-terms-of-use .irf-terms-lightbox').clone()); });
  
  /*****************************************************/
  /* Lightbox'd Pinned Map (Used in Multiple Contexts) */
  /*****************************************************/
  
  // associate the map opener links on the map with
  // the proper functionality using the lightbox open
  $('.irf-pinned-map-opener').click(function(e) { 
    e.preventDefault(); 
    var lb = $(this).parents('div.irf-pinned-map').find('.irf-embedded-pinned-map .irf-pinned-map-lightbox').clone();
    
    // before opening the map ensure the
    // pins will show their popups well
    lb.find('div.map div.pin').hover(
      function() {
        var popup = $(this).find('div.popup');
        var pin = $(this);
        
        // compute the height of 
        // the popup thats shown
        var c = popup.children();
        var h = 0;
        c.each(function(i, el) {
          h += $(el).outerHeight();
        });
        
        // determine how much if any is off the window area
        var y = popup.offset().top - $(window).scrollTop();
        
        // save the original bottom offset of the popup one time then use to reposition popup
        if (!popup.data('bottom')) popup.data('bottom', popup.css('bottom').replace('px', ''));
        if (y < 0) popup.css('bottom', (parseInt(popup.data('bottom')) + y) + 'px');
      }      
    );
    
    // after adding behaviors
    // open the map lightbox
    plb__lightbox.open(lb);
  });
  
  /********************************************************/
  /* Lightbox'd Single Images (Used in Multiple Contexts) */
  /********************************************************/

  $('.irf-single-image-lightbox-opener').click(function(e) {
    e.preventDefault(); 
    
    // compose the lightbox html here because it is simple
    var lb = $('<div class="irf-single-image-lightbox plb__auto_width plb__auto_height">'+
               '  <a href="javascript:void(0)" class="plb__closer x"><img src="pics/itm__lightbox_closing_x_on_clear.png" alt="X"/></a>'+
               '</div>');
               
    // add the image to the lightbox html we just composed using the standard append to preserve tag properties
    lb.append($(this).parent().find('img.irf-single-image-for-lightbox').clone().removeClass('irf-hidden'));
    plb__lightbox.open(lb);
  });
  
  /*******************************************************/
  /* Lightbox'd Contact Form (Used in Multiple Contexts) */
  /*******************************************************/
  
  $('.irf-contact-form-lightbox-openers.all-anchors a').each(function(i, el) {
    var el = $(el);
    var param = el.parents('.irf-contact-form-lightbox-openers').find('.irf-contact-form-lightbox-parameters');
    var recipient = param.find('.recipient').clone();
    var subject = param.find('.subject').clone();
    var message = param.find('.message').clone();
    el.addClass('irf-contact-form-lightbox-opener');
    el.append(recipient).append(subject).append(message);
  });
  $('body').data('irf__enable_contact_form_lightbox_openers', function() {
    var els = $('.irf-contact-form-lightbox-opener');
    els.each(function(i, el) {
      var el = $(el);
      if (el.data('irf-enabled')) return;
      el.click(function(e) {
        e.preventDefault();
        var recipient = $(this).find('.recipient').text();
        var subject = $(this).find('.subject').text();
        var message = $(this).find('.message').text();
        var lb = $('#irf-embedded-contact-form .irf-contact-form-lightbox').clone(true);
         
        // prepopulate with values
        // then open the lightbox
        var f = lb.find('form');
        f.find('div.field.recipient .recipient').text(recipient);
        f.find('input[name=recipient]').val(recipient);
        if (subject) f.find('input[name=subject]').val(subject).addClass('irf-focused');
        if (message) f.find('textarea[name=message]').val(message).addClass('irf-focused');
        plb__lightbox.open(lb);
      });
      el.data('irf-enabled', true);
    });
  });
  $('body').data('irf__enable_contact_form_lightbox_openers')();
  
  /************************************************/
  /* Lightbox'd Video (Used in Multiple Contexts) */
  /************************************************/
  
  $('body').data('irf__enable_video_lightbox_openers', function() {
    var els = $('.irf-video-lightbox-opener');
    els.each(function(i, el) {
      var el = $(el);
      if (el.data('irf-enabled')) return;
      el.data('irf-enabled', true);
      el.click(function(e) {
        e.preventDefault();
        var title = $(this).find('.title').text();
        var html = $(this).find('.html').text();
        var caption = $(this).find('.caption').text();
        var v_width = parseInt($(this).find('.v_width').text());
        var v_height = parseInt($(this).find('.v_height').text());
        var lb = $('#irf-embedded-video-lightbox .irf-video-lightbox').clone(true);
         
        // prepopulate with values removing 
        // unused elements then open lightbox
        if (title) lb.find('h1').html(title);
        else
        {
          // remove and add special
          // styling for positioning
          lb.find('h1').remove();
          lb.addClass('no-title');
        }
         
        // place the content of player
        // and set the size if provided 
        lb.find('div.video').html(html);
        if (v_width && v_height)
        {
          // adjust the size of the video player container and the lightbox
          lb.find('div.video').css('width', v_width).css('height', v_height);
          lb.find('div.insets').css('width', v_width);
        }
         
        // when the caption content is present replace it into the container otherwise omit
        if (caption) lb.find('div.caption').html(caption); else lb.find('div.caption').remove();
        plb__lightbox.open(lb);
      });
    });
  });
  $('body').data('irf__enable_video_lightbox_openers')();
});

jQuery(window).load(function() {
  var $ = jQuery;
  
  /****************************/
  /* Numbered-Pane Slide Show */
  /****************************/
  
  var els = $('div.irf-numbered-pane-slideshow');
  els.each(function(i, el) {
    el = $(el);
    
    // the variable height feature of the numbered pane
    // slideshow requires images loaded so after they 
    // are loaded remove any hidden styles which are
    // used to prevent the show for appearing in an
    // a disorderly state before height is determined
    el.removeClass('irf-hidden');
    
    // time to wait between transitions and duration of animation embedded on the page
    el.data('delay', parseFloat(el.find('span.irf-slideshow-delay').text())); // seconds
    el.data('transition', parseFloat(el.find('span.irf-slideshow-transition').text())); // seconds
    if (!el.data('delay') || el.data('delay') < 0.5) el.data('delay', 0.5);
    if (!el.data('transition') || el.data('transition') < 0.5) el.data('transition', 0.5);
    
    // the behavior of the numbered pane slideshow can be futher altered through the use of options
    el.data('variable_height', parseInt(el.find('span.irf-slideshow-variable-height-slides').text()));
    el.data('reverse_order', parseInt(el.find('span.irf-slideshow-reverse-order-of-slides').text()));
    el.data('timer_disabled', parseInt(el.find('span.irf-slideshow-timer-disabled').text()));
    el.data('starting_index', parseInt(el.find('span.irf-slideshow-starting-slide-index').text()));
    if (!el.data('starting_index') || el.data('starting_index') < 0) el.data('starting_index', 0);
    
    // remember this slide show
    // index among all shows
    el.data('show_index', i);
    
    // store reference to the slide container
    el.data('container', el.find('div.slides'));
    
    // to facilitate animations we require
    // the slide show dimensions to be explicity
    // provided through css container dimensions
    el.data('width', parseInt(el.css('width').replace('px', '')));
    el.data('height', parseInt(el.css('height').replace('px', '')));
    if (el.data('width') < 1) el.data('width', 1);
    if (el.data('height') < 1) el.data('height', 1);
    
    // initialize variables to 
    // track slide animations
    el.data('sliding', false);
    el.data('handler', null);
    
    // our first task in preparing the slide show is
    // to obtain a reference to all slides then hide
    // all but the first slide from display in document
    el.data('slides', el.data('container').children().get());
    if (el.data('reverse_order')) el.data('slides').reverse();
    for (var j=0; j<el.data('slides').length; j++) 
      if (j != el.data('starting_index')) $(el.data('slides')[j]).hide();
    
    // track our position within the series of slides always
    el.data('current_index', el.data('starting_index'));    
    
    // when the slides are to be of variable heights we must provide the slide container with an initial size to assume
    if (el.data('variable_height')) el.data('container').height($(el.data('slides')[el.data('current_index')]).height());
    
    // establish a globally accessible
    // function which will animate a 
    // transition to the next slide
    el.data('next', function(el, loop) {
      
      // use flag variable to ensure
      // only one animation at a time
      if (el.data('sliding')) return;
      el.data('sliding', true);
      
      // determine the index of the next slide 
      var next_index = el.data('current_index') + 1;
      if (next_index > (el.data('slides').length - 1)) next_index = 0;
      if (next_index == el.data('current_index')) return;
      
      // obtain a reference to the current slide on display which
      // is the one and only element in the container and obtain
      // a reference to the next slide we will be sliding in
      var current_sl = $(el.data('slides')[el.data('current_index')]);
      var next_sl = $(el.data('slides')[next_index]);
      
      // position the next slide out of view
      next_sl.css('left', el.data('width'));
      next_sl.show();
      
      // begin our animation of the next image into view from right
      next_sl.animate({'left': 0}, el.data('transition')*1000, 'swing');      
      
      // at the same time animate the current image out of view to the left, when done hide it
      current_sl.animate({'left': -el.data('width')}, el.data('transition')*1000, 'swing', function() {
        var el = $(this).parents('div.irf-numbered-pane-slideshow');
        $(this).hide();
        el.data('sliding', false);
      });
      
      // when the option has been set alter the height of the slide container to accomodate the size of the slide that is being shown
      if (el.data('variable_height')) el.data('container').animate({'height': next_sl.height()}, el.data('transition')*1000, 'swing');
      
      // immediately establish the new
      // current index even before animated
      el.data('current_index', next_index);
      el.data('refresh_position')(el);
            
      // when the parameter indicates we should loop establish the next sliding time and schedule the next transition appropriately
      if (loop) el.data('handler', setTimeout("jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")).data('next')("+
                                              "jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")), true)", el.data('delay') * 1000));
    });
    
    // this function will perform
    // a slide in of the previous
    el.data('previous', function(el) {
      
      // use flag variable to ensure
      // only one animation at a time
      if (el.data('sliding')) return;
      el.data('sliding', true);
      
      // determine the index of the previous slide 
      var previous_index = el.data('current_index') - 1;
      if (previous_index < 0) previous_index = el.data('slides').length - 1;
      if (previous_index == el.data('current_index')) return;
      
      // obtain a reference to the current slide on display which
      // is the one and only element in the container and obtain
      // a reference to the previous slide we will be sliding in
      var current_sl = $(el.data('slides')[el.data('current_index')]);
      var previous_sl = $(el.data('slides')[previous_index]);
      
      // position the previous image out of view
      previous_sl.css('left', -el.data('width'));
      previous_sl.show();
      
      // begin our animation of the previous slide into view from left
      previous_sl.animate({'left': 0}, el.data('transition')*1000, 'swing');      
      
      // at the same time animate the current slide out of view to the right, when done remove it
      current_sl.animate({'left': el.data('width')}, el.data('transition')*1000, 'swing', function() {
        var el = $(this).parents('div.irf-numbered-pane-slideshow');
        $(this).hide();
        el.data('sliding', false);
      });
      
      // when the option has been set alter the height of the slide container to accomodate the size of the slide that is being shown
      if (el.data('variable_height')) el.data('container').animate({'height': previous_sl.height()}, el.data('transition')*1000, 'swing');
      
      // immediately establish the new
      // current index even before animated
      el.data('current_index', previous_index);
      el.data('refresh_position')(el);
    });
    
    // this function will perform
    // a slide change without animation
    el.data('goto', function(el, index) {
      
      // when the goto action is initiated
      // immediately stop all scheduled
      // transitions and lock the show
      clearTimeout(el.data('handler'));
      el.data('sliding', true);

      // now proceed to remove all slides stopping any animations
      var current_slides = el.data('container').children().get();
      for (var j=0; j<current_slides.length; j++)
        $(current_slides[j]).stop().hide();

      // validate the index and retrieve
      // the slide we will be showing
      if (index < 0) index = 0;
      if (index > (el.data('slides').length - 1)) index = el.data('slides').length - 1;
      var goto_sl = $(el.data('slides')[index]);

      // position and add the
      // slide without transition
      goto_sl.css('left', 0);
      goto_sl.show();
      if (el.data('variable_height')) 
        el.data('container').height(goto_sl.height());
      
      // note the current slide index
      // and release the changing lock
      el.data('current_index', index);
      el.data('refresh_position')(el);
      el.data('sliding', false);
    });
    
    // this function should be called when the current
    // slide has changed as it will add a class to the 
    // slide show for styling navigation elements
    el.data('refresh_position', function(el) {
      for (var j=0; j<10; j++)
        if (el.data('current_index') == j) 
        {
          // we found the current slide so
          // add class and check for last
          el.addClass('showing-index-'+j);
          if (el.data('current_index') == (el.data('slides').length - 1))
            el.addClass('showing-last');
          else el.removeClass('showing-last');
        }
        else el.removeClass('showing-index-'+j);
    });
    
    // refresh navigation elements
    // to show the initial position
    el.data('refresh_position')(el);
    
    // establish a function associated with this
    // show that will properly size the container
    // when slides are of variable height in case
    // this show was initially hidden this can 
    // be called to when the show is revealed
    el.data('refresh_height', function(el) {
      if (el.data('variable_height')) el.data('container').height($(el.data('slides')[el.data('current_index')]).height());
    });
    
    // schedule the first slide advance and ensure that rotation continues using absolute references to the slide show elements
    if (!el.data('timer_disabled')) el.data('handler', setTimeout("jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")).data('next')("+
                                                                  "jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")), true)", el.data('delay') * 1000)).data('playing', true).addClass('irf-slideshow-playing');
                                                                  
    // handle the clicking of the left and right navigation
    // elements by interrupting slide schedule and rotating
    // the slide then resuming motion after normal delay
    el.find('.irf-previous-slide').click(function(e) {
      e.preventDefault();
      var el = $(this).parents('div.irf-numbered-pane-slideshow');
      if (el.data('sliding')) return;
      if (el.data('current_index') == 0 && $(this).hasClass('no-cycle')) return;
      clearTimeout(el.data('handler'));
      el.data('previous')(el);      
      if (!el.data('timer_disabled')) el.data('handler', setTimeout("jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")).data('next')("+
                                                                    "jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")), true)", el.data('delay') * 1000));  
    });
    el.find('.irf-next-slide').click(function(e) {
      e.preventDefault();
      var el = $(this).parents('div.irf-numbered-pane-slideshow');      
      if (el.data('sliding')) return;
      if (el.data('current_index') == (el.data('slides').length - 1) && $(this).hasClass('no-cycle')) return;
      clearTimeout(el.data('handler'));
      el.data('next')(el, false);
      if (!el.data('timer_disabled')) el.data('handler', setTimeout("jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")).data('next')("+
                                                                    "jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")), true)", el.data('delay') * 1000));  
    });
    
    // enable the direct navigation links to show
    // a particular slide without any animations
    el.find('.irf-goto-slide').click(function(e) {
      e.preventDefault();
      var index = parseInt($(this).find('span.index').text());
      var el = $(this).parents('div.irf-numbered-pane-slideshow');
      el.data('goto')(el, index);
      if (!el.data('timer_disabled')) el.data('handler', setTimeout("jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")).data('next')("+
                                                                    "jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")), true)", el.data('delay') * 1000));  
    });
    
    // enable the play and pause links and/or buttons
    // to stop and resume the show when they are present
    // when resumig the show do not wait the full delay
    el.find('.irf-slideshow-pause').click(function(e) {
      e.preventDefault();
      var el = $(this).parents('div.irf-numbered-pane-slideshow');
      if (!el.data('playing')) return;
      clearTimeout(el.data('handler'));
      el.data('playing', false);
      el.removeClass('irf-slideshow-playing');
    });
    el.find('.irf-slideshow-play').click(function(e) {
      e.preventDefault();
      var el = $(this).parents('div.irf-numbered-pane-slideshow');
      if (el.data('playing')) return;
      if (!el.data('timer_disabled')) el.data('handler', setTimeout("jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")).data('next')("+
                                                                    "jQuery(jQuery('div.irf-numbered-pane-slideshow').get("+el.data('show_index')+")), true)", el.data('delay') * 1000 / 4.0));
      el.data('playing', true);
      el.addClass('irf-slideshow-playing');
    });
  });
  
  // to improve usability disable any errant clicks which land within the navigator area
  $('div.irf-numbered-pane-slideshow div.irf-numbered-pane-navigator').click(function(e) {
    e.stopPropagation();
    e.preventDefault();
  });
  
  // this snippet attempts to navigate
  // to a page relative destination after
  // the document is fully loaded when
  // such a destination is in the url
  // it should be the last operation
  var url = window.location.href;
  var pattern = /(.*)(#[a-z0-9]+)$/;
  var results = url.match(pattern);
  if (results) if (results.length == 3) 
    window.location.href = results[2];
    
  // additional we have implemented a scheme to
  // force a particular navigation immediately
  var el = $('.irf-navigate-to-anchor-on-load');
  if (el.size() && el.text()) window.location.href = '#'+el.text();
});
