/*  snip.js  -  implements the snip function.
 *  (C) Copyright 2003  Kars Meyboom <kars@kde.nl>
 *
 *  LICENSE:
 * 
 *  This code 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.
 * 
 *  This code 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

// cuts as many characters from 'str' as necessary to ensure that
// the resulting string contains at most 'maxlen' characters.
function snip (str, maxlen) {
	var newStr;

	if (str.length <= maxlen) {
		return str;
	}

	newStr = str.replace(/^([^ ]+ [^ ]+ [^ ]+).*( [^ ]+ [^ ]+ [^ ]+$)/, "$1 ... $2");
	if (newStr != str && newStr.length <= maxlen) {
		return newStr;
	}

	newStr = str.replace(/^([^ ]+ [^ ]+ [^ ]+).*( [^ ]+ [^ ]+$)/, "$1 ... $2");
	if (newStr != str && newStr.length <= maxlen) {
		return newStr;
	}

	newStr = str.replace(/^([^ ]+ [^ ]+).*( [^ ]+ [^ ]+ [^ ]+$)/, "$1 ... $2");
	if (newStr != str && newStr.length <= maxlen) {
		return newStr;
	}

	newStr = str.replace(/^([^ ]+ [^ ]+).*( [^ ]+ [^ ]+$)/, "$1 ... $2");
	if (newStr != str && newStr.length <= maxlen) {
		return newStr;
	}

	newStr = str.replace(/^([^ ]+).*( [^ ]+ [^ ]+$)/, "$1 ... $2");
	if (newStr != str && newStr.length <= maxlen) {
		return newStr;
	}

	newStr = str.replace(/^([^ ]+ [^ ]+).*( [^ ]+$)/, "$1 ... $2");
	if (newStr != str && newStr.length <= maxlen) {
		return newStr;
	}

	newStr = str.replace(/^([^ ]+).*( [^ ]+$)/, "$1 ... $2");
	return newStr;

//	if (newStr.length <= maxTitleLength) {
//		return newStr;
//	}
//	newStr = str.substr(0, maxTitleLength/2)
//		 + '...'
//		 + str.substr(length - maxTitleLength/2, length);
//	return newStr;
}

