// ***** Copyright ©2003 Frogtrade Limited. All rights reserved *****
//
// Purpose: 
//
// Notes: 
//


// *********************** CONSTRUCTOR *****************************
// All components should be a javascript object and should have a unique object_ref
// *****************************************************************
function popup_menu(object_ref, css_class)
{
	// initialize needed in all
	this.object_ref=object_ref;
	this.object_type="popup_menu";
	this.debug_messages=(typeof(ft_debug_messages)!="undefined");
	// register this so html can access the object
	if (typeof(get_broker)!="undefined")
	{
		var broker;
		if (!(broker=get_broker()))
		{
			show_ft_debug_message("popup_menu js class",
								  "Could not add this object: " + this.object_type + " to the broker as the broker cannot be found\n" +
								  "This object will not be able to function properly");
		}
		else
		{
			get_broker().add_js_object(this);
		}
	}
	else if (this.debug_messages)
	{
		show_ft_debug_message("popup_menu js class",
							  "Could not add this object: " + this.object_type + " to the broker as the get_broker function cannot be found\n" +
							  "This object will not be able to function properly");
	}
	
	// initialize
	this.css_class=css_class;
	this.menu_items=new Array();
	this.visible=true;
	this.popup_controller=null;
	this.parent_popup=null;
	this.child_popup=null;
	this.parent_button=null;
	this.creation_window=window; // used to get the style from the window in which the popup was initially created
	
	// general functions
	//this.add_object=popup_menu_add_menu_item;

	this.add_menu_item=popup_menu_add_menu_item;
	this.get_menu_item=popup_menu_get_menu_item;
	this.set_menu_item_visibilities=popup_menu_set_menu_item_visibilities;
	this.enable_menu_items=popup_menu_enable_menu_items;
	this.set_visibility=popup_menu_set_visibility;
	this.menu_item_selected=popup_menu_menu_item_selected;
	this.menu_item_clicked=popup_menu_menu_item_clicked;
	this.show=popup_menu_show;
	this.hide=popup_menu_hide;
	this.get_dimensions=popup_menu_get_dimensions;

	//output functions
	this.get_html=popup_menu_get_html;
	this.set_html_window=popup_menu_set_html_window;
}

// *********************** GENERAL FUNCTIONS ***********************
// General functions should be named "OBJECTNAME"_"FUNCTIONNAME"
// *****************************************************************
function popup_menu_add_menu_item(menu_item_obj, overwrite)
{
	if (!this.menu_items[menu_item_obj.object_ref] || overwrite)
	{
		// needed to set the location of any children that may of been added
		menu_item_obj.set_html_window(this.html_window);
		menu_item_obj.parent_popup=this;
		this.menu_items[menu_item_obj.object_ref]=menu_item_obj;
		var div_obj=get_html_element(this.object_type, this.object_ref, false, this.html_window);
		if (div_obj)
		{
			div_obj.all.measure.insertAdjacentHTML("BeforeEnd", menu_item_obj.get_html());
		}
		return true;
	}
	else
	{
		return false;
	}
}

function popup_menu_get_menu_item(menu_item_ref)
{
	return this.menu_items[menu_item_ref]
}

function popup_menu_set_menu_item_visibilities()
{
	var tmp_menu_item;
	var numargs = popup_menu_set_menu_item_visibilities.arguments.length;
	for (var i=0; i<numargs; i=i+2)
	{
		if (tmp_menu_item=this.menu_items[ popup_menu_set_menu_item_visibilities.arguments[i] ])
		{
			tmp_menu_item.set_visibility(popup_menu_set_menu_item_visibilities.arguments[i+1], get_html_element(this.object_type, this.object_ref, false, this.html_window));
		}
	}
}

