/**
* JSAmp
* Unobtrusive, client-side ampersand replacer. Styled with CSS.
*
* @author   zytzagoo <zytzagoo at gmail dot com>
* @version  0.3
* 
* @license  The MIT License http://www.opensource.org/licenses/mit-license.php
* 
* @usage    See the comments at the end of the file explaining the meaning of 
*           certain options. Then you have at least two ways of doing stuff:
*           a) Change JSAmp.options below and set autorun to true, then the 
*           only thing you need to do is create a new instance of JSAmp on 
*           every page that you want amplified:
*           var jsamp = new JSAmp();
*           b) Set custom options, and call amplify() yourself when you need it,
*           like so:
*           var opts = { foo: bar, baz: woot };
*           var jsamp = new JSAmp(opts);
*           jsamp.amplify();
*
* @todo     Create a jQuery plugin doing the same thing basically, but we'll be
*           able to use jQuery's powerful CSS selector syntax and much more 
*           robust DOM manipulation.
* @todo     Maybe make the regex configurable (thus making the string to search for
*           configurable). That might defeat the purpose of a very simple
*           ampersand replacer though, since we'll end up with a pretty "generic" 
*           js search & replacer for stuff inside html element's contents)
*
* Copyright (c) 2008 zytzagoo.
*
* 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.
*/
function JSAmp(cfg) {
    var opts = cfg || JSAmp.options;
    // var regex = /&#?(x26|amp|38|038);?|&(?=\s)/g;
    var regex = /&#?(x26|amp|38|038);/g;
    /**
    * Replace contents of an element.
    * doing it outside of the DOM is why this is blazing fast!
    */
    var replace_html = function(el, html) {
        var ns = el.nextSibling;
        var p = el.parentNode;
        p.removeChild(el);
        el.innerHTML = html;
        if (ns) {
            p.insertBefore(el, ns);
        } else {
            p.appendChild(el);
        }
    };
    /**
    * Scans the document for specified elements, and
    * checks each element for presence of ampersands.
    * In case we find some, replace them.
    */
    this.amplify = function() {
        var new_amp = '<span class="' + opts.className + '">' + opts.replacement + '</span>';
        for (var i = 0; i < opts.elements.length; i++) {
            var els = document.getElementsByTagName(opts.elements[i]);
            if (els.length > 0) {
                for (var j = 0; j < els.length; j++) {
                    var el = els[j];
                    var content = el.innerHTML;
                    if (content.search(regex) !== false) {
                        var new_content = content.replace(regex, new_amp);
                        replace_html(el, new_content);
                    }
                }
            }
        }
    };
    var attach_handler = function (o, evt, f) {
        if (o !== null) {
            var existing_handler = o[evt];
            if (typeof o[evt] !== 'function') {
                o[evt] = f;
            } else {
                o[evt] = function (e) {
                    existing_handler.apply(o, arguments);
                    f.apply(o, arguments);
                };
            }
        }
    };
    if (opts.autorun) {
        attach_handler(window, 'onload', this.amplify);
    }
}
/**
 * Default JSAmp options
 */
JSAmp.options = {
    // an array of elements which are scanned for presence of ampersands
    elements: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
    // the string which will be used as a replacement for the amps found
    replacement: '&#x26;',
    // className to apply to the <span> element that is wrapped around the replacement string
    className: 'amp',
    // trigger amplify() automatically when the document loads?
    autorun: false
};