/*
	Copyright	(c)	2006-2007, epages	Software GmbH
	All	Rights Reserved.

	epages.cartridges.de_epages.presentation.widget.Toolbox	$Revision: 1.22 $

*/
dojo.provide("epages.cartridges.de_epages.presentation.widget.Toolbox");
dojo.require("epages.widget");
dojo.require("dijit._Container");
dojo.require("epages.widget.Menu");

dojo.declare("epages.cartridges.de_epages.presentation.widget.Toolbox",
	[dijit._Widget, dijit._Templated, dijit._Container, dijit._Contained],
	{
		/**
		 * public properties
		 */
		caption				:	"",
		_menuWidget		: null,
		showCollectionName : '0',
		'class'       : '',

		/**
		 * public properties
		 */
		_lastSelectedMenuItem: null,
		_lastSelectedToolCollection: null,

		/**
		 * widget properties
		 */
		templatePath:  dojo.moduleUrl('epages.cartridges.de_epages.presentation.widget', "templates/Toolbox.html"),
		imagePath:     epages.themeUrl('images'),

		// override
		startup:	function() {
                // summary: creates a menu of nested "Toolcollection" widgets
			this.inherited("startup", arguments);
			// add old classes
			dojo.addClass(this.domNode, this['class']);

			var childrenArray=this.getChildren();
			if(childrenArray.length>1) {
				this._menuWidget= new epages.widget.Menu();

				for(var	i=0,l=childrenArray.length; i<l; i++)	{
					if(childrenArray[i].declaredClass=="epages.cartridges.de_epages.presentation.widget.Toolcollection"){
						this._addRow(this.getChildren()[i]);
					}
				}
				dojo.addClass(this.boxTitleNode, "WithMenu");
			}

			if(childrenArray[0] !== undefined){
				this.switchTool(childrenArray[0], (this._menuWidget !== null && this._menuWidget.getChildren().length>0) ? this._menuWidget.getChildren()[0] : null);
			}
		},

		_addRow: function(/*Widget*/toolCollection) {
		// summary: add menu entry (link)
			var parentWidget=this;
			this._menuWidget.addChild(new dijit.MenuItem({
				label: toolCollection.caption,
				iconClass: 'MinimizeIcon',
				onClick: function() {
					parentWidget.switchTool(toolCollection, this);
				}
			}));
		},

		// menu / popup
		showMenu:	function()	{
		// summary: show menu
			dijit.popup.open({
				parent: this.domNode,
				orient: {'BL':'TL', 'BR':'TR', 'TL':'BL', 'TR':'BR'},
				popup : this._menuWidget,
				around: this.domNode
			});
		},

		_onBlur: function () {
			dijit.popup.close(this._menuWidget);
		},

		// event handler
		switchTool:	function(/* widget */toolCollection, /* widget */menuItem) {
                    	// summary: switch to the toolcollectio with the passed widgetid
			dijit.popup.close(this._menuWidget);
			if(this._lastSelectedMenuItem !== null){
				dojo.removeClass(this._lastSelectedMenuItem, "Selected");
			}
			if(this._lastSelectedToolCollection !== null){
				this._lastSelectedToolCollection.style.display="none";
			}

			// change toolbox title
			if(this.showCollectionName =='1') {
				var boxTitle=this.caption;
				boxTitle+=(toolCollection.caption!='' && this.caption !='') ? ":" : '';
				boxTitle+=toolCollection.caption;
				this.titleNode.innerHTML=boxTitle;
			}

			// show tool colection
			toolCollection.domNode.style.display="";
			// save selected toolcollection
			this._lastSelectedToolCollection=toolCollection.domNode;

			if(menuItem !== null) {
				// highlight menuitem (selected)
				dojo.addClass(menuItem.domNode, "Selected");
				// save selected menuItem
				this._lastSelectedMenuItem=menuItem.domNode;
			}
		}
});