function LanguageChanger(options) {
	this.options = {
		"width": 500,
		"langs": [
			[ "en", "English", "Bahamas, Canada, Grenada, Guyana, Ireland, Jamaica, Kenya, Malawi, Malta, New Zealand, South Africa, United Kingdom, United States, ..." ],
			//[ "es", "Español",  "Argentina, Bolivia, Chile, Colombia, Costa Rica, Cuba, Dominican Republic, Mexico, Nicaragua, Panama, Puerto Rico, España (Spain), Uruguay, Venezuela,..." ],
			//[ "fr", "Français", "Democratic Republic of the Congo, France, Canada, Madagascar, Niger, Senegal, Mali, Belgium, Guinea, Rwanda,  Haiti, Switzerland, Luxembourg, Monaco, ..."],
			[ "pt", "Português", "Angola, Brasil, Cabo Verde, Timor Leste, Guiné-Bissau, Macau, Moçambique, Portugal, São Tomé e Príncipe" ]
		]
	};
	Object.extend(this.options, options || {});
	
	if ((p = location.pathname.match(/^\/([a-z]{2})/)) !== null) {
		var lang = p[1];
		
		for (var i = 0; i < this.options.langs.length; i++) {
			if (this.options.langs[i][0] == lang) {
				this.current = i;
				this.build(this.options.langs[i][0], this.options.langs[i][1]);
				break;
			}
		}
	}
};
LanguageChanger.prototype.build = function (langId, langName) {
	var lc = this;
	
	$("langs").insert(
		new Element("span").insert(
			new Element("img", { "src": "/res/icons/flags/"+langId+"16.png", "align": "absmiddle" })
		).insert(" " + langName).observe("click", function (ev) {
			lc.showLanguageList();
		})
	);
};
LanguageChanger.prototype.showLanguageList = function () {
	var lc = this;
	
	this.darkdv = new Element("div").setStyle({
		"position": "absolute",
		"left": 0,
		"top": 0,
		"right": 0,
		"bottom": 0,
		"background": "#000000"
	}).observe("click", function (ev) {
		lc.kill();
	}).hide();
	this.langdv = new Element("div", { "class": "languagechanger" }).setStyle({
		"position": "absolute",
		"left": Math.round((document.viewport.getWidth() - this.options.width) / 2) + "px",
		"top": "100px",
		"width": this.options.width + "px"
	}).hide();
	
	var dv = new Element("div", { "class": "innerbox" });
	var dv2 = new Element("div");
	var dv3 = new Element("div").setStyle({ "clear": "both", "textAlign": "right" }).insert(
		new Element("img", { "src": "/res/icons/lang_go.png" }).setStyle({ "cursor": "pointer" }).observe("click", function () {
			lc.goToCurrent();
		})
	);
	
	this.currentLanguageFlag = new Element("img", { "src": "/res/icons/flags/"+this.options.langs[this.current][0]+"48.png", "align": "left" });
	this.currentLanguageName = new Element("strong").update(this.options.langs[this.current][1]);
	this.currentLanguageDescription = new Element("p", { "align": "justify" }).update(this.options.langs[this.current][2]);
	
	dv.insert(
		new Element("div", { "class": "currentlanguage" }).insert(
			this.currentLanguageFlag
		).insert(
			this.currentLanguageName
		).insert(new Element("br")).insert(
			this.currentLanguageDescription
		)
	);
	
	for (var i = 0; i < this.options.langs.length; i++) {
		dv2.insert(
			new Element("div", { "class": "language" }).insert(
				new Element("img", { "src": "/res/icons/flags/"+this.options.langs[i][0]+"16.png", "align": "left" })
			).insert(
				new Element("span").update(this.options.langs[i][1])
			).observe("mouseover", this.showLanguageDetails.curry(i).bind(this)).observe("click", this.goToLanguage.curry(this.options.langs[i][0]).bind(this))
		);
	}
	
	this.langdv.insert(dv.insert(dv2).insert(dv3));
	
	$("pagebody").insert(this.darkdv);
	$("pagebody").insert(this.langdv);
	
	this.darkdv.appear({
		"duration": 0.5,
		"to": 0.8,
		"afterFinish": function () {
			lc.langdv.appear({ "duration": 0.5 });
		}
	});
};
LanguageChanger.prototype.showLanguageDetails = function (index) {
	this.currentLanguage = this.options.langs[index][0];
	this.currentLanguageFlag.writeAttribute({ "src": "/res/icons/flags/"+this.options.langs[index][0]+"48.png" })
	this.currentLanguageName.update(this.options.langs[index][1]);
	this.currentLanguageDescription.update(this.options.langs[index][2]);
};
LanguageChanger.prototype.goToCurrent = function () {
	this.goToLanguage(this.currentLanguage);
};
LanguageChanger.prototype.goToLanguage = function (langName) {
	location.href = "/" + langName + location.pathname.substr(3);
};
LanguageChanger.prototype.kill = function () {
	this.darkdv.remove();
	this.langdv.remove();
};

Event.observe(window, "load", function () {
	new LanguageChanger();
});
