$(function() {
  
  // Home page rotating bill boards
  var rotation_timeout = null;
  var rotation_interval = null;
  var rotation_period = 10000; // 10 seconds
  var rotate = function($trigger) {
    if ($trigger.hasClass('current')) { return false; }
    var index = parseInt($trigger.attr('data-trigger'));
    var l = (-980 * index) + 'px';
    $('#rotations .scroller').animate({ left: l }, 500, 'swing');
    // update the controls
    $trigger.parent().find('.current').removeClass('current');
    $trigger.addClass('current');
  };
  var auto_rotate = function() {
    var index = parseInt($('#rotations .ctrl li.current').attr('data-trigger'));
    var next = index + 1;
    var $li = $('#rotations .ctrl li');
    if (next == $li.length) {
      next = 0;
    }
    rotate($($li.get(next)));
  }
  if ($('#rotations').length > 0) {
    rotation_interval = window.setInterval(auto_rotate, rotation_period);
  }
  $('#rotations .ctrl li').click(function() {
    rotate($(this));
    window.clearInterval(rotation_interval);
    window.clearTimeout(rotation_timeout);
    rotation_timeout = window.setTimeout(function() {
      rotation_interval = window.setInterval(auto_rotate, rotation_period);
    }, rotation_period * 2);
  });

  // sub nav
  if ($('ul.nav_copy').length > 0) {
    $('.copy .section').hide();
    $('ul.nav_copy li a').click(function() {
      var $me = $(this);
      var selector = $me.attr('href');
      $('.copy .section').hide();
      $(selector).show();
      $('ul.nav_copy .current').removeClass('current');
      $me.parent().addClass('current');
      $(document).trigger('height_change');
      return false;
    });
    $('ul.nav_copy .current a').click();
  }
  $('.about ul.nav_copy li a').click(function() {
    $('.bb.mic').show();
    if ($(this).parent().hasClass('about_radio')) {
      $('.bb.mic').hide();
    }
    return false;
  });


  // forms
  // validation
  var validate = function($form) {
    var valid = true;
    $form.find('.req').each(function() {
      if ($.trim($(this).val()) == '' && !$(this).hasClass('decoy')) {
        $('div[data-for='+$(this).attr('id')+']').show();
        valid = false;
      }
    });
    $form.find('.valid_email:visible').each(function() {
      if ($(this).val().match(/^\s*\w+@\w+\.\w+\s*$/) === null && !$(this).hasClass('decoy')) {
        $('div[data-for='+$(this).attr('id')+']').show();
        valid = false;
      }
    });
    $(document).trigger('height_change');
    return valid;
  };
  // Compact forms (label appears in the input)
  $('form.compact input[type=text], form.compact textarea').compact();
  // Submission of all forms
  $('form .button').click(function() {
    //$('#question').find('input, textarea').each(function() { });
    var $form = $(this).closest('form');
    $form.find('.error').hide();
    $(document).trigger('height_change');
    if (!validate($form)) { return false; }

    $.post($form.attr('action'), $form.serialize(), function(data) {
      // post success processing
      if (data != ':(') {
        $form.find('.initial').hide();
        if (data.indexOf('pdf') > 0) {
          $('.msg a.button').attr('href', data);
        }
        $('.msg').show();
      }
    });
    return false;
  });


  // secondary height match
  $(document).bind('height_change',function() {
    window.setTimeout(function() {
      var $s = $('.secondary');
      var min = parseInt($s.css('min-height'));
      var h = $('.main').height();
      if (h > min) {
        $s.css('height',h+'px');
      }
      else {
        $s.css('height',min+'px');
      }
    }, 10);
  });
  $(document).trigger('height_change');

  // hash links
  if (window.location.hash.indexOf('radio') > 0) {
    $('.nav_copy .about_radio a').click();
  }
  $('a[href=/about/#radio]').click(function() {
    if (window.location.href.indexOf('about') > 0) {
      $('.nav_copy .about_radio a').click();
    }
  });
  if (window.location.href.indexOf('download') > 0) {
    if (window.location.hash.indexOf('ira') > 0) {
      $('.summary.ira').show();
      $('#r_pdf').val("FIC IRA Rollover");
    }
    else {
      $('.summary.retirement').show();
      $('#r_pdf').val("FIC Retirement Insights");
    }
  }

  // touts
  $('.bb, .question, .free_copy').click(function() {
    if ($(this).is('a')) { return; }
    window.location = $(this).find('a').attr('href');
    return false;
  });

  $('#retiree img').click(function() {
    window.location = $(this).parent().parent().find('a').attr('href');
    return false;
  });

});

