/**********************************************************************
* /js/component/parallel-text-search.js
* Copyright (C) 2007-2008 Kyoto University
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-
* 1301  USA
***********************************************************************/
/**
 * require util/utilities.js
 */

var ParallelTextSearch = Class.create();
var SourceLanguageMenu = Class.create();
var TargetLanguageMenu = Class.create();
var ResultArea = Class.create();
var ResultParallelTextContainer = Class.create();

//* new した後にAjax.Requestにより入手したデータをloadParallelTextDataで処理する必要がある
ParallelTextSearch.prototype = {
	initialize: function(wordAreaId,matchingMethodId,sourceLanguageId,targetLanguageId,resultAreaId){
		this.wordAreaId = wordAreaId;
		this.matchingMethodId = matchingMethodId;
		this.sourceLanguageMenu = new SourceLanguageMenu(sourceLanguageId);
		this.targetLanguageMenu = new TargetLanguageMenu(targetLanguageId);
		this.resultArea = new ResultArea(resultAreaId);
	},
	
	changeSourceLanguages: function(languages){
		this.sourceLanguageMenu.setMenuLanguages(languages);
		this.sourceLanguageMenu.draw();
	},
	
	changeTargetLanguages: function(languages){
		this.targetLanguageMenu.setMenuLanguages(languages);
		this.targetLanguageMenu.draw();
	},
	
	resetSourceLanguages: function(){
		this.sourceLanguageMenu.resetSelectedLanguage();
		this.sourceLanguageMenu.resetMenuLanguage();
		this.sourceLanguageMenu.draw();
	},
	
	resetTargetLanguages: function(){
		this.targetLanguageMenu.resetSelectedLanguage();
		this.targetLanguageMenu.resetMenuLanguage();
		this.targetLanguageMenu.draw();
	},
	
	isEmptyWordArea: function(){
		return $(this.wordAreaId).value == "";
	},
	
	unselectedTargetLanguages: function(){
		return this.targetLanguageMenu.getSelectedLanguages() == null;
	},
	
	getSelectedSourceLanguage: function(){
		return this.sourceLanguageMenu.getSelectedLanguage();
	},
	
	getSourceLanguageMenu: function(){
		return this.sourceLanguageMenu;
	},
	
	getTargetLanguageMenu: function(){
		return this.targetLanguageMenu;
	},
	
	getResultArea: function(){
		return this.resultArea;
	},
	
	getWordArea: function(){
		return $(this.wordAreaId);
	},
	
	getMatchingMethod: function(){
		return $(this.matchingMethodId);
	}
};

SourceLanguageMenu.prototype = {
	initialize: function(id){
		this.id = id;
		this.element = $(this.id);
		this.selectedLanguage = null;
		this.menuLanguages = new Array();
	},
	
	setSelectedLanguage: function(language){
		this.selectedLanguage = language;
	},
	
	getSelectedLanguage: function(){
		return this.selectedLanguage;
	},
	
	getElement: function(){
		return this.element;
	},
	
	setMenuLanguages: function(languages){
		this.menuLanguages = languages;
	},
	
	resetSelectedLanguage: function(){
		this.selectedLanguage = null;
	},
	
	resetMenuLanguage: function(){
		this.menuLanguages = new Array();
	},
	
	draw: function(){
		this.element.innerHTML = '';
		if (this.menuLanguages.size() == 0) {
			var opt = document.createElement('option');
			opt.innerHTML = '&nbsp;';
			this.element.appendChild(opt);
		}
		else {
			var hasPreviousSelectedLanguage = false;
			for (var n = 0; n < this.menuLanguages.size(); n++) {
				var opt = document.createElement('option');
				if (this.menuLanguages[n] == this.selectedLanguage) {
					opt.selected = "true";
					hasPreviousSelectedLanguage = true;
				}
				opt.value = this.menuLanguages[n];
				opt.innerHTML = Language.getNameByTag(this.menuLanguages[n]);
				this.element.appendChild(opt);
			}
			if (!hasPreviousSelectedLanguage) {
				this.element.firstChild.selected = "true";
				this.selectedLanguage = this.element.firstChild.value;
			}
		}
	}
};

