/*

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) 
e| ..... (converts to "student" email userid@email.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: e|
    page = page.replace(/\[\[\s*[eE]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@email.wm.edu\">$1@email.wm.edu</a>");
    page = page.replace(/\[\[\s*[eE]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@email.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@vims.edu\">$1@vims.edu</a>");
    page = page.replace(/\[\[\s*([a-zA-Z0-9\_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@vims.edu\">$2</a>");



    // directory url, make it link. (Saving the client a little effort and putting this rewrite here too...)
    page = page.replace(/\{\{\s*(https*\:\/\/[^\}\,]+)\s*\}\}/g, "<a href=\"$1\">$1</a>");
    page = page.replace(/\{\{\s*(https*\:\/\/[^\}\,]+)\s*\,{1}\s*([^\}]*)\s*\}\}/g, "<a href=\"$1\">$2</a>");



    /* ======== EMBED YOUTUBE VIDEO ============
     *
     * Sizes a different "tag" for each size:
     * 
     *   youtube:small = small player (240x180)
     *   youtube:medium = medium player (425x238)
     *   youtube:large = large player (693x388)
     * 
     * Placement Options:
     *
     *   "Default" is left-align, no wrapping text: {{youtube:size|YouTubeVideoId}}
     *   Center-align, no wrapping text: {{youtube:size:center|YouTubeVideoId,optional description}}
     *   Left-align with wrapping text: {{youtube:size:left|YouTubeVideoId,optional description}}
     *   Right-align with wrapping text:{{youtube:size:right|YouTubeVideoId,optional description}}
     * 
     * */
    
    
    /* Small Player (240x134) */ 


    // look for {{youtube:small|videoid}} ... no position given (left-aligns, no text wrap)
    page = page.replace(/\{\{\s*youtube:\s*small\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-left-nofloat yt-sm"><iframe title="$2" allowfullscreen="true" frameborder="0" height="134" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="240"></iframe><p class="yt-captiontext yt-sm">$2</p></div>');

    // look for {{youtube:small:center|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*small:\s*center\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-center yt-sm"><iframe style="cursor:pointer;" title="$2" allowfullscreen="true" frameborder="0" height="134" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="240"></iframe><p class="yt-captiontext yt-sm">$2</p></div>');

    // look for {{youtube:small:left|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*small:\s*left\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-left"><iframe style="cursor:pointer;" title="$2" allowfullscreen="true" frameborder="0" height="134" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="240"></iframe><p class="yt-captiontext yt-sm">$2</p></div>');

    // look for {{youtube:small:right|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*small:\s*right\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-right"><iframe style="cursor:pointer;" title="$2" allowfullscreen="true" frameborder="0" height="134" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="240"></iframe><p class="yt-captiontext yt-sm">$2</p></div>');



    /* Medium Player (425x238) */


    // look for {{youtube:medium|videoid}} ... no position given (left-aligns, no text wrap)
    page = page.replace(/\{\{\s*youtube:\s*medium\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-left-nofloat yt-med"><iframe title="$2" allowfullscreen="true" frameborder="0" height="238" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="425"></iframe><p class="yt-captiontext yt-med">$2</p></div>');

    // look for {{youtube:medium:center|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*medium:\s*center\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-center yt-med"><iframe title="$2" allowfullscreen="true" frameborder="0" height="238" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="425"></iframe><p class="yt-captiontext yt-med">$2</p></div>');

    // look for {{youtube:medium:left|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*medium:\s*left\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-left"><iframe title="$2" allowfullscreen="true" frameborder="0" height="238" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="425"></iframe><p class="yt-captiontext yt-med">$2</p></div>');

    // look for {{youtube:medium:right|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*medium:\s*right\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-right"><iframe title="$2" allowfullscreen="true" frameborder="0" height="238" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="425"></iframe><p class="yt-captiontext yt-med">$2</p></div>');



    /* Large Player (693x388) */


    // look for {{youtube:large|videoid}} ... no position given (left-aligns, no text wrap)
    page = page.replace(/\{\{\s*youtube:\s*large\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-left-nofloat yt-lg"><iframe title="$2" allowfullscreen="true" frameborder="0" height="388" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="693"></iframe><p class="yt-captiontext yt-lg">$2</p></div>');

    // look for {{youtube:large:center|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*large:\s*center\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-center yt-lg"><iframe title="$2" allowfullscreen="true" frameborder="0" height="388" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="693"></iframe><p class="yt-captiontext yt-lg">$2</p></div>');

    // look for {{youtube:large:left|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*large:\s*left\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-left"><iframe title="$2" allowfullscreen="true" frameborder="0" height="388" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="693"></iframe><p class="yt-captiontext yt-lg">$2</p></div>');

    // look for {{youtube:large:right|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*large:\s*right\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div class="yt-right"><iframe title="$2" allowfullscreen="true" frameborder="0" height="388" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="693"></iframe><p class="yt-captiontext yt-lg">$2</p></div>');



    /* Popup Player (402x242) */


    // look for {{youtube:popup|videoid}} ... no position given (left-aligns, no text wrap)
    page = page.replace(/\{\{\s*youtube:\s*popup\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div><iframe title="$2" allowfullscreen="true" frameborder="0" height="242" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="402"></iframe></div>');

    // look for {{youtube:popup:center|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*popup:\s*center\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div><iframe title="$2" allowfullscreen="true" frameborder="0" height="242" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="402"></iframe></div>');

    // look for {{youtube:popup:left|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*popup:\s*left\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div><iframe title="$2" allowfullscreen="true" frameborder="0" height="242" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="402"></iframe></div>');

    // look for {{youtube:popup:right|videoid}}
    page = page.replace(/\{\{\s*youtube:\s*popup:\s*right\s*\|\s*([a-zA-Z0-9_\.-]+)\s*\,?(.*)\}\}/ig, '<div><iframe title="$2" allowfullscreen="true" frameborder="0" height="242" src="http://www.youtube-nocookie.com/embed/$1?rel=0&amp;showinfo=0&amp;autohide=1&amp;wmode=transparent" width="402"></iframe></div>');




    document.body.innerHTML = page;
    
    /*** Photosets ***/
	if (jQuery("#wm-photoset").length > 0) {
		jQuery("#wm-photoset").wmPhotoset();
	}
    
}); 

