/**
 * PageData Class
 *
 * This is the core class to make a site "ajaxified".
 * The class is created when the window.onload event is fired, this "onload"
 * script is generated by the server-side Pager
 */
PageData = 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.createLinks();
    },

    /**
     * @return void
     */
    createLinks: function() {
        // get all child anchors
        var anchor_arr = $ES('a');

        for (var i = 0; i < anchor_arr.length; i++) {
            // get the href property of the anchor, remove the base_url (if provided)
            var href = anchor_arr[i].getProperty('href').replace(this.base_url, '');
            var rel = anchor_arr[i].getProperty('rel');

            // regexp for an uri that starts with http://, this means that it is an external url
            var url_reg_exp = /^http:\/\//i;

            // regexp for an file
            var file_reg_exp = /\.[a-z0-9]{2,4}$/i;

            // image file
            var image_reg_exp = /\.(gif|jpg)$/i;

            if (url_reg_exp.test(href) || file_reg_exp.test(href) || rel == 'rss') {
                // external link
                this.createExternalLink(anchor_arr[i], href);
            }
        }
    },

    /**
     * Create a link to a location on an other server/domain
     *
     * @param Element, an anchor with a href to another domain
     * @return void
     */
    createExternalLink: function(anchor, href) {
        anchor.href = 'javascript:void(null);';

        // add onclick event
        anchor.addEvent('click', function(e) {
            // stop the default event
            new Event(e).stop();

            window.open(href);
        });
    }
});
