/*******************************************************************************

FILE: mud_TextFlow.js
REQUIRES: mud_API.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 1.1
DATE: 10/09/2005

--------------------------------------------------------------------------------

This file is part of MudTextFlow.

	MudTextFlow is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	MudTextFlow 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 General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Foobar; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*******************************************************************************/

// CONSTRUCTOR
function MudTextFlow(cont, conts) {
	this.content = cont.replace("\n", "");
	this.containers = conts;
	
	
	
	
}

MudTextFlow.prototype.update = function() {
	// clear all columns
	this.clear();
	var _containerNum = 0;
	var _content = this.content;
	while (_content) {
		_content = this.flow(this.containers[_containerNum], _content);
		_containerNum++;
		
		if (_containerNum >= this.containers.length) {
			break;
		}
	}
}

MudTextFlow.prototype.clear = function() {
	for (var i = 0; i < this.containers.length; i++) {
		this.containers[i].innerHTML = "";
	}
}

MudTextFlow.prototype.flow = function(container, content) {

	// get height of container
	var containerH = getObjectHeight(container+"-wrapper");

	// insert content into container
	getRawObject(container).innerHTML = content;
	// height of content
	var contentH = getObjectHeight(container);
	
	
	
	if (contentH > containerH) {
		var _textArray = content.split(".");
		var _tmpText = "";
		var _linecount = 0;
		// insert content line per line
		do {
			_tmpText += _textArray[_linecount] + ".";
			getRawObject(container).innerHTML = _tmpText;
			contentH = getObjectHeight(container);
			_linecount++;
			if (_linecount >= _textArray.length) break;
		} while (contentH < containerH);
		
		// if there is overflow text
		if (_linecount < _textArray.length) {
			// take out last line and add word by word
			_tmpText = _tmpText.substring(0, _tmpText.length - _textArray[_linecount-1].length-1);
			var _last = _textArray[_linecount-1] + ".";
			var _lastArray = _last.split(" ");
			var _lastLinecount = 0;
			var _tmpLastText = "";
			do {
				_tmpLastText += _lastArray[_lastLinecount] + " ";
				getRawObject(container).innerHTML = _tmpText + _tmpLastText;
				contentH = getObjectHeight(container);
				_lastLinecount++;
			} while (contentH < containerH);
				
			// take out last word
			_tmpLastText = _tmpLastText.substring(0, _tmpLastText.length - _lastArray[_lastLinecount-1].length-1);
			getRawObject(container).innerHTML = _tmpText + _tmpLastText;
			
			// overflow text
			var overflow = _last.substring(_tmpLastText.length);
			for (var i = _linecount; i < _textArray.length; i++) {
				if (_textArray[i].match(/[\S]./g)) {
					overflow += _textArray[i] + ".";
				}
			}
			
			return overflow;
		}
	}
	return false;
}