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

		// call superclass
		Voyeur.Tool.Bubbles.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.ballsreader.BallsReader" archive="'+this.getApplication().getBaseUrl()+'resources/lib/balls-reader/ballsreader-0.0.1-SNAPSHOT-jar-with-dependencies.jar" width="800" height="600">'+
					'<param name="source" value="'+this.getTromboneUrl()+'?tool=DocumentExporter&outputFormat=text&' + Ext.urlEncode(params)+ '" />'+
					'<param name="title" value="'+this.localize('title')+' in '+this.localize('title','app')+'" />';
				if (params.stopList) {
					content += '<param name="stopList" value="'+params.stopList+'" />';
				}
				content +=	'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: {
		/**
		 * @property docIndex The document index to restrict results to.
		 * @type Integer
		 * @default null
		 */
		'docIndex': {
			'default': null
			,'type': Number
			,'required': false
			,'value': null
			,'multiple': true
			,'example': 0
		}
		/**
		 * @property docId The document ID to restrict results to.
		 * @type String
		 * @default null
		 */
		,'docId': {
			'default': null
			,'type': String
			,'required': false
			,'value': null
			,'multiple': true
			,'example': 'a_valid_document_id'
		}
		/**
		 * @property template The template to use with DocumentExporter.
		 * @type String
		 * @default docExport2plainText
		 */
		,'template': {
			'default': 'docExport2plainText'
			,'type': String
			,'required': false
			,'value': null
			,'multiple': false
		}
		/**
		 * @property stopList The stop list to use to filter results.
		 * Choose from a pre-defined list, or enter a comma separated list of words, or enter an URL to a list of stop words in plain text (one per line).
		 * @type String
		 * @default null
		 * @choices stop.en.taporware.txt, stop.fr.veronis.txt
		 */
		,'stopList': {
			'default': null
		}
		,toolType: ['Visualization']
		,listeners: ['CorpusSummaryResultLoaded']
	}
	
	,thumb: {
		large: 'Bubbles.png'
	}

	// localization variables
	,i18n : {
		title : {en: "Bubbles"}
	    ,type : {en: "Visualization"}
		,help: {en: "This tool reads the words in a document and displays the highest frequency words proportionately large bubbles."}
		,adaptedFrom: {en: "Bubbles is adapted from Martin Ignacio Bereciartua's <a href=\"http://www.m-i-b.com.ar/mib/letter_pairs/eng/\" target=\"_blank\">Letter Pairs</a>."}
	}
});

Ext.reg('voyeurBubbles', Voyeur.Tool.Bubbles);

