// create namepsace
Core.createNamespace('nl.code.pager');

/**
 * Uri Class
 */
nl.code.pager.Uri = {
    /**
     * @var string, the base uri of this project
     */
    base_uri: null,

    /**
     * @var string, this the base_url with http://domain in front of it
     */
    base_url: null,

    /**
     * @var object
     */
    open_in_new_window_options: {
        other_domain: true,
        files: true,
        rel_blank: true,
        exception_href_arr: []
    },

    /**
     * Store the base uri in the static variable
     *
     * @param base_uri string, the base uri of this project
     * @return void
     */
    setBaseUri: function(base_uri) {
        nl.code.pager.Uri.base_uri = base_uri;
    },

    /**
     * Make links open in a new browser/tab
     *
     * @param root Element, the html element to scan for anchors, defaults to body
     * @param array
     * @return void
     */
    parseLinksToOpenInNewWindow: function(element, open_in_new_window_options) {
        element = element || document.id(document.body);
        open_in_new_window_options = $merge(nl.code.pager.Uri.open_in_new_window_options, open_in_new_window_options);

        // get all anchors and loop through them
        var anchor_arr = element.getElements('a');
        for (var i = 0; i < anchor_arr.length; i++) {
            var url = nl.code.pager.Uri.getAnchorUrl(anchor_arr[i]);

            if (!url) {
                continue;
            }

            var exception_url = nl.code.pager.Uri.isExceptionUrl(url, open_in_new_window_options.exception_href_arr);

            if (
                (open_in_new_window_options.rel_blank && nl.code.pager.Uri.isBlankUrl(url, anchor_arr[i])) ||
                (!exception_url && open_in_new_window_options.other_domain && nl.code.pager.Uri.isExternalUrl(url, anchor_arr[i])) ||
                (!exception_url && open_in_new_window_options.files && nl.code.pager.Uri.isFileUrl(url, anchor_arr[i]))
                ) {
                nl.code.pager.Uri.openLinkInNewWindow(anchor_arr[i]);
            }
        }
    },

    /**
     * Checks if the url must be opened in a new window
     *
     * @param string
     * @param Element
     * @return boolean
     */
    isBlankUrl: function(url, anchor) {
        var rel = anchor.get('rel');

        if (rel == '_blank') {
            anchor.addClass('js-blank-uri');
            return true;
        }

        return false;
    },

    /**
     * @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;
    },

    /**
     * Checks if the url goes to another domain
     *
     * @param string
     * @param Element
     * @return boolean
     */
    isExternalUrl: function(url, anchor) {
        // the regular expressions a href has to match to open in a new window/tab
        var http_re = /^https?\:\/\//i;

        if (http_re.test(url)) {
            anchor.addClass('js-external-uri');
            return true;
        }

        return false;
    },

    /**
     * Checks if the url links to a file
     *
     * @param string
     * @param Element
     * @return boolean
     */
    isFileUrl: function(url, anchor) {
        var file_re       = /\.[a-z0-9]{2,4}$/i;
        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 (file_re.test(url) && !(mailto_re.test(url) || javascript_re.test(url))) {
            anchor.addClass('js-file-uri');
            return true;
        }

        return false;
    },

    /**
     * @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 Element
     * @return void
     */
    openLinkInNewWindow: function(anchor) {
        anchor.addEvent('click', function(event) {
            event.stop();

            // open a new window/tab
            window.open(this.get('href'), '_blank');
        });
    },

    /**
     * @param Element
     * @return string
     */
    getAnchorUrl: function(anchor) {
        var url     = anchor.get('href');
        var http_re = /^https?\:\/\//i;

        // guard
        if (!url) {
            return;
        }

        // this becomes something like http://www.example.com
        var base_url_re = new RegExp(window.location.protocol +'\/\/(www\.)?'+ window.location.host.replace('www.', ''), 'i');

        // remove the base url of the url
        url = url.replace(base_url_re, '');

        // remove the base path if it has a "http://" in it
        if (http_re.test(nl.code.pager.Uri.base_uri)) {
            url = url.replace(nl.code.pager.Uri.base_uri);
        }

       return url;
    },

    /**
     * @return string
     */
    getBaseUrl: function() {
        if (!nl.code.pager.Uri.base_url) {
            nl.code.pager.Uri.base_url = window.location.protocol + window.location.host;
        }

        return nl.code.pager.Uri.base_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;
    }
};