/**
 * @author Andrea Bruglia, Davide Stefanini
 */
/*
 * Fix per lo scrolling della griglia su google chorme
 */
Ext.override(Ext.grid.GridView, {
    layout: function(){
        if (!this.mainBody) {
            return;
        }
        var g = this.grid;
        var c = g.getGridEl();
        var csize = c.getSize(true);
        var vw = csize.width;
        if (vw < 20 || csize.height < 20) {
            return;
        }
        if (g.autoHeight) {
            this.scroller.dom.style.overflow = 'visible';
            this.scroller.dom.style.position = 'static';
        }
        else {
            this.el.setSize(csize.width, csize.height);
            var hdHeight = this.mainHd.getHeight();
            var vh = csize.height - (hdHeight);
            this.scroller.setSize(vw, vh);
            if (this.innerHd) {
                this.innerHd.style.width = (vw) + 'px';
            }
        }
        if (this.forceFit) {
            if (this.lastViewWidth != vw) {
                this.fitColumns(false, false);
                this.lastViewWidth = vw;
            }
        }
        else {
            this.autoExpand();
            this.syncHeaderScroll();
        }
        this.onLayout(vw, vh);
    },
	
	setRowError: function(id,qtipError,style){
		if (typeof(id) != 'number') {
			id = this.grid.store.indexOf(id);
		}
		var tr = this.getRow(id);
		style = style || {color:'#e10000',textDecoration:'line-through'};
		this.setRowStyle(id,style);
		new Ext.ToolTip({
	        target: tr,
	        width:300,
	        html: qtipError || 'Rilevato un errore nel rigo',
	        trackMouse:true,
			cls: 'x-form-invalid-tip'
	    });
	},
	
	setRowStyle: function(id,style){
		if (typeof(id) != 'number') {
			id = this.grid.store.indexOf(id);
		}
		var td = this.getCell(id,0);
		var tr = this.getRow(id);
		for (var i=0; td.nextSibling != null ; i++) {
			td = this.getCell(id,i);
			Ext.apply(td.style, style);
		};
	},
	
	clearStyle: function() {
		//debugger;
		this.grid.store.each(function(row){
			var tr = this.getRow(this.grid.store.indexOf(row));
			Ext.QuickTips.unregister(tr);
			td.style ={};
		},this)
	}
});

/*
 * Fix per la visualizzazione delle combobox senza trigger in Explorer
 */
Ext.form.TriggerField.override({
    afterRender: function(){
        Ext.form.TriggerField.superclass.afterRender.call(this);
        var y;
        if (Ext.isIE && !this.hideTrigger && this.el.getY() != (y = this.trigger.getY())) {
            this.el.position();
            this.el.setY(y);
        }
    }
});

/*
 * Fix Problema nel destroy della EditorGrid
 */
Ext.override(Ext.Editor, {
    beforeDestroy: function(){
        Ext.destroy(this.field);
        this.field = null;
    }
});

/* Fix per il getName() della ComboBox */
Ext.override(Ext.form.ComboBox, {
    getName: function(){
        return this.rendered && this.hiddenField && this.hiddenField.name ? this.hiddenField.name : (this.hiddenName || Ext.form.ComboBox.superclass.getName.call(this));
    }
});

/*
 * Metodo per cambiare il tooltip di un bottone a runtime
 */
Ext.override(Ext.Button, {
    setTooltip: function(tt){
        var btnEl = this.el.child(this.buttonSelector);
        Ext.QuickTips.unregister(btnEl.id);
        if (typeof tt == 'object') {
            Ext.QuickTips.register(Ext.apply({
                target: btnEl.id
            }, tt));
        }
        else {
            btnEl.dom[this.tooltipType] = tt;
        }
    }
});

/*
 * Override del QuickTip per aggiungere il tagConfig ext:dismiss-delay
 */
Ext.override(Ext.QuickTip, {
	tagConfig : {
        namespace : "ext",
        attribute : "qtip",
        width : "qwidth",
        target : "target",
        title : "qtitle",
        hide : "hide",
        cls : "qclass",
        align : "qalign",
		dismissDelay: "dismiss-delay"
    },
	
	onTargetOver : function(e){
        if(this.disabled){
            return;
        }
        this.targetXY = e.getXY();
        var t = e.getTarget();
        if(!t || t.nodeType !== 1 || t == document || t == document.body){
            return;
        }
        if(this.activeTarget && t == this.activeTarget.el){
            this.clearTimer('hide');
            this.show();
            return;
        }
        if(t && this.targets[t.id]){
            this.activeTarget = this.targets[t.id];
            this.activeTarget.el = t;
            this.delayShow();
            return;
        }
        var ttp, et = Ext.fly(t), cfg = this.tagConfig;
        var ns = cfg.namespace;
        if(this.interceptTitles && t.title){
            ttp = t.title;
            t.qtip = ttp;
            t.removeAttribute("title");
            e.preventDefault();
        } else{
            ttp = t.qtip || et.getAttributeNS(ns, cfg.attribute);
        }
        if(ttp){
            var autoHide = et.getAttributeNS(ns, cfg.hide);
            this.activeTarget = {
                el: t,
                text: ttp,
                width: et.getAttributeNS(ns, cfg.width),
                autoHide: autoHide != "user" && autoHide !== 'false',
                title: et.getAttributeNS(ns, cfg.title),
                cls: et.getAttributeNS(ns, cfg.cls),
                align: et.getAttributeNS(ns, cfg.align),
				dismissDelay: et.getAttributeNS(ns, cfg.dismissDelay)
            };
            this.delayShow();
        }
    }
})

/*
Ext.override(Ext.form.BasicForm, {
	
    setValues: function(values, setOriginalValues){
        if (Ext.isArray(values)) { // array of objects. Convert to object hash
            var valuesObject = {};
            for (var i = 0, len = values.length; i < len; i++) {
                valuesObject[values[i].id] = values[i].value;
            }
            return this.setValues(valuesObject);
        }
        else { // object hash
            for (var i = 0, items = this.items.items, len = items.length; i < len; i++) {
                var form_field = items[i];
                var v = values[form_field.id] || values[form_field.hiddenName || form_field.name];
                if (typeof v !== 'undefined') {
                    switch (form_field.getXType()) {
                        case 'namirial_textfield':
                            try {
                                v = Ext.util.Format.trim(v);
                            }
                            catch (e) {
                            // null
                            }
                            form_field.setValue(v);
                            break;
                        default:
                            // standard behaviour => setValue()
                            form_field.setValue(v);
                    }
                    if (this.trackResetOnLoad) {
                        form_field.originalValue = form_field.getValue();
                    }
                    if (form_field.isLookup) {
                    
                        if (form_field.lookup.updateDescriptionOnLoad) {
                            form_field.lookup.updateDescription();
                        }
                    }
                }
                else {
                    try {
                        //log.error("Il campo " + form_field.name + " non ha un corrispettivo nello store. Verificare il nome.")
                    } 
                    catch (e) {
                    }
                }
            }
        }
        
        return this;
    }
})
*/
