ontos.pandora = function(aString,aLength){
	return (aString.length>aLength)?aString.substring(0,aLength)+"...":aString;
};
//////////////////////// get external people bank //////////////////

function getPhoto(aConfig){
	var theType = aConfig[1].entityType;
	if (theType=="Person" || theType=="Персона")
  		ontos.widgets.LightEntityObject.dataStore.load({params:{cmd:"htmlscraper", scraper:"spock",text:ontos.encode(aConfig[1].entityLabel), characterset:"UTF-8"}});
  	else
		ontos.widgets.LightEntityObject.dataStore.load({params:{google:true,cmd:"jsonscraper", url:"http://www.searchmash.com/results/images:%22"+ontos.encode(aConfig[1].entityLabel)+"%22?i=1&n=1", characterset:"UTF-8"}});		
	return aConfig[1].text;
}

ontos.widgets.LightEntityObject = function(anEntityID,anEntityLabel,anEntityType){
	this.entityID = anEntityID;
	this.entityLabel = anEntityLabel; 
	this.entityType = anEntityType;
	this.stats = ""
}

ontos.widgets.LightEntityObject.prototype = {
	render : function(aContainer) {
		var c = "<a class='entity' href='#'>";		
		c+=ontos.pandora(this.entityLabel,20);
		c+="</a>"; 
		return c;
	},
	render2 : function(aContainer) {
		var c = ontos.pandora(this.entityLabel,20);
		return c;
	},
	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=="Персона";
	},
	getTooltipCfg : function() {
		//var isPersonVar = this.isPerson();
		//if (isPersonVar){
		var theType = this.pureType();
		return {entityLabel:this.entityLabel,entityType:theType,callback:getPhoto,text:this.getTooltipCode()};
		//} else {
		//	return {text:this.getTooltipCode()};
		//}
	},
	registerTooltip : function(theElement) {
		new ontos.widgets.EntityTooltip({target: theElement, entityID:this.entityID, entityLabel:this.entityLabel,entityType:this.entityType});
		//Ext.QuickTips.register(Ext.apply(this.getTooltipCfg(), {target:theElement}));
	},
	getTooltipCode : function(){
		var theType = this.pureType();
		var theHref = "command?cmd=summary&uri="+encodeURIComponent(this.entityID)+"&lang="+_lang;
		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 id='photo'><img src='images/loading.gif' width='55'/></td>";
			theCode+="<td style='font-size:12px;text-align:center'>"+this.stats+"</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>";

		theCode+="</table>";
		return theCode;
	}
}

ontos.widgets.EntityObject = function(anEntityID,anEntityLabel,anEntityType,aConfig){
	aConfig = aConfig || {};
	this._delagate = new ontos.widgets.LightEntityObject(anEntityID,anEntityLabel,anEntityType);
	this._onlyTooltip = aConfig.onlyTooltip;
	ontos.widgets.EntityObject.superclass.constructor.call(this, aConfig);
};

Ext.extend(ontos.widgets.EntityObject, Ext.Component, {
	setStats : function (s) {
		this._delagate.stats = s;
	},
	render : function(aContainer) {
		if (this.stats) {
			this.setStats(this.stats);
		}
		var theElement;
		if (!this._onlyTooltip) {
			var c = this._delagate.render();
			theElement = Ext.DomHelper.append(aContainer,c,true);
		} else {
			theElement = Ext.get(aContainer);
		} 
		
		this._delagate.registerTooltip(theElement);
		theElement.on("click",this.onClick,this,this);
	},
	onClick : function(anEvent,anDomElement,anObject){
		this.fireEvent("click", this.getSpec());
	},
	getSpec : function(){
		return {entityID:this._delagate.entityID,entityLabel:this._delagate.entityLabel,entityType:this._delagate.entityType};
	}
});

ontos.widgets.LightEntityObject.onDataReady =function(aStore,aRecords,anOptions){
	var theContainer = document.getElementById("photo");
	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'/>";
	}
};

ontos.widgets.LightEntityObject.dataStore =  new Ext.data.Store({
	proxy: new Ext.data.ScriptTagProxy({url: "command",timeout: 3000000}),
    // create reader that reads the Topic records
    reader: new Ext.data.JsonReader({root: 'results',totalProperty: 'totalCount',id: 'post_id'}, [{name: 'x', mapping: 'x'}])}
);

ontos.widgets.LightEntityObject.dataStore.addListener("load",ontos.widgets.LightEntityObject.onDataReady,this);
