/*

	-- -- -- -- -- -- --
	css sprites 2
	nav behaviour

	http://www.alistapart.com/articles/sprites2
	-- -- -- -- -- -- --

*/
jQuery(document).ready(function(){
    generateSprites(".topnav", "current_page_item", true, 150, "fade");
});

function generateSprites(parent, selectedPrefix, setActive, hoverSpeed, style) {
	// start a loop that cycles through each of the li elements inside the parent element
	jQuery(parent).children("li").each(function() {
		// create a few variables that we'll need during this function:
		// myClass = the class of the object we're currently inspecting
		// current = what the selected class should look like for the parent of the object we're currently inspecting
		var myClass = /(id-[\w+-]+\b)/.exec(this.className)[0];

		// turn on nav events for element this loop identifies
		attachNavEvents(parent, myClass, setActive, hoverSpeed, style);

		// let's hide the CSS-defined background image, but only if this isn't the currently-selected item
		var current = jQuery(this).hasClass(selectedPrefix);
		if (current === false) {
			jQuery(this).children("a").css({'background-image':'none'});
		}

	});
}


function attachNavEvents(parent, myClass, setActive, hoverSpeed, style) {
	jQuery(parent + " ." + myClass).mouseover(function() {
		// create pseudo-link
		jQuery(this).append('<div class="nav-' + myClass + ' sprites2"></div>');
		// either slide or fade, depending on the style value
		if (style == "slide") {
			// slide down the pseudo-link
			jQuery("div.nav-" + myClass).css({'display':'none'}).slideDown(hoverSpeed);
		} else {
			// fade in the pseudo-link
			jQuery("div.nav-" + myClass).css({'display':'none'}).fadeIn(hoverSpeed);
		}
	}).mouseout(function() {
		// either slide or fade, depending on the style value
		if (style == "slide") {
			// slide up & destroy pseudo-link
			jQuery("div.nav-" + myClass).slideUp(hoverSpeed, function() {
				jQuery(this).remove();
			});
		} else {
			// fade out & destroy pseudo-link
			jQuery("div.nav-" + myClass).fadeOut(hoverSpeed, function() {
				jQuery(this).remove();
			});
		}
	});


	// we only want to check the mousedown/up events if the CSS exists for :active states
	// if so, let's apply our selective filtering to undo the events above
	if (setActive) {
		jQuery(parent + " ." + myClass).mousedown(function() {
			jQuery("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click clicker");
		}).mouseup(function() {
			jQuery("div.nav-" + myClass + "-click").attr("class", "clicker nav-" + myClass);
		});
	}
}

