/**
 * Sammy JS App that handles #/foo/bar routing for the Staff Applications page.
 *
 * URLs:
 * - /<department-name>
 * - /<department-name>/<position-name>
 * - /apply/<department-name>/<position-name>
 */
var staffApp = $.sammy(function(){

	this.element_selector = 'body.staff-index #body';

	// App-wide Variables
	var jobs = '#jobs';


	/****    Controller Actions    ****/

	this.get('#/', function(){
		// NOP
		// TODO: Select first department?
	});

	this.get('#/:department', function(){
		// Args: departmentSlug, selectFirstPosition
		this.selectDepartment(this.params['department'], true);
	});

	this.get('#/:department/:position', function(){
		this.selectPosition(this.params['department'], this.params['position']);
	});

	// Constructor
	this.bind('run', function(){
		// More NOP; this is here as an example.
	});


	/****    Application Helper Functions    ****/

	this.helpers({

		// Select a particular department; if selectFirstPosition is given/true, 
		// then select the first position in the department.
		// (Returns the chosen department element.)
		selectDepartment: function(departmentSlug, selectFirstPosition){
			// Clear all the departments first
			this.deselectDepartments();

			// Just clear the positions we're about to make visible; everything else
			// will be hidden anyway.
			this.deselectDepartmentPositions(departmentSlug);
			
			var dept = this.getDepartment(departmentSlug);
			if (selectFirstPosition) {
				dept.find('li:first').addClass('selected');
			}
			dept.addClass('selected');
			return dept;
		},

		// Select a department, and a position within that department.
		// (Returns the chosen position element.)
		selectPosition: function(departmentSlug, positionSlug){
			// Select the department first.
			var dept = this.selectDepartment(departmentSlug, false);

			// Now the position.
			return dept.find('ul.positions > li#position-'+escape(departmentSlug)+'-'+escape(positionSlug)).addClass('selected');
		},

		// Quick helpers for the selection helpers
		getDepartment: function(departmentSlug) {
			return $(jobs+' li#department-'+escape(departmentSlug));
		},
		deselectDepartments: function(){
			$(jobs+' ul.departments > li.selected').removeClass('selected');
		},
		deselectDepartmentPositions: function(departmentSlug){
			this.getDepartment(departmentSlug).find('ul.positions > li.selected').removeClass('selected');
		}
	});

});

 
$(document).ready(function(){

	// Bind all /staff/foo internal link clicks to /staff#/foo.
	// Note: Always pointing to "/staff#/foo/bar/baz.." means that, if a user comes in
	//       via a /staff/foo/bar URL, they will be redirected to the proper root instead
	//       of /staff/foo/bar#/foo/bar.

	var convertHrefToHashLink = function(){
		// We already know there's at least one /staff/ at the beginning; just
		// go and replace it.
		var elt = $(this);
		var href = elt.attr('href');
		elt.attr('href', href.replace('/staff/', '/staff#/'));
	};


	// Set up Sammy on the /staff/index page.
	// (But not if we're directly hitting the /staff/apply page.)

	if ($('body').hasClass('staff-index')) {

		// Set up the hash-links.
		// HACK: We're removing /staff/apply for the moment until the panel loading / animation
		//       is sorted; this is what the not() business is about.
		//       For the moment, this means that /staff/apply/x/y is not #-ified.
		$('#body a[href^="/staff/"]:not([href^=/staff/apply/])').each(convertHrefToHashLink);

		// Go Sammy Go.
		staffApp.run('#/');
	}


	// Hash links on the /staff/apply page.
	// This is just a nicety; if JS is on, then link directly
	// back to /staff#/foo/bar from the application form
	// (instead of /staff/foo/bar).

	if ($('body').hasClass('staff-apply')) {
		$('#body a.return[href^="/staff/"]').each(convertHrefToHashLink);
	}

});




