var Cathmhaol = window.Cathmhaol || {};

/**
* @library     Cathmhaol.Email
* @description Creates an email object to interface with installed email client using "mailto".
* @author      Robert King
*
* @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() {
	this._initialize();
};
Cathmhaol.Email.prototype = {
	/**
	* @method	Adds a recipient
	* @returns	{void}
	* @param	{string} addr	Email address
	*/
	addRecipient: function(addr) {
		if ((/\w{2,}@\w{2,}\.\w{2,}/).test(addr)) {
			this._addresses.push(addr);
		}
	},

	/**
	* @property	Message
	* @type	{string}
	*/
	body: "",

	/**
	* @method	Returns recipients as a semi-colon separated string
	* @returns	{string}
	*/
	getRecipients: function() {
		if (this._addresses.length > 0) {
			return this._addresses.join(";");
		} else {
			return "";
		}
	},

	/**
	* @property	State
	* @type	{States}
	* @default	OK
	*/
	state: {code: 0, message:""},

	/**
	* @property	Subject
	* @type	{string}
	*/
	subject: "",

	/**
	* @method	Opens the email
	* @returns	{void}
	*/
	send: function() {
		var uri = "mailto:" + this.getRecipients() + "?" + (this.subject ? "subject=" + encodeURIComponent(this.subject) + "&" : "") + "body=" + encodeURIComponent(this.body);
		if (uri.length > 2000) {
			this.error = this.States.MESSAGE_TOO_LONG;
		} else {
			var win = window.open(uri, "email");
			win.close();
		}
		return (this.error.code == this.States.OK.code);
	},

	/**
	* @private
	* @property	Email addresses
	* @type	{string[]}
	*/
	_addresses: null,

	/**
	* @private
	* @method	Initializes the object
	* @returns	{void}
	*/
	_initialize: function() {
		this._addresses = new Array();
	},

	States: {
		OK: {code: 0, message: ""},
		MESSAGE_TOO_LONG: {code: 1, message: "URI exceeds maximum allowed length" }
	}
};