/**
 * scripts for making slideshows work
 */

function slideshowSwitch() {
  var fadeSpeed = 1000;
  var $active = $('#slideshow-picture div.active');
  if ( $active.length == 0 ) $active = $('#slideshow-picture div:last');
  var $next =  $active.next().length ? $active.next()
      : $('#slideshow-picture div:first');
  $next.addClass('nextup');
  $active.fadeOut(fadeSpeed, function () {
        $(this).removeClass('active');
        $(this).css('display','block');
        $next.addClass('active');
        $next.removeClass('nextup');
  });
  var slideshowTitle = $('#slideshow-picture div.nextup img').attr("alt");
  $('#slideshow-title').append('<span style="display: none;">'+slideshowTitle+'</span>');
  $('#slideshow-title span:first').fadeOut(fadeSpeed, function() {
        $(this).remove();
  });
  $('#slideshow-title span:last').fadeIn(fadeSpeed);
}

/*
// do vertical centering of any pictures that are too wide relative to height
$(document).ready(function(){
  $("div#slideshow-picture div").each(function (i){
    var divheight = $(this).height();
    var imgheight = $(this).children("img").height();
    var topmargin = (divheight - imgheight) / 2;
    $(this).children("img").css("margin-top",topmargin+"px");
  });
});
*/

// start the slideshow once all images have been loaded!
$(window).load(function(){
  slideshowInterval = setInterval( "slideshowSwitch()", 2500 );
});

/**
 * for ajax form submission
 */


// this assumes .ajax-form-wrapper .submitting and .ajax-form-wrapper .submitted
// have border width of 1.  Change to 0 (no border) or 2x border width
// if you change the css
borderWidthSubmitting = 0;
borderWidthSubmitted = 2;

$(document).ready(function(){

  $('.ajax-form').each(function() {
    var w = $(this).outerWidth();
    var h = $(this).outerHeight();
    var mt = (h/2)-11;
    var ml = (w/2)-51;
    var w1 = w - borderWidthSubmitting;
    var h1 = h - borderWidthSubmitting;
    var subhtml1 = '<div class="submitting" style="width: '+w1+'px; height: '+h1+'px;"><div style="margin: '+mt+'px 0 0 '+ml+'px;">&nbsp;</div></div>';
    var w2 = w - borderWidthSubmitted;
    var h2 = h - borderWidthSubmitted;
    var subhtml2 = '<div class="submitted" style="width: '+w2+'px; height: '+h2+'px;"><div></div></div>';
    $(this).wrap('<div class="ajax-form-wrapper" style="width: '+w+'px; height: '+h+'px;"></div>');
    $(this).parent().prepend(subhtml1).append(subhtml2);
  });

  $('.ajax-form').submit(function() {
    var $form = $(this);
    $form.find('#subform-fullname').val($form.find('#subform-name').val() + ' ' + $form.find('#subform-uelocal').val());
    var data = $form.find('input');
    $form.parent().children('.submitting').css('visibility','visible');
    $.post($form.attr('action'), data, function (data, textStatus) {
      if (textStatus != 'success') {
        var response = 'Error.  Please refresh the page and try again.';
      } else {
        var idx1 = data.indexOf('</h1>');
        var idx2 = data.indexOf('<hr>');
        var response = data.substr(idx1+5,(idx2-idx1)-5)+' Please refresh the page if you made an error and need to try again.';
      }
      $form.parent().children('.submitting').css('visibility','hidden');
      $form.parent().children('.submitted').children('div').html(response);
      $form.parent().children('.submitted').css('height','auto');
      $form.slideUp('slow');  
    });

    return false;
  });

});
