function NewWindow(mypage, myname, w, h, scroll) {
    LeftPosition = screen.width ? (screen.width - w) / 2 : 0;
    TopPosition = screen.height ? (screen.height - h) / 2 : 0;
    settings = "height=" + h + ",width=" + w + ",top=" + TopPosition + ",left=" + LeftPosition + ",scrollbars=" + scroll + ",resizable=no";
    win = window.open(mypage, myname, settings);
    return win;
}

function NewWindowPrivacy(mypage, myname, w, h, scroll) {
    LeftPosition = screen.width ? (screen.width - w) / 2 : 0;
    TopPosition = screen.height ? (screen.height - h) / 2 : 0;
    settings = "height=" + h + ",width=" + w + ",top=" + TopPosition + ",left=" + LeftPosition + ",scrollbars=" + scroll + ",resizable=yes";
    win = window.open(mypage, myname, settings);
    return win;
}

var current_ad_index = 0;

LoadFrontAd = function(index) {
  current_ad_index = index;
$('#home .ad .ad-image').fadeTo(500, 0.20, function() {
    $('#home .ad .ad-image img').hide();
    $('#home .ad .ad-image img:eq('+index+')').show();
    $('#home .ad .ad-controls .pages a').removeClass('selected');
    $('#home .ad .ad-controls .call .caption').html(featured_ads[index].caption);
    $('#home .ad .ad-controls .call a').attr('href',featured_ads[index].url);
    $('#home .ad .ad-controls .pages a:eq(' + index + ')').addClass('selected');
  });
  $('#home .ad .ad-image').fadeTo(500, 0.99);
}

function switchAd(ads) {
  LoadFrontAd((current_ad_index + 1 ) % ads.length);
}

$(function() {

  $('.key-features').wrapInner("<div class='key-features-wrapper'></div>");

  $('#externalLinkDisclaimer a').click(function() {
    tb_remove();
  });

  $('a').linkDisclaimer({
    // Have to allow 'ExternalContent.spr' and 'javascript' due to sloppy programming by Silver Cloud
    valid_domains: ['sffcu.beta.trabian.com', 'sffcu.org', 'www.sffcu.org', 'sffcu.com', 'www.sffcu.com', 'sffirecu.com', 'www.sffirecu.com', 'sffire.org', 'www.sffire.org', 'sffirecu.coop', 'www.sffirecu.coop', 'secure.andera.com', 'www.24x7loans.com', 'sffirecu.loanadministration.com', 're.sffcu.org', 'eloans.mortgagemaximum.com', 'rb.sffirecu.org', 'rb.sffcu.org', 'partners.leadfusion.com', 'km2.silvercloudsoftware.com', 'ExternalContent.spr', 'ExternalSubmit.spr', 'ExternalIndex.spr', 'javascript', 'survey.sffirecu.org', '1078.extraawards.com', 's3.amazonaws.com'],
    showDisclaimer: function(link, settings) {
      $('#externalLinkDisclaimer .accept').attr('href', link.attr('href')).attr('target', '_blank');
      tb_show('External Link Disclaimer', '#TB_inline?height=190&width=350&inlineId=externalLinkDisclaimer');
      return false;
    }
  });

  $('a.button').prepend("&raquo;").wrapInner(document.createElement("span"));
  $('a.apply').wrapInner(document.createElement("span"));

  $('input[@type=text]').addClass('text')

  $('.rate_table h5').wrapInner(document.createElement("span"));

  $('a[@href=/access/online/live-chat]').click(function(e) {
    e.preventDefault();
    var new_win = NewWindow('https://srv0.velaro.com/visitor/requestchat.aspx?siteid=2921&showwhen=inqueue','VelaroChat', '400','400','no');
    new_win.focus();
    new_win.opener = window;
  });

  $('#home .ad .ad-controls .pages a').click(function(e) {
    e.preventDefault();
    clearInterval(ad_rotator);
    LoadFrontAd(parseInt($(this).text()) - 1);
  });
  
  $('.section_summaries .section_summary:first').addClass('first');

  $('.related_articles, #home ul.list').each(function() {$('li:last', this).addClass('last')});
  
  $('ul.list li:has(a[data-commentable=1])').addClass('commentable');

  $('#utility-nav li.rss').hoverIntent({
    over: function() {
      $('#rss_info_content').show();
    },
    timeout: 1000,
    out: function() {
      $('#rss_info_content').hide();
    }
  });
  
  $('.comment_admin a.mine').click(function(e) {
    e.preventDefault();
  })
  
  $('.comment_admin').hoverIntent({
    over: function() {
      $(this).find('.comment-edit').show();
    },
    timeout: 1000,
    out: function() {
      $(this).find('.comment-edit').hide();
    }
  });
  
  $('.comment_admin .comment-edit form').ajaxForm({
    success: function(responseText, statusText) {
      $('.comment_admin .comment-edit').html('Thanks!  You should be receiving an email shortly with a url for accessing your review.');
    },
    error: function() {
      $('.comment_admin .comment-edit input.text').addClass('error');
    }
  });
  
  $('#page_feedback form').ajaxForm({
    success: function(responseText, statusText) {
      $('#page_feedback .page_feedback_form').html('<p class="submitted">Thanks for the feedback!</p>');
      // $('#page_feedback input').hide();
    },
    error: function() {
      $('#page_feedback .page_feedback_form').addClass('error');
    }
  });
  
  $('.askdiana form').ajaxForm({
    success: function(responseText, statusText) {
      $('.askdiana div.box').html('<p class="submitted">Thank you for submitting a question to Diana. You can expect a response within 24 hours.</p>');
      // $('#page_feedback input').hide();
    },
    error: function() {
      $('.askdiana div.box').prepend('<p class="error">Please make sure you have included a valid email address and question.</p>');
    }
  });
  
  $('#response form :text,#response form textarea, .get-involved :text, .get-involved textarea').each(function() {
    this.value = this.title;
  });
  $('#response form').ajaxForm({
    beforeSubmit: function(formData, formObject, formOptions) {
      labeled_objects = formObject.find(':text, textarea').map(function() {
        if (this.title == this.value) {return false;}
      });
      if (0 < labeled_objects.length) {
        formOptions.error();
        return false;
      } else {return true;}
    },
    success: function(responseText, statusText) {
      $('#response .response_form').html('<p class="submitted">Thank you for your response.</p>');
    },
    error: function() {
      $('.response_form').prepend('<p class="error">Please complete all fields before sending!</p>');
    }
  });
  
  if (0 < $('#videoplayer').length) {
    flowplayer("videoplayer","/javascripts/flowplayer-3.0.5.swf", {
      clip: {autoPlay: false},
      canvas: {backgroundImage: 'url(' + $('#videoplayer img').attr('src') + ')'}
    }).load();
  }
  
  $.validator.addMethod('required_with_title',function(value,element) {
    return value.length && value != element.title;
  }, $.format(" is required."));
  $('.get-involved form').validate({
    ignoreTitle: true,
    showErrors: function(errorMap, errorList) {
      if (0 < this.numberOfInvalids()) {$('.get-involved .errors').show();}
      else {$('.get-involved .errors').hide();}
      this.defaultShowErrors();
    },
    errorPlacement: function(error,element) {return;}
  });
  $('.get-involved :text, .get-involved textarea').focus(function() {
    if (this.title == this.value) this.value='';
  }).blur(function() {
    if ('' == $(this).val()) this.value=this.title;
  });

});