/*

A function that runs after page load looking for double square brackets with contents like [[wmuserid,text]] 
and turns the contents into a mailto link.

------------------------------------

uses 2 regexs:
1) if there is only a userid in the brackets use the local site's default email address as the link text
2) if there are 2 things in the brackets use the second thing as the link text 

-------------------------------------

To allow sites to 'protect' multiple email address formats the following can be prepended to the userid:
w|  ..... (converts to W&M email format userid@wm.edu)
v| ..... (converts to VIMS email format userid@vims.edu)
m| ..... (converts to Mason School of Business email format userid@mason.wm.edu) 

These work in all sites. Note that if there is no letter followed by pipe in front of the userid, the site's default email address format will be used. 

The defaults per Cascade site are:

www.wm.edu (includes A&S) ..... userid@wm.edu
vims.edu ..... userid@vims.edu
business.wm.edu ...... userid@mason.wm.edu
education.wm.edu ..... userid@wm.edu
law.wm.edu ...... userid@wm.edu

------------------------------------

TODO: If possible use xslt and regex to find double closed square brackets with stuff in it [[...]] and append after
it a no script tag like:
<noscript>&nbsp;(email this user id at wm.edu)&nbsp;</noscript>

------------------------------------

Requirements:
- jquery is used for the document.ready function only 

-----------------------------------

*/

$(document).ready(function(){

    //check browser for javascript    
    if (!document.getElementsByTagName) return false;
    if (!document.createElement) return false;

    /* 
    Pages where we don't want to run this code (site W&M only):
    /aboutthissite/howto/editwebpages/facultydirectory/
    /aboutthissite/howto/editwebpages/basics/
    */

/*
    myUrl = location.href;
    var pattern = new RegExp('(/aboutthissite/howto/editwebpages/facultydirectory/)|(/aboutthissite/howto/editwebpages/basics/)','i');
    if(pattern.test(myUrl)) {
        //alert("Email replace turned off.");
        return false;
    }
*/
    


    page = document.body.innerHTML;

    // CASE: w|
    page = page.replace(/\[\[[\s*[wW]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@wm.edu\">$1@wm.edu</a>");
    page = page.replace(/\[\[\s*[wW]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@wm.edu\">$2</a>");
    
    // CASE: v|
    page = page.replace(/\[\[\s*[vV]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@vims.edu\">$1@vims.edu</a>");
    page = page.replace(/\[\[\s*[vV]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@vims.edu\">$2</a>");
    
    // CASE: m|
    page = page.replace(/\[\[\s*[mM]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@mason.wm.edu\">$1@mason.wm.edu</a>");
    page = page.replace(/\[\[\s*[mM]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@mason.wm.edu\">$2</a>");
    

    // CASE: site default. (DIFFERENT IN EACH SITE)
    page = page.replace(/\[\[\s*([a-zA-Z0-9\_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@wm.edu\">$1@wm.edu</a>");
    page = page.replace(/\[\[\s*([a-zA-Z0-9\_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@wm.edu\">$2</a>");



    // directory url, make it link. (Saving the client a little effort and putting this rewrite here too...)
    page = page.replace(/\{\{\s*(http\:\/\/[^\}\,]+)\s*\}\}/g, "<a href=\"$1\">$1</a>");
    page = page.replace(/\{\{\s*(http\:\/\/[^\}\,]+)\s*\,{1}\s*([^\}]*)\s*\}\}/g, "<a href=\"$1\">$2</a>");

    document.body.innerHTML = page;
    
}); 
