// JavaScript Document
// CSS Browser Selector v0.4.0 (Nov 02, 2010) | Rafael Lima (http://rafael.adm.br) | http://rafael.adm.br/css_browser_selector | License: http://creativecommons.org/licenses/by/2.5/ | Contributors: http://rafael.adm.br/css_browser_selector#contributors
function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

// jCarousel callback
function mycarousel_initCallback(carousel) {
  jQuery('.jcarousel-control a').bind('click', function() {
    carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr("data-position")));
    return false;
  });

  jQuery('#carousel-next').bind('click', function() {
    carousel.next();
    return false;
  });

  jQuery('#carousel-prev').bind('click', function() {
    carousel.prev();
    return false;
  });
};

// Pub Nite Helper
var RPN = new function() {
  var MONTHS = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November'
  ];

  var BUMPS = {
    0: 1, // SUN
    1: 0,
    2: 6,
    3: 5,
    4: 4,
    5: 3,
    6: 2  // SAT
  };

  function firstMonday(date) {
    date.setDate(1);
    date.setDate(1 + BUMPS[date.getDay()]);
    return date;
  }

  function thirdMonday(date) {
    var d = firstMonday(date);
    d.setDate(d.getDate() + 14);
    return d;
  }

  function cpdate(date) {
    return new Date(date.valueOf());
  }

  function isSameDate(date1, date2) {
    return date1.getFullYear() === date2.getFullYear() &&
           date1.getMonth()    === date2.getMonth() &&
           date1.getDate()     === date2.getDate();
  }

  function getDate(date) {
    var now     = date ? cpdate(date) : new Date(),
        thisRPN = thirdMonday(date ? cpdate(date) : new Date()),
        nextRPN;

    if (thisRPN.getDate() < now.getDate()) {
      if (now.getMonth() > 9) {
        nextRPN = new Date(now.getFullYear() + 1, 0);
      } else {
        nextRPN = new Date(now.getFullYear(), now.getMonth() + 1);
      }
    } else {
      nextRPN = now;
    }

    return thirdMonday(nextRPN);
  }

  this.formattedDate = function(today) {
    var now   = today ? cpdate(today) : new Date(),
        date  = getDate(today),
        month = MONTHS[date.getMonth()],
        day   = date.getDate(),
        year  = date.getFullYear();

    if (isSameDate(now, date)) {
      return 'Tonight! &ndash; 7.00 to 11.00pm';
    } else {
      return 'Monday, '+month+' '+day+', '+year+' &ndash; '+'7.00 to 11.00pm';
    }
  };
};

$(document).ready(function() {
  // Set the date for Pub Nite!
  $('#pub-nite-date').html(RPN.formattedDate());

  // Try to curb the dumbest of spammers
  $('#magic').val('3');

  // nav item sliding  
  $('#main-nav li').hover(function() {
    $(this).find('b').stop().animate({height: 80}, 550);
  }, function(){
    $(this).find('b').stop().animate({height: 0}, 250);
  });
    
  // kill animation if on selected nav item
  $('#main-nav li.selected').hover(function(){
    $(this).find('b').stop();
  });

  // jCarousel init
  $('#featured-carousel').jcarousel({
    wrap: 'circular',
    scroll: 1,
    auto: 18,
    animation: 800,
    initCallback: mycarousel_initCallback,
    itemVisibleInCallback: function(carousel, item, index, action) {
      var itemClass = $(item).attr("class").split(' ')[0];
      $('#slide-controls li').removeClass('active');
      $('#slide-controls .' + itemClass).addClass('active');
    },
    buttonNextHTML: null,
    buttonPrevHTML: null
  });
});
