/* TODO: 
*  1. Create a pre animation effect where the section is moved off screen, displayed, changed as need then brought back.
*  2. Create an '__all__' id for post_animation functions that will be applied to all sections
*/
/**
* Attach show behaviour to each menu link
*
* the hash address (after the #) must be the same as the div id
*
* @param links
*   jQuery object with the following properties:
*	
*	Properties:
*		links_to_bind: jQuery object of link selectors that will be bound
*		post_animation: array  - [id, function] pairs
*			- function operates on element (id) after animation has completed
*		link_current_class_name: name of class to add to a link after click indicating current navigation element
*			(basically where we are - what section is showing)
*
*  Usage:
*    The next and prvious links must a have a class of .next and .previous if they exist at all
*    - The table of content must have an id of #toc
*/
function bind_links(links){
	// track which section is showing. Only showing one at a time
	var section_showing = null,
	
		// Check if there is a cookie. A section to reload
		section_name_from_cookie = $.cookie('section'),
		
		// Check if there is a hash on the end of the URL - hash overrides cookie.
		section_name_from_hash = location.hash,
		
		// used to pass parameters to change_section function to match link.bind("click", {}, change_section) format 
		param_object = {},
		
		section_to_show, link;
		
	// if cookie is set, show that section
	if (section_name_from_hash || section_name_from_cookie) {
		var section_name = '';
		
		if (section_name_from_hash) {
			// Remove # from beginning of hash
			// Find last #
			var poundlocation = section_name_from_hash.lastIndexOf("#"), 
			// Find anchor address
			section_name = section_name_from_hash.substring(poundlocation + 1, section_name_from_hash.length)
			
			// If there is no matching section (and hence no text) remove hash from location and reload page
			if (! $("#toc a[href=#" + section_name + "]").length ) {
				window.location.replace(window.location.href.substring(0, window.location.href.lastIndexOf("#"))); 	
			}	
		}
		else {
			section_name = section_name_from_cookie;
		}
		
		// show section
		section_to_show = $('#' + section_name);
	
		// find toc link with a href of section_name
		link = $('#toc a[href=#' + section_name + ']');		
		
		linkInfo = get_link_info(link);
		
		param_object.data = {"linkInfo" : linkInfo, "section_to_show" : section_to_show, "links" : links};
		
		if ( section_to_show.length ) { // if there's a match
			change_section(param_object);
		}
	};	
	
	links.links_to_bind.each(function(){
		var link = $(this),
			linkInfo = get_link_info(link);
		
		if (!linkInfo.anchoraddress.length) return; // if nothing after the #

		// find target
		section_to_show = $("#" + linkInfo.anchoraddress);
		
		if ( section_to_show.length ) { // if there's a match
			//link.bind("click", change_section(link, linkInfo, section_to_show, links));
			link.bind("click", {"linkInfo" : linkInfo, "section_to_show" : section_to_show, "links" : links}, change_section);	
		}
	}); // end of each

	/**
	* Hides previous section, shows new section
	*
	* @param link
	*	jQuery object encapsulating link to bind
	*
	* @param section_to_show
	* 	jQuery object encapsulating div to display
	*
	* @param links
	*	object @see bind_links() docs
	*/
	function change_section(event) {
		var linkInfo = event.data.linkInfo,
			section_to_show = event.data.section_to_show,
			links = event.data.links;

		// hide currently showing section, remove prior navigation style if nav style set
		if (section_showing) {
			section_showing.hide();		
		}
		// if links.post_animation wasn't passed in, it will be undefined
		if (links.post_animation == undefined) {
			section_to_show.slideDown('slow');
		}
		else {
			// There is a post animation
			for (var i = 0, found_match = false; i < links.post_animation.length; i = i + 2) {
				// if this link has same anchoraddress as post animation selector...
				if (linkInfo.anchoraddress == links.post_animation[i]) {
					section_to_show.slideDown('slow', links.post_animation[i + 1]);
					found_match = true;
					break;
				}
			} // end of for
			// if didn't find a match run the animation without a follow up function
			if (! found_match) {
				section_to_show.slideDown('slow');
			}
		}

		// Set navigation style on toc links
		if (links.link_current_class_name !== undefined) {
			$('#toc a').removeClass(links.link_current_class_name);
			$('#toc a[href=#' + linkInfo.anchoraddress + ']').addClass(links.link_current_class_name);	
		}

		//Set global
		section_showing = section_to_show;
		// Set a cookie, "section" equal to the anchoraddress to remember where user was
		$.cookie('section', linkInfo.anchoraddress, {expires: 30});
	};
	
	/**
	* Get the text and anchoraddress from the link and return them.
	*
	* @param event
	*   jQuery wrapped 'this' : the link that was clicked
	* @return
	*   Object with two properties: anchoraddress, words
	*/
	function get_link_info(link) {
		var href = link.attr("href"),
			words = link.text(),
			//find last #
			poundlocation = href.lastIndexOf("#"), 
			//find anchor address
			anchoraddress = href.substring(poundlocation + 1, href.length); //go one position past #
		return {
			"anchoraddress" : anchoraddress,
			"words" : words			
		}
	};
};