// ***** 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 menu_item(object_ref, label, item_type, out_css_class, over_css_class, disabled_css_class)
{
	// initialize needed in all
	this.object_ref=object_ref;
	this.object_type="menu_item";
	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("menu_item 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("menu_item 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");
	}

	this.ref_id=this.object_type + "_" + this.object_ref;

	// initialize
	this.label=label;
	this.item_type=item_type;	// "SPACER" "MENU_ITEM"
	this.out_css_class=out_css_class;
	this.over_css_class=over_css_class;
	this.disabled_css_class=disabled_css_class
	this.enabled=true;
	this.visible=true;
	this.popup_menu=null;
	this.parent_popup=null;
	
	// general functions
	this.set_enabled=menu_item_set_enabled;
	this.set_visibility=menu_item_set_visibility;
	this.set_label=menu_item_set_label;
	this.add_popup_menu=menu_item_add_popup_menu;
	this.get_dimensions=menu_item_get_dimensions;
	this.get_absolute_position=menu_item_get_absolute_position;

	// event functions
	this.onclick=menu_item_onclick;
	this.onmouseover=menu_item_onmouseover;
	this.onmouseout=menu_item_onmouseout;
	
	// add event callback functions
	this.add_onclick_cb=menu_item_add_onclick_cb;
	this.add_onmouseover_cb=menu_item_add_onmouseover_cb;

	//output functions
	this.get_html=menu_item_get_html;
	this.set_html_window=menu_item_set_html_window;
}

// *********************** GENERAL FUNCTIONS ***********************
// General functions should be named "OBJECTNAME"_"FUNCTIONNAME"
// *****************************************************************
function menu_item_set_enabled(enabled, parent_html_elem)
{
	this.enabled=enabled;
	var div_obj=get_html_element(this.object_type, this.object_ref, parent_html_elem, this.html_window);
	if (!enabled && div_obj)
	{
		var doc=div_obj.ownerDocument;
		if(doc.getElementById(this.ref_id + "_label")){ doc.getElementById(this.ref_id + "_label").className=this.disabled_css_class; }
		if(doc.getElementById(this.ref_id + "_arrow")){ doc.getElementById(this.ref_id + "_arrow").className=this.disabled_css_class; }
	}
	else if (div_obj)
	{
		var doc=div_obj.ownerDocument;
		if(doc.getElementById(this.ref_id + "_label")){ doc.getElementById(this.ref_id + "_label").className=''; }
		if(doc.getElementById(this.ref_id + "_arrow")){ doc.getElementById(this.ref_id + "_arrow").className=''; }
	}
}

function menu_item_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 menu_item_set_label(label, parent_html_elem, retry)
{
	if (!retry)
	{
		this.label=label;
	}
	var div_obj=get_html_element(this.object_type, this.object_ref, parent_html_elem, this.html_window);
	if (div_obj)
	{
		if (div_obj.document.readyState!="complete" || div_obj.label.innerHTML)
		{// will crash browser if not ready and try to set innerHTML
			window.setTimeout("get_broker().get_js_object('" + this.object_type + "', '" + this.object_ref + "').set_label('', false, true)",500);
		}
		else
		{
			div_obj.label.innerHTML="<nobr>" + this.body_html + "</nobr>";
		}
	}
}

function menu_item_add_popup_menu(popup_menu)
{
	this.popup_menu=popup_menu;
}

function menu_item_get_dimensions()
{
	var div_obj=get_html_element(this.object_type, this.object_ref, false, this.html_window);
	var dims=new Object();

	var doc=div_obj.ownerDocument;
	dims.width=doc.getElementById(this.ref_id + "_row").offsetWidth;
	dims.height=doc.getElementById(this.ref_id + "_row").offsetHeight;

	return dims;
}

function menu_item_get_absolute_position()
{
	var div_obj=get_html_element(this.object_type, this.object_ref, false, this.html_window);
	return get_absolute_position(div_obj);
}

// ***************************** ADD_POPUP IS NEEDED

// *********************** EVENT FUNCTIONS ***********************
// Event functions should be named "OBJECTNAME"_"EVENTNAME"
// ***************************************************************
function menu_item_onclick(div_obj)
{
	if (this.enabled && this.item_type!="SPACER")
	{
		if (this.parent_popup && !this.popup_menu)
		{
			this.parent_popup.menu_item_clicked();

			if (this.onclick_cb_function)
			{
				this.onclick_cb_function(this, "onclick");
			}
		}
	}
}

function menu_item_onmouseover(div_obj)
{
	if (this.enabled && this.item_type!="SPACER")
	{
		var doc=div_obj.ownerDocument;
		doc.getElementById(this.ref_id + "_label").className=this.over_css_class;

		if (this.parent_popup)
		{
			this.parent_popup.menu_item_selected(this);

			var pop_arr=doc.getElementById(this.ref_id + "_popup_arrow")
			if (pop_arr)
			{
				pop_arr.src='/sysicons/popup_arrow_over.gif';
				doc.getElementById(this.ref_id + "_arrow").className=this.over_css_class;
			}

			if (this.onmouseover_cb_function)
			{
				this.onmouseover_cb_function(this, "onmouseover");
			}
		}
	}
}

function menu_item_onmouseout(div_obj)
{
	if (this.enabled && this.item_type!="SPACER")
	{
		var doc=div_obj.ownerDocument;
		doc.getElementById(this.ref_id + "_label").className=this.out_css_class;

		var pop_arr=doc.getElementById(this.ref_id + "_popup_arrow")
		if (pop_arr)
		{
			pop_arr.src='/sysicons/popup_arrow_out.gif';
			doc.getElementById(this.ref_id + "_arrow").className=this.out_css_class;
		}
	}
}

// *********************** EVENT CALLBACK FUNCTIONS ***********************
// Event callback functions should be named "OBJECTNAME"_add_"EVENTNAME"_cb
// ************************************************************************
function menu_item_add_onclick_cb(cb_function, cb_args)
{
	this.onclick_cb_function=cb_function;
	this.onclick_cb_args=cb_args;
}

function menu_item_add_onmouseover_cb(cb_function, cb_args)
{
	this.onmouseover_cb_function=cb_function;
	this.onmouseover_cb_args=cb_args;
}


// *********************** OUTPUT FUNCTIONS ************************
// Output functions should be named "OBJECTNAME"_"FUNCTIONNAME"
// *****************************************************************
function menu_item_get_html()
{
	var style_str=(!this.visible?"display:none;":"");
	var class_str=(!this.enabled && this.item_type!="SPACER"?this.disabled_css_class:this.out_css_class);
	var get_obj_str="get_broker().get_js_object('" + this.object_type + "', '" + this.object_ref + "')";
	var label_str=(this.item_type=="SPACER"?'<img src="/sysicons/menuSpacer.gif" height="2" width="90%">':this.label);
	var popup_arrow_str=(this.popup_menu?"<td align='right' id='" + this.ref_id + "_arrow' class='" + class_str + "'><img id='" + this.ref_id + "popup_arrow' src='/sysicons/popup_arrow_out.gif'></td>\n":"");
	
	// If there is an image in the label of the menu item, the label needs to be split out and have classed DIVs applied to the content
	if(label_str.match("<img") != null)
	{
		// Image should always be at the start, so basically find the first instance of a '>' to find the end of the IMG tag
		var end_tag = label_str.indexOf(">");
		var start_of_image = label_str.indexOf("src=") + 4;
		var end_of_image = label_str.indexOf(" ", start_of_image);

		if ((end_of_image == -1) || (end_tag < end_of_image))
		{
			end_of_image = end_tag;
		}

		var image = label_str.substring(start_of_image, end_of_image);
		var content = label_str.substring((end_tag+1), label_str.length);
		label_str = '<div class="menu_image" style="background-image: url(' + image + ');"></div><div class="menu_text">' + content + '</div>';
	}
	else
		label_str = '<div class="menu_image">&nbsp;</div><div class="menu_text" style="padding-left: 8px;">' + label_str + '</div>';

	return "<table id='" + this.object_type + "_" + this.object_ref + "' width='100%' border='0' cellpadding='0' cellspacing='0' " + 
	"style='" + style_str + "' " + "' class='" + this.out_css_class + "' " + 
	"onclick=\"" + get_obj_str + ".onclick(this)\" " +
	"onmouseover=\"" + get_obj_str + ".onmouseover(this);\" " +
	"onmouseout=\"" + get_obj_str + ".onmouseout(this);\">\n" +
	"<tr id='" + this.ref_id + "_row' style='width:100%; height:100%'><td style='white-space:nowrap' id='" + this.ref_id + "_label' class='" + class_str + "' " + (this.item_type=="SPACER"?'align="center"':'') + ">" + 
	"<nobr>" + label_str + "</nobr></td>\n" + popup_arrow_str + "</tr></table>\n";
}

function menu_item_set_html_window(html_window)
{
	this.html_window=html_window;
}
