var load_method = window.ie ? 'load' : 'domready';
var ACCconfigurator = {};
var acCookie = null;
var tokens=null; 
var acSearchBox = null;
var acDelay		=	150; /*ms*/
var acResizable = true;

ACConfigurator = new Class({

   options: {
		IT_LANG_PREFIX : 'IT',
		EN_LANG_PREFIX : 'EN',
		LANG_MAP: new $H({IT: '.it',EN: '.com'})
	},

	initialize: function(options) {
		this.setOptions(options);
		this.enableHistory=		false;
		this.cookieMaxElement= 	5;
		this.cookieName=		'toplist'; 
		this.coookieOptions = 	{duration: 60, autoSave: false};
		this.acResizable	=   true;
		this.language		=   this._getLang(this.options.language);
		this.configure();
	},
	/* Metodo privato */
	_getLang: function(prx){
		if(prx && prx.test(this.options.EN_LANG_PREFIX, "i"))
				return  this.options.EN_LANG_PREFIX;
		else
			return  this.options.IT_LANG_PREFIX;
	},
	configure: function(){
		this.cookie_conf();
		this.tokens_conf();
		this.autocompleter_conf();
		
	},
	cookie_conf: function(){
		if(this.options.enableHistory) this.enableHistory = this.options.enableHistory;
		if(this.enableHistory){
			if(this.options.maxCookies) this.cookieMaxElement = this.options.maxCookies;
			if(this.options.cookieName) this.cookieName = this.options.cookieName;
			if(this.options.cookieOptions) this.coookieOptions = this.options.coookieOptions;
			acCookie = new ACCookie.Local(this.cookieMaxElement,{'cookieName':  this.cookieName,'cookieOptions': this.coookieOptions});
		}
	},
	tokens_conf: function(){
	
		if(this.options.tokens)
			tokens = this.options.tokens;
	    else
	    {
			this.setTokensByLang(this.language);
	    }
		if(acCookie && acCookie.length()>0)
			tokens= acCookie.toArray().copy().merge(tokens);
	},
	autocompleter_conf: function(){
		if(this.options.acDelay) acDelay = this.options.acDelay;
		if(this.options.acResizable) acResizable = this.options.acResizable;
		acSearchBox = this.options.searchBoxId;

	},
	setTokensByLang: function(lang){
	
	 if(lang==this.options.EN_LANG_PREFIX)
		   tokens   = [['', '']];
	  else
		   tokens   =  [['', '']];
	},
	saveCookie: function(){
		if(acCookie){
			if(!tokens[0].contains($(acSearchBox).value.trim()))
				acCookie.add($(acSearchBox).value,this.options.LANG_MAP.get(this.language));
			}
	}

});

ACConfigurator.implement(new Options);

window.addEvent(load_method,function(){
						    var el = $(acSearchBox);
							var completer1 = new Autocompleter.Local(el, tokens, {
								'delay': acDelay,
								'filterTokens': function() {
									var regex = new RegExp('^' + this.queryValue.escapeRegExp(), 'i');
									return this.tokens.filter(function(token){
										 return (regex.test(token[0]));
										// return (regex.test(token[0]) || regex.test(token[1]));
									});
								},
								'injectChoice': function(choice,i) {
									var el = new Element('li')
										.setHTML((choice[1]=='')? this.markQueryValue(choice[0]) : this.markQueryValue(choice[0],true))
										.adopt(new Element('span', (choice[1]=='') ? {'class': 'info'} : {'class': 'info_i'})
											   .setHTML(choice[1])
										);
									el.inputValue = choice[0];
									this.addChoiceEvents(el).injectInside(this.choices);
								}
								,'resizable': this.acResizable
									});
	});
	
/*** INIZIONE SEZIONE DA CONFIGURARE **/
var acConf = new ACConfigurator({
		language: 'EN',
		tokens: tokens_EN,
		searchBoxId: 'string', /*OBBLIGATORIO*/
		maxCookies: 5,		/* def: 5 */
		enableHistory: true, 	/* def: false */
		cookieName: 'toplist', /* def: toplist */
		cookieOptions: {duration: 60, autoSave: false},   /* def: {duration: 60, autoSave: false} */
		acDelay: 400, 			/* def: 150 ms */
		resizable: false		/* def: true */
		});
				