//-----------------------------------------------
// Summariser class
//-----------------------------------------------
// TO DO - Automatic closure of open HTML tags
// TO DO - Remove HTML entities e.g. &nbsp
// TO DO - If no summary needed (i.e. short
// enough), don't add continue dots...
// TO DO - If adding continue dots, ensure
// that weird symbols before it are removed.
//-----------------------------------------------

//-----------------------------------------------
// Class definition
//-----------------------------------------------
function Summariser() {
	this.bAddDots = true;
	this.numChars = 250; // Default summary length in characters
	this.classTag = 'div.summary'; // Default summary class tag

	// Methods
	this.stripTags = stripTags;
	this.summarise = summarise;
	this.summariseByClassTag = summariseByClassTag;
	this.summariseById = summariseById;

}

//-----------------------------------------------
// Strip all html tags from supplied string
//-----------------------------------------------
function stripTags(htmlString) {
	return htmlString.replace(/(<([^>]+)>)/ig, '');

}

//-----------------------------------------------
// Shorten supplied html code.
//-----------------------------------------------
function summarise(htmlString) {
	var textString = this.stripTags(htmlString); // Strip html tags
	var spaceIndex = textString.indexOf(' ', this.numChars); // First space index after summary length

	if(spaceIndex != -1) {
		// There are still spaces after the required number of characters.
		// Move index to ensure last word is not chopped then add continue dots...
		textString = textString.substr(0, spaceIndex);
		if(this.bAddDots) textString = textString + '...';

	}
	return textString;

}

//-----------------------------------------------
// Summarise all elements with specified class
//-----------------------------------------------
// *** REQUIRES jQuery -- (class search) ***
function summariseByClassTag() {
	var summaryElements = $(this.classTag); // Array of elements with specified class
	var numTease = summaryElements.length;

	for(var z = 0; z < numTease; z++) {
		summaryElements.eq(z).html(this.summarise(summaryElements.eq(z).html()));

	}

}

//----------------------------------------------
// Summarise element with specified id
//----------------------------------------------
function summariseById(sId) {
	var e = document.getElementById(sId);
	if(e) e.innerHTML = this.summarise(e.innerHTML);

}

/*
function getLeadingHtml(input, maxChars) {
	// token matches a word, tag, or special character
	var	token = /\w+|[^\w<]|<(\/)?(\w+)[^>]*(\/)?>|</g,
		selfClosingTag = /^(?:[hb]r|img)$/i,
		output = '',
		charCount = 0,
		openTags = [],
		match;

	// Set the default for the max number of characters
	// (only counts characters outside of HTML tags)
	maxChars = maxChars || 250;

	while ((charCount < maxChars) && (match = token.exec(input))) {
		
		// If this is an HTML tag
		if (match[2]) {
			output += match[0];
			// If this is not a self-closing tag
			if (!(match[3] || selfClosingTag.test(match[2]))) {
				// If this is a closing tag
				if (match[1]) openTags.pop();
				else openTags.push(match[2]);

			}

		}
		else {
			charCount += match[0].length;
			if (charCount <= maxChars) output += match[0];

		}

	}

	// Close any tags which were left open
	var i = openTags.length;
	while (i--) output += "</" + openTags[i] + ">";

	return output;

};

*/