// TemplateCodes.com Custom Accordion Menu © 2009 All Rights Reserved.

$(document).ready(function() {
	$("#accordion div").removeClass("active");
	$("#accordion a.toggle").children().text("+");

	if ($.cookie('openItem')) {
		var openItem = $.cookie('openItem');
	}else{
		openItem = 'tcmenu_1';
	}
			
	var divID = "#" + openItem;
	$(divID).addClass('active');
	
	if($(divID).prev().hasClass("btm")){
		$(divID).prev().toggleClass("btm").toggleClass("mid");
	}
	$(divID).prev().children().text("-");
	$("#accordion > div").not('.active').hide();//close only first level divs that are not active
	
	$('#accordion a.toggle').click(function(e) {
		e.preventDefault();
			if ($("#accordion > div:last").hasClass("active")) {
				$("#accordion > a:last").toggleClass("mid").toggleClass("btm");
			}
			if ($(this).next().hasClass("active")) {
				var open = true;//is open we are closing
			} else {
				var open = false;//is closed we are opening
			}
			$("#accordion div").removeClass("active");//remove all active classes
			$('#accordion a span').text('+');//set all to closed
			if (open == true) { //is open we are closing
				$(this).children().text("+");//set this anchors span
				$(this).next("div").slideUp('normal');//close this anchors following div
			} else { //is closed we are opening
				$(this).children().text("-");//set this anchors span
				$(this).next().slideDown('normal').addClass("active");//open this anchors following div
				$("#accordion > div").not('.active').slideUp();//close only first level divs that are not active
			}
			if ($("#accordion > div:last").hasClass("active")) {
				$("#accordion > a:last").toggleClass("btm").toggleClass("mid");
			}
			$.cookie('openItem', $('#accordion > div.active').attr('id'));
		});
	});