/*	public static final int kFrom = 0;
	public static final int kTo = 1;
	public static final int kCc = 2;
	public static final int kBcc = 3;
	public static final int kReplyTo = 4;

Copyright 2009 Palm, Inc.  All rights reserved.

*/

		
var EmailRecipient = Class.create({
	initialize: function(){
	}
});


EmailRecipient.roleFrom = 0;
EmailRecipient.roleTo = 1;
EmailRecipient.roleCc = 2;
EmailRecipient.roleBcc = 3;
EmailRecipient.roleReplyTo = 4;

EmailRecipient.separateRecipients =  function(recipList){
	 	var from = [];
	 	var cc = [];
	 	var bcc = [];
	 	var to = [];
	 	var list = [];
	 	$A(recipList).each(function(r){
	 		switch (r.role) {
	 			case EmailRecipient.roleFrom:{
					if (from.length === 0) {
		 				from[0] = r;
					}
					break;
	 			}
	 			case EmailRecipient.roleReplyTo:{
	 				from[0] = r;
					break;
	 			}
	 			case EmailRecipient.roleTo:{
	 				to.push(r);
					break;
	 			}
	 			case EmailRecipient.roleCc:{
	 				cc.push(r);
					break;
	 			}
	 			case EmailRecipient.roleBcc:{
	 				bcc.push(r);
					break;
	 			}
	 		}
	 	});
	 	list[EmailRecipient.roleFrom] = from;
	 	list[EmailRecipient.roleTo] = to;
	 	list[EmailRecipient.roleCc] = cc;
	 	list[EmailRecipient.roleBcc] = bcc;
	 	return list;
	 };

/**
 * The contacts picker widget uses different properties
 * it wants 'value', we have 'address'
 * it wants 'contactDisplay', we have 'displayName'
 * it wants 'personId', we have 'id'
 */
EmailRecipient.addAddressPickerFields = function(recipList){
		// Add the properties that address picker uses
		recipList.each(function(r) {
			r.type = 'email';
			r.value = r.address;
			//r.personId = r.id;
			if (r.displayName) {
				r.displayName = r.displayName.escapeHTML();
			} else {
				r.displayName = r.address;
			}
			r.contactDisplay = r.displayName;
		});
	};


EmailRecipient.getDetails = function(controller, address, callback){
		if (!address) {
			Mojo.Log.error("EmailRecipient.getDetails called with blank address");
		} else {
			Mojo.Log.info("EmailRecipient.getDetails address=", address);
			controller.serviceRequest('palm://com.palm.contacts', {
					method: 'reverseLookup',
					parameters: {'value':address, 'type':"email"},
					onSuccess: callback
				});
		}
	};
	
EmailRecipient.batchGetDetails = function(controller, recipientAddresses, callback){
		if (!recipientAddresses || recipientAddresses.length <= 0) {
			Mojo.Log.error("EmailRecipient.getDetails called with blank recipients list");
		} else {
			controller.serviceRequest('palm://com.palm.contacts', {
					method: 'reverseLookupEmails',
					parameters: {"emails": recipientAddresses},
					onSuccess: callback
				});
		}
	};

/*
 * This used to launch the contacts app. Now it pushes a contacts pseudo card
 * for viewing the contact
 */
EmailRecipient.launchContactDetails = function(controller, contactId) {
	Pseudocard.pushPseudocard(controller.stageController, { personId:contactId });
};

/*
 * This used to launch the contacts app. Now it pushes a contacts pseudo card
 * for creating a new contact
 */
EmailRecipient.addToContacts = function(controller, addr, name) {
	var c = {
		emailAddresses: [{ value: addr, label:'2' }], // '2' represents 'Other' type
		fullName: name
	};
	Pseudocard.pushPseudocard(controller.stageController,{ contact:c });
};
