/*
 * D2JS
 * Copyright (C) SatisKia. All rights reserved.
 */
function _StringUtil()
{
	this.h = "";
	this.e = "";
}
_StringUtil.prototype = {
	setHeadWrap : function( str )
	{
		this.h = str;
	},
	setEndWrap : function( str )
	{
		this.e = str;
	},
	truncate : function( str, width, truncation )
	{
		if( _stringWidth( str ) <= width )
		{
			return str;
		}
		width -= _stringWidth( truncation );
		var ret = "";
		for( var i = 0; i < str.length; i++ )
		{
			ret += str.charAt( i );
			if( _stringWidth( ret ) > width )
			{
				if( ret.length > 1 )
				{
					ret = ret.substring( 0, ret.length - 1 );
					break;
				}
			}
		}
		return ret + truncation;
	},
	wrap : function( str, width )
	{
		var ret = new Array();
		var chr;
		var j = 0;
		ret[j] = "";
		for( var i = 0; i < str.length; i++ )
		{
			ret[j] += str.charAt( i );
			if( _stringWidth( ret[j] ) > width )
			{
				if( ret[j].length > 1 )
				{
					ret[j] = ret[j].substring( 0, ret[j].length - 1 );
					i--;
					if( this.h.length > 0 )
					{
						while( true )
						{
							if( i + 1 < str.length )
							{
								chr = str.charAt( i + 1 );
								if( this.h.indexOf( chr ) >= 0 )
								{
									ret[j] += chr;
									i++;
								}
								else
								{
									break;
								}
							}
							else
							{
								break;
							}
						}
					}
					if( this.e.length > 0 )
					{
						while( true )
						{
							if( ret[j].length > 1 )
							{
								chr = ret[j].charAt( ret[j].length - 1 );
								if( this.e.indexOf( chr ) >= 0 )
								{
									ret[j] = ret[j].substring( 0, ret[j].length - 1 );
									i--;
								}
								else
								{
									break;
								}
							}
							else
							{
								break;
							}
						}
					}
				}
				j++;
				ret[j] = "";
			}
		}
		return ret;
	}
};
