 if (!window.mobileRedirect) {window.mobileRedirect = {};}
    mobileRedirect.redirection_mobile = function(configuration) {

        // Helper function for adding time to the current date
        var addTimeToDate = function(msec) {

            // Get the current date
            var exdate = new Date();

            // Add time to the date
            exdate.setTime(exdate.getTime() + msec);

            //Return the new Date
            return exdate;
        };

        // Helper function for getting a value from a parameter in the querystring
        var getQueryValue = function(param) {

            if (!param) {
                return;
            }
            var querystring = document.location.search,
                queryStringArray = querystring && querystring.substring(1).split("&"),
                i = 0,
                length = queryStringArray.length;
            for (; i < length; i++) {
                var token = queryStringArray[i],
                    firstPart = token && token.substring(0, token.indexOf("="));
                if (firstPart === param ) {
                    return token.substring(token.indexOf("=") + 1, token.length);
                }
            }
        };
            // Constants
            FALSE = "false",
            TRUE = "true",

            // configuration object
            config = configuration || {},

            // parameter to pass in the URL to avoid the redirection
            redirection_param = config.noredirection_param || "noredirection",

            // new url for the mobile site domain
            mobile_url = config.mobile_url,

            // protocol for the mobile site domain
            mobile_protocol = config.mobile_scheme ?config.mobile_scheme + ":" :document.location.protocol,

            // URL host of incoming request
            host = document.location.host,

            // value for the parameter passed in the URL to avoid the redirection
            queryValue = getQueryValue(redirection_param),

            // Compose the mobile hostname considering "mobile_url" or hostname
            mobile_host = mobile_url ||(!!host.match(/^www\./i) ?host.substring(4) :host),

            // Expiry hours for cookie
            cookie_hours = config.cookie_hours || 1,

            // Parameters to determine if the pathname and the querystring need to be kept
            keep_path = config.keep_path || false,
            keep_query = config.keep_query || false,

            // Check if the UA is a mobile or tablet with width lessthan 1000
               isUAMobile = ((screen.width < 1000) && (navigator.userAgent.match(/iPad/i) == null))?"true":"false";

            // Check if the referrer was a mobile page (probably the user clicked "Go to full site") or in the
            // querystring there is a parameter to avoid the redirection such as "?mobile_redirect=false"
            // (in that case we need to set a variable in the sessionStorage or in the cookie)
            if (document.referrer.indexOf("mobile") >= 0 || queryValue === TRUE ) {

                if (window.sessionStorage) {
                    window.sessionStorage.setItem(redirection_param, TRUE);
                } else {
                    document.cookie = redirection_param + "=" + TRUE + ";expires="+
                        addTimeToDate(3600*1000*cookie_hours).toUTCString();
                }
            }

            // Check if the sessionStorage contain the parameter
            var isSessionStorage = (window.sessionStorage) ?
                    (window.sessionStorage.getItem(redirection_param) === TRUE) :
                        false,

                // Check if the Cookie has been set up
                isCookieSet = document.cookie ?
                    (document.cookie.indexOf(redirection_param) >= 0) :
                        false;

            // Check that User Agent is mobile, cookie is not set or value in the sessionStorage not present
            if ((isUAMobile == "true") && !(isCookieSet || isSessionStorage)) {
                // Callback call
                if (config.beforeredirection_callback) {
                    if (!config.beforeredirection_callback.call(this)) {
                        return;
                    }
                }
                var path_query = "";

                if(keep_path == "true") {
                   path_query += document.location.pathname;
                   if(path_query.indexOf("/browse/") != -1){
                      path_query = path_query.replace("/browse/","/browseMobile/");
                   }
                   if(path_query.indexOf("/product/") != -1){
                      path_query = path_query.replace("/product/","/mobileProduct/");
                   }
                }

                if (keep_query == "true") {
                    path_query += document.location.search;
                }
                if (isUAMobile == "true") {
                    document.location.href = mobile_protocol + "//" + mobile_host + path_query;
                }
            }
    };
