/*
menu.js (this script) copyright (c) Stephen Tucker 2006.
Pretty please do not use this script without my knowledge and permission
but by all means feel free to read through it for educational and learning purposes.
Cheers!
*/

var $menu_open_height = 465;
var $menu_closed_height = 11;
var $menu_start_height = 11;
var $active_mouseout = true;

var $head_element = document.getElementsByTagName("head");
var $link_element = document.createElement("link");
$link_element.setAttribute("rel","stylesheet");
$link_element.setAttribute("href","css/menu.css");
$link_element.setAttribute("media","screen");
$head_element[0].appendChild($link_element);

function createMenuButton() {
	if (!document.getElementById) return false;
	var $menu = document.getElementById('menu');
	var $menu_button = document.createElement('div');
	
	$menu_button.setAttribute('id','menu_button');
	$menu_button.className = 'menu_button_open';
	$menu_button.onclick = function() {
		toddleMenuButtonAction()
		openCloseMenu($menu_open_height);
	}
	$menu.appendChild($menu_button);
	return true;
}
addLoadEvent(createMenuButton);


function toddleMenuButtonAction() {
	if (!document.getElementById) return false;
	if (!document.getElementById('menu_button'))return false;
	var $menu_button = document.getElementById('menu_button');
	
	if ($menu_button.className == 'menu_button_open') {
		$menu_button.className = 'menu_button_closed';
		$menu_button.onclick = function() {
			toddleMenuButtonAction()
			openCloseMenu($menu_closed_height);
		}
	}
	else {
		$menu_button.className = 'menu_button_open';
		$menu_button.onclick = function() {
			toddleMenuButtonAction()
			openCloseMenu($menu_open_height);
		}
	}
	return true;
}


function prepareMenu() {
	if (!document.getElementById) return false;
	var $menu = document.getElementById('menu');
	$menu.style.height = $menu_start_height+'px';
	$menu.onmouseout = function() {
		if (!document.getElementById) return false;
		var $menu = document.getElementById('menu');
		if ($menu.style.height != $menu_closed_height+'px' && $active_mouseout == true) {
			var $repeat = "toddleMenuButtonAction(); openCloseMenu("+$menu_closed_height+");";
			$menu.mouseout = setTimeout($repeat,200);
		}
	}
	$menu.onmouseover = function() {
		if (!document.getElementById) return false;
		var $menu = document.getElementById('menu');
		if ($menu.mouseout) {
			clearTimeout($menu.mouseout);
		}
	}
}
addLoadEvent(prepareMenu);


function openCloseMenu($final_y) {
	if (!document.getElementById) return false;
	if (!document.getElementById('menu')) return false;
	var $menu = document.getElementById('menu');
	
	if ($menu.mouseout) {
		clearTimeout($menu.mouseout);
	}
	if ($menu.movement) {
		clearTimeout($menu.movement);
	}
	
	if (!$menu.style.height) {
		$menu.style.height = $menu_closed_height+'px';
	}
	
	var $position_y = parseInt($menu.style.height);

	if ($position_y == $final_y) {
		return false;
	}
	
	if ($position_y > $final_y) {
		var $px_y = $position_y - Math.ceil(($position_y - $final_y)/15);
		$menu.style.height = $px_y+'px';
	}
	else if ($position_y < $final_y) {
		var $px_y = $position_y + Math.ceil(($final_y - $position_y)/15);
		$menu.style.height = $px_y+'px';
	}

	var $repeat = "openCloseMenu("+$final_y+")";
	$menu.movement = setTimeout($repeat,5);
}