TargetLanguageMenu.prototype = {
	initialize: function(id){
		this.id = id;
		this.element = $(this.id);
		this.selectedLanguages = new Array();
		this.previousSelectedLanguages = new Array();
		this.menuLanguages = new Array();
	},
	
	setSelectedLanguage: function(language){
		this.selectedLanguages.push(language);
	},
	
	removeSelectedLanguage: function(language){
		this.selectedLanguages = this.selectedLanguages.without(language);
	},
	
	getSelectedLanguages: function(){
		var languages = new Array();
		$$('#'+this.id+' input').each(function(i){
			if(i.checked) languages.push(i.value);
		});
		return languages;
	},
	
	setMenuLanguages: function(languages){
		this.previousSelectedLanguages = this.getSelectedLanguages();
		this.menuLanguages = languages;
	},
	
	resetSelectedLanguage: function(){
		this.selectedLanguages = new Array();
	},
	
	resetMenuLanguage: function(){
		this.menuLanguages = new Array();
	},
	
	draw: function(){
		this.element.innerHTML = '';
		if(this.previousSelectedLanguages.size() + this.menuLanguages.size() == this.previousSelectedLanguages.concat(this.menuLanguages).uniq().size()){
			for(var n=0;n<this.menuLanguages.size();n++){
				if(n == 0){
					this.element.innerHTML = this.element.innerHTML + '<input id="' + 'to'+n + '" name="'+'to'+n+'" type="checkbox" value="'+this.menuLanguages[n]+'" checked />'
					+ ' ' + Language.getNameByTag(this.menuLanguages[n]) + '  ';
				}else{
					this.element.innerHTML = this.element.innerHTML + '<input id="' + 'to'+n + '" name="'+'to'+n+'" type="checkbox" value="'+this.menuLanguages[n]+'" />'
					+ ' ' + Language.getNameByTag(this.menuLanguages[n]) + '  ';
				}
			}
		} else {
			for(var n=0;n<this.menuLanguages.size();n++){
				if(this.previousSelectedLanguages.indexOf(this.menuLanguages[n]) != -1){
					this.element.innerHTML = this.element.innerHTML + '<input id="' + 'to'+n + '" name="'+'to'+n+'" type="checkbox" value="'+this.menuLanguages[n]+'" checked />'
					+ ' ' + Language.getNameByTag(this.menuLanguages[n]) + '  ';
				}else{
					this.element.innerHTML = this.element.innerHTML + '<input id="' + 'to'+n + '" name="'+'to'+n+'" type="checkbox" value="'+this.menuLanguages[n]+'" />'
					+ ' ' + Language.getNameByTag(this.menuLanguages[n]) + '  ';
				}
			}
		}
	}
};

ResultArea.prototype = {
	initialize: function(id){
		this.id = id;
		this.element = $(this.id);
	},
	
	draw: function(response,resourceSelection){
		var resultObj = eval("("+response+")");
		this.element.innerHTML = '';
		var checker = new StatusProcessor(resultObj);
		checker.warning = function(){
			if (this.isLimitationWarning()) alert(this.message);
			return true;
		}
		checker.error = function(){
			if (this.isLimitationError()) {
				alert(this.message);
				return false;
			} 
			return true;
		}
		if(!checker.check()) return;
		
		resultObj = resultObj['contents'];
		for(var i=0;i<resultObj.length; i++){
			var container = new ResultParallelTextContainer(resultObj[i]);
			this.element.appendChild(container.getElement(resourceSelection));
		}
	}
};

ResultParallelTextContainer.prototype = {
	initialize: function(resultObj,number){
		this.resultObj = resultObj;
		this.number = number;
	},
	
	getElement: function(resourceSelection){
		var resultDl = document.createElement('dl');
		
		var checker = new StatusProcessor(this.resultObj.result);
		var number = this.number;
		checker.error = function(){
			var divElement = document.createElement('div');
			// koyama mod 20090718 start
//			divElement.className = "error-message";
//			divElement.appendChild(document.createTextNode("ERROR : "+this.response.message));
			if (this.response.message.match("is not supported by the service.")
					|| this.response.message.match("is under suspension.")) {
				divElement.className = "info-message";
				divElement.appendChild(document.createTextNode(this.response.message));
			} else {
				divElement.className = "error-message";
				divElement.appendChild(document.createTextNode("ERROR : "+this.response.message));
			}
			// koyama mod 20090718 end
			resultDl.appendChild(divElement);
			return true;
		}
		checker.warning = function(){
			//* var divElement = document.createElement('div');
			//* divElement.className = "warning-message";
			//* divElement.appendChild(document.createTextNode("WARNING : "+this.response.message));
			//* resultDl.appendChild(divElement);
			return this.ok();
		}
		checker.ok = function(){
			for(var i=0; i < this.response.contents.length; i++){
				if (typeof(this.response.contents[i].sourceText) == 'undefined') continue;
				var slw = document.createElement('dt');
				slw.id = "result" + number + "-" + i;
				slw.className = 'dt-normal';
				slw.innerHTML = this.response.contents[i].sourceText;
				resultDl.appendChild(slw);
				
				for(var j=0; j < this.response.contents[i].targetTexts.length; j++){
					var tw = document.createElement('dd');
					tw.id = "result" + number + "-" + i + "-" + j;
					tw.innerHTML = this.response.contents[i].targetTexts[j].targetLanguage + " : " + this.response.contents[i].targetTexts[j].targetText;
					resultDl.appendChild(tw);
				}
			}
			return true;
		}
		
		if(!checker.check()) return;
		
		var resourceName = document.createElement('div');
		resourceName.className = 'resource-name';
		if (resourceSelection.getUrlFromId(this.resultObj.id) != "") {
			var atag = document.createElement('a');
			atag.appendChild(document.createTextNode(resourceSelection.getResourceNameFromId(this.resultObj.id)));
			atag.setAttribute('href', resourceSelection.getUrlFromId(this.resultObj.id));
		} else {
			var atag = document.createElement('span');
			atag.appendChild(document.createTextNode(resourceSelection.getResourceNameFromId(this.resultObj.id)));
		}
		resourceName.appendChild(atag);
		resultDl.appendChild(resourceName);
		return resultDl;
	}
};

ParallelTextSearch.Event = {
	changeSourceLanguage: function(event,resourceSearch){
		var language = resourceSearch.getSourceLanguageMenu().getElement().value;
		resourceSearch.getSourceLanguageMenu().setSelectedLanguage(language);
	}
}

