
ontos.widgets.EntityTooltip = function(cfg) {
	this.entityID = cfg.entityID;
	this.entityLabel = cfg.entityLabel; 
	this.entityType = cfg.entityType;
	cfg.html = this.getTooltipCode();
	
	this.dataStore = new Ext.data.Store({
		proxy: new Ext.data.ScriptTagProxy({url: "command",timeout: 3000000,autoAbort:true}),
	    // create reader that reads the Topic records
	    reader: new Ext.data.JsonReader({root: 'results',totalProperty: 'totalCount',id: 'post_id'}, [{name: 'x', mapping: 'x'}])}
	);
	this.dataStore.addListener("load",this.onDataReady,this);
	
	ontos.widgets.EntityTooltip.superclass.constructor.call(this, cfg);
	this.on("show", function() {
		if (false && this.isPerson())
	  		this.dataStore.load({params:{cmd:"htmlscraper", scraper:"spock",text:encodeURIComponent(this.entityLabel), characterset:"UTF-8"}});
	  	else
			this.dataStore.load({params:{google:true,cmd:"jsonscraper", url:"http://www.searchmash.com/results/images:%22"+encodeURIComponent(this.entityLabel)+"%22?i=1&n=1", characterset:"UTF-8"}});  		
		//this.dataStore.load.defer(100, this.dataStore, {params:{google:true,cmd:"jsonscraper", url:"http://www.searchmash.com/results/images:%22"+anOptions.params.text+"%22?i=1&n=1", characterset:"UTF-8"}});
	}, this);
} 

Ext.extend(ontos.widgets.EntityTooltip, Ext.ToolTip, {
	autoHeight: true,
	autoHide:true,
	hideDelay:1500,
	hideOnClick:true,
    pureType : function() {
		var theType = this.entityType.split("#")[1];
		if (!theType) return this.entityType;
		return theType;
	},
	isPerson : function() {
		var theType = this.pureType();
		return theType=="Person" || theType=="Персона";
	},
	getTooltipCode : function() {
		var theType = this.pureType();
		var theCode = "<table>";		
		theCode+="<tr>";
			theCode+="<td colspan='2' style='font-size:12px;font-weight:bold;'>"+theType+"</td>";
		theCode+="</tr>";
		theCode+="<tr>";
			theCode+="<td colspan='2' style='font-size:12px;'>"+this.entityLabel+"</td>";
		theCode+="</tr>";
		
		//if (this.isPerson()){
			theCode+="<tr>";
			theCode+="<td class='cls-photo'><img src='images/loading.gif' width='55'/></td>";
			theCode+="<td class='cls-stats' style='font-size:12px;text-align:center'></td>";
			theCode+="</tr>";

//		} else {
//			theCode+="<tr>";
//			theCode+="<td colspan='2' style='font-size:12px;text-align:center'>"+this.stats+"</td>";
//			theCode+="</tr>";			
//		}

		//theCode+="<tr>";
		//	theCode+="<td colspan='2'><a style='font-size:12px;font-weight:bold;' target='_blank' href='"+theHref+"'>"+"Generate Summary"+"</a></td>";
		//theCode+="</tr>";
		
		var theHref = _news_url + "?searchlabel="+encodeURIComponent(encodeURIComponent(this.entityLabel))+"&lang="+_lang+"&cache=" + _cache;
		theCode+="<tr>";
			theCode+="<td colspan='2'><a style='font-size:12px;font-weight:bold;' href='"+theHref+"'>"+"Navigate"+"</a></td>";
		theCode+="</tr>";

		theCode+="</table>";
		return theCode;
	},
	onDataReady: function(aStore,aRecords,anOptions){
		var theContainer = this.getEl().child("td.cls-photo", true);
		if (anOptions.params.google){
			if (aRecords.length > 0 )
				theContainer.innerHTML = "<img src='"+aRecords[0].json.thumbnailUrl+"' width='55'/>";
			else
				theContainer.innerHTML = "";
			return;
		}
		/*
		if (aRecords.length==0 || aStore.getAt(0).json.image==""){
			// try to use google
			//ontos.widgets.LightEntityObject.dataStore.load({params:{cmd:"htmlscraper", scraper:"google-images",text:anOptions.params.text, characterset:"UTF-8"}});
			ontos.widgets.LightEntityObject.dataStore.load({params:{google:true,cmd:"jsonscraper", url:"http://www.searchmash.com/results/images:%22"+anOptions.params.text+"%22?i=1&n=1", characterset:"UTF-8"}});
			theContainer.innerHTML = "";
			return;
		}
		if (theContainer) {
			theContainer.innerHTML = "<img src='"+aRecords[0].json.image+"' width='55'/>";
		}
		* */
	}
});
/*
new Ext.ToolTip({
        target: 'ajax-tip',
        width: 200,
        autoLoad: {url: 'ajax-tip.html'},
        dismissDelay: 15000 // auto hide after 15 seconds
    });
*/