function swap_image(ptr, image) {
	ptr.src = image;
}

shade_amount = 500;

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) + 5;
	
	menu_inner.style.height = '1px';
	menu_outer.style.height = '1px';
	
	if(pos[0] + target_width > 780)
		target_width = target_width * -1;

	// position the item
	set_position(menu_outer, pos[0] + target_width, pos[1]);
	set_position(menu_inner, pos[0] + target_width, pos[1]);

	// height of each
	shade_div_down(menu_outer, target_height);
	shade_div_down(menu_inner, target_height);
}

function show_menu(ptr, id) {
  	close_menu();
	
	hide_select();

	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);
	target_width  = get_width(menu_inner);
	
	offset = -1;

	menu_inner.style.height = '1px';
	menu_outer.style.height = '1px';

	// position them
	set_position(menu_outer, pos[0] + offset, pos[1] + 92);
	set_position(menu_inner, pos[0] + offset, pos[1] + 92)

	// height of each
	shade_div_down(menu_outer, target_height);
	shade_div_down(menu_inner, target_height);
}

function show_menu_alt(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);
	target_width  = get_width(menu_inner);
	
	offset = 0;

	menu_inner.style.height = '1px';
	menu_outer.style.height = '1px';
	
	pos_anch = find_pos(document.getElementById('body_anchor'));
	pos_anch[0] = 0;
	pos_anch[1] = 0;

	// position them
	set_position(menu_outer, pos[0] - 103 - pos_anch[0], pos[1] + 26 - pos_anch[1]);
	set_position(menu_inner, pos[0] - 103 - pos_anch[0], pos[1] + 26 - pos_anch[1])

	// 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);
	
	unhide_select();
}

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];
}

function hide_select() {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j]
		for(i=0; i<theForm.elements.length; i++){
			if(theForm.elements[i].type == "select-one") {
				theForm.elements[i].style.visibility = "hidden";
			}
		}
	}
}

function unhide_select() {
	var theObj = document.getElementsByTagName("select");
	for(j=0; j<theObj.length; j++)
		theObj[j].style.visibility = "visible";
}

function menu_out(i) {
	
}