/*****************************
 * extjscsienricher v. 1.4.1 *
 *****************************/


/**
 * @class ExtCsi.enrichments.Menus
 *        <p>
 *        La classe Menus permette di convertire un menu esistente in un menu a
 *        tendina
 *        </p>
 *        <p>
 *        <b><u>Esempi</u></b>
 *        </p>
 *        <p>
 *        Questo esempio mostra inizializzare il menu<br>
 * 
 * <pre><code>
 * Ext.onReady(function() {
 * 			var tb = ExtCsi.enrichments.Menus.buildMenus('mainMenu');
 * 		});
 * </code></pre>
 * 
 * </p>
 */
ExtCsi.enrichments.Menus = function() {
	return {
		/**
		 * nasconde il menu esistente allo startup
		 */
		autoHide : function(el) {
			Ext.each(el, function(elemento) {
						Ext.fly(elemento).setStyle('display', 'none');
					});
		},
		/**
		 * costruisce il menu
		 * 
		 * @param {String}
		 *            idContainer Rappresenta l'id del div che contiene il menu
		 */
		buildMenus : function(idContainer) {
			//***se il menu non è orizontale non lo arricchisco ed esco
			var elMenu = Ext.query('div.menuPanel');
			var enrichMenu = false;
			Ext.each(elMenu, function(elemento) {
				enrichMenu = Ext.fly(elemento).hasClass('horizontal');
			});
			if(enrichMenu){
				this.buildMenu(idContainer);
			}			
		},
		/**
		 * costruisce il menu
		 * @private
		 * @param {String}
		 *            idContainer Rappresenta l'id del div che contiene il menu
		 */
		buildMenu : function(idContainer) {
			
			// ***recupero solo gli ul figli del div
			var uls = Ext.query('div#' + idContainer + ' > ul');
			
			//***imposto sul container il padding a 0
			Ext.fly(idContainer).setStyle('padding', '0');
			
			this.autoHide(uls);
			var idToolbar = 'id-toolbal-'+idContainer;
			
			//***se il menu esiste già, lo distruggo e poi lo ricreo
			var vwp = Ext.getCmp(idToolbar);
			if(vwp){
				vwp.destroy();
			}
			//***creo il menu solo se non è già esistente
			
			var tb = new Ext.Toolbar({
						id: idToolbar,
						enableOverflow: true,
						renderTo : idContainer,
						cls: 'tbl-Menu',
						height : 30
					});
	
			for (var i = 0; i < uls.length; i++) {
				var elemento = uls[i];
				var han = Ext.query(" > li", elemento);
				for (var j = 0; j < han.length; j++) {
					var el = han[j];
					var texts = Ext.query(" > a", el);
					var href = null;
					if (texts != null && texts != '') {
						var text = texts[0].text || texts[0].innerText;// texts[0].text
						if(text.replace(/[\n\r\t]/g, '').length == 0){
							text = texts[0].innerHTML;
						}
						href = texts[0].href;
						text = text.replace(/[\n\r\t]/g, '');
					} else {
						//var texts12 = Ext.query("span", el);
						//var text12 = texts12[0].text || texts12[0].innerText;// texts[0].text						
						if(el.innerHTML.toLowerCase().indexOf('<span') != -1){
							text = el.innerHTML.substring(el.innerHTML.toLowerCase().indexOf('<span'), el.innerHTML.toLowerCase()
										.indexOf('</span>'));
						}else if(el.innerHTML.indexOf('<') != -1){
							text = el.innerHTML.substring(0, el.innerHTML
										.indexOf('<'));
						}else{
							text = el.innerHTML;
						}
						
						text = text.replace(/[\n\r\t]/g, '');
					}
					if (el.nodeName.toUpperCase() == 'LI') {
						tb.add({
									text : text,
									href : href,
									xtype: href && !Ext.isEmpty(this.buildMenuItem(el)) ? 'splitbutton' : 'button',
									handler : function(item) {
										if (item.href) {
											location.href = item.href;
										}
									},
									cls: 'button-Menu',
									overCls: 'extra-over-button-Menu',
									menu : this.buildMenuItem(el)
								}, '&nbsp;');
					}
				}
			}
			tb.doLayout();
			
		},
		/**
		 * @private
		 */
		buildMenuItem : function(el, sottoMenu) {
			if (sottoMenu == null) {
				sottoMenu = new Ext.menu.Menu();
			}
			var han = Ext.query(" > li, > ul", el);
			if (han == null || han == '') {
				return null;
			}

			for (var i = 0; i < han.length; i++) {
				var elemento = han[i];
				if (elemento.nodeName.toUpperCase() == 'LI') {
					var text = elemento.innerHTML;
					var href = null;
					if(elemento.innerHTML.toUpperCase().indexOf('</A>') != -1){
						var texts = Ext.query(" > a", elemento);
						
						if (texts != null && texts != '') {
							var text = texts[0].text || texts[0].innerText;// texts[0].text
							if(text.replace(/[\n\r\t]/g, '').length == 0){
								text = texts[0].innerHTML;
							}
							href = texts[0].href;
							text = text.replace(/[\n\r\t]/g, '');
						} else {
							if(el.innerHTML.toLowerCase().indexOf('<span') != -1){
									text = el.innerHTML.substring(el.innerHTML.toLowerCase().indexOf('<span'), el.innerHTML.toLowerCase()
										.indexOf('</span>'));
							}else if(el.innerHTML.indexOf('<') != -1){
									text = el.innerHTML.substring(0, el.innerHTML
												.indexOf('<'));
							}else{
								text = el.innerHTML;
							}
							// text = text.split('
							// ').join('').replace(/[\n\r\t]/g,'')
							text = text.replace(/[\n\r\t]/g, '');
						}
					}
					sottoMenu.add({
								text : text,
								href : href,
								handler : function(item) {
									if (item.href) {
										location.href = item.href;
									}
								},
								menu : this.buildMenuItem(elemento)
							});
				} else {
					this.buildMenuItem(elemento, sottoMenu);
				}
			}

			return sottoMenu;
		}
	}
}();