/*idiomas.js*/

var flag_ban = 0;
function showFlags(){
	if(flag_ban == 0){
		flag_ban = 1;
		Ext.get('idio-select-flags').slideIn('t', {
		    easing: 'easeOut',
		    duration: .5
		});
	}else{
		flag_ban = 0;
		Ext.get('idio-select-flags').slideOut('t', {
		    easing: 'easeOut',
		    duration: .5
		});
	}
}




/*buscador.js*/

Ext.ux.BuscadorClasico = Ext.extend(Ext.util.Observable, {
	divListado : null,
	widthList : 250,
	entidad_id : 1,
	nchars : 2,
	bOver : false,
	url : '/',
	emptyText : 'Buscar',
	tplResults : new Ext.XTemplate(
	 	'<div class="suggestions-box-autocomplete">'
			,'<span class="title-list-autocomplete">{msg}</span>'
			,'<div class="suggestion-list-autocomplete">'
				,'<tpl for="data">'
				,'<div class="suggestion-list-option">'
				,'<a href="{url}">'
				,'<div>'
				,'<div class="suggestions-{clase}">'
				,'<h1>{tipo}</h1>'
				,'<h2>{resultados}</h2>'
				,'</div>'
				,'<span>{mostrar}</span>'
				,'</div>'
				,'</a>'
				,'</div>'
				,'</tpl>'
			,'</div>'
			,'<a class="button" ><div> mostrar busqueda avanzada </div></a>'
		,'</div>'
	),
	constructor: function(elId, config){
		this.elId = elId;
		this.input = Ext.get(this.elId);
		if(!this.input) return;
		config = config || {};
		Ext.apply(this, config);

		this.parent = this.input.parent();
		this.parent.position('relative');
		this.divList = this.parent.createChild({tag:'div', cls:'busq-list-div pie', style:'display:none;', html:''});
		this.divList.setVisibilityMode(Ext.Element.DISPLAY);
		this.divList.setWidth(this.widthList);

		this.divList.hover(function(){this.bOver = true;}, function(){this.bOver = false;}, this);

		this.input.on('click',function(e){
			if(this.input.dom.value == this.emptyText){
				this.input.dom.value = '';
				this.input.replaceClass('buscador-input-empty','buscador-input-text');
			}else{
				this.showList();
			}
		}, this);


		this.input.on('blur',function(e){
			if(this.bOver == false){
				if(this.input.dom.value.trim() == ''){
					this.input.replaceClass('buscador-input-text','buscador-input-empty');
					this.input.dom.value = this.emptyText;
				}
				this.hideList();
			}
		}, this);

		this.input.on('keyup',function(e){
			var keyCode = e.getKey();
			var text = this.input.dom.value.trim();
			this.input.replaceClass('buscador-input-empty','buscador-input-text');
			if (keyCode == 13){
				if (text != ''){
					/*
					if(entidad_nombre == 'no_conocida')
						document.location = url('/buscador/'+value);
						//document.location = url('/buscador/buscar/text:'+value+'/pagina:1');
					else
					*/
					document.location = this.url+'buscador/'+text;
						//document.location = url('/buscador/buscar/text:'+value+'/pagina:1', entidad_nombre);
				}
			}else{
				this.tplResults.overwrite(this.divList, {msg:'Procesando...', data : []});
				this.showList();
				this.__lookupForAutoComplete(text);
			}
		}, this);

		var objCloseList = this.parent.parent().child('span.busq-close-list');
		if(objCloseList){
			objCloseList.on('click',function(e){
				this.input.focus();
				this.input.dom.value = '';
				this.hideList();
			}, this);
		}
	},
	__lookupForAutoComplete : function(inputString){
		if(inputString.length < this.nchar){
			this.hideList();
		}else{
			Ext.Ajax.autoAbort = true;
			Ext.Ajax.request({
				url: GLOBAL_PATH+'crequest/buscador/autocompletar_agrupado/'+inputString+'/'+this.entidad_id,
				success: function(response, opts){
					var obj = Ext.decode(response.responseText);
					if(obj.success){
						this.showList();
						this.tplResults.overwrite(this.divList, obj);
					}
				},
				failure: function(response, opts) {
				},
				scope:this
			});
		}
	},
	showList : function(){
		this.divList.show();
		this.divList.setY(this.parent.getY()+this.parent.getHeight());
		this.divList.setX(this.parent.getX()-(this.widthList-this.parent.getWidth()));
	},
	hideList : function(){
		this.divList.hide();
	}
});

/*menu.js*/

Ext.ux.Menu = Ext.extend(Ext.util.Observable, {
	el : null,
	bShowLayer : true,
	bHideLayer : true,
	tagTD : null,
	lastIndex : null,
	selectedIndex : null,
	visible : false,
	constructor: function(elId, layerId, config){
		this.el = Ext.get(elId);
		this.layer = Ext.get(layerId);
		this.delay = config.delay || 250;
        config = config || {};

        this.selector = this.el.select('div.selector').item(0) || false;

        this.layer.setVisibilityMode(Ext.Element.DISPLAY);
        //this.layer.setTop(this.el.getTop()+this.el.getHeight());
		this.layer.setTop(this.el.dom.offsetTop+this.el.getHeight());
        this.layer.setLeft(this.el.getLeft());

        this.selector.setVisibilityMode(Ext.Element.DISPLAY);
        this.selector.setTop(this.el.getHeight()-this.selector.getHeight());
        this.selector.hide();

        this.selector.hover(
        	function(_this){
        		_this.bShowLayer = true;
    			_this.bHideLayer = false;
	    	}.createCallback(this),
	    	function(){

	    	}
	    );

        this.layer.hover(
        	function(_this){
    			_this.bHideLayer = false;
    			if(_this.lastIndex !== _this.selectedIndex){
    				_this.showLayer(_this.lastIndex);
    			}
	    	}.createCallback(this),
	    	function(_this){
        		_this.bHideLayer = true;
        		var task = new Ext.util.DelayedTask(function(){
        			if(_this.bHideLayer)
				    	this.hideLayer();
				},_this);

				task.delay(_this.delay);
	    	}.createCallback(this)
    	);

        this.items = this.el.select('td.hover');
        for(var x = 0; x < this.items.elements.length; x++){
        	var div = Ext.get(this.items.elements[x]);
        	//var hiddenMenu = div.select('div.hiddenMenu').item(0) || false;
        	div.hover(function(_this, index, div){
        		//console.log('_this',_this);
        		//console.log('index',index);
        		//console.log('div',div);
        		_this.bShowLayer = true;
        		_this.bHideLayer = false;
        		_this.showSelector(div);
        		_this.lastIndex = index;
        		var task = new Ext.util.DelayedTask(function(){
        			if(_this.bShowLayer)
				    	this.showLayer(index);
				},_this);

				task.delay(_this.delay);
        	}.createCallback(this, x, div),
        	function(_this){
        		_this.bShowLayer = false;
        		_this.bHideLayer = true;
        		var task = new Ext.util.DelayedTask(function(){
        			if(_this.bHideLayer){
				    	this.hideLayer();
        			}
				},_this);

				task.delay(_this.delay);
        	}.createCallback(this));
        }
	},
	showLayer : function(index){
		if(index !== false){
			this.selectedIndex = index;
			this.layer.dom.innerHTML = Ext.get(this.items.elements[this.selectedIndex]).select('div.hiddenMenu').item(0).dom.innerHTML;
			if(this.layer.select('div.submenu_box').getCount() <= 0){
				this.hideLayer();
				return 0;
			}
			//this.layer.show();
			if (!this.visible){
				this.layer.setLeft(this.el.getLeft());
				this.layer.stopFx();
				this.layer.slideIn('t', {
	                duration: 0.5,
	                callback : function(){
	                },scope : this
	            });
	            this.visible = true;
			}
			var img = this.layer.select('img').item(0);
			if(img){
				var div_descrip = this.layer.select('div.descrip').item(0);
				var arrli = this.layer.select('li');
				var img_src_orginal = img.dom.src;
				var div_descrip_orginal = div_descrip.dom.innerHTML;
				for(var xj = 0; xj<arrli.elements.length; xj++){
					var li = Ext.get(arrli.elements[xj]);
					li.hover(
							function(_this,objli,ObjImg,div_descrip){
								if(typeof objli.getAttribute('descrip') != 'undefined' && objli.getAttribute('descrip') != ''){
									div_descrip.fadeOut();
									div_descrip.dom.innerHTML = objli.getAttribute('descrip');
									div_descrip.stopFx();
									div_descrip.fadeIn();
								}
								if(typeof objli.getAttribute('img') != 'undefined' && objli.getAttribute('img') != ''){
									ObjImg.fadeOut();
									ObjImg.dom.src = objli.getAttribute('img');
									ObjImg.stopFx();
									ObjImg.fadeIn();
								}
							}.createCallback(this,li,img,div_descrip),
							function(_this,objli,ObjImg,div_descrip){
								if(typeof objli.getAttribute('descrip') != 'undefined' && objli.getAttribute('descrip') != ''){
									div_descrip.fadeOut();
									div_descrip.dom.innerHTML = div_descrip_orginal;
									div_descrip.stopFx();
									div_descrip.fadeIn();
								}
								if(typeof objli.getAttribute('img') != 'undefined' && objli.getAttribute('img') != ''){
									ObjImg.fadeOut();
									ObjImg.dom.src = img_src_orginal;
									ObjImg.stopFx();
									ObjImg.fadeIn();
								}
							}.createCallback(this,li,img,div_descrip)
					);
				}
			}
			$('#menu_layer .menuWrapper').masonry({
				columnWidth: 400,
				itemSelector: '.submenu_box'
			});
		}
	},
	showSelector : function(div){
		this.selector.show();
		if (this.tagTD !== div){
			var x = div.getLeft()+(div.getWidth()/2)-(this.selector.getWidth()/2);
			this.tagTD = div;
			this.selector.stopFx();
			this.selector.shift({
	            duration: this.tmOption,
	            x: x,
	            easing: 'easeOut'
	        });
		}
	},
	hideLayer : function(){
		this.layer.stopFx();
		this.selector.hide();
		this.layer.slideOut();
		//this.layer.hide();
		this.visible = false;
	}
});