function popup_menu_enable_menu_items()
{
	var tmp_menu_item;
	var numargs = popup_menu_enable_menu_items.arguments.length;
	for (var i=0; i<numargs; i=i+2)
	{
		if (tmp_menu_item=this.menu_items[ popup_menu_enable_menu_items.arguments[i] ])
		{
			tmp_menu_item.set_enabled(popup_menu_enable_menu_items.arguments[i+1], get_html_element(this.object_type, this.object_ref, false, this.html_window));
		}
	}
}

function popup_menu_set_visibility(visible, parent_html_elem)
{
	this.visible=visible;
	var div_obj=get_html_element(this.object_type, this.object_ref, parent_html_elem, this.html_window);
	if (div_obj)
	{
		div_obj.style.display=(visible?"block":"none");
	}
}

function popup_menu_menu_item_selected(menu_item)
{
	if (this.child_popup)
	{
		this.child_popup.hide();
		this.child_popup=null;
	}
	if (menu_item.popup_menu)
	{
		var coords=menu_item.get_absolute_position();
		var dims=menu_item.get_dimensions();
		this.child_popup=menu_item.popup_menu;
		this.child_popup.show(this.popups_parent, coords.x, coords.y, coords.x + dims.width, coords.y + dims.height, "R_D", this);
	}
}

function popup_menu_menu_item_clicked()
{
	this.hide(true);
}

function popup_menu_show(popups_parent, x1, y1, x2, y2, display_how, parent_popup)
{
	if (this.popup_controller)
	{
		this.popups_parent=popups_parent;
		this.parent_popup=parent_popup;
		// the + 2 on y1 coord is to take into account the border on the container iframe
		this.popup_controller.show_popup(popups_parent, this.object_ref, x1, y1, x2, y2, display_how);
		if (this.parent_button)
		{
			this.parent_button.menu_showing(true);
		}
	}
	else
	{
// ERROR: trying to show popup which is not registered
	}
}

function popup_menu_hide(hide_parents)
{
	if (this.popup_controller)
	{
		if (this.parent_button)
		{
			this.parent_button.menu_showing(false);
		}

		if (this.child_popup)
		{
			this.child_popup.hide()
		}
		this.popup_controller.hide_child_popup(this.object_ref);
		if (hide_parents && this.parent_popup)
		{
			this.parent_popup.hide(true);
		}
	}
	else
	{
// ERROR: trying to hide popup which is not registered
	}
}

function popup_menu_get_dimensions(parent_html_elem)
{
	if (this.popup_controller) this.popup_controller.reset();
	var div_obj=get_html_element(this.object_type, this.object_ref, parent_html_elem, this.html_window);
	if (div_obj)
	{
		var dims=new Object();
		dims.width=div_obj.ownerDocument.getElementById('measure').offsetWidth;
		dims.height=div_obj.ownerDocument.getElementById('measure').offsetHeight;

		return dims;
	}
	else
	{
		return false;
	}
}

// *********************** OUTPUT FUNCTIONS ************************
// Output functions should be named "OBJECTNAME"_"FUNCTIONNAME"
// *****************************************************************
function popup_menu_get_html()
{
	var tmp_str="<div id='" + this.object_type + "_" + this.object_ref + "' class='" + this.css_class + "' " + 
	"style='" + (!this.visible?"display:none;":"") + "width:100%;position:absolute;top:-4px;left:0px;padding-bottom: 4px;'><table cellpadding=0 cellspacing=0 border=0 id='measure' style='width:auto; height:auto;'><tr><td>\n";
	var menu_item_ref;
	for (menu_item_ref in this.menu_items)
	{
		tmp_str += this.menu_items[menu_item_ref].get_html();
		//alert(this.menu_items[menu_item_ref].get_html());
	}
	return tmp_str + "</td></tr></table></div>\n";
}

function popup_menu_set_html_window(html_window)
{
	this.html_window=html_window;
	var menu_item_ref;
	for (menu_item_ref in this.menu_items)
	{
		this.menu_items[menu_item_ref].set_html_window(html_window);
	}
}