var Cathmhaol = window.Cathmhaol || {};

/**
* Creates an email object to interface with installed email client using "mailto".
*
* @author	Robert King (hrobertking@cathmhaol.com)
*
* @example	var oEmail = new Cathmhaol.Email(); oEmail.addRecipient("roking@paypal.com"); oEmail.addRecipient("hrobertking@yahoo.com"); oEmail.body = "This is a test"; oEmail.subject = "Test"; oEmail.send();
*/
Cathmhaol.Email = function() {
	/**
	* @method	Adds a recipient email address.
	* @returns	{void}
	* @param	{string} addr
	*/
	this.addRecipient = function(addr) {
		if ((/\w{2,}@\w{2,}\.\w{2,}/).test(addr)) {
			_addresses.push(addr);
		}
	};

	/**
	* @property	Message.
	* @type	{string}
	*/
	this.body = "";

	/**
	* @method	Returns recipients as a semi-colon separated string.
	* @returns	{string}
	*/
	this.getRecipients = function() {
		if (_addresses.length > 0) {
			return _addresses.join(";");
		} else {
			return "";
		}
	};

	/**
	* @property	One of Cathmhaol.Email.States
	* @type	{Cathmhaol.Email.States}
	* @default	OK
	*/
	this.state = {code: 0, message:""};

	/**
	* @property	Subject.
	* @type	{string}
	*/
	this.subject = "";

	/**
	* @method	Opens the email
	* @returns	{void}
	*/
	this.send = function() {
		var uri = "mailto:" + me.getRecipients() + "?" + (me.subject ? "subject=" + encodeURIComponent(me.subject) + "&" : "") + "body=" + encodeURIComponent(me.body);
		if (uri.length > 2000) {
			me.error = me.States.MESSAGE_TOO_LONG;
		} else {
			var win = window.open(uri, "email");
			win.close();
		}
		return (me.error.code == me.States.OK.code);
	};

	/**
	* @private
	* @property	Email addresses
	* @type	{string[]}
	*/
	var _addresses = new Array();
};
/**
* Enumerated states for messages.
*
* @author	Robert King
* @version	1.0
*/
Cathmhaol.Email.States: {
	OK: {code: 0, message: ""},
	MESSAGE_TOO_LONG: {code: 1, message: "URI exceeds maximum allowed length" }
};

