/**********************************************************************
* /js/composite-translation-services-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
***********************************************************************/
/**
 * component/composite-translation.js
 * component/translation-log-display.js
 * component/user-dictionary.js
 * component/exclusive-dictionary-selection.js
 * component/parallel-text-selection.js
 */
var CompositTranslationServices = Class.create();

var compositeTranslation = null;
var translationLogDisplay = null;
var dictionarySelection = null;
var parallelTextSelection = null;
var serviceInformation = null;

var editDictionary = null;

//*///////////	user-dictionary.js   ///////////////
//*var dataTable = null;
//*var addRecordButton = null;
//*var addLanguageButton = null;
//*var inputWordField = null;
//*var userDictionaryLoadButton = null;
//*var userDictionaryStoreButton = null;
//* userDictHelpButton = null;

Event.observe(window,'load',function(){
	
	Toggler.setToggleEvent('translation-log');
	Toggler.setToggleEvent('translation');
	Toggler.setToggleEvent('edit-dictionary');
	Toggler.setToggleEvent('dictionary-selection');
	Toggler.setToggleEvent('parallel-text-selection');
	Toggler.setToggleEvent('service-information');
	
	serviceInformation = new ServiceInformation('service-information-form-area');
	parallelTextSelection = new ParallelTextSelection('parallel-texts');
	
	new HelpPanel("translation-log",
		'<b>What This Box Does</b><br /><br />'
		+ 'This area displays a log of translation results that you selected and posted.<br><br>'
		+ 'You can change the log\'s font size using the "Font Size" menu located below this HELP message.<br /><br />'
		+ 'Please click on the HELP menu below to read instructions on how to perform multihop translation.');
	new HelpPanel("translation",
		'<b>How to Perform Multihop Translation</b><br /><br />'
		+ 'You can perform multihop translation. Multihop translation allows bilingual translations to be sequentially '
		+ 'executed, for instance, from Japanese-to-English and then Englisht-to-Spanish.<br><br>'
		+ '1. First, select the source language under "From:".<br><br>'
		+ '2. Type in a sentence or sentences you wish to translate.<br><br>'
		+ '3. Select a translator you wish to use.<br><br>'
		+ '4. Choose target languages. You can choose multiple languages.<br><br>'
		+ '5. Click on the "Translate" button.<br><br>'
		+ '6. Select a language for next translation.<br><br>'
		+ '7. Repeat steps 3-6.');
	new HelpPanel("user-dictionary",
		'<b>How to Create User Dictionary</b><br /><br />'
		+ 'This area displays a dictionary that you created.');
	new HelpPanel("dictionary-selection",
		'<span style="font-weight:bold;">How to Select Dictionary</span><br /><br />'+
		'Select a dictionary 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/>');
	new HelpPanel("parallel-text-selection",
		'<span style="font-weight:bold;">How to Select Parallel Texts</span><br /><br />'+
		'Select parallel texts you wish to search.'+
		'<br /><span style="color:orange;font-weight:bold;">Orange</span> indicates that the dictionary is SELECTED.'+
		'<br /><span style="color:blue;font-weight:bold;">Blue</span> indicates that the dictionary is NOT SELECTED.<br />');
		
	compositeTranslation = new CompositeTranslation('translation-form-area','input-text-from','translation-post','translation-post-all', serviceInformation);
	translationLogDisplay = new TranslationLogDisplay('translation-log','logger-font','load-button','store-button');
	dictionarySelection = new ExclusiveDictionarySelection('dictionaries');
	//* LOAD系イベント
	CompositeTranslation.Event.load(compositeTranslation);
	ExclusiveDictionarySelection.Event.load(dictionarySelection);
	ParallelTextSelection.Event.load(parallelTextSelection);
	//* その他イベント
	compositeTranslation.getPostButtonElement().observe('click',function(event){
		CompositTranslationServices.Event.clickPostButton(event,compositeTranslation,translationLogDisplay);
	});
	compositeTranslation.getPostAllButtonElement().observe('click',function(event){
		CompositTranslationServices.Event.clickPostAllButton(event,compositeTranslation,translationLogDisplay);
	});
	dictionarySelection.getElement().down('table').observe('click',function(event){
		ExclusiveDictionarySelection.Event.clickButton(event,dictionarySelection);
		CompositTranslationServices.Event.clickDictionaryButton(event,compositeTranslation,dictionarySelection);
	});
	dictionarySelection.getElement().down('table').observe('mouseover',function(event){
		ExclusiveDictionarySelection.Event.mouseoverButton(event,dictionarySelection);
	});
	dictionarySelection.getElement().down('table').observe('mouseout',function(event){
		ExclusiveDictionarySelection.Event.mouseoutButton(event,dictionarySelection);
	});
	parallelTextSelection.getElement().down('table').observe('click',function(event){
		ParallelTextSelection.Event.clickButton(event,parallelTextSelection);
		CompositTranslationServices.Event.clickParallelTextButton(event,compositeTranslation,parallelTextSelection);
	});
	parallelTextSelection.getElement().down('table').observe('mouseover',function(event){
		ParallelTextSelection.Event.mouseoverButton(event,parallelTextSelection);
	});
	parallelTextSelection.getElement().down('table').observe('mouseout',function(event){
		ParallelTextSelection.Event.mouseoutButton(event,parallelTextSelection);
	});
	$('use-all-parallel-text').observe('click',function(event){
		ParallelTextSelection.Event.clickAllButton(event,parallelTextSelection);
		CompositTranslationServices.Event.clickAllParallelTextButton(event,compositeTranslation,parallelTextSelection);
	});
	
	editDictionary = new EditDictionary();

	
	
	
//*	compositeTranslation.getSourceLanguageMenuElement().observe('change',function(event){
//*		CompositeTranslation.Event.changePrimeSourceLanguage(event,compositeTranslation);
//*	});
//*	
//*	//* user-dictionary.js
//*	//dataTable = new DataTable('user-dictionary-table',3,4);
//*
//*	/*
//*	Event.observe($('user-dictionary-table').down('tbody'), 'click', function(event) {
//*		var tableTds = $$('#user-dictionary-table tbody td');
//*		tableTds.each(function(e){e.removeClassName('selected-row');});
//*		var elementArray = $(Event.element(event)).up('tr').childElements();
//*		elementArray.each(function(e){
//*			e.addClassName('selected-row');
//*			});
//*	});
//*	Event.observe($('user-dictionary-table').down('tbody'), 'dblclick', function(event) {
//*		var tableTds = $$('#user-dictionary-table tbody td');
//*		tableTds.each(function(e){e.removeClassName('selected-row');});
//*		var element = $(Event.element(event));
//*		var tablePosition = element.id.split('-');
//*		var cellPosition = element.cumulativeOffset();
//*		var bodyPosition = $$('body')[0].cumulativeOffset();
//*		inputWordField.show(cellPosition[0]-bodyPosition[0]-5,cellPosition[1]-bodyPosition[1]-5,parseInt(tablePosition[0]),parseInt(tablePosition[1]));
//*	});
//*	Event.observe($('user-dictionary-table').down('tbody'), 'mouseover', function(event) {$(Event.element(event)).addClassName('mover');});
//*	Event.observe($('user-dictionary-table').down('tbody'), 'mouseout', function(event) {$(Event.element(event)).removeClassName('mover');});
//*	Event.observe('load-user-dictionary', 'click', function(event){
//*		userDictionaryLoadButton.showPane(Event.pointerX(event)-200,Event.pointerY(event)-15);
//*	});
//*	
//*	*/
	
});

