$(function() {
  
  var cms_domains = [
    'javascript',
    'belvoircreditunion.org',
    'belvoirfcu.org',    
    $.uri.key('host')
  ];

  var internal_domains = [
    'javascript',
    'belvoircreditunion.org',
    'belvoirfcu.org',
    'bob.ftbfcu.net',
    'ezcardinfo.com',
    'eservices.digitalmailer.com',
    'belvoircuxcel.com',
    'secure.loanspq.com',
    'cuview.com',
    'usa.visa.com',
    'feeds2.feedburner.com',
    'cucalc.cuna.org',
    'webpmt.com',
    $.uri.key('host')
  ];

  var trusted_domains = [
    'memberadvantagemortgage.com',
    'memberadvantagemortgage.mortgagewebcenter.com',
    'fiswitchkit.com',
    'belvoirfs.org',
    'cunamutual.com',
    'reorder.libertysite.com',
    'scorecardrewards.com',
    'clickvue.com',
    'iwsgroup.com',
    'belvoircreditunion.webex.com',
    'hffo.cuna.org',
    'anytime.cuna.org',
    'gti.cuna.org',
    'googolplex.cuna.org',
    'cu24.com',
    'mastercard.com',
    'affn.org',
    'mapquest.com',
    'cuservicecenter.com',
    'ezforex.com',
    'investor.newtekbusinessservices.com',
    'newtekreferrals.com'
  ]

  var showDisclaimer = function(link, message_id, message_title, message_settings) {
    $('#' + message_id + ' .accept').unbind('click').click(function(e) {
      e.preventDefault();
      window.location = link;
      tb_remove();
    });
    tb_show(message_title, message_settings);
    return false;  
  }

  $('a[href]').livequery(function() {

    var host = this.hostname.replace('www.', '');
    
    var link = $(this);
    
    $.each(['#', '/', 'mailto'], function() {
  	  if (link.attr('href').indexOf(this) == 0) {
  	    return;
  	  } else if (0 > jQuery.inArray(host, cms_domains)) {
  	    // Bind a click event to log all outbound clicks with google ananyltics
  	    link.unbind('click.google_outbound_tracking');
        link.bind('click.google_outbound_tracking', function(e) {
          try {
            console.log('logging...', host)
            pageTracker._trackPageview('/outbound/' + host + this.search);
            secondaryTracker._trackPageview('/outbound/' + host + this.search);
          } catch(e) {;}
          return true; // continue processing events
        });
  	  }
  	});
        
    if (jQuery.inArray(host, internal_domains) >= 0) {
      // Don't do anything
    } else if (jQuery.inArray(host, trusted_domains) >= 0) {
      $(this).addClass('speedbump');
      $(this).click(function(e) {
        e.preventDefault();
        showDisclaimer($(this).attr('href'), "trusted-speedbump", "You have requested an external link from a trusted provider.", '#TB_inline?height=90&width=350&inlineId=trusted-speedbump');
      });
    } else {
      $(this).addClass('speedbump')
      $(this).click(function(e) {
        e.preventDefault();
        showDisclaimer($(this).attr('href'), "speedbump", "You have requested an external link.", '#TB_inline?height=210&width=350&inlineId=speedbump');
      });
    }
  });

  $('table.styled tr:even:not(.header),table.rates tbody tr:even').addClass('alt');

  $('#navigation li.depth_0:not(.selected)').hoverIntent({
    sensitivity: 3,
    interval: 50,
    timeout: 300,
    over: function() {
      $('#navigation li.selected ul').hide();
      $(this).find('a').addClass('hover');
      $(this).addClass('hover');
    },
    out: function() {
      $(this).removeClass('hover');
      $(this).find('a').removeClass('hover');
      $('#navigation li.selected ul').show();
    }
  });

  $('#home-page-ads').cycle({
    containerResize: false,
    pager: '#ad_nav',
    speed: 2000,
    timeout: 6000
  });

  $('.online-banking select').change(function() {
    $('.online-banking form > div').hide();
    $('#' + $(this).fieldValue() + '-buttons').show();
  });

  $('#left .poll').wrap('<div class="poll-wrapper"></div>');

  $('.section_summary:last, .panel li:last').addClass('last');
  
  $('.section_summary:first, .panel li:first').addClass('first');
  
  $('#sidebar-navigation > ul > li:has(ul)').append("<a href='#' class='trigger'>trigger</a>");

  $('#sidebar-navigation li').hoverIntent(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });

  $('#sidebar-navigation .trigger').livequery(function() {
    $(this).click(function() {
      $(this).parent('li').toggleClass('open');
    });
  });

  $('.quick-nav form input:text').focus(function() {
    $(this).val('').unbind('focus');
  });

  $('body:not(.no-right-sidebar) .white-box .overview-list').column_list({
    selector: '.overview-section',
    columns: 2
  });

  $('.no-right-sidebar .white-box .overview-list').column_list({
    selector: '.overview-section',
    columns: 3
  });
  
  $('.column_list_2').column_list({columns: 2});
  $('.column_list_3').column_list({columns: 3});

  $('#content ul.actions').wrap("<div class='action_wrapper'></div>");

  $('.faqs').prev('h2').append(' <a href="#" class="expand_all">Expand all</a>');
  
  $('.expand_all').click(function(e) {
    e.preventDefault();
    $('.faqs blockquote').show();
  });

  $('.faqs h3').click(function(e) {
    $(this).next('blockquote:first').toggle();
  });

  $('.faqs h2:first').addClass('first');
  
  $('#rss_info_content').appendTo('#hd .quick-nav li:has(a.subscribe)');

  $('#hd .quick-nav li:has(a.subscribe)').hoverIntent({
    over: function() {
      $('#hd .quick-nav form, #navigation .meet-us, #navigation .updates').hide();
      $('#rss_info_content').show();
    },
    timeout: 1000,
    out: function() {
      $('#rss_info_content').hide();
      $('#hd .quick-nav form, #navigation .meet-us, #navigation .updates').show();
    }
  });
  
  $('#TB_ajaxContent a').livequery(function() {
    $(this).click(function(e) {
      e.preventDefault();
      tb_remove();
    });
  });
  
  $('ul.actions li.email').click(function(e) {
    e.preventDefault();
    tb_show('Share this page with a friend:', '#TB_inline?height=150&width=350&inlineId=share_email');
    $('#share-email-input').val(window.location).focus()[0].select();
  });
  
  $('ul.actions li.print').click(function(e) {
    e.preventDefault();
    window.print();
  });

  $('ul.actions li.apply a:not(.speedbump)').click(function(e) {
    if ($(this).hasClass('speedbump')) {return true;}
    e.preventDefault();
    var url = $(this).attr('href');
    if (url.indexOf('#') >= 0) {
      window.location = "https://bob.ftbfcu.net/cgi-bin/mcw000.cgi?MCWSTART";
    } else {
      window.location = url;
    }
  });

  $('ul.actions li.open a').click(function(e) {
    e.preventDefault();
    var url = $(this).attr('href');
    if (url.indexOf('#') >= 0) {
      window.location = "https://secure.loanspq.com/Consumer/login/default.aspx?LenderID=abc91586ee2248138d4e69e3533c8062";
    } else {
      window.location = url;
    }
  });

  var tab = $('li:has(a[href$="#tab-' + location.hash.replace('#', '') + '"])');
  var tab_index = $('#tabs li').index(tab[0]);

  $('#tabs').tabs(tab_index + 1);

  $.historyInit(function(hash) {
    // $('ul#tabs').tabs('select',hash);
    // return true;
  });


  $("ul#tabs li a").livequery(function() {
    $(this).click(function(e){
      $.historyLoad(this.href.replace(/^.*#tab-/, ''));
      return false;
    });
  });

  $('.for-life form').submit(function(e) {
    e.preventDefault();
    window.location = $('#for-life-navigation').val();
    return false;
  });

  $('#for-life-navigation-go').click(function(e) {
    e.preventDefault();
    $('.for-life form').submit();
  });
  
  $('#comment-form form').ajaxForm({
		success: function(responseText, statusText) {
			$('#comment-form form').hide();
			$('#comment-submitted').show();
		}
	});

});