// ***** 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 register_menubar(mb_obj)
{
	if (!window.menubar_register)
	{
		window.menubar_register=new Array();
	}

	window.menubar_register[window.menubar_register.length]=mb_obj;
}

function resize_menubars()
{
    if (typeof(window.menubar_register) == 'undefined') return;
    var i,length = window.menubar_register.length;
	for (i=0;i<length;i++)
	{
		window.menubar_register[i].resizebar();
	}
}


function menubar(object_ref, css_class)
{
	register_menubar(this);

	// initialize needed in all
	this.object_ref=object_ref;
	this.object_type="menubar";
	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");
	}
	
	// initialize
	this.css_class=css_class;
	this.buttons=new Array();
	this.visible=true;
	this.child_popup=null;
	this.display_menus=false;

	// general functions
	this.add_object=menubar_add_button;
	this.set_object_visibilities=menubar_set_button_visibilities;
	this.enable_objects=menubar_enable_buttons;

	this.add_button=menubar_add_button;
	this.get_button=menubar_get_button;
	this.set_button_visibilities=menubar_set_button_visibilities;
	this.enable_buttons=menubar_enable_buttons;
	this.set_visibility=menubar_set_visibility;
	this.button_overed=menubar_button_overed;
	this.menu_showing=menubar_menu_showing;
	this.resizebar = menubar_resizebar;

	// event functions
	this.onmousedown=menubar_onmousedown;
	
	// add event callback functions
	this.add_onmousedown_cb=menubar_add_onmousedown_cb;

	//output functions
	this.write_html=menubar_write_html;
	this.get_html=menubar_get_html;
	this.set_html_window=menubar_set_html_window;

	//extra functions
	this.resize_gradient=resize_gradient;
}

// *********************** GENERAL FUNCTIONS ***********************
// General functions should be named "OBJECTNAME"_"FUNCTIONNAME"
// *****************************************************************
function menubar_add_button(button_obj, overwrite)
{
	if (!this.buttons[button_obj.object_ref] || overwrite)
	{
		// needed to set the location of any children that may of been added
		this.set_html_window(this.html_window);
		button_obj.parent_menubar=this;
		this.buttons[button_obj.object_ref]=button_obj;
		var div_obj=get_html_element(this.object_type, this.object_ref, false, this.html_window);
		if (div_obj)
		{
//alert('adding_button')
			try{
				div_obj.insertAdjacentHTML("BeforeEnd", button_obj.get_html());
			}
			catch(err)
			{
				div_obj.innerHTML = div_obj.innerHTML + button_obj.get_html();
			}

			this.resizebar();
		}
		return true;
	}
	else
	{
		return false;
	}
}

function menubar_resizebar()
{
	var maindiv = document.getElementById(this.object_type + "_" + this.object_ref + "_table");
	var graddiv = document.getElementById(this.object_type + "_" + this.object_ref);

	if(location.href.indexOf('preview_pane.phtml') === -1) // preview pane does not like this at all
	{
		// See if we have any buttons showing in this menu bar
		var divs = graddiv.getElementsByTagName('div');
		var i = divs.length;
		var buttons = [];
		while(i--)
		{
			if(divs[i].className.indexOf('picture_button') >= 0 && divs[i].style.display !== 'none')
			{
				buttons.push(divs[i]);
			}
		}

		if(buttons.length) // we have some buttons showing, so show the menu
		{
			maindiv.style.display = 'block';
			graddiv.style.height = maindiv.scrollHeight + "px";
			graddiv.style.display = 'block';
		}
		else // no buttons?  no menu
		{
			maindiv.style.display = graddiv.style.display = 'none';
		}
	}
}

function menubar_get_button(button_ref)
{
	return this.buttons[button_ref]
}

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

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

function menubar_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 menubar_button_overed(picture_button)
{
	if (this.button_showing_menu && this.button_showing_menu!=picture_button)
	{
		this.button_showing_menu.show_popup(false);
		picture_button.show_popup(true);
		this.button_showing_menu=picture_button;
	}
}

function menubar_menu_showing(is_showing, picture_button)
{
	if (is_showing)
	{
		this.button_showing_menu=picture_button;
	}
	else
	{
		this.button_showing_menu=null;
	}
}

// *********************** EVENT FUNCTIONS ***********************
// Event functions should be named "OBJECTNAME"_"EVENTNAME"
// ***************************************************************
function menubar_onmousedown(div_obj)
{
}

// *********************** EVENT CALLBACK FUNCTIONS ***********************
// Event callback functions should be named "OBJECTNAME"_add_"EVENTNAME"_cb
// ************************************************************************
function menubar_add_onmousedown_cb(cb_function)
{
	this.onmousedown_cb_function=cb_function;
}


// *********************** OUTPUT FUNCTIONS ************************
// Output functions should be named "OBJECTNAME"_"FUNCTIONNAME"
// *****************************************************************
function menubar_write_html()
{
	document.write(this.get_html());
}

function resize_gradient(){
	alert(this.object_type + "_" + this.object_ref);
}

function menubar_get_html()
{

	var tmp_str = "";
	tmp_str += "<table id='" + this.object_type + "_" + this.object_ref + "_table' width=100% border=0 cellpadding=0 cellspacing=0 class='menubar_colour menubar_gradient'>";
	tmp_str += "<tr><td>";
	tmp_str += "<div id='" + this.object_type + "_" + this.object_ref + "'  " + "'  " +
	"style='" + (!this.visible?"display:none;":"") + "'" +
	"onmousedown=\"get_broker().get_js_object('" + this.object_type + "', '" + this.object_ref + "').onmousedown(this)\" " +
	">\n";

	var i,length = this.buttons.length;
	for (i = 0; i < length; i++)
	{
		tmp_str += this.buttons[i].get_html();
	}
	tmp_str += "</div>";
	tmp_str += "<div style='position: absolute; top: 0px; left: 0px; display: none;' id='" + this.object_type + "_" + this.object_ref + "_colour'><div id='" + this.object_type + "_" + this.object_ref + "_grad' ></div></div>";
	tmp_str += "</td></tr></table>";
	return tmp_str;
}

function menubar_set_html_window(html_window)
{
	this.html_window=html_window;
	var i,length = this.buttons.length;
	for (i = 0; i < length; i++)
	{
		this.buttons[i].set_html_window(html_window);
	}
}

