/**
 * @class Voyeur.Tool.WordCountFountain 
 * @extends_ext Ext.Panel
 * @extends Voyeur.Tool
 * @namespace Voyeur.Tool
 * @author Stéfan Sinclair
 * @since 0.1.1
 */
Voyeur.Tool.WordCountFountain = Ext.extend(Ext.Panel, {
	constructor : function(config) {
	
		// inherit Voyeur Tool
		Ext.apply(this, new Voyeur.Tool(config, this))

		// call superclass
		Voyeur.Tool.WordCountFountain.superclass.constructor.apply(this, arguments);
		
		// create applet when the corpus is loaded
		/**
		 * @event CorpusSummaryResultLoaded
		 * @type listener
		 */
		this.addListener('CorpusSummaryResultLoaded', function() {
			if (this.rendered) {this.fireEvent('afterrender', this);}
		}, this);
		
		// create applet when the corpus is loaded
		this.addListener('afterrender', function() {
			
			var content = '';
			
			var corpus = this.getCorpus();
			var size = corpus.getSize();
			if (size>0) {
				
				// set core parameters
				var params = this.getApiParams();
				
				// check to make sure we don't have too many words
				if (size>1 && !params.docIndex && !params.docId) {
					var docIndex = [];
					var words = 0;
					for (var i=0;i<size;i++) {
						var doc = corpus.getDocument(i);
						docIndex.push(doc.getIndex())
						words += doc.get('totalWordTokens');
						if (words>100000) {
							params.docIndex = docIndex;
							break;
						}
					}
				}
				
				// create applet
				content = '<div align="center"><applet code="ca.hermeneuti.processing.wordcountfountain.WordCountFountain" archive="'+this.getToolDirectoryUrl()+'WordCountFountain.jar" width="1200" height="600">'+
					'<param name="source" value="'+this.getTromboneUrl()+'?tool=DocumentExporter&outputFormat=text&template=docExport2plainText&' + Ext.urlEncode(params)+ '" />'+
					'<param name="title" value="'+this.localize('title')+' in '+this.localize('title','app')+'" />'+
					'To view this content, you need to install Java from <A HREF="http://java.com">java.com</A></applet><div>'+this.localize('adaptedFrom')+'</div></div>'
			}
			
			// update body contents	with applet
			this.body.update(content);
			
		}, this)
	}

	,api: {
		toolType: ['Visualization']
		,listeners: ['CorpusSummaryResultLoaded']
	}
	
	,thumb: {
		large: 'WordCountFountain.png'
	}

	// localization variables
	,i18n : {
		title : {en: "Word Count Fountain"}
	    ,type : {en: "Visualization"}
		,help: {en: "This tool visualizes word frequencies as a fountain."}
		,adaptedFrom: {en: "Word Count Fountain is adapted from <a href=\"http://iragreenberg.com/\" target=\"_blank\">Ira Greenberg</a>'s <a href=\"http://iragreenberg.com/poetess/viz01/\" target=\"_blank\">work</a> with <a href=\"http://www.users.muohio.edu/mandellc/\" target=\"_blank\">Laura Mandell</a>."}
	}
});

Ext.reg('voyeurWordCountFountain', Voyeur.Tool.WordCountFountain);

