// jsPathto v0.0.2 07-17-09 09:52
// A nifty bit'o code to make use of RapidWeaver's %pathto()% syntax in javascript files.
// Assembled for your RapidWeaver developing pleasure by Adam Merrifield (http://www.seydoggy.com)

// 1. Put this file in your RapidWeaver theme, and establish a link to it from your index.html template.
//    In this case, I am putting it in a folder I have in all my themes called "scripts":
//<script type="text/javascript" src="%pathto(scripts/jspathto.js)%"></script>

// 2. Next we need to establish a good ol' PHP style rtim function, javascript style.
//    I borrowed this one from http://kevin.vanzonneveld.net (as it states), but you could easily
//    write your own.
function rtrim ( str, charlist ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: rtrim('    Kevin van Zonneveld    ');
    // *     returns 1: '    Kevin van Zonneveld'

    charlist = !charlist ? ' \s\xA0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    var re = new RegExp('[' + charlist + ']+$', 'g');
    return (str+'').replace(re, '');
}

// 3. Then we need to establish a variable that will get RapidWeaver to assign a path. You can do
//    this with any file in the theme but we'll keep it simple and just reference this one. So you
//    need to insert this between the <head> tags of your index.html file ABOVE the call for this
//    file (and uncommented, of course):

//<script type="text/javascript" charset="utf-8">var rwPathto = "%pathto(scripts/jspathto.js)%";</script>

// 4. Next we define stuff we want to strip out; the folder and file reference, which, in our case,
//    is THIS file in the SCRIPTS folder. We do this with the rtrim function we made above, like this:

// 4.1 We define a variable with the file name characters.
// 	   It can be a string, like this, "ahjopst./" or for readability, "/jspathto.js".
trimFromPatha = "/jspathto.js";

// 4.2 Then we trim that variable from the original rwPathto
jsPathtoa = rtrim(rwPathto, trimFromPatha);

// 4.3 We then define the next string of characters, the folder name, as a variable.
//     It can be a string, "ciprst" or a more readable, "scripts".
trimFromPathb = "scripts";

// 4.4 Lastly we define our final trimmed path, called jsPathto (which only seemed appropriate):
jsPathto = rtrim(jsPathtoa, trimFromPathb);

// You now have a persistent variable for file paths to use in your RapidWeaver themes to allow
// linkage to things like 'blank.gif' and onther such things.

// EXAMPLE
// In iepngfix.htc (version 2) you're default path is IEPNGFix.blankImg = "images/blank.gif";
// which breaks in RapidWeaver without some intervention on your part, like this:
// IEPNGFix.blankImg = jsPathto + "images/blank.gif";