var Topwords = {};

Topwords.Base = new Class({
	options: {
	url: null,
	CHANNELS_MAP: new $H({1: 'GRUPPO',2: 'INNOVAZIONE',3: 'SOSTENIBILITA',4: 'GOVERNACE', 5: 'INVESTITORI',6: 'MEDIA',7: 'CARRIERA'})
	},
	initialize: function(options) {
		this.setOptions(options);
		this.container = null;
		this.initialized = false;
		this.url = null;
		this.queryValue = null;
		this.configure();
	},
	configure: function(){
		try{
			this.language   = this.options.language;
			this.intChannel = this.options.channel;
			this.strChannel = this.options.CHANNELS_MAP.get(this.options.channel);
			this.url = this.buildUrl();
			this.container = $(this.options.containerID);
			this.initialized = (this.base_url!=null);
		}catch(e){
			
		}
	},
	buildUrl: function(){
		return this.options.data_url;
	},
	
	start: function(){
		var request = new Json.Remote(this.url, {
			onComplete: function(jsonObj) {
				this.buildHtml(jsonObj.tags);
			}
		}).send();
	
	},
	buildHtml: function(tags) {
	},
	
	destroy: function() {
		this.container.empty();
	}
});

Topwords.Base.implement(new Options);



Topwords.Json = Topwords.Base.extend({
	options: {
		jsonOptions: {},
		onRequest: Class.empty,
		onComplete: Class.empty
	},
	initialize: function(options) {
		this.parent(options);
		this.indicator = new Element('div', {'id': 'jsl', 'class': 'json-loading', 'styles': {'display': 'none'}}).setHTML('').injectInside(this.container);
		this.json = new Json.Remote(this.url, $merge({
			method: 'get',
			autoCancel: true
		}, this.options.jsonOptions));
		
		this.json.addEvent('onComplete', function(jsonObj) {
				this.indicator.remove();
				this.buildHtml(eval('jsonObj.tags_' + this.language));
			}.bind(this));
		
		this.json.addEvent('onRequest', function(jsonObj) {
				this.indicator.setStyle('display', '');
			}.bind(this));	
	},
    start: function(){
		this.json.send();
	},
   buildHtml: function(tags) {
	var mainDiv = this.container;
	tags.each(function(tag) {
		var link = new Element('a',{'href': tag.href, 'title': tag.description, 'class': 'tw_size' + tag.size}).setHTML(" " + tag.word +" ").inject(mainDiv);
	});
   }

});
function debug(txt){}