/* Author: Jeff Gould

*/
(function($){$(document).ready(function(){
  
  $fpSlider = $('#fp-slider');
  if($fpSlider.length){
    $fpSlider
    .after('<div id="fp-slide-nav" />')
    .cycle({
      pager: '#fp-slide-nav',
      timeout: 8000,
      speed: 1500,
      fx: 'fade'    
    });
  }


  $nav = $('#nav');
  
  $nav.find('>ul>li').hoverIntent({
    sensitivity: 7,
    interval: 100, 
    over: function(){
      $(this).addClass('hover').siblings().removeClass('hover');
    }, 
    timeout: 500, 
    out: function(){
      $(this).removeClass('hover');
    }
  });
  
  
  $eventBoxes = $('li.event-container');
  if($eventBoxes.length){
    
    var boxOffset = '-25 0';
    if( $eventBoxes.parents('.week').length ) boxOffset = ''; //in the week view events boxes don't need an offset
    
    $eventBoxes.find('>.event')
      .css({display: 'block', opacity: 0})
      .each(function(){
        $this = $(this);
        $this.position({
            my: 'left center',
            at: 'right center',
            of: $(this).parent(),
            offset: '-25 0',
            collision: 'flip fit'
        }).hide();
      }
    );
    $eventBoxes.hoverIntent({
      sensitivity: 7,
      interval: 200,
      timeout: 200,
      over: function(e){
        var $this = $(this),
            eventID = $this.attr('data-id');
            
        $this.addClass('active');
        $eventBoxes.filter('[data-id="'+ eventID +'"]').addClass('sub-active');
        
        $this.find(' > .event')
          .css({display: 'block', opacity: .05})
          .position({
            my: 'left center',
            at: 'right center',
            of: $this,
            offset: boxOffset,
            collision: 'flip fit'
          })
          .fadeTo(400, 1)
          ;
      },
      out: function(e){
        $this = $(this);
        $this.find('>.event').fadeOut('fast');
        $eventBoxes.removeClass('active').removeClass('sub-active');
      }
    });
      
  }



$('.entry-content ol > li').each(function(){ //make pretty OLs when it's easy (just text inside of the <li> no other tags)
  if(!$(this).children().length) $(this).html('<p>' + $(this).text() + '</p>').parent().addClass('wrapped');
})

$('.post-footer').equalHeights(); //make the post-footer sections the same height
$('#update-articles').equalHeights(); //make the front page update articles the same height

$('.sidebar-section').filter(':first-child').addClass('first').end().filter(':last-child').addClass('last');
if(!$('.sidebar-topper').children().length) $('.sidebar-topper').remove();

$('a#sba-disclaimer').tooltip({
  effect: 'slide'
}).click(function(e){
  e.preventDefault();
});



$datepicker = $('#datepicker');

if($datepicker.length){
  
  var homeURL = $datepicker.data('url'),
      goURL = homeURL + 'calendar/',
      dpDate = $datepicker.data('date');
  
  $datepicker.datepicker({
    dateFormat: 'yy-mm-dd',
    defaultDate: dpDate,
    onSelect: function(date, instance){
      var qs = window.location.search,
          sep = '&';
      
      if(!qs.length) window.location = goURL + '?cdate=' + date;
      else{
        qs = qs.replace(/\&?cdate\=[^&]+/, '');
        qs = qs.replace(/\&?view\=[^&]+/, '');
        qs = qs.replace(/\&?cweek\=[^&]+/, '');
        qs = qs.replace(/\&?cmonth\=[^&]+/, '');
        qs = qs.replace(/\&?cyear=[^&]+/, '');
        qs = qs.replace(/\&?cday\=[^&]+/, '');
        if(qs == '?') sep='';
        if(qs == '') sep = '?';
        window.location = goURL + qs + sep + 'cdate=' + date;
      }
    }//onSelect
  });
  
}

$filterChecks = $('.filters').find('input.filter[type="checkbox"]');
if($filterChecks.length){
  
  $filterChecks.change(function(e){
    var qs = window.location.search,
        sep = '&',
        categoryQS = '',
        campusQS = '';
    qs = qs.replace(/\&?category\=[^&]+/, '');
    qs = qs.replace(/\&?campus\=[^&]+/, '');
    if(qs == '?') sep='';
    if(qs == '') sep = '?';
    
    $categoryChecks = $filterChecks.filter('[name="category"]').filter('[checked]');
    $campusChecks = $filterChecks.filter('[name="campus"]').filter('[checked]');
    
    if($categoryChecks.length){
      categoryQS = sep + 'category=';
      sep = '&';
      $categoryChecks.each(function(index){
        categoryQS += this.value
        if(index+1 < $categoryChecks.length) categoryQS += ',';
      });
    }//if any category checks are checked
    
    if($campusChecks.length){
      campusQS = sep + 'campus=';
      sep = '&';
      $campusChecks.each(function(index){
        campusQS += this.value
        if(index+1 < $campusChecks.length) campusQS += ',';
      });
    }//if any campus checks are checked
        
    window.location.search = qs + categoryQS + campusQS;
    
  });//change checks

}



});})(this.jQuery);
