var NewMovieLangController = new Class({ 
	initialize: function(options) {
		this.options = options;
		this.notPrefUserLangs = null;
		this.loadingLangs = false;
	},
	execute: function() {
		if (this.loadingLangs) return;
		if (this.notPrefUserLangs == null) {
			this.loadingLangs = true;
			RequestProcessor.getNotPrefUserLangs(function(langList) {
				this.notPrefUserLangs = langList;
				this.prepareControls();
				this.loadingLangs = false;
			}.bind(this));
		} else 
			this.prepareControls();
	},
	prepareControls: function() {
		var select = $(this.options.selectId);
		var panel = $(this.options.panelId);
		var textElem = $(this.options.textElem);
		var textElemTemplate = $(this.options.textElemTemplate);
		if (!select || !panel || !textElem || !textElemTemplate) return;
		var langId = select.getValue();
		if (this.isNotPrefLangId(langId)) {
			panel.style.display = '';
			var lang = this.getNotPrefLang(langId);
			textElem.innerHTML = textElemTemplate.innerHTML.replace('{0}', lang.name);
		} else {
			panel.style.display = 'none';
			textElem.innerHTML = '';
		}
		
	}, 
	isNotPrefLangId: function(langId) {
		for(var i=0; i<this.notPrefUserLangs.length; i++) 
			if (this.notPrefUserLangs[i].id == langId) return true;
		return false;
	},
	getNotPrefLang: function(langId) {
		for(var i=0; i<this.notPrefUserLangs.length; i++) 
			if (this.notPrefUserLangs[i].id == langId) return this.notPrefUserLangs[i];
		return null;
	}
});

var newMovieLangController = new NewMovieLangController({
	'selectId':'newMovieLangIdSel', 
	'panelId':'selectMovieLangCbPanel', 
	'textElem':'selectMovieLangCbText', 
	'textElemTemplate':'selectMovieLangCbTextTemplate'
});
