var at1 = {};
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
at1.cookie = function(name, value, options) {

    function trim(text) {
        return (text || "").replace( /^\s+|\s+$/g, "" );
    }

    if (typeof value !== 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires === 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
at1.abView = function() {
    var that = {};

    var AT_VERSION_COOKIE_NAME = "atversion";
    var rampupCookieName = "rampup";

    function getBetaUrl() {
        return location.protocol + '//www2.' + location.host.replace('www.',''); // hardcoded at2 url!
    }

    that.getABCookie = function() {
        return at1.cookie(AT_VERSION_COOKIE_NAME);
    };

    that.setABCookie = function(cookieValue) {
        at1.cookie(AT_VERSION_COOKIE_NAME, cookieValue, {path: '/', expires: 365});
    };

    that.getABBucket = function(abRatio) {
        return Math.random() <= abRatio ?  at1.rampup.AT2 : at1.rampup.AT1;
    };

    that.setRampupCookie = function(rampupCookieValue, identifier) {
        at1.cookie(rampupCookieName, identifier + "|" + rampupCookieValue, {path: '/', expires: 365});
    };

    that.getRampupCookie = function(excludes) {
        var cookieValue = at1.cookie(rampupCookieName);
        if (!cookieValue) {
            return null;
        }
        var idAndVersion = cookieValue.split('|');
        for (var i = 0 ; i < excludes.length ; i++) {
            if (excludes[i].toString() === idAndVersion[0]) {
                return null;
            }
        }
        return idAndVersion[1];
    };

    that.redirectToAT2 = function() {
        window.location = getBetaUrl();
    };

    return that;
};
at1.continueFromRampupController = function(view) {
    var that = {};

    that.continueFromRampup = function(abRatio, excludes) {
        if(view.getABCookie() === at1.rampup.AT2) {
            view.redirectToAT2();
            return;
        }

        var abValue = view.getRampupCookie(excludes) || view.getABCookie();
        if (!abValue) {
            abValue = view.getABBucket(abRatio);
            view.setABCookie(abValue);
        }

        if (abValue === at1.rampup.AT2) {
            view.redirectToAT2();
        }
    };

    return that;
};

at1.continueFromRampup = function(config) {
    var view = at1.abView();
    var abRatio = config ? config.ratio || 0.1 : 0.1;
    var excludes = config ? config.excludes || [] : [];
    at1.continueFromRampupController(view).continueFromRampup(abRatio, excludes);
};
at1.rampupController = function(view) {
    var that = {};

    that.rampup = function(identifier, abRatio, excludes) {
        if(view.getABCookie() === at1.rampup.AT2) {
            view.redirectToAT2();
            return;
        }

        var abValue = view.getRampupCookie(excludes);
        if (!abValue) {
            abValue = view.getABBucket(abRatio);
            view.setRampupCookie(abValue, identifier);
        }

        if (abValue === at1.rampup.AT2) {
            view.redirectToAT2();
        }
    };

    return that;
};

at1.rampup = function(identifier, rampupConfig) {
    var view = at1.abView();
    var abRatio = rampupConfig ? rampupConfig.ratio || 0.5 : 0.5;
    var excludes = rampupConfig ? rampupConfig.excludes || [] : [];
    at1.rampupController(view).rampup(identifier, abRatio, excludes);
};

at1.rampup.AT1 = "1.0";
at1.rampup.AT2 = "2.0";
