function $( id ) {
	return document.getElementById( id );
}

var swapDelay = 500; // 500 миллисекунд
var swapTimer = 0;
var popupTimer = 0;
var shownPopup = '';

function swap_to_default() {
	var id1 = $('admobl-submenu_'+sel_item);
	var id2 = $('admobl-submenu_'+def_item);
	if ( !id1 || !id2 ) return;
	
	id1.style.display = 'none';
	id2.style.display = 'block';
	
	shownPopup = '';
	
	restore_bg();
	
	sel_item = def_item;
}

function restore_bg() { // восcтановление фона у стандартных элементов меню
	var ind = 1;
	while ( true ) {
		var obj = $( 'admobl-menu_'+ind );
		if ( !obj ) break;
		++ind;
		if ( (ind-1) == def_item ) continue;
		obj.className = 'menu_btn_td std';
	}
}

function restore_color( div ) {
	var i = div.id.toString();
	i = i.substr( i.indexOf( '_' )+1 );
	i = i * 1;
	
	var ind = 1;
	while ( true ) {
		var obj = $( 'admobl-menu_'+ind );
		if ( !obj ) break;
		++ind;
		if ( (ind-1) == i ) continue;
		if ( (ind-1) == def_item ) continue;
		
		if ( (ind-1) == sel_item )	obj.className = 'menu_btn_td selected_menu';
		else						obj.className = 'menu_btn_td std';
	}
}

function shell_out( event, div ) {
	if ( swapTimer ) {
		clearTimeout( swapTimer );
	}
	swapTimer = setTimeout( 'swap_to_default()', swapDelay );
	
	if ( shownPopup.length > 0 ) {
		ao_popup_menu_out_delay( shownPopup );
	}
	
//	event.cancelBubble = true;
//	event.returnValue = false;
	return false;
}

function shell_over( event ) {
	if ( swapTimer ) {
		clearTimeout( swapTimer );
		swapTimer = 0;
	}
	
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}

function menu_div_over( event, div ) {
	shell_over( event );
	
	{ // проверка на наличие подменю
		var divs = div.getElementsByTagName( 'DIV' );
		for ( var i=0; i < divs.length; i++ ) {
			if ( divs[i].id.length && ( divs[i].id.substring( 0, divs[i].id.length-1 ) == 'ao-popup-menu-' ) ) {
				divs[i].style.display = 'block';
				if ( shownPopup.length > 0 ) {
					swap_to_default();
				}
				shownPopup = divs[i].id;
				return;
			}
		}
	}
	
	var i = div.id.toString();
	i = i.substr( i.indexOf( '_' )+1 );
	i = i * 1;
	
	if ( i == sel_item ) return;
	
	{ // смена фоновой картинки
		restore_bg();
		
		{ // задаю новый фон
			if ( i != def_item ) {
				var obj = $( 'admobl-menu_'+i );
				if ( obj ) {
					obj.className = 'menu_btn_td selected_menu';
				}
			}
		}
	}
	
	var id1 = $('admobl-submenu_'+sel_item);
	var id2 = $('admobl-submenu_'+i);
	if ( !id1 || !id2 ) return;
	
	id1.style.display = 'none';
	id2.style.display = 'block';
	
	sel_item = i;
	
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}

function ao_popup_menu_hide( id ) {
	clearTimeout( popupTimer );
	popupTimer = 0;
	
	var obj = $( id );
	if ( obj ) obj.style.display = 'none';
}

function ao_popup_menu_out_delay( id ) {
	if ( popupTimer ) {
		clearTimeout( popupTimer );
	}
	popupTimer = setTimeout( 'ao_popup_menu_hide( "'+id+'" )', swapDelay );
}

function ao_popup_menu_over( id ) {
	if ( popupTimer ) {
		clearTimeout( popupTimer );
		popupTimer = 0;
	}
	
//	event.cancelBubble = true;
//	event.returnValue = false;
	return false;
}