function WebWordLinker2(identifier)
{
	this.languageCode = null;
	this.items = null;
	this.lockLanguageSelection = false;
	this.value = null; // id of the chosen word
	this.onCommit = null; // function which is launched when the user commits his choice
	this.mode = null;
	this.expressionKindCode = null;
	this.popupTitle = null;

	var id = identifier;
	var comboLanguage = $(id + "_language");
	var textExpression = $(id + "_expression");
	var divResults = $(id + "_results");
	var comboPartOfSpeech = $(id + "_partOfSpeech");
	var divSave = $(id + "_save");

	function specialCharacterButtonOnClick()
	{
		textExpression.value += this.value;
		textExpression.focus();
	}

	function countPartsOfTheSameKind(partsOfSpeech, kind)
	{
		if(kind == null) return 0;
		if("" == kind) return 0;
		var totalCount = 0;
		for(var i=0; i<partsOfSpeech.length; i++) {
			if(partsOfSpeech[i].expressionKindCode == null) continue;
			if(partsOfSpeech[i].expressionKindCode == kind) totalCount++;
		}
		return totalCount;
	}

	function countLanguageExpressionTypes(lng)
	{
		var count = 0;
		for(var i=0; i<webWordLinker2_partsOfSpeech.length; i++) {
			var partOfSpeech = webWordLinker2_partsOfSpeech[i];
			if(partOfSpeech.lng == lng) count++;
		}
		return count;
	}

	function buildPartOfSpeechOptions(topPOS, otherPOS)
	{
		if(topPOS.length > 0) {
			if(topPOS.length > 1)
				_dynahtmlAddOptionToSelect(comboPartOfSpeech, webWordLinker2_labels[6], "");
			for(var i=0; i<topPOS.length; i++) {
				var partOfSpeech = topPOS[i];
				if(countPartsOfTheSameKind(topPOS, partOfSpeech.expressionKindCode) == 1)
					_dynahtmlAddOptionToSelect(comboPartOfSpeech, partOfSpeech.comboText, partOfSpeech.expressionKindCode);
				else
					_dynahtmlAddOptionToSelect(comboPartOfSpeech, partOfSpeech.comboText, partOfSpeech.id);
			}
			var optgroup = document.createElement("optgroup");
			optgroup.label = "----------";
			comboPartOfSpeech.appendChild(optgroup);
		} else {
			_dynahtmlAddOptionToSelect(comboPartOfSpeech, webWordLinker2_labels[6], "");
		}
		for(var j=0; j<otherPOS.length; j++) {
			var partOfSpeech2 = otherPOS[j];
			if(countPartsOfTheSameKind(otherPOS, partOfSpeech2.expressionKindCode) == 1)
				_dynahtmlAddOptionToSelect(comboPartOfSpeech, partOfSpeech2.comboText, partOfSpeech2.expressionKindCode);
			else
				_dynahtmlAddOptionToSelect(comboPartOfSpeech, partOfSpeech2.comboText, partOfSpeech2.id);
		}
	}

	// called on show to clean the element look
	this.clean = function()
	{
		this.value = null;
		divResults.style.display = 'none';
		divResults.innerHTML = "";
		divSave.style.display = 'none';
		$(id + "_mustChoose").style.display = 'none';
		textExpression.value="";
		if(this.languageCode != null) {
			$(id + '_languageLabelCell').style.display = 'none';
			$(id + '_languageComboCell').style.display = 'none';
			textExpression.style.width = "270px";
			comboLanguage.disabled = this.lockLanguageSelection;
			comboLanguage.selectedIndex = _dynahtmlFindOptionIndex(this.languageCode, comboLanguage.options);
		} else {
			$(id+"_language").disabled = false;
			$(id + '_languageLabelCell').style.display = '';
			$(id + '_languageComboCell').style.display = '';
			textExpression.style.width = "160px";
			comboLanguage.selectedIndex = 0;
		}
		this.changeLanguage(); // this function comes from WebWordLinker control
	}

	// called when the user selects a word and presses "save"
	this.save = function()
	{
		var i=0;
		do {
			var option = $(id + "_radio_" + i);
			if(option == null) {
				// we are out of the range and nothing was selected
				alert(webWordLinker2_labels[4]);
				return;
			} else {
				// this is the selected word
				if(option.checked) {
					this.value = option.value;
					this.commit();
					return;
				}
			}
			i++;
		} while(true);
	}

	// called when the exprid is known and we commit users choice
	this.commit = function()
	{
		if(this.value==null)return;
		if(this.onCommit != null && typeof(this.onCommit) == "function")this.onCommit();
		_popupHide(this.id);
	}

	// called when the user changes the language in the select-list
	this.changeLanguage = function()
	{
		var lng = _getSelectValue(id + "_language");
		_dynahtmlRemoveSelectOptions(id + "_partOfSpeech");
		if(lng == null || "" == lng) {
			comboPartOfSpeech.disabled=true;
			//$(id + "_option0").innerHTML = webWordLinker2_labels[5];
			$(id + "_keypad1").style.display = 'none';
			return;
		}
		comboPartOfSpeech.disabled=false;
		var topPOS = [];
		var otherPOS = [];
		for(var i=0; i<webWordLinker2_partsOfSpeech.length; i++) {
			var partOfSpeech = webWordLinker2_partsOfSpeech[i];
			if(partOfSpeech.lng != lng)continue;
			if(this.expressionKindCode != null) {
				if(partOfSpeech.expressionKindCode == this.expressionKindCode)
					topPOS[topPOS.length] = partOfSpeech;
				else
					otherPOS[otherPOS.length] = partOfSpeech;
			} else
				otherPOS[otherPOS.length] = partOfSpeech;
		}
		if(topPOS.length == 0 && otherPOS.length == 0) {
			$(id + "_posComboCell").style.display = "none";
			$(id + "_posLabelCell").style.display = "none";
		} else {
			$(id + "_posComboCell").style.display = "";
			$(id + "_posLabelCell").style.display = "";
			buildPartOfSpeechOptions(topPOS, otherPOS);
		}
		//$(id + "_option0").innerHTML = webWordLinker2_labels[6];
		_charactersReplaceKeypadDiv(id + "_keypad1", id + "_keypad2",lng, specialCharacterButtonOnClick);
	}

	this.show = function()
	{
		this.clean();
		if(this.popupTitle != null)
			$(id + '_title').innerHTML = this.popupTitle;
		_popupShow(id);
		textExpression.focus();
	}

	this.search = function()
	{
		var expr = textExpression.value;
		var lng = (this.languageCode != null ? this.languageCode : _getSelectValue(id + "_language"));
		var exprType = _getSelectValue(id + "_partOfSpeech");

		if(expr == null || expr == "") {
			alert(webWordLinker2_labels[1]);
			textExpression.focus();
			return;
		}
		if(lng == null || lng == "") {
			alert(webWordLinker2_labels[2]);
			return;
		}
		if((exprType == null || exprType == "") && countLanguageExpressionTypes(lng) > 0) {
			alert(webWordLinker2_labels[3]);
			return;
		}
		if(exprType == null)
			exprType = "";
		_ajaxLoadListItems('ajax.jsp?method=linkword&ln='+gInterfaceLanguageCode+'&expr='+encodeURIComponent(expr)+'&language='+lng+'&exprtype='+exprType, this);
	}

	this.refreshList = function()
	{
		if(this.items == null) {
			// there must have been some error in ajax call
			return;
		}
		if(this.items.length == 0) {
			// there must have been some error in ajax call
			//divNothingFound.innerHTML = webWordLinker2_labels[7].replace("$search$", textExpression.value); // REMOVE
			return;
		}

		if(this.items.length == 1) {
			// just 1 word created - save it!
			this.value = this.items[0].value;
			this.commit();
			return;
		}

		// we need to ask the user which word to save
		divResults.innerHTML = '';
		for(var i=0; i<this.items.length; i++) {
			var obj = this.items[i];
			var div = document.createElement("div");
			div.className = (i%2 == 1) ? "rot1" : "rot2";
			div.radioId = id + "_radio_" + i;
			div.onclick = function(){$(this.radioId).checked = true;};
			div.style.cursor = "pointer";
			var input = document.createElement("input");
			input.type = "radio";
			input.id = div.radioId;
			input.name = "dynamic_radio";
			input.value = obj.value;
			div.appendChild(input);
			var span = document.createElement("span");
			span.innerHTML = obj.text;
			span.innerHTML += " <a target='_blank' href='search.jsp?ln="+ gInterfaceLanguageCode +"&exprid="+ obj.value +"' style='font-style:italic' title='"+ webWordLinker2_labels[9] +"'>"+ webWordLinker2_labels[8] +"</a>";
			div.appendChild(span);
			divResults.appendChild(div);
		}
		$(id + "_mustChoose").innerHTML = webWordLinker2_labels[4];
		$(id + "_mustChoose").style.display = '';
		divResults.style.display = '';
		$(id + "_radio_0").checked = true;
		divSave.style.display = '';
	}
}

