window.cmu = window.cmu || {};
window.cmu.redirect = window.cmu.redirect || {};

/* Fundamental Ajax redirect URL configuration
 *
 * This provides the possible URL matches and logic for rewriting the URL for
 * Ajax use. This does not do any redirects by itself, but it is used in two
 * situations:
 *
 *     1. Hijacking Ajax links (cmu.hijack.js)
 *     2. Redirect non-Ajax views to the Ajax-enabled views
 */
(function (ns) {
    var my = {};
    my.logger = gj.logging.getLogger('cmu.redirect');

    ns.config = [
        [
            /\/object\/(\d+)/,
            function (match) {
                return cmu.urls.objectSearch + '?img_only=1#o:' + match[1];
            }
        ],
        [
            /\/(exhibition|set)\/(\d+)/,
            function (match) {
                var objectType = match[1];
                var objectId = match[2];
                return cmu.urls.setSearch + '#' + objectType.charAt(0) + ':' + objectId;
            }
        ]
    ];

    ns.getMatch = function (href) {
        my.logger.debug('getMatch()');
        var configEntry, match, redirectUrl;

        for (var i = 0; i < ns.config.length; i++) {
            configEntry = ns.config[i];
            match = href.match(configEntry[0]);

            if (match) {
                redirectUrl = configEntry[1](match);
                my.logger.info('Found match for "' + href + '": "' + redirectUrl + '"');
                return redirectUrl;
            }
        }
        return null;
    };

})(window.cmu.redirect);


