/* Copyright 2009 Palm, Inc.  All rights reserved. */

var ContactDecorator = Class.create({
	
	decorateListItem: function(c, filter, sortOrder) {
		
		c.dividerText = c.sortKey.slice(0, 1);
		if (c.dividerText == '\uFAD7') {
			c.dividerText = '#';
		}
		
		var accentIdx = ContactDecorator.ACCENTS[0].indexOf(c.dividerText);
		if (accentIdx != -1) {
			c.dividerText = ContactDecorator.ACCENTS[1].charAt(accentIdx);
		}
		
		var display = (c.displayText) ? c.displayText : "";
		if (sortOrder == PrefsConstants.lastFirst || sortOrder == PrefsConstants.firstLast) {
			
			if (display.blank()) {
				display = Contact.getFullName(c);
				if (display.blank()) {
					display = c.companyName || "";
				}
			}
			
		} else if (sortOrder == PrefsConstants.companyLastFirst || sortOrder == PrefsConstants.companyFirstLast) {
			if (display.blank()) {
				display = Contact.getFullName(c);
				
				if (display.blank()) {
					display = c.companyName || "";
				}
			}
			
			c.dividerText = c.companyName || $L("None");
		} else {
			c.dividerText = "";
			Mojo.Log.warn("!!!UNKNOWN SORT ORDER: " + sortOrder);
		}
		//TODO Lets do this in CSS
		c.dividerText = c.dividerText.toUpperCase();
		
		// FIXME: perf - should cache filter match patterns btw invocations
		if (!filter || filter.length === 0) {
			c.formattedText = display;
		} else {
			c.formattedText = Mojo.PatternMatching.addContactMatchFormatting(display, filter);
		}
		c.formattedText = c.formattedText.strip() || $L("[No Name Available]");
		
	}
});

// for de-accenting
ContactDecorator.ACCENTS = ["ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ", "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy"];
