/* Copyright 2009 Palm, Inc.  All rights reserved. */
var EmailDecorator = Class.create({ 
	initialize: function() { 
	}
});

// Highlights all the occurences of the filter strings that are found in input string 
EmailDecorator.decorateFilterResult = function(input, filter) {
	//Mojo.Log.info("input=%s, filter=%s", input, filter);
	if (!input || input.blank())
		return "";
		
	if (!filter || filter.blank())
		return input;
		
	// we need to keep the original input text when formatting it
	var pos = 0;
	var ndx = -1;
	var lcInput = input.toLowerCase();
	var lcFilter = filter.toLowerCase();
	var lcLeadingWordFilter = " " + lcFilter;
	var result = "";
	
	if (lcInput.startsWith(lcFilter)) {
		result += "<span class='list-highlight'>";
		result += input.substring(pos, lcFilter.length);
		result += "</span>";
		pos = lcFilter.length; 
	}
	
	do {
		ndx = lcInput.indexOf(lcLeadingWordFilter, pos);
		if (ndx === -1) 
			result += input.substring(pos, input.length);
		else {
			result += input.substring(pos, ndx + 1);
			result += "<span class='list-highlight'>";
			result += input.substring(ndx + 1, ndx + lcLeadingWordFilter.length);
			result += "</span>";
		}
		pos = ndx + lcLeadingWordFilter.length;
	} while (ndx !== -1)
	
	return result;
};
