var just_opened, open_this_id, is_opened=0, menu_tag = "#menu li a.tita";	// 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();
		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");
				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
				just_opened=$(menu_tag).index(this);			// refreshing index of opened list
			}
		
    		} else {
    			$(this).next("ul").slideToggle("slow");			// toggling required list
    			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') : '';  	
});

