/*
SBMS Menu javascript
(c) 2010 Jake Teton-Landis
requires jquery & jquery.cookie & jquery.jsoncookie
*/

/* Use this with a semantic navigation list, so things are searchable.
Example:
<ul class="navigation">
	<li>
		<h3>List Part Header</h3>
		<ul>
			<li><a href="foo.html">Foo</a></li>
			<li><a href="foo.html">Foo</a></li>
			<li><a href="foo.html">Foo</a></li>
			<li><a href="foo.html">Foo</a></li>
		</ul>
	</li>
	<li>
		<h3>List Part Header Too</h3>
		<ul>
			<li><a href="foo.html">Foo</a></li>
			<li><a href="foo.html">Foo</a></li>
			<li><a href="foo.html">Foo</a></li>
			<li><a href="foo.html">Foo</a></li>
		</ul>
	</li>
</ul>
*/

/*
The state variable records the open/closed state using a {key: value, ...} dictionary:
where the key is the name of the heading, and the value is a boolean that answers the question
"Is this heading currently hidden?"
false => show
true => hide

this means you can't have two headings with the same title.  Titles MUST be unique
*/

//wrap in a function in case $ is being used by something else
;(function($){
	
$.fn.SBMSnav = function( options ){
	opts = $.extend(true, {}, $.fn.SBMSnav.defaults, options);
	return this.each(function(){
		// Set up instance variables
		var $root = $(this).addClass(opts.runningClass); // add a class for javascript users
		var $headings = $root.children().children( opts.headings );
		//setup state
		var state = getState(); // returns a state object or null
		if (state == null) {
			//no preexisting state.  Setup new state.
			state = new Object();
			for (var i = 0; i < $headings.length; i++){
				state[ $headings.eq(i).text() ] = true;
			}
			// if you're running some sort of crazy show, uncomment the following line
			// saveState( state, opts )
		}
		
		// headings act as clickable links
		// $headings.wrapInner('<a />');
		// add a click handler to the headings
		$headings.css('cursor', 'pointer');
		$headings.click(function() {
			// when clicked:
			$(this).toggleClass(opts.headingsActiveClass).next().slideToggle( opts.speed );
			// modify state
			if ( state[$(this).text()] ) { state[$(this).text()] = false; }
			else { state[$(this).text()] = true; }
			saveState(state, opts);
			// prevent IE from doing stupid, stupid things.
			return false;
		});
		
		// load state, execpt if we're in this section, then show reguardless
		var classRX = new RegExp( $('body').attr('class').split('_')[0],   'i') //regex based on first word of class
		$headings.each(function(){
			// true => hidden, false => shown, active.
			if ( state[$(this).text()] ) {
				if ( $(this).text().match(classRX) ) {
					$(this).toggleClass(opts.headingsActiveClass);
				} else {
					$(this).next().hide();
				}
			}
			else { $(this).toggleClass(opts.headingsActiveClass) }
		});
	});
}

// does what is says on the tin
function getState() {
	var obj = $.JSONCookie('SBMSnavState');
	// if the object is an empty (IE, no cookie exists), return null
	for(var prop in obj) {
		if(obj.hasOwnProperty(prop))
			return obj;
	}
	return null;
}

function saveState( state, opts ) {
	// console.log(state)
	if (opts.useSessionCookie) {
		return $.JSONCookie('SBMSnavState', state); }
	else
		return $.JSONCookie('SBMSnavState', state, opts.cookieSettings );
}

function resetState() {
	$.cookie('SBMSnavState', null);
	return null;
}

// defaults.  Don't modify this.  Instead, call the script with different options
$.fn.SBMSnav.defaults = {
	headings: ".label",
	runningClass: ".accordionNav",
	headingsActiveClass: 'SBMSnavActive',
	speed: "slow",
	useSessionCookie: true,
	cookieSettings: { //only used if useSessionCookie is false
		domain: "sbms.org",
		expires: 364*20 // twenty year cookie, with fudge factor
	}/*,
	mappings: {} */// maps classes to text of list
}

})(jQuery);

// to call the function when the document is ready,
// jQuery(document).ready(function(){
// 	nav = $('.SBMSnav').SBMSnav({ headings: 'h6', speed: 'normal' });
// });
