// Filename: myfuncs.js
//
// Purpose: contains javascript functionality for musicianbrain.com, particularly for AJAX pageloads
//
// Author: Justin Bachorik
//
// Date: 9/08



// this function is called whenever the page loads
function init() {
	// check to see if we have a hash value
	if(document.location.hash != '') {
		hashVal = document.location.hash.substr(1,document.location.hash.length-1);
		// check for legitimacy
		// checks whether the hash indicates a page
		if(is_legit_page(hashVal))
			load_page(hashVal);
		// checks whether the hash indicates a method on the methods page
		else if(is_legit_method(hashVal))
			load_page('methods');
		// checks whether the hash indicates a project on the projects page
		else if(is_legit_project(hashVal))
			load_page('projects');
		else
			load_page('index');
	} else {
		load_page('index');
	}

	// these lines set up a timer that check the hash to ensure proper navigation
	var hash = document.location.hash;

	setInterval(function()
	{
			if (document.location.hash != hash)
			{
					hashVal = document.location.hash.substr(1,document.location.hash.length-1);
					// these checks need to be done because otherwise, going to the anchors on the projects and methods page would trigger an undefined action
					if(!is_legit_project(hashVal) && !is_legit_method(hashVal)) {
						load_page(hashVal);	
						hash = document.location.hash;
					}
			}
	}, 100);

}

// function to check to see if a page exists
function is_legit_page(pageName) {
	legitNames = ['index','people','publications','projects','methods','funding','inthenews','schlaugcv','schlaugbiblio'];
	for(i=0;i<legitNames.length;i++) {
		if(legitNames[i] == pageName) {
			return true;
		}
	}	
	return false;
}

// function to check to see if a method exists
function is_legit_method(pageName) {
	legitNames = ['pagetop','smri','fmri','dti','asl','spectroscopy','tdcs','tms','eeg','psychophysical'];
	for(i=0;i<legitNames.length;i++) {
		if(legitNames[i] == pageName) {
			return true;
		}
	}	
	return false;
}

// function to check to see if a project exists
function is_legit_project(pageName) {
	legitNames = ['pagetop','aphasia','singing','tonedeaf','motor','emotions','autism','children','brain','adult','absolute','acute'];
	for(i=0;i<legitNames.length;i++) {
		if(legitNames[i] == pageName) {
			return true;
		}
	}	
	return false;
}


// this function is called whenever a link on the navbar is clicked
// it sets the document.hash to be picked up by the timer, which then calls load_page
function set_hash(pageName) {
	document.location.hash = pageName;
	return false;
}

// this function actually loads content dynamically
// it's responsible for setting up the ajax call to the appropriate PHP file and using
// scriptaculous to blind up the content div
function load_page(pageName) {
	document.location.hash = pageName;
	// name of the php script to hit for the ajax call
	fileToLoad = pageName + '.php';
	// scriptaculous effect, complete with afterFinish handler to set up the ajax call
	Effect.BlindUp('content', { queue: 'end', afterFinish: function() {show_content_and_blind_down(fileToLoad)} } );
	titleName = get_friendly_name(pageName);
	document.title = 'The Music And Neuroimaging Lab | '+titleName;
}


// this is the callback function for the scriptaculous animation
// it handles the ajax call to retrieve the page, as well as the follow-up blinddown animation
function show_content_and_blind_down(fileToLoad) {
	new Ajax.Request(fileToLoad, {
					method: 'get',
					parameters: { ajax: true},
					onSuccess: function(transport) {
						$('content').innerHTML = transport.responseText;
						Effect.BlindDown('content', { queue: 'end' });
						}
					});
			
}


// this function is just a lookup table to turn filenames into page title names
function get_friendly_name(pageName){
	switch(pageName) {
		case 'people':
			return "People";
			break;
		case 'publications':
			return "Publications";
			break;
		case 'projects':
			return "Projects";
			break;
		case 'methods':
			return "Methods";
			break;
		case 'funding':
			return "Funding";
			break;
		case 'inthenews':
			return "In The News";
			break;
		case 'index':
			return "Welcome";
			break;
		case 'schlaugcv':
			return "Gottfried Schlaug CV";
			break;
		case 'schlaugbiblio':
			return "Gottfried Schlaug Bibliography";
			break;
	}
}

