/**********************************************************************
* /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/concept-dictionary-selection.js
 * require component/concept-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(){
	
	
	//* コンポーネントのバーの部分を生成
	Toggler.setToggleEvent('search');
	Toggler.setToggleEvent('dictionary-selection');
	Toggler.setToggleEvent('service-information');//* デフォルトはクローズ
	//* var name = 'service-information';
	//* Toggler.toggleFast(name+'-form-area');
	//* $('img-close-'+name).hide();
	//* $('img-open-'+name).show();
	
	//* サービス情報表示モジュールの作成
	serviceInformation = new ServiceInformation('service-information-form-area');
	
	new HelpPanel("search",
		'<span style="font-weight:bold;">How to Search in Concept 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 want 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;">Click on the "Search" button.</li>'+
		'</ul>');
	new HelpPanel("dictionary-selection",
		'<span style="font-weight:bold;">How to Select Dictionaries</span><br /><br />'+
		'Select Dictionaries you want 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 {
			Dictionary.Ajax.search();
		}
	},
	
	keypressOnWordArea: function(event){
		if(event.keyCode == 13) Dictionary.Event.clickSearchButton(event);
	},
	
	changeSourceLanguage: function(event){
	},
	
	clickResourceButton: function(event){
		var languages = dictionarySelection.getLanguages()
		if(languages.size() != 0){
			dictionarySearch.changeLanguages(languages);
		} else {
			dictionarySearch.resetLanguages();
		}
	},

	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(),
			idNum: dictionarySelection.getIdOfSelectedResources().size(),
			matching: dictionarySearch.getMatchingMethod().value
		};
		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/concept-dictionary/searchRelated.php',
			//*'./php/ajax/concept-dictionary/search.php',
			{
				method		:'post',
				parameters	:formText,
				onSuccess	:function(httpObj){
					dictionarySearch.getResultArea().draw(httpObj.responseText, dictionarySelection);
					/*serviceInformation.callNodes = httpObj.responseJSON.contents;
					//* 仮想複合サービス
					serviceInformation.serviceName = 'Cross Multilingual Concept Dictionary';
					serviceInformation.copyright   = 'Kyoto University';
					serviceInformation.render();*/
					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 = '';
				}
			});
	}
}


