/**********************************************************************
* Copyright (C) 2007 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
***********************************************************************/
/*
 * This file is the component controller file for morphological analyzer
 * including layout and error message functions. 
 */
var Analyze = Class.create();
var SourceLanguageMenu = Class.create();
var TargetLanguageMenu = Class.create();
var ResultArea = Class.create();
var ResultTranslationContainer = Class.create();

//* new した後にAjax.Requestにより入手したデータをloadTranslationDataで処理する必要がある
//* It is necessary to process the data from Ajax.Request in loadTranslationData after invoking "new"
Analyze.prototype = {
	initialize: function(wordAreaId,sourceLanguageId,targetLanguageId,resultAreaId){
		this.wordAreaId = wordAreaId;
		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();
	},
	
	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);
	}
};

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.getMultilingualLanguageNameByTag(this.menuLanguages[n], 'en');
				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 />'
					+ '<img src="img/common/space.gif" width="2px"/>' + this.menuLanguages[n] + '<img src="img/common/space.gif" width="7px"/>';
				}else{
					this.element.innerHTML = this.element.innerHTML + '<input id="' + 'to'+n + '" name="'+'to'+n+'" type="checkbox" value="'+this.menuLanguages[n]+'" />'
					+ '<img src="img/common/space.gif" width="2px"/>' + this.menuLanguages[n] + '<img src="img/common/space.gif" width="7px"/>';
				}
			}
		} 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 />'
					+ '<img src="img/common/space.gif" width="2px"/>' + this.menuLanguages[n] + '<img src="img/common/space.gif" width="7px"/>';
				}else{
					this.element.innerHTML = this.element.innerHTML + '<input id="' + 'to'+n + '" name="'+'to'+n+'" type="checkbox" value="'+this.menuLanguages[n]+'" />'
					+ '<img src="img/common/space.gif" width="2px"/>' + this.menuLanguages[n] + '<img src="img/common/space.gif" width="7px"/>';
				}
			}
		}
	}
};

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 ResultTranslationContainer(resultObj[i]);
			this.element.appendChild(container.getElement(resourceSelection));
		}
	}
};

ResultTranslationContainer.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.analyze);
		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));vi 
			}
			// 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 true;
		};		
			//* Result output table-layout 
			checker.ok = function(){  
			var slw = document.createElement('dt');
				var header = "<table border=\"0\"><tr><td width=\"130\">Word</td><td width=\"130\">Lemma</td><td width=\"130\">Part Of Speech</td></tr></table>";
				slw.innerHTML = header;
				resultDl.appendChild(slw);
			
			for(var i=0; i < this.response.contents.length; i++){								
				//* If-clause for long words. Mozilla does not recognize long words in tables without word-break.
				//* <wbr /> tag does not work properly in Opera browser, but the normal output layout does.
				if(this.response.contents[i].word.length > 12 ){
					var wordbreakWord = "";
					var wordbreakLemma = "";					
					//* Long words layout
					for (var charCount = 0; charCount < this.response.contents[i].word.length; charCount++) {
						wordbreakWord += this.response.contents[i].word.charAt(charCount) + "<wbr />";
						wordbreakLemma += this.response.contents[i].lemma.charAt(charCount) + "<wbr />";
					}	
						var result = "<table border=\"0\"><tr><td width=\"130\">" + wordbreakWord + "</td><td width=\"130\">" + wordbreakLemma + "</td><td width=\"130\">" + this.response.contents[i].partOfSpeech + "</td></tr></table>";
						var tw = document.createElement('dd');
						tw.innerHTML = result;
						resultDl.appendChild(tw);						
					}
					//* Normal layout
					else{
					var result = "<table border=\"0\"><tr><td width=\"130\">" + this.response.contents[i].word + "</td><td width=\"130\">" + this.response.contents[i].lemma + "</td><td width=\"130\">" + this.response.contents[i].partOfSpeech + "</td></tr></table>";
					var tw = document.createElement('dd');
					tw.innerHTML = result;
					resultDl.appendChild(tw);
				}
			}			
			return true;
		};
		
		if(!checker.check()){
			return;
		}
		var resourceName = document.createElement('div');
		resourceName.className = 'resource-name';
		var atag = document.createElement('a');
		atag.appendChild(document.createTextNode(resourceSelection.getResourceNameFromId(this.resultObj.id)));
		atag.setAttribute('href',resourceSelection.getUrlFromId(this.resultObj.id));
		resourceName.appendChild(atag);
		resultDl.appendChild(resourceName);
		return resultDl;
	}
};

Analyze.Event = {
	changeSourceLanguage: function(event,resourceSearch){
		var language = resourceSearch.getSourceLanguageMenu().getElement().value;
		resourceSearch.getSourceLanguageMenu().setSelectedLanguage(language);
	}
}
