jQuery.fn.adjacent = function(filter) {
	var $all = this;
	for (var $curr = this.prev(filter); $curr.length > 0; $curr = $curr.prev(filter)) {
		$all = $all.add($curr);
	}
	for (var $curr = this.next(filter); $curr.length > 0; $curr = $curr.next(filter)) {
		$all = $all.add($curr);
	}
	return $all;
};
jQuery.fn.unwrapDiv = function() {
	this.parent('div').each(function(){
		$(this).replaceWith( this.childNodes );
	});
	return this;
};
jQuery(document).ready(function(){
	
	$(".navnest").each(function() {
		$(this).adjacent(".navnest").unwrapDiv();
		$(this).adjacent(".navnest").wrapAll("<div class=\"accordion_bellows\"></div>");
	});
	$(".existing-hidden ul").each(function() {
		$(this).parents(".existing-hidden").eq(0).adjacent(".existing-hidden").unwrapDiv();
		$(this).parents(".existing-hidden").eq(0).adjacent(".existing-hidden").wrapAll("<div class=\"accordion_bellows\"></div>");
	});
	$("li.subactive.existing-hidden").eq(0).parents(".existing-hidden").eq(0).prev("[navlink]").not("[heirarchy='1'").eq(0).addClass("active");
	$("li.subactive.existing-hidden").eq(0).parents(".existing-hidden").eq(0).siblings(".existing-hidden").prev("[navlink]").not("[heirarchy='1'").eq(0).addClass("active");
	$(".accordion_bellows").prev().wrap("<div class=\"accordion_box\"></div>");
	$(".accordion_bellows").hide();
	$("ul.nav li.subactive").parents(".accordion_bellows").eq(0).prev('.accordion_box').eq(0).children("li").eq(0).addClass("active");
	var activeBellows = $("ul.nav li.active").parents(".accordion_box").eq(0).next('.accordion_bellows');
	$(activeBellows).addClass("accordion_active");
	$(activeBellows).show();
	$(".accordion_box").hoverIntent(function() {
		$('.accordion_bellows:visible').not('.accordion_active').not($(this).next('.accordion_bellows')).hide('blind');
		$(this).next('.accordion_bellows:hidden').show('blind');
		return false;
	},
	function() {});
});
