function Tabs(sel,cookie) {
  var tabs = jQuery(sel).find('a'),
      hasCookie = typeof cookie != 'undefined',
      inited = false;
  tabs.Hide = function() {
    tabs.each(function() {
      var anch = this.href.replace(/^.*#/,'#');
      jQuery(this.parentNode).removeClass('selected');
      jQuery(anch).hide();
    }); 
  }; 
  tabs.Hide(); 
  tabs.bind('click',function() {
    var anch = this.href.replace(/^.*#/,'#');
    tabs.Hide();
    jQuery(this.parentNode).addClass('selected');
    jQuery(this).blur();
    jQuery(anch).show();
    if (hasCookie) {
      jQuery.cookie(cookie,anch.slice(1),{expires:100});
    }
    return false;
  }); 
  tabs.showTab = function(tab) {
    var anch = null;
    if (typeof tab == 'number') anch = tabs.eq(tab);
    else for (var i=0; i<tabs.length; i++) {
      if (tabs.eq(i).attr('href').replace(/^[^#]*#/g,'') == tab) anch = tabs.eq(i);
    }
    if (anch) {
      anch.trigger('click');
      return true;
    }
    return false;
  }
  if (hasCookie) {
    var anch = jQuery.cookie(cookie);
    if (typeof anch == 'string' && anch.length > 0) inited = tabs.showTab(anch);
  }
  if (!inited) tabs.showTab(0);  
  return tabs;
}

function MSC() {
  return MSC;
};

MSC.now = (function() {
  var today = new Date;
  today.setDate(today.getDate() - 1); // take off a day so today's events are still in the 'future'
  function num(n) { return n <= 9 ? '0'+n : ''+n; }
  return ['D_',today.getFullYear(),num(today.getMonth()+1),num(today.getDate()),
              num(today.getHours()),num(today.getMinutes())].join('');
})();
MSC.now_ = MSC.now.replace(/^D_/,'D');
MSC.today = MSC.now.slice(2,10);

function breadCrumbs(a) {
  var s = [];
  a.unshift(['home','/']);
  for (var i=0; i<a.length; i++) {
    var crumb = a[i];
    if (crumb.length > 1) s.push('<a href="' + crumb[1] + '">' + crumb[0] + '</a>');
    else s.push(crumb[0]);
  }
  jQuery(document).ready(function() {
    jQuery('#breadCrumbs').html("you are here: <span>"+s.join(' &rsaquo; ')+"</span>");
  });
}

function expireAndAwakenAll() {
  var dateRE = /D_{0,1}(\d{8})/;
  jQuery('.expires').each(function() {
      var expiration = this.className.match(dateRE);
    if (expiration && MSC.today >= expiration[1]) this.style.display = 'none';
  });
  jQuery('.awakens').each(function() {
    var display = 'block',awakens = this.className.match(dateRE);
    if (awakens && MSC.today >= awakens[1]) {
      if (this.nodeName.toLowerCase == 'span') display = 'inline';
      else if (this.className.match('display:inline')) display = 'inline';
      this.style.display = display;
    }
  })
}

function showNav(id,submenu) {
  $('#subNav').html('').append(submenu.clone(true).css('display','block'));
}
function clearNav() {jQuery('#subNav').html('');}
function defSubMenu(btn,menu) {
  $(btn).hoverIntent(function() {showNav(btn,$(menu))},function() {});
}                                
function curMenu(id,subid) {
  jQuery(document).ready(function() {
    jQuery(id).addClass('active');
    if (subid) {
      jQuery(subid).addClass('active');
      showNav(id,$(subid).parent());
    }
  });
}
jQuery(document).ready(function() {
  function showTime() {
    var now = new Date,s,hr,mn,ampm;
        mon = ['january','february','march','april','may','june','july','august','september','october','november','december'];
     
    hr = now.getHours();
    mn = now.getMinutes();
    if (mn < 10) mn = '0' + mn;
    ampm = (hr < 12) ? 'am' : 'pm';
    if (hr > 12) hr -= 12;
    
    s = hr + ":" + mn + ampm + " - " +  
        mon[now.getMonth()] + " " + now.getDate() + ", " + now.getFullYear();
    jQuery('#today').html(s.replace(/ /g,'&nbsp;'));
    return arguments.callee;
  }
  setInterval(showTime(),60000);
  expireAndAwakenAll();
  

  jQuery('a.emailAddr').each(function() {this.href = 'mailto:' + jQuery(this).text();});
  jQuery('a.emailRel').each(function() {
    this.href = 'mailto:' + this.rel.replace(/\s+/g,'').replace(/\*/,'@');      
  })
  defSubMenu('#clubBtn','#clubNav');
  defSubMenu('#socialBtn','#socialNav');
  defSubMenu('#teamBtn','#teamNav');
  defSubMenu('#pgmBtn','#programNav');
  defSubMenu('#facilBtn','#facilitiesNav');
  jQuery('#topNav a #homeBtn').hover(
    function() {clearNav()},
    function() {}
  );
  jQuery('a:not([title])').filter('.NEW-WIN,.PDF,.WORD').each(function() {
    var t = jQuery(this),title = [];
    if (t.hasClass('NEW-WIN')) title.push('Link opens in a new window');
    if (t.hasClass('PDF')) title.push('PDF document');
    else if (t.hasClass('WORD')) title.push('Microsoft Word document');
    this.title = title.join('\r\n');
  });
});