CompositTranslationServices.Event = {
	clickPostButton: function(event,compositeTranslation,translationLogDisplay){
		var log = {source: compositeTranslation.getChildContainer(0).getHashData()[0].text,
			translationResults: [compositeTranslation.lastContainer().getHashData()]};
		translationLogDisplay.addLog(log);
		translationLogDisplay.draw();
		
		var scrollTop = $('translation-log').getHeight();
		$("translation-log-outer").scrollTop = scrollTop;
	},
	
	clickPostAllButton: function(event,compositeTranslation,translationLogDisplay){
		var hashResults = [];
		for(var i=1;i<compositeTranslation.sizeOfContainers();i++){
			hashResults.push(compositeTranslation.getChildContainer(i).getHashData());
		}
		var log = {source: compositeTranslation.getChildContainer(0).getHashData()[0].text,
			translationResults: hashResults};
		translationLogDisplay.addLog(log);
		translationLogDisplay.draw();
		
		var scrollTop = $('translation-log').getHeight();
		$("translation-log-outer").scrollTop = scrollTop;
	},
	
	clickDictionaryButton: function(event,compositeTranslation,dictionarySelection){
		compositeTranslation.setLargeDictionaryId(dictionarySelection.getIdsOfSelectedResources());
	},
	
	clickParallelTextButton: function(event,compositeTranslation,parallelTextSelection){
		compositeTranslation.getAllAutocompleteContainers().each(function(a){
			a.setQueryResourceIds(parallelTextSelection.getIdOfSelectedResources());
		});
	},
	
	clickAllParallelTextButton: function(event,compositeTranslation,parallelTextSelection){
		CompositTranslationServices.Event.clickParallelTextButton(event,compositeTranslation,parallelTextSelection);
	}
}

CompositTranslationServices.Connector = {
	getIdOfSelectedParallelText: function(){
		return parallelTextSelection.getIdOfSelectedResources();
	}
}

