function swap_image(ptr, image) {
	if(ptr.tagName == 'A')
		ptr = ptr.parentNode.getElementsByTagName('IMG')[0];
	else
		ptr = ptr.getElementsByTagName('IMG')[0];
	ptr.src = image;
}

shade_amount = 5;

function shade_div_down(obj, div_height) {
	var box_height = obj.offsetHeight;

	if(box_height < div_height - shade_amount) {
		obj.style.height = (box_height + shade_amount) + 'px';

		setTimeout(function () { shade_div_down(obj, div_height); }, 1);
	} else
		obj.style.height = div_height + 'px';
}

active_menu 	= false;
active_sub_menu = false;

function close_active_menu(id, m) {
	if(m != false) {
		menu_outer = document.getElementById(id + '_outer');
		menu_inner = document.getElementById(id + '_inner');

		menu_outer.style.display = 'none';
		menu_inner.style.display = 'none';
	}
}

function set_position(obj, x, y) {
	obj.style.left 	= x + 'px';
	obj.style.top 	= y + 'px';
}

function show_sub_menu(id) {
       	close_active_menu('sub_' + active_sub_menu, active_sub_menu);

 	menu_inner 	= document.getElementById('sub_' + id + '_inner');
	menu_outer 	= document.getElementById('sub_' + id + '_outer');

	if(!menu_inner) {
		active_sub_menu = false;
		return;
	}

	active_sub_menu = id;

	parent_inner_id = document.getElementById('menu_' + id + '_link');
	pos 		= find_pos(parent_inner_id);

	menu_outer.style.display = '';
	menu_inner.style.display = '';
	
	target_height 		= get_height(menu_inner);
	target_width		= get_width(menu_inner) + 0;
	
	menu_inner.style.height = '1px';
	menu_outer.style.height = '1px';

	// position the item
	set_position(menu_outer, pos[0] + 0, pos[1]);
	set_position(menu_inner, pos[0] + 0, pos[1])

	// height of each
	shade_div_down(menu_outer, target_height);
	shade_div_down(menu_inner, target_height);
}

// double vision DX
function show_menu(ptr, id) {
  	close_menu();

	active_menu = id;
	
	menu_inner = document.getElementById(id + '_inner');
	menu_outer = document.getElementById(id + '_outer');

	menu_outer.style.display = '';
	menu_inner.style.display = '';

	pos = find_pos(ptr);

	target_height = get_height(menu_inner);

	menu_inner.style.height = '1px';
	menu_outer.style.height = '1px';

	// position them
	set_position(menu_outer, pos[0] + 0, pos[1] + 39);
	set_position(menu_inner, pos[0] + 0, pos[1] + 39)

	// height of each
	shade_div_down(menu_outer, target_height);
	shade_div_down(menu_inner, target_height);
}

function close_menu() {
	if(cur_mnu)
		swap_image(cur_mnu, cur_img);

       	close_active_menu(active_menu, active_menu);
       	close_active_menu('sub_' + active_sub_menu, active_sub_menu);
}

function get_height(obj) {
	xPos = obj.offsetHeight;

	return xPos;
}

function get_width(obj) {
	xPos = obj.offsetWidth;

	return xPos;
}

function find_pos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop 	= obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop 	+= obj.offsetTop
		}
	}

	return [curleft,curtop];
}
