/**
 * Pager Class
 */
Pager = new Class({

    /**
     * Constructor
     *
     * @param string, the base url, corresponds with the php constant BASE_URL
     */
    initialize: function(base_url) {
        this.base_url = base_url;

        this.parseExternalLinks();
    },

    /**
     * @param Element, optional HTML element
     * @return void
     */
    parseExternalLinks: function(root) {
        if (! $type(root)) {
            root = $(document.body);
        }

        // the regular expressions a href has to match to open in a new window/tab
        var file_re       = /\.[a-z0-9]{2,4}$/i;
        var javascript_re = /^javascript\:/;
        var http_re       = /^https?\:\/\//i;
        var mailto_re     = /^mailto\:/;

        // get all anchors and loop through them
        var anchor_arr = root.getElements('a');
        for (var i = 0; i < anchor_arr.length; i++) {
            var href = anchor_arr[i].get('href');

            if (http_re.test(this.base_url)) {
                href = href.replace(this.base_url, '');
            }

            // if the href does not match go to the next anchor in the array
            // a mailto href with an email address will match the re for a file
            if (mailto_re.test(href) || javascript_re.test(href) || ! (file_re.test(href) || http_re.test(href))) {
                continue;
            }

            // open the uri in a new window at the onclick event
            anchor_arr[i].addEvent('click', function(e) {
                // stop default behavior
                new Event(e).stop();

                // open a new window/tab
                window.open(this.get('href'), '_blank');
            });
        }
    }
});