function cms_showCategory ( blocktype, id )
{
	var target = "#cms_block_" + blocktype + "_category";
	var menu = "#cms_block_" + blocktype + "_menu"; 
	var item = "#cms_menu_" + blocktype;
	var disp = false;
	var first = 0;
	
	for ( var i = 1; i <= 10; i ++ ) {
	
		if ( $(target+i).size ( ) > 0 ) {
			if ( i == id ) {
				$(target+i).css("display", "block");
				$(menu).disableContextMenuItems ( item+i );
				disp = true;
			}
			else {
				$(target+i).css("display", "none");
				$(menu).enableContextMenuItems ( item+i );
			}
			
			if ( first == 0 ) {
				first = i;
			}
		}
	}

	if ( !disp ) {
		if ( first > 0 ) {
			$(target+first).css("display", "block");
			$(menu).disableContextMenuItems ( item+first );
		}
	}
}

function cms_initMenu ( railwaycategory, stationcategory, areacategory )
{
	$(document).ready (
	
		function ( ) {

			var railway = $(".cms_block_railway_select");
			if ( railway.size ( ) > 0 ) {
					
				railway.contextMenu ( { menu: 'cms_block_railway_menu' },
					function(action, el, pos) {
						var id = action.substr ( 'cms_menu_railway'.length, 2 );
						cms_showCategory ( 'railway', id );
						$.cookie ( 'railwaycategory', id );
					}
				);
				
				var c_railwaycategory = $.cookie ( 'railwaycategory' ); 
				if ( ( c_railwaycategory != null ) && ( $( '#cms_block_railway_category' + c_railwaycategory ).size ( ) > 0 ) ) 
					cms_showCategory ( 'railway', c_railwaycategory );
				else
					cms_showCategory ( 'railway', railwaycategory );
			}

			var station = $(".cms_block_station_select");
			if ( station.size ( ) > 0 ) {
					
				station.contextMenu ( { menu: 'cms_block_station_menu' },
					function(action, el, pos) {
						var id = action.substr ( 'cms_menu_station'.length, 2 );
						cms_showCategory ( 'station', id );
						$.cookie ( 'stationcategory', id );
					}
				);
				
				var c_stationcategory = $.cookie ( 'stationcategory' ); 
				if ( ( c_stationcategory != null ) && ( $( '#cms_block_station_category' + c_stationcategory ).size ( ) > 0 ) ) 
					cms_showCategory ( 'station', c_stationcategory );
				else
					cms_showCategory ( 'station', stationcategory );
			}
			
			var area = $(".cms_block_area_select");
			if ( area.size ( ) > 0 ) {
					
				area.contextMenu ( { menu: 'cms_block_area_menu' },
					function(action, el, pos) {
						var id = action.substr ( 'cms_menu_area'.length, 2 );
						cms_showCategory ( 'area', id );
						$.cookie ( 'areacategory', id );
					}
				);
				
				var c_areacategory = $.cookie ( 'areacategory' ); 
				if ( ( c_areacategory != null ) && ( $( '#cms_block_area_category' + c_areacategory ).size ( ) > 0 ) ) 
					cms_showCategory ( 'area', c_areacategory );
				else
					cms_showCategory ( 'area', areacategory );
			}
		}
	);
}
