/**
 * Uri Class
 *
 * @static
 */
Core.createNamespace('nl.code.pager');
nl.code.pager.Uri = {
    /**
     * @var string, the base url of this project
     */
    base_url: null,

    /**
     * Store the base url in the static variable
     *
     * @param base_url string, the base url of this project
     * @return void
     */
    setBaseUrl: function(base_url) {
        nl.code.pager.Uri.base_url = base_url;
    },

    /**
     * Make external links open in a new browser/tab
     *
     * @param root Element, the html element to scan for anchors, defaults to body
     * @param array
     * @return void
     */
    parseExternalLinks: function(root, exception_arr) {
        if (! $type(root)) {
            root = $(document.body);
        }

        if (! $type(exception_arr)) {
            exception_arr = [];
        }

        // get all anchors and loop through them
        var anchor_arr = root.getElements('a');
        for (var i = 0; i < anchor_arr.length; i++) {
            var url = nl.code.pager.Uri.getAnchorUrl(anchor_arr[i]);
            var rel = anchor_arr[i].get('rel');

            if (! rel || rel != '_blank') {
                if (nl.code.pager.Uri.isExceptionUrl(url, exception_arr)) {
                    continue;
                }

                if (! nl.code.pager.Uri.isExternalUrl(url)) {
                    continue;
                }
            }

            anchor_arr[i].addEvent('click', function(event) {
                event.stop();

                // open a new window/tab
                window.open(this.get('href'), '_blank');
            });
        }
    },

    /**
     * @param string
     * @param array
     * @return boolean
     */
    isExceptionUrl: function(url, exception_arr) {
        for (var i = 0; i < exception_arr.length; i++) {
            if (url.test(exception_arr[i])) {
                return true;
            }
        }

        return false;
    },

    /**
     * @param string
     * @return boolean
     */
    isExternalUrl: function(url) {
        // 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 http_re       = /^https?\:\/\//i;

        // the regular expressions a href has NOT to match to open in a new window/tab
        var mailto_re     = /^mailto\:/;
        var javascript_re = /^javascript\:/;

        // 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(url) || javascript_re.test(url) || ! (file_re.test(url) || http_re.test(url))) {
            return false;
        }

        return true;
    },

    /**
     * @param Element
     * @return boolean
     */
    isInternalUrl: function (url) {
        // the regular expressions a href has NOT to match to do an AJAX request
        var file_re       = /\.[a-z0-9]{2,4}$/i;
        var javascript_re = /^javascript\:/;
        var http_re       = /^https?\:\/\//i;
        var mailto_re     = /^mailto\:/;

        if (! file_re.test(url) && ! javascript_re.test(url) && ! http_re.test(url) && ! mailto_re.test(url)) {
            return true;
        }

        return false;
    },

    /**
     * @param Element
     * @return string
     */
    getAnchorUrl: function(anchor) {
        var url = anchor.get('href');
        var http_re = /^https?\:\/\//i;
        var base_url = window.location.protocol +'//'+ window.location.host;

        // guard
        if (! url) {
            return;
        }

        // ie makes the href absolute
        url = url.replace(base_url, '');

        if (http_re.test(nl.code.pager.Uri.base_url)) {
            url = url.replace(nl.code.pager.Uri.base_url);
        }

       return url;
    },

    /**
     * Get the hash part of the url in the address bar of the browser
     *
     * @return string
     */
    getHash: function() {
        var hash = window.location.hash;

        hash = hash.replace('#/', '');

        return hash;
    },

    /**
     * Get the hash part of the url in the address bar of the browser
     * Remove the first "#/"
     *
     * @param string
     * @return void
     */
    setHash: function(hash) {
        window.location.hash = hash;
    }
};