$(function() {

/*******************************************************************************
 * Countdown function
 ******************************************************************************/
   function limitText(limitField, limitCount, limitNum) {
  	if (limitField.value.length > limitNum) {
  		limitField.value = limitField.value.substring(0, limitNum);
  	} else {
  		limitCount.value = limitNum - limitField.value.length;
  	}
  } 

  $('a.emailtofriend').click(function() {    
    mail_str = "mailto:?subject=Check out the " + document.title;
    mail_str += "&body=I thought you might be interested in the " + document.title;
    mail_str += ". You can view it at, " + location.href;
    location.href = mail_str;
    return false;
  });
  
  $('a.printpage').click(function() {
    window.print();
    return false;
  });
  
  $('#main ol').css('font', 'italic bold 16px Georgia');
  $('#main ol li').wrapInner('<span></span>');
  
  $('#main table.data tr:even').css('background', '#EFFBFF');
  
/*******************************************************************************
 * Form scripting
 ******************************************************************************/
 
  // allow only one click
  /*
  $('#submitDonation').click(function() {
    $(this).attr('disabled', 'disabled');
    return true;
  });
  */
/** Investment Amount/Type ---------------------------------------------------*/  

  if ($('input[name="investmenttype"][value="multi"]').attr('checked') == false) {
    $('#wrap-multiinvest').css('display', 'none');
    $('#amount-range input').attr('disabled', 'disabled');
  } else {
    $('input[name="samount"]').attr('disabled', 'disabled');
  }
  
  
  $('input[name="investmenttype"]').click(function() {
    if($(this).attr('checked') && $(this).attr('value') == 'multi') {
      $('#amount-range input').attr('disabled', '');
      $('input[name="samount"]').attr('disabled', 'disabled');
      $('#wrap-multiinvest').slideDown('slow');
    } else {
      $('#amount-range input').attr('disabled', 'disabled');
      $('input[name="samount"]').attr('disabled', '');
      $('#wrap-multiinvest').slideUp('slow');
    }
    
  });

/** Tribute Gift -------------------------------------------------------------*/   

  if ($('input[name="tg[gift]"]').attr('checked') == false) {
    $('#tribute').css('display', 'none');
  } else {
    $('#btnTribute').empty().append('Cancel Tribute Gift');
  }
  // causes bug in Firefox... makes radio button switch on refresh:
  //$('#tributeButtonToggle').prepend('<button id="btnTribute">Make this a Tribute Gift</button>&nbsp;&nbsp;');
  $('#btnTribute').css('display', 'inline');


  //tribute - slide open function
  $('#btnTribute').toggle(
  function () {
    $('#tribute').slideDown('slow');
    $(this).empty().append('Cancel Tribute Gift');
    $("input[name='tg[gift]']").attr('checked', 'checked');
  },
  function () {
    $('#tribute').slideUp('slow');
    $(this).empty().append('Make this a Tribute Gift');
    $("input[name='tg[gift]']").attr('checked', '');
  })

  $("#tributeCheckbox").css('display', 'none');

  $('#personalMessage').keydown(function () {
    limitText(this.form.personalmessage,this.form.countdown,250);
  });
  
  $('#personalMessage').keyup(function () {
    limitText(this.form.personalmessage,this.form.countdown,250);
  });

/** Investment Designation ---------------------------------------------------*/  

  $('#department').click(function(){
    $('#directGift').attr('checked', 'checked');
  });

/** Investor Information -----------------------------------------------------*/
  if ($('input[name="optional[alumni]"]').attr('checked') == false) {
    $('#alumni-year').css('display', 'none');
  }
  
  $('input[name="optional[alumni]"]').click(function() {
       if($(this).attr("checked")) {
          $('#alumni-year').slideDown('slow');
       } else {
         $('#alumni-year').slideUp('slow');
       }  
  
  });   
  
/*******************************************************************************
 * Popup Boxes
 ******************************************************************************/

  $('#main a[class="mediapopup"]').click( function() {
    window.open( $(this).attr('href'), 'clovisMediaWindow', 'width=420,height=370,status=0,toolbar=0,menubar=0,scrollbars=1,left=300,top=200' );
    return false;
  });
  
  $('#main a[class="tributepopup"]').click( function() {
    window.open( $(this).attr('href'), 'clovisTributeWindow', 'width=620,height=540,status=0,toolbar=0,menubar=0,scrollbars=1,left=300,top=200' );
    return false;
  });  

/*******************************************************************************
 * Bolding Beginning of Captions on Directors Page
 ******************************************************************************/

  $('.wp-caption-text').each(function(){
    var mystring = $(this).html();
    var firstword = mystring.replace(/-/,'</strong><br/><span style="display: none;">-</span>');
    var newthing = "<strong>"+firstword;
    $(this).html(newthing);
  });

/****** end function ********/
});
