//Activate menus.  Any menu with the 'activateable' class on its <ul> element will be activated by these functions.
//Being 'activated' means that when a menu item is clicked, its classes are updated to be 'active' while all other menu items' 'active' classes are removed.

$(document).ready(function() {
	//Activate the appropriate button according to the number in the hash (used by coda slider)
	activate_menu_by_index(location.hash.substr(1));
	
	//Activate menu buttons when clicked
  $("ul.activateable a").click(function(event){
		var item = $(event.target).parent().parent();
		var list = item.parent().parent();
		activate_menu(list, item);
  });
});

//Called directly from Coda Slider's right and left arrow click functions.  (For details, search jquery.coda-slider-2.0.js for 'ADDED BY RICH')
function activate_menu_by_index(i) {
	i = parseInt(i);
	if(isNaN(i))
		return;
	
	//A little bit of a hack.
	if(location.href.indexOf('case-studies')==-1)
		i = i-1;
	
	//console.log('Activating index:' + i);
	var list = $('ul.slider-menu.activateable');
	if(i>0)	var item = $('li:nth-child('+i+')', list);
	else		var item = null;
	activate_menu(list, item);
}

function activate_menu(list, item) {
	//console.log(item);
	$("a, span", list).removeClass("active"); //Remove all 'active' classes from the <ul>
	if(item!=null)
		$("a, span", item).addClass("active"); //Add 'active' class to the correct <li>'s <a> and <span>
}
