/**********************************************************************
* /js/component/fujimi-chat-log.js
* Copyright (C) 2007-2008 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 util/utilities.js
 */

var FujimiChatLog = Class.create();
var LogArea = Class.create();

FujimiChatLog.prototype = {
	initialize: function(id,fontSizeMenuId,deleteButtonId,leftLanguageId,rightLanguageId){
		this.element = $(id);
		this.fontSizeMenuElement = $(fontSizeMenuId);
		this.deleteButtonElement = $(deleteButtonId);
		this.logContainerPair = new Pair(new LogArea(leftLanguageId,'left'),new LogArea(rightLanguageId,'right'));
	},
	
	getFontMenuElement: function(){
		return this.fontSizeMenuElement;
	},
	
	getDeleteButtonElement: function(){
		return this.deleteButtonElement;
	},
	
	getLeftLogArea: function(){
		return this.logContainerPair.car();
	},
	
	getRightLogArea: function(){
		return this.logContainerPair.cdr();
	},
	
	getLogArea: function(position){
		if(position == 'left'){
			return this.getLeftLogArea();
		} else if(position == 'right'){
			return this.getRightLogArea();
		}
		return null;
	},
	
	getFont: function(){
		var size = this.fontSizeMenuElement.value;
		if(size == 'small') return 13;
		if(size == 'medium') return 16;
		if(size == 'large') return 20;
		return 16;
	},
	
	pushLog: function(color,textPairs){
		this.getLeftLogArea().pushLog(new Pair(color,textPairs));
		this.getRightLogArea().pushLog(new Pair(color,textPairs));
	},
	
	draw: function(){
		var html = '';
		for(var i=0;i<this.getLeftLogArea().getLogSize();i++){
			html = html +'<tr>';
			html = html + this.getLeftLogArea().getLogHTML(i,this.getFont());
			html = html + this.getRightLogArea().getLogHTML(i,this.getFont());
			html = html + '</tr>';
		}
		this.element.childElements().each(function(e){
			e.remove();
		});
		this.element.insert(html);
	},
	
	deleteLog: function(){
		this.getLeftLogArea().resetLogTexts();
		this.getRightLogArea().resetLogTexts();
		this.draw();
	}
}

LogArea.prototype = {
	initialize: function(languageAreaId,position){
		this.element = $(languageAreaId);
		this.logTexts = new Array();
		this.language = null;
		this.position = position;
	},
	
	setLanguage: function(languageTag){
		this.language = languageTag;
		var img = document.createElement('img');
		img.src = Language.getFlagSrcByTag(languageTag);
		var spacer = document.createElement('img');
		spacer.src = "img/common/space.gif";
		spacer.setAttribute('width', '5px');
		var text = document.createTextNode(Language.getNameByTag(languageTag));
		this.element.innerHTML = '';
		this.element.appendChild(img);
		this.element.appendChild(spacer);
		this.element.appendChild(text);
	},
	
	getLanguage: function(){
		return this.language;
	},
	
	pushLog: function(pair){
		this.logTexts.push(pair);
	},
	
	getLogSize: function(){
		return this.logTexts.size();
	},
	
	getLogElements: function(){
		var logElements = new Array();
		for(var i=0;i<this.logTexts.size();i++){
			var element = document.createElement('td');
			element.className = 'log-area-'+this.position;
			element.setStyle({color:this.logTexts[i].car()});
			var textPairs = this.logTexts[i].cdr();
			var text = null;
			for(var j=0;j<textPairs.size();j++){
				if(this.getLanguage() == textPairs[j].car()){
					element.appendChild(document.createTextNode(textPairs[j].cdr()));
				}
			}
			logElements.push(element);
		}
		return logElements;
	},
	
	getLogHTML: function(number,fontSize){
		var html = '<td class="log-area-'+this.position+'" style="';
		html += 'color:'+this.logTexts[number].car()+';'
		html += 'font-size:'+fontSize+'pt;'
		html += '">'
		var textPairs = this.logTexts[number].cdr();
		for(var i=0;i<textPairs.size();i++){
			if(this.getLanguage() == textPairs[i].car()){
				html+=textPairs[i].cdr();
				break;
			}
		}
		html += '</td>';
		return html;
	},
	
	resetLogTexts: function(){
		this.logTexts = new Array();
	}
}

FujimiChatLog.Event = {
	clickDeleteButton: function(event,fujimiChatLog){
		fujimiChatLog.deleteLog();
	},
	
	changeFont: function(event,fujimiChatLog){
		fujimiChatLog.draw();
	}
}

