﻿var App = {
	start: function() {
		App.FavoriteResults.initialize();
		App.VietTyping.initialize();
		document.searcher.query.focus();
	}
};

App.FavoriteResults = {
	initialize: function() {
		//$E("a.favorites", "top-bar").setText( //Mootools 1.1
		//	"Kết quả ưa thích (" + new GM.Utils.FavoriteManager({maxEntries: GM.Consts.numFavoriteResults}).count() + ")"
		//);
		$$("a.favorites", "top-bar").set('text',
			"Kết quả ưa thích (" + new GM.Utils.FavoriteManager({maxEntries: GM.Consts.numFavoriteResults}).count() + ")"
		);
	}
};

App.VietTyping = {
	_el: null,
	
	initialize: function() {
		//this._el = $E("img.vietTyping", "search-box"); //Mootools 1.1
		this._el = $$("img.vietTyping", "search-box"); //Mootools 1.2
		if ($defined(this._el)) {
			this._el.addEvent("click", this._toggleOnOff.bind(this));
			
			//var on = Cookie.get("AVIM_on_off"); //Mootools 1.1
			var on = Cookie.read("AVIM_on_off"); //Mootools 1.2
			if (on && on.toInt())
				this._toggleOnOff();
		}
	},
	
	_toggleOnOff: function() {
		if (this._el.hasClass("on")) {
			AVIMObj.setMethod(-1);
			
			this._el.title = "Bật bộ gõ tiếng Việt";
		}
		else {
			AVIMObj.setMethod(0);
			
			this._el.title = "Tắt bộ gõ tiếng Việt";
		}
		
		this._el.toggleClass("on");
	}
};