$(document).ready( function() {
	
	// Side cats main
	$('ul#sidecats a.group').click( function() {
		var sTarget = this.id.replace('toggle_','group_');
		$('ul#sidecats li').removeClass('link-active');
		
		var that=this;
		
		
		$('ul#sidecats ul.sidegroup').each( function() {
			if(this.id == sTarget) {
				
				if($(this).hasClass('down')) {
					// Move up
					$(this).slideUp('fast'); 
					$(this).removeClass('down');
					$(that).parent().removeClass('link-active');
					
					
				} else {
					// Move down
					$(this).slideDown('fast');
					$(this).addClass('down');
					$(that).parent().addClass('link-active');
					$.post(LINKROOT+'/do/frontend/setlastactive','cat='+this.id)
				}
				
				
			} else {
				// Move down
				$(this).slideUp('fast');
				$(this).removeClass('down');
				//$(this).parent().addClass('link-active');
				
			}
		});		
	});
	
	
		
	
	// Side cats sub
	$('ul#sidecats a.subgroup').click( function() {
		
		var sTarget = this.id.replace('toggle_','childrenof_');
		
		
		
		if($('#'+sTarget).hasClass('down')) {
			// Up
			$('#'+sTarget).slideUp('medium');
			$('#'+sTarget).removeClass('down');
			$(this).removeClass('link-active');
			
		} else {
			// Down
			$(this).addClass('link-active');
			$('ul.subchildren').slideUp('fast');
			$('ul.subchildren').removeClass('down');
			$('#'+sTarget).slideDown('medium');
			$('#'+sTarget).addClass('down');
			$('a.subgroup').removeClass('link-active');
			$.post(LINKROOT+'/do/frontend/setlastactive','cat='+this.id);
		}
		
				
		
	});
	
	$('a.prodfromlist').click( function() {
		$.post(LINKROOT+'/do/frontend/setlastactive','cat='+this.id);
	});
});


function foldMenu(sItem) {
	if(sItem.indexOf('group') != -1) {
		// Item in maincategory
		$('#'+sItem).show();
		
		var sLink = sItem.replace('group_','toggle_');
		$('#'+sLink).parent().addClass('link-active');
		
		
	} else {
		// Item in subcategory
		
		//alert(sItem);
		
		/*
		$('#'+sItem.replace('toggle_','childrenof_')).show();
		$('#'+sItem.replace('toggle_','childrenof_')).addClass('down');
		$('#'+sItem.replace('toggle_','childrenof_')).parent().show();
		$('#'+sItem.replace('toggle_','childrenof_')).parent().addClass('down');
		
		var sParent = $('#'+sItem.replace('toggle_','childrenof_')).parent().attr('id');
		var sParent = $('#'+sItem.replace('toggle_','parent_childrenof_')).attr('id');
		
		

		$('#'+sParent).parent().addClass('down');
		var sParentLink = sParent.replace('group_','toggle_');
		$('#'+sParentLink).parent().addClass('link-active');
		*/
		
		var sRel = $('#'+sItem).attr('rel');

		// Show main
		$('#'+sRel).show();
		$('#'+sRel).parent().addClass('down');
		$('#'+sRel).parent().addClass('link-active');
		
		// Show sub
		var sSub = sItem.replace('toggle_','childrenof_');
		$('#'+sSub).show();
		$('#'+sSub).addClass('down');
		
		
	}
}

function setMenuActive(iMenuItem) {
	$('#li_'+iMenuItem).addClass('active');
}
