/**********************************************************************
* /js/kawasaki-parallel-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/kawasaki-parallel-text-selection.js
 * require component/kawasaki-category-selection.js
 * require component/kawasaki-parallel-text-log.js
 */

var KawasakiParallel = Class.create();

var kawasakiParallelTextSelection = null;
var kawasakiCategorySelection = null;
var kawasakiParallelTextLog = null;
var serviceInformation      = null;

var VIEW_LANGUAGE = 'ja';

Event.observe(window,'load',function(){
	
	//* サービス情報表示モジュールの構築
	serviceInformation = new ServiceInformation('service-information-form-area');
	Toggler.setToggleEvent('service-information');//* デフォルトはクローズ
	//* var name = 'service-information';
	//* Toggler.toggleFast(name+'-form-area');
	//* $('img-close-'+name).hide();
	//* $('img-open-'+name).show();
	
	kawasakiParallelTextSelection = new KawasakiParallelTextSelection('parallel-texts',VIEW_LANGUAGE);
	kawasakiCategorySelection = new KawasakiCategorySelection(
				'kawasaki-category','tree-category-viewer','auto-search',
				'contents-description',VIEW_LANGUAGE);
	kawasakiParallelTextLog = new KawasakiParallelTextLog(
				'kawasaki-parallel-log-form-area','erase-all-log-button','log',
				'source-language','target-language','input-text','search-button','post-button',
				'result-area','result-message');
	
	KawasakiParallelTextSelection.Event.load(kawasakiParallelTextSelection);
	KawasakiParallelTextLog.Event.load(kawasakiParallelTextLog);
	
	kawasakiParallelTextSelection.getElement().down('table').observe('click',function(event){
		KawasakiParallelTextSelection.Event.clickButton(event,kawasakiParallelTextSelection);
		KawasakiParallel.Event.clickDictionaryButton(event,kawasakiParallelTextSelection,kawasakiCategorySelection);
	});
	kawasakiParallelTextLog.getSearchButtonElement().observe('click',function(event){
		KawasakiParallel.Event.clickSearchButton(event,kawasakiParallelTextSelection,kawasakiParallelTextLog, serviceInformation);
	});
	kawasakiParallelTextLog.getTextInputElement().observe('focus',function(event){
		KawasakiParallelTextLog.Event.onFocusTextInput(event,kawasakiParallelTextLog);
	});
});

KawasakiParallel.Event = {
	clickDictionaryButton: function(event,kawasakiParallelTextSelection,kawasakiCategorySelection){
		//* dictionaryStatusPair  --> Pair: first -> dictionaryHash:Object, second -> isSelected:boolean
		var dictionaryStatusPair = kawasakiParallelTextSelection.getMostRecentlyClickedDictionary();
		if(dictionaryStatusPair != null){
			kawasakiCategorySelection.setDictionaryNode(dictionaryStatusPair);
		}
	},
	
	//* yui-tasknode.jsから
	//* 仕組みが難しいので大域変数を利用
	clickTreeNode: function(){
		var treeChildren = kawasakiCategorySelection.getChildren();
		kawasakiParallelTextLog.searchParallelTexts(treeChildren, serviceInformation);
	},
	
	clickSearchButton: function(event,kawasakiParallelTextSelection,kawasakiParallelTextLog, serviceInformation){
		var treeChildren = kawasakiCategorySelection.getChildren();
		kawasakiParallelTextLog.searchParallelTexts(treeChildren, serviceInformation);
	}
}

