/*
 * Ext Action Bar Skinning Script Release 2
 * M D McNamara
 *
 * 12/17/07 -- Updated with ability to render action bar at bottom of form
 *
*/

// Assign namespaces
Ext.namespace("EXTMD", "EXTMD.ACTIONBAR");

Ext.onReady(function(){
	// The init() method is called here for convienence purposes -- 
	// it can be removed and placed elsewhere if so desired
	EXTMD.ACTIONBAR.init({
		// true if you want the actionbar to render at the bottom of the
		// default DOMINO <form>
		renderAtBottom: false
	});
});

EXTMD.ACTIONBAR = function ()
{
	//*****
	// PRIVATE INTERFACE
	//*****
	
	// Initialize variables
	var Ext_ActionBar = null;
	var Ext_MenuObject = null;
	var actionSubmenuPrefix = null;
	var clearOutCurrentMenu = false;
	var CONFIG = null;
	
	var addActionToExtObj = function(extObj, domObj, title) {
		// Dom href must be pulled into a variable here or IE 6 will prefix with "about:blank"
		var dom_href = domObj.href;

		extObj.add({
			id: "DOMINO-tb-" + escape(title),		
			text: title,
			handler: (
				domObj.onclick ? 
				domObj.onclick :
				function () {
					window.open(dom_href, domObj.target);
				}
			)
		});
	}
	
	var privInit = function (configObj) {
		// ******
		// Bring in passed config object (if present)
		// ******
		CONFIG = configObj;
		
		// ******
		// Grab pre-exisiting actions as provided by Domino -- has to be performed first
		// ******
		var Domino_toolbar = Ext.query('a', Ext.query('table')[0]);
		Ext.get(Ext.DomQuery.selectNode('table')).set({id: 'Original_Domino_Toolbar'});
	
		// *****
		// Create new toolbar element at top (or bottom) of Domino form
		// *****
		Ext_ActionBar = new Ext.Toolbar(
			(CONFIG.renderAtBottom) ?
				Ext.DomHelper.insertAfter(Ext.DomQuery.selectNode('form'), 
				'<div id="DOMINO-Toolbar"></div>') :
				Ext.DomHelper.insertFirst(Ext.DomQuery.selectNode('form'), 
				'<div id="DOMINO-Toolbar"></div>')
		);
	
		// *****
		// Process Default Domino toolbar actions
		// *****
		Ext.each(Domino_toolbar, function (obj, num)
		{
			// Only check for new submenu title match if one doesn't currently exist.
			if (!actionSubmenuPrefix)
			{
				actionSubmenuPrefix = obj.innerHTML.match(/^.+\\/);
			}
			
			if (actionSubmenuPrefix)
			// If submenu title has been detected...
			{
				if (!Ext_MenuObject)
				// If menu is not being constructed, then create new menu and add to toolbar
				{
					Ext_MenuObject = new Ext.menu.Menu();
					Ext_ActionBar.add({
						cls: 'x-btn-text-icon bmenu',
            				text: actionSubmenuPrefix[0].substr(0, actionSubmenuPrefix[0].length - 1),
            				menu: Ext_MenuObject
            			});
				}
				
				// Add menu item to menu currently under construction
				addActionToExtObj(
					Ext_MenuObject, 
					obj, 
					obj.innerHTML.match(/\\.+/)[0].substr(1)
				);
				
				// Attempt a lookahead to next action bar item
				if (Domino_toolbar[num + 1])
				{
					// If next action item exists, 
					// check to see if it is part of a submenu, too
					if (Domino_toolbar[num + 1].innerHTML.match(/^.+\\/))
					{
						// If next item was part of a submenu, check to see
						// if its prefix is the same as this item's
						if ((Domino_toolbar[num + 1].innerHTML.match(/^.+\\/))[0] != actionSubmenuPrefix[0])
						{
							// Finish menu if next item's submenu has different prefix
							clearOutCurrentMenu = true;
						}
					}
					else
					{
						// Finish menu if next item is standalone
						clearOutCurrentMenu = true;
					}
				}
				else
				{
					// If no other action bar item exists, then finish current menu
					clearOutCurrentMenu = true;
				}

				// Clear out menu variables to "complete" menu
				if (clearOutCurrentMenu)
				{
            			Ext_MenuObject = null;
            			actionSubmenuPrefix = null;
            			clearOutCurrentMenu = false;
				}
			} // end if (actionSubmenuPrefix)
			else
			{
				// If this is a regular, single button, add immediately to toolbar
				addActionToExtObj(Ext_ActionBar, obj, obj.innerHTML);
			} // end else [if (actionSubmenuPrefix)]
		}); // end inline Ext.each function 	
		
		// *****
		// Remove default Domino DOM items
		// *****
		Ext.get('Original_Domino_Toolbar').remove();
		Ext.get(Ext.DomQuery.selectNode('hr')).remove();
	}
	
	//*****
	// PUBLIC INTERFACE
	//*****
	
	return {
		init: function (configObj)
		{
			privInit(configObj);
		}
		,
		add: function (buttonObj)
		{
			Ext_ActionBar.add(buttonObj);
		}
	}
}();
