/* Multibox rotation and selection */

 // Number of seconds between rotations
var multiboxRotateInterval = 12;

// Ordered array of links to each multibox section's archive page
var archiveLinksArray = new Array(
		'faculty-spotlight-archive', 
		'department-announcement-archive'
	);



/* ------------------------------------------------------------------------------- */
var currentMultiboxNum = 1;
var number_of_multibox_content_panes = 0;
var multiboxInterval = null;

$(document).ready(function() {
	number_of_multibox_content_panes = $('#multibox div.multibox_content').size();
	
	if($('#multibox').size()){
		multiboxInterval = setInterval("multiboxRotate()",multiboxRotateInterval*1000);
	}
});


/* Called on a timer to select the next button in the sequence */
function multiboxRotate(){
	button = $('#multibox ul li.multibox-button').get(currentMultiboxNum);
	multiboxSelect(button, currentMultiboxNum);
	currentMultiboxNum = (currentMultiboxNum == (number_of_multibox_content_panes-1)) ? 0 : currentMultiboxNum + 1;
}


/* Does the actual change of selected button and content */
function multiboxSelect(button, button_index) {
	multiboxSwapButtons(button);
	multiboxSwapContent(button_index);
	multiboxSwapLinks(button_index);
}

/* Swap unselected/selected buttons */
function multiboxSwapButtons(button) {
	$('#multibox ul li.selected').removeClass('selected');									//unselect button
	$(button).addClass('selected');															//select selected button
}

/* Swap in the active content pane */
function multiboxSwapContent(button_index) {
	$('#multibox div.selected').removeClass('selected').fadeOut();							//unselect/fadeout content
	$($('#multibox div.multibox_content').get(button_index)).addClass('selected').fadeIn(); //select/fadein selected content
}

/* Swap the header and archive links for the active content archive link */
function multiboxSwapLinks(button_index) {
	var link_url = archiveLinksArray[button_index];
	var replacement_url = null;
	
	var header_anchor = $('#multibox h2 a#multibox-header-link');
	var archive_anchor = $('#multibox #archive-link a');
	
	var header_url = header_anchor.attr('href'); // Both url's will be the same, using the header 'just cause'
	var base_path = multiboxGetBasePath(header_url, link_url);

	//change archive/header link 
	if(!base_path) replacement_url = '#'; //base_path not found, lets use an empty link
	replacement_url = base_path + link_url;
	header_anchor.attr('href',replacement_url);
	archive_anchor.attr('href',replacement_url);
}


/* Get the base path for the site */
function multiboxGetBasePath(url, link_url) {
	// url = full path (including link)
	// link_url = the subpath we WANT to glue at the end
	// archiveLinksArray = list of all subpaths
	
	for(var i=0; i<archiveLinksArray.length; i++) {
		var matchIndex = url.indexOf(archiveLinksArray[i]);
		if(matchIndex != -1) {
			return url.substring(0,matchIndex); //we found a match, return the base path
		}
	}
}


/* Onclick function call for a button */
function multiboxClick(button) {
	var button_index = getMultiboxButtonIndex(button);
	clearInterval(multiboxInterval);
	multiboxSelect(button, button_index);
}


/* Determines the index of the clicked button */
function getMultiboxButtonIndex(clickedButton) {
	var button_number = 0;
	$('#multibox ul li.multibox-button').each(function(i) {
		if(clickedButton == this) return false;
		button_number++;
	});
	 
	return button_number;
}