var just_opened, open_this_id, is_opened=0, menu_tag = "#leftside li > span";	// make var for checking if we have opened lists
$(document).ready(function(){

	// check if we have to open any list - if true read value from cookie
	if ($.cookie('counter')) {
		open_this_id=parseInt($.cookie('counter'));
		$(menu_tag).eq(open_this_id).next("ul").slideDown();
		$(menu_tag).eq(open_this_id).toggleClass("active"); 
		just_opened=open_this_id;
		is_opened = 1;		 
	}

	$(menu_tag).click(function () {
		// check if we haven't opened list yet. if false - close previously opened
		if (is_opened == 1) {
			// if all lists are collapsed - delete cookie
			if (just_opened==$(menu_tag).index(this)) {
				$(this).next("ul").slideUp("slow");
				$(this).toggleClass("active"); // toggle class to change bg with arrow 
				is_opened=0;
				$.cookie('counter',null);
			} else {
				$(this).next("ul").slideToggle("slow");								   // toggling required list
				$(menu_tag).eq(just_opened).next("ul").slideToggle(500); // closing previously opened list
				$(this).toggleClass("active");                           // toggle class for clicked span
				$(menu_tag).eq(just_opened).toggleClass("active"); 			 // toggle class for previously opened class
				just_opened=$(menu_tag).index(this);								     // refreshing index of opened list
			}
		} else {
			$(this).next("ul").slideToggle("slow");		// toggling required list
			$(this).toggleClass("active");				// toggle class to change bg with arrow 
			just_opened=$(menu_tag).index(this);		// index of opened list
			is_opened=1;								// mark that we have opened
   	}

	$.cookie('counter', just_opened, {expires: 0.001, path: '/category/'}); 
    });
    	
    	//$.cookie('counter') ? open_this_id=$.cookie('counter') : '';  	
});
