/*
 * guardMailto Copyright (c) 2006 Doug Mayo-Wells.
 * V 1.0.01, July 2006
 * Includes code from Prototype (c) 2005 Sam Stephenson
 * The contributions and inspiration of Rich Thornett (www.thornett.com) are gratefully acknowledged
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and  
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Dynamically replace objects in the page (such as a span, div, or image) that
 * express email addresses in spam-harvester-proof form with live mailto: links.
 * Examples:
 *     new guardMailto("emailSpan2","postmaster","example",{linkText:"contact the Postmaster"});
 *     new guardMailto("emailLink","donaldt","csu",{domainSuffix:"edu"});
 */

/*
 * $() Borrowed from Prototype JavaScript framework, version 1.4.0_rc2
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 */
if ("function"!=typeof $) {
	function $() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
		   var element = arguments[i];
		   if (typeof element == 'string')
			   element = document.getElementById(element);
		   if (arguments.length == 1)
			   return element;
		   elements.push(element);
		}
		return elements;
	}
}


// make sure master namespace is established
if ("undefined" == typeof mwmw) {
    var mwmw = {};
}

/**
 * Wrap the specified _target with an email link.
 * Optionally replace the contents of _target with an alternate presentation
 * @param target HTML element (or ID string)
 * @param username string 
 * @param domain string
 * @param options object
 *	   domainSuffix:string, domain suffix e.g, "org", "edu", default "com"
 *	   linkAddress:boolean
 *			when true, use the email address as the link text
 *     linkClass:string className pass-through attribute for created link
 *     linkHtml:string (html) replace innerHTML of created link
 *     linkId:string id pass-through attribute for created link
 *     linkText:string replace txt of _target
 *     linkTitle:string pass-through title attribute for created link
 */
mwmw.guardMailto = function(target,userName,domain,options) {
    this._options = options || {};
    this._target = $(target);
    this._userName = userName;
    this._domain = domain;
    this._options.linkTitle = this._options.linkTitle || "";
    this._options.linkClass = this._options.linkClass || "";
    this._options.linkId = this._options.linkId || "";
    this._options.domainSuffix = this._options.domainSuffix || "com";
    this._link();
};

mwmw.guardMailto.prototype = {
	/* 
	 * wrap _target in a new link.
	 * replace the contents of new link or _target, if specified in options
	 */
    _link:function() {
    // make the new link element
    if (!this._target) {return;}
        var obj = document.createElement('A');
        obj.href = this._buildMailto();
		// only add attributes to the element if they are set
        if (this._options.linkTitle) {
			obj.title = this._options.linkTitle;
        }
		if (this._options.linkClass) {
			obj.className = this._options.linkClass;
		}
		if (this._options.linkId) {
			obj.id = this._options.linkId;
		}
        
        // insert the new node in the DOM right before _target
        this._target.parentNode.insertBefore(obj,this._target);
        // append _target to the new node
        obj.appendChild(this._target);

		// use the email address itself as the link text?
		if (this._options.linkAddress) {
			this._options.linkText = this._buildAddress();
			}
        // override the previous contents?
        if (this._options.linkText) {
			// test type in case user wants to replace ""
			if ("string" == typeof this._target.textContent) {
				// W3C DOM
				this._target.textContent = this._options.linkText;
			}
			else if ("string" == typeof this._target.innerText) {
				// IE
				this._target.innerText = this._options.linkText;
			}

        }
        if ((this._options.linkHtml)&&(obj.innerHTML)) {
            obj.innerHTML = this._options.linkHtml;
        }
    },
	/*
     * assemble an address
     */
	_buildAddress:function() {
        return (this._userName + String.fromCharCode(64) + this._domain + "." + this._options.domainSuffix);
	},
	/*
     * assemble a mailTo link
     */
    _buildMailto:function() {
        return ("mailto:" + this._buildAddress());
    }
};
