Ext.BLANK_IMAGE_URL = 'images/default/s.gif';

Ext.ux.PersistentTip = Ext.extend(Ext.ToolTip,{
	  initComponent: function() {
	    Ext.apply(this,{
	      showDelay: 100,
	      hideDelay: 500,
	      autoHide: false
	    });
	    Ext.ux.PersistentTip.superclass.initComponent.call(this);
	  },
	  
	  afterRender: function() {
	    Ext.ux.PersistentTip.superclass.afterRender.call(this);
	    this.el.on('mouseout', this.onTargetOut, this);
	    this.el.on('mouseover', this.onElOver, this);
	  },
	  
	  checkWithin: function(e) {
	    if(this.el && e.within(this.el.dom, true)) {
	      return true;
	    }
	    if(this.disabled || e.within(this.target.dom, true)){
	      return true;
	    }
	    return false;
	  },
	  
	  onElOver: function(e) {
	    if (this.checkWithin(e)) {
	      this.clearTimer('hide');
	    }
	  },
	  
	  onTargetOver : function(e){
	    if(this.disabled || e.within(this.target.dom, true)){
	        return;
	    }
	    this.clearTimer('hide');
	    this.targetXY = e.getXY();
	    this.delayShow(e);
	    },

	  delayShow : function(e){
	    this.showTimer = this.doShow.defer(this.showDelay, this, [e]);
	    },
	  
	  doShow: function(e) {
	    var xy = e.getXY();
	    var within = this.target.getRegion().contains({left: xy[0], right: xy[0], top: xy[1], bottom: xy[1]});
	    if (within) {
	      this.show();
	    }
	  },
	  
	  onTargetOut: function(e){
	    if (this.checkWithin(e)) {
	      this.clearTimer('hide');
	    } else if (this.hideTimer) {
	      this.hide();
	    } else {
	      this.hideTimer = this.onTargetOut.defer(this.hideDelay,this,[e]);
	    }
	    }
	});
Ext.reg('persistenttip',Ext.ux.PersistentTip); 

	
function getXY(e)
{
	var x,y;
	e = e || window.event;
	if (navigator.appName.indexOf('Microsoft') != -1)
	{
		var parent = e.srcElement;
		x=e.clientX + document.body.scrollLeft;
		y=e.clientY + document.body.scrollTop;
	}
	else
	{
		x =e.pageX
		y= e.pageY
	}
	var XY = new Object();
	XY.x = x;
	XY.y = y;
	return XY;
}
 
var cttd = function(event ,obj){
	var tt = obj.dextt;
	xy = getXY(event);
	if (obj.dextt == null){
		var persistentTip = false;
		if (obj.href.indexOf('DexVizual') != -1) {
			persistentTip = true;
		}
		str = obj.href.replace('CautareDex', 'DexTooltip').replace('Gramatica', 'GramaticaTooltip').replace('CautareWebster', 'WebsterTooltip').replace('Dictionar', 'DictionarTooltip').replace('DexVizual', 'DexTooltip');
		var config = {
				target: obj,
				autoHeight: true,
				showDelay: 500,
				autoLoad: {url: str},
				width: 400,
				autoShow: true,
				autoHide: !persistentTip,
				dismissDelay: 30000 // auto hide after 30 seconds
			};
		if (persistentTip) {
			obj.dextt = new Ext.ux.PersistentTip(config);
		} else {
			obj.dextt = new Ext.ToolTip(config);
		}
		tt = obj.dextt;
		tt.addEvents({
	        mouseout: true
	    });
		tt.showTimer = tt.show.defer(tt.showDelay, tt);
	};
};

var exp = function(event ,obj){
	if (obj.expDone == null){
		obj.expDone = 1;
		var handleQuery = function( r, params ){
		      if (r.readyState == 4) {
		    	  	var show = function(){
			        var xmlDoc = r.responseXML;
			        // obtain the array of synsets and loop through it
			        var synsets = xmlDoc.documentElement.getElementsByTagName("synset");
			        if (synsets.length == 0) {
			    	  	var synNode = obj.synNode.addChild(new TreeNode("syn_N_"+obj.synNode.id, 40,
			                     40,  obj.synNode['position']['x'], obj.synNode['position']['y']));
			        	 createTooltipWindow(synNode, "Nu au fost găsite sinonime.", false, 'levelN');
			        } else {
				        for( var i=0; i<synsets.length; i++ ) {
				        	var crtWord = synsets[i].getAttribute('word');
				        	var id = synsets[i].getAttribute('id');
				        	var newSynNode = obj.synNode.addChild(new TreeNode("syn_N_"+obj.synNode.id + "_" + id, 60,
				                    60, obj.synNode['position']['x'], obj.synNode['position']['y']));
				          createTooltipWindow(newSynNode, crtWord, true, 'levelN');
				        }
				        obj.synNode['arc'] += 6 * obj.synNode['children'].length;
				        obj.synNode.updateChildren();
	//			        obj.synNode.parent.updateChildren();
	//			        var distributionListener = new DistributionListener();
	//		            distributionListener.update();
		    	  	} 
		    	  	}
		    	  	show.defer(500, this);
		      }
		};
	    get("DexVizualXML?query=" + obj.innerHTML,handleQuery,null);
	}
	
}

Ext.onReady(function(){
    Ext.QuickTips.init();
});
