function Menu (menus) {
this._menus = menus;
this._menu_top        = 0;
this._sub_x_offset    = 0;
this._sub_y_offset    = 0;
this._close_menus_timer = null;
object_pointer = this;
Menu.POINTERS.push(this);
this.pointer_index = Menu.POINTERS.length - 1;
}
Menu.POINTERS = new Array;
Menu.prototype.draw_menus = function () {
for (var menu_index = 0; menu_index < this._menus.length; menu_index++) {
if (this._menus[menu_index]) {
var menu = this._menus[menu_index];
this._render_menu(menu, [menu_index], this._menus[menu_index].top);
}
}
}
Menu.prototype._render_menu = function (this_menu, path_list, top, z_index, is_sub_menu) {
if (!z_index) {
z_index = 2;
}
var left = (this_menu.left) ? this_menu.left : 0;
this_menu.open_sub = null;
var instance = 'Menu.POINTERS[' + this.pointer_index + ']';
var this_top = top;
this_menu.width = 0;
for (var row_index = 0; row_index < this_menu.content.length; row_index++) {
var row = this_menu.content[row_index];
row.parent = this_menu;
var extras = [];
var this_id_list = Menu.copy_array(path_list);
this_id_list.push(row_index);
var id_list_string = this_id_list.join('_');
extras.push('id="menu_' + id_list_string + '"');
row.id = 'menu_' + id_list_string;
var style = ['position:absolute;',
'visibility:hidden;',
'top:' + this_top + 'px;',
'left:' + left + 'px;',
'z-index:' + z_index + ';'
];
if (row_index == 0 && row_index == this_menu.content.length - 1) {
row.position = 'FirstLast';
} else if (row_index == 0) {
row.position = 'First';
} else if (row_index == this_menu.content.length - 1) {
row.position = 'Last';
} else {
row.position = '';
}
var sub = (is_sub_menu) ? 'Sub' : '';
var classes = ['MENUMenuItem' + sub + row.position];
if (row.class_name) {
classes.push(row.class_name);
}
var row_style;
if (row.url) {
extras.push('onclick="document.location.href=\'' + row.url + '\';"');
extras.push('onmouseover="' + instance + '._roll_over([' + this_id_list.join(',') + ']);"');
extras.push('onmouseout="' + instance + '._roll_out([' + this_id_list.join(',') + ']);"');
row_style = 'cursor:pointer;';
} else if (row.javascript) {
extras.push('onclick="javascript:' + row.javascript + ';"');
extras.push('onmouseover="' + instance + '._roll_over([' + this_id_list.join(',') + ']);"');
extras.push('onmouseout="' + instance + '._roll_out([' + this_id_list.join(',') + ']);"');
row_style = 'cursor:pointer;';
} else {
extras.push('onmouseover="' + instance + '._roll_over([' + this_id_list.join(',') + ']);"');
extras.push('onmouseout="' + instance + '.schedule_close_menus();"');
row_style = 'cursor:default;';
}
if (row.content && row.content.length) {
extras.push('onmouseover="' + instance + '._roll_over([' + this_id_list.join(',') + ']);"');
extras.push('onmouseout="' + instance + '.schedule_close_menus();"');
row_style = 'cursor:pointer;';
classes.push('MENUSubMenuPointer');
}
style.push(row_style);
row.classes = classes;

var class_string  = classes.join(' ');
var style_string  = style.join(' ');
var extras_string = (extras.length) ? ' ' + extras.join(' ') : '';
var this_row = '<div class="' + class_string + '" style="' + style_string + '"'+ extras_string + '>' + row.text + '</div>';
document.write(this_row);
var this_height = document.getElementById('menu_' + id_list_string).offsetHeight;
var this_width  = document.getElementById('menu_' + id_list_string).offsetWidth;
if (this_width > this_menu.width) {
this_menu.width = this_width;
}
if (row.content && row.content.length) {
this._render_menu(row, this_id_list, this_top + this._sub_y_offset, z_index + 2, true);
}
this_top += this_height;
}

for (var row_index = 0; row_index < this_menu.content.length; row_index++) {
var row = this_menu.content[row_index];
document.getElementById(row.id).style.width = this_menu.width + 'px';
}
}
Menu.prototype._roll_over = function (path_list) {
	this._clear_timer();
var row = this._get_menu_object(path_list);
if ((row.parent.open_sub || row.parent.open_sub == 0) && row.parent.open_sub != path_list[path_list.length - 1]) {
var open_sub_path_list = Menu.copy_array(path_list);
open_sub_path_list.pop();
open_sub_path_list.push(row.parent.open_sub);
open_menu = this._get_menu_object(open_sub_path_list);
this._hide_single_menu(open_menu, open_sub_path_list);
}
if (row.url || row.javascript || row.content) {
if (path_list.length > 2) {
var parent_list = Menu.copy_array(path_list);
parent_list.pop();
var parent_menu_row = this._get_menu_object(parent_list);
this._show_single_menu(parent_menu_row, parent_list);
}
if (row.content) {
this._show_single_menu(row, path_list);
row.parent.open_sub = path_list[path_list.length - 1];
}
var id_list_string = path_list.join('_');
var new_classes = Menu.copy_array(row.classes);
new_classes.shift();
var sub = (path_list.length > 2) ? 'Sub' : '';
new_classes.unshift('MENUMenuItemOver' + sub + row.position);
document.getElementById(row.id).className = new_classes.join(' ');
}
}
Menu.prototype._roll_out = function (path_list) {
var row = this._get_menu_object(path_list);
var new_classes = Menu.copy_array(row.classes);
new_classes.shift();
var sub = (path_list.length > 2) ? 'Sub' : '';
new_classes.unshift('MENUMenuItem' + sub + row.position);
document.getElementById(row.id).className = new_classes.join(' ');
this.schedule_close_menus();
if (row.content) {
this._hide_single_menu(row, path_list);
} else {
row.parent.open_sub = null;
}
}
Menu.prototype._get_menu_object = function (path_list) {
var row_object = this._menus[path_list[0]];
for (var i = 1; i < path_list.length; i++) {
var index = path_list[i];
row_object = row_object.content[index];
}
return row_object;
}
Menu.copy_array = function (array) {
var new_array = new Array();
for (var index = 0; index < array.length; index++) {
new_array[index] = array[index];
}
return new_array;
}
Menu.prototype.show_menu = function (menu_index) {
this._clear_timer();
this._hide_all_menus();
this._show_single_menu(this._menus[menu_index], [menu_index]);
}
Menu.prototype._show_single_menu = function (row, path_list) {
if (path_list.length > 1) {
var parent = row.parent;
var right_left = row.parent.left + row.parent.width + this._sub_x_offset;
var right_right = right_left + row.width;
var left_left = row.parent.left - row.width - this._sub_x_offset;
var screen_width = Menu.get_screen_width();
if (screen_width && right_right > screen_width && left_left > 0) {
row.left = left_left;
} else {
row.left = right_left;
}
var new_classes = Menu.copy_array(row.classes);
new_classes.shift();
new_classes.unshift('MENUMenuItemOver' + row.position);
document.getElementById(row.id).className = new_classes.join(' ');
}
if (row.over_function) {
row.over_function();
}
for (var row_index = 0; row_index < row.content.length; row_index++) {
var this_path_list = Menu.copy_array(path_list);
this_path_list.push(row_index);
var this_row = this._get_menu_object(this_path_list);
document.getElementById(this_row.id).style.left = row.left + 'px';
document.getElementById(this_row.id).style.visibility = 'visible';
if (this_row.content) {
this._hide_single_menu(this_row, this_path_list);
}
}
}
Menu.prototype._hide_all_menus = function () {
for (var menu_index = 0; menu_index < this._menus.length; menu_index++) {
if (this._menus[menu_index]) {
this._hide_single_menu(this._menus[menu_index], [menu_index]);
}
}
}
Menu.prototype._hide_single_menu = function (row, path_list) {
if (row.out_function) {
row.out_function();
}
for (var row_index = 0; row_index < row.content.length; row_index++) {
var this_path_list = Menu.copy_array(path_list);
this_path_list.push(row_index);
var this_row = this._get_menu_object(this_path_list);
document.getElementById(this_row.id).style.visibility = 'hidden';
if (this_row.content) {
this._hide_single_menu(this_row, this_path_list);

var new_classes = Menu.copy_array(this_row.classes);
new_classes.shift();
new_classes.unshift('MENUMenuItem' + this_row.position);
document.getElementById(this_row.id).className = new_classes.join(' ');
}
}
if (row.parent && (row.parent.open_sub || row.parent.open_sub == 0)) {
var new_classes = Menu.copy_array(row.classes);
new_classes.shift();
new_classes.unshift('MENUMenuItem' + row.position);
document.getElementById(row.id).className = new_classes.join(' ');
}
}
Menu.prototype.schedule_close_menus = function () {
this._clear_timer();
this._close_menus_timer = setTimeout('object_pointer._hide_all_menus();', 350);
}
Menu.prototype.close_menus_now = function () {
this._clear_timer();
this._hide_all_menus();
}
Menu.prototype._clear_timer = function () {
	clearTimeout(this._close_menus_timer);
}
Menu.get_screen_width = function () {
var scrollbars = 25;
if (Menu._max_width) {
return Menu._max_width;
} else if (document.body.offsetWidth) {
return document.body.offsetWidth - scrollbars;
} else if (window.innerWidth) {
return window.innerWidth - scrollbars;
} else {
return false;
}
}
Menu.prototype.sub_x_offset = function (offset) {
if (offset) {
this._sub_x_offset = offset;
}
return this._sub_x_offset;
}
Menu.prototype.sub_y_offset = function (offset) {
if (offset) {
this._sub_y_offset = offset;
}
return this._sub_y_offset;
}
Menu.prototype.max_width = function (width) {
if (width) {
this._max_width = width;
}
return this._max_width;
}
Menu.prototype.row_height = function (height) {
if (height) {
this._row_height = height;
}
return this._row_height;
}
