
// dom ready
$(document).ready(function(){
  // set up foldable boxes
	$(".box-top.open, .box-top.close").setTogglePlus();
	$('.scroll-to-top').scrollToAnchor(500, 'easeOutSine');
	$('.scroll-to-anchor').scrollToAnchor(500, 'easeOutSine');
	// fancybox
	if($.fn.fancybox){
    	$("a[rel='fancybox']").fancybox();
  }
  // tabs
  setTabs();
});


jQuery.fn.extend ({
	// expand/fold boxes
	setTogglePlus: function(e) {
	  	return this.each(function(i, elt) {
			 	var box = $(elt);
			  	var toggle= box.children().find('.toggle');
		   		var h3 = box.children().find('h3');
			toggle.bind("click", function(e){
			    if(box.hasClass("open")){
					h3.next('.box-content').slideUp(150);
					box.removeClass("open").addClass("close");
			    } else if(box.hasClass("close")) {
					h3.next('.box-content').slideDown(150);
					box.removeClass("close").addClass("open");
			    }
			});
		}); 
	}, 

	//  Scroll to anchor
	scrollToAnchor: function(s, e) {
	  	return this.each(function() {
			$(this).click(function(){
					$(document).scrollTo($(this).attr('href'),s,{ easing: e });
					return false;
			}); 
		});
	}
});

function setTabs() {
  var tabs = $('.tabs');
  var items_prefix = "news-";
  if(tabs.length > 0){
    var lis = tabs.find('li');
    lis.each(function(i, elt){
      // items associated to each year/tab
      var year = $(elt).text();
      var year_items = $('.' + items_prefix + year);
      var other_items = $('div[class^=' + items_prefix + ']').filter(function () { return !($(this).hasClass(items_prefix + year)); });
      var other_lis = lis.filter(function (j) { return i !== j; });
      // when the tab is clicked, it displays the year's items
      $(elt).bind('click', 
        function(e){
          $(this).addClass('current').focus();
          other_lis.removeClass('current');
          year_items.show();
          other_items.hide();
      });
      // set current tab
      if(i===0) {
        $(elt).trigger('click');
      }
    });
  }
}
