/**********************************************************************
* /js/dictionary-main.js
* Copyright (C) 2007-2009 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 component/dictionary-selection.js
 * require component/dictionary-search.js
 * require component/service-information.js
 */

var Dictionary = Class.create();

var dictionarySelection = null;
var dictionarySearch = null;
var serviceInformation = null;

Event.observe(window,'load',function(){
	//* サービス情報表示モジュールの作成
	serviceInformation = new ServiceInformation('service-information-form-area');
	
	//* コンポーネントのバーの部分を生成
	Toggler.setToggleEvent('search');
	Toggler.setToggleEvent('dictionary-selection');
	Toggler.setToggleEvent('service-information');
	
	new HelpPanel("search",
		'<span style="font-weight:bold;">How to Search Multiple Dictionaries</span><br /><br />'+
		'First, choose your dictionary from the Dictionary Selection below.<br />' +
		'Once the dictionaries are selected,<br /><br />'+
		'<ul>'+
		'<li style="list-style:decimal inside;">Type in a word you wish to search.</li>'+
		'<li style="list-style:decimal inside;">Select a matching method.</li>'+
		'<li style="list-style:decimal inside;">Select the source language of the typed word.</li>'+
		'<li style="list-style:decimal inside;">Choose target languages. You can choose multiple languages.</li>'+
		'<li style="list-style:decimal inside;">Click on the "Search" button.</li>'+
		'</ul>');
	new HelpPanel("dictionary-selection",
		'<span style="font-weight:bold;">How to Select Dictionaries</span><br /><br />'+
		'Select translators you wish to use.'+
		'<br><span style="color:red;font-weight:bold;">Red</span> indicates that the dictionary is SELECTED.'+
		'<br><span style="color:green;font-weight:bold;">Green</span> indicates that the dictionary is NOT SELECTED.<br/>');
		
	//* 専門対訳辞書の読み込み
	dictionarySelection = new DictionarySelection('dictionaries');
	dictionarySearch = new DictionarySearch('input-text','matching-method','from','to','result');
	
	
	
	$('use-all').checked = false;
	
	DictionarySelection.Event.load(dictionarySelection);
	dictionarySelection.getElement().down('table').observe('click',function(event){
		DictionarySelection.Event.clickButton(event,dictionarySelection);
		Dictionary.Event.clickResourceButton(event);
	});
	dictionarySelection.getElement().down('table').observe('mouseover',function(event){
		DictionarySelection.Event.mouseoverButton(event,dictionarySelection);
	});
	dictionarySelection.getElement().down('table').observe('mouseout',function(event){
		DictionarySelection.Event.mouseoutButton(event,dictionarySelection);
	});
	$('use-all').observe('click',function(event){
		DictionarySelection.Event.clickAllButton(event,dictionarySelection);
		Dictionary.Event.clickResourceAllButton(event);
	});
	
	dictionarySearch.getWordArea().observe('keypress',function(event){
		Dictionary.Event.keypressOnWordArea(event);
	});
	$('search').observe('click',function(event){
		Dictionary.Event.clickSearchButton(event);
	});
	dictionarySearch.getSourceLanguageMenu().getElement().observe('change',function(event){
		DictionarySearch.Event.changeSourceLanguage(event,dictionarySearch);
		Dictionary.Event.changeSourceLanguage(event);
	});
});

Dictionary.Event = {
	clickSearchButton: function(event){
		if(dictionarySearch.isEmptyWordArea()) {
			//* cancel
		} else if (dictionarySearch.unselectedTargetLanguages()) {
			//* cancel
		} else {
			Dictionary.Ajax.search();
		}
	},
	
	keypressOnWordArea: function(event){
		if(event.keyCode == 13) Dictionary.Event.clickSearchButton(event);
	},
	
	changeSourceLanguage: function(event){
		dictionarySearch.changeTargetLanguages(dictionarySelection.getTargetLanguages(dictionarySearch.getSelectedSourceLanguage()));
	},
	
	clickResourceButton: function(event){
		var sourceLanguages = dictionarySelection.getSourceLanguages()
		if(sourceLanguages.size() != 0){
			//*dictionarySearch.getSourceLanguageMenu().resetSelectedLanguage();
			dictionarySearch.changeSourceLanguages(sourceLanguages);
			dictionarySearch.changeTargetLanguages(dictionarySelection.getTargetLanguages(dictionarySearch.getSelectedSourceLanguage()));
		} else {
			dictionarySearch.resetSourceLanguages();
			dictionarySearch.resetTargetLanguages();
		}
	},
	
	clickResourceAllButton: function(event){
		Dictionary.Event.clickResourceButton(event);
	}
}

Dictionary.Ajax = {
	search: function(){
		$('searching').innerHTML = '<img src="img/anime/dictionary.gif" />';
		var callobj = {
			inputtext: dictionarySearch.getWordArea().value.replace(/\r\n/g," ").replace(/\n/ig," "),
			from: dictionarySearch.getSourceLanguageMenu().getSelectedLanguage(),
			toNum: dictionarySearch.getTargetLanguageMenu().getSelectedLanguages().size(),
			idNum: dictionarySelection.getIdOfSelectedResources().size(),
			matching: dictionarySearch.getMatchingMethod().value
		};
		var selectedTargetLanguageArray = dictionarySearch.getTargetLanguageMenu().getSelectedLanguages();
		for(var i=0;i<selectedTargetLanguageArray.size();i++){
			callobj['to'+i] = selectedTargetLanguageArray[i];
		}
		var selectedResourceIdArray = dictionarySelection.getIdOfSelectedResources();
		for(var i=0; i < selectedResourceIdArray.size(); i++){
			callobj['id'+i] = selectedResourceIdArray[i];
		}
		
		var hash = $H(callobj);
		var formText=hash.toQueryString();
		new Ajax.Request(
			'./php/ajax/dictionary/search.php',
			//*'./php/ajax/dictionary/search-new.php',
			{
				method		:'post',
				//* パラメータはinputtext,from,to,dic0,dic1,dic2,dic3,dic4,dic5
				parameters	:formText,
				onSuccess	:function(httpObj){
				
					//* タイムアウトのエラー処理
					if (httpObj.responseText == "" || httpObj.responseText.match(/Maximum execution time of \d+ seconds exceeded in/)) {
						alert("Server Error. 対象を絞りこんで再度検索してください. ");
						return;
					}
					
					dictionarySearch.getResultArea().draw(httpObj.responseText, dictionarySelection);
					//* 横断検索
					for (var i=0; i< httpObj.responseJSON.contents.size();i++) {
						serviceInformation.update(httpObj.responseJSON.contents[i]);
					}
				},
				onFailure	:function(){
					alert('Server Error.');
				},
				onComplete	:function(){
					$('searching').innerHTML = '';
				}
			});
	}
}

