var Surveyio = {

    VERSION: 1.3,

    CONST: {
        cdn: "http://cdn.survey.io/embed/1.0",
        cdn_ssl: "https://s3.amazonaws.com/cdn.survey.io/embed/1.0",
        frame_source: "http://survey.io/survey/",
        id_prepend: "surveyio_",
        log: {
            servers: ["log1.survey.io"],
            callback: "Surveyio.log_callback",
            timeout: 2000
        },
        super_callback: "Surveyio.log",
        local_cookie: "__sio_local",
        action_timeout: 1000
    },

    init: function(a) {
        this.options = a;
        this.log_server = this.CONST.log.servers[Math.floor(Math.random() * this.CONST.log.servers.length)];
        if (this.options.use_ssl) {
            this.CONST.cdn = this.CONST.cdn_ssl
        }
        if (this.options.survey_id && this.options.token) {
            //this.super_session();
            this.local_session();
            setTimeout(function() {
                //Surveyio.log()
            },
            this.CONST.log.timeout);
            this.css();
            this.place_overlay();
            this.place_action();
            this.action_timeout = setTimeout(function() {
                Surveyio.show_action()
            },
            this.CONST.action_timeout)
        } else {
            alert("You must provide a survey ID and token.")
        }
    },
    __: function(a) {
        return document.getElementById(this.CONST.id_prepend + a)
    },
    set_url_parts: "",
    set: function(a, b) {
        this.set_url_parts += "&set[" + encodeURIComponent(a) + "]=" + encodeURIComponent(b)
    },
    local_session: function() {
        if (!this.get_cookie(this.CONST.local_cookie)) {
            var a = this.sha1(location.hostname + navigator.userAgent + this.options.survey_id + this.options.token + new Date().getTime());
            this.set_cookie(this.CONST.local_cookie, a, 500);
            this.local_session = a
        } else {
            this.local_session = this.get_cookie(this.CONST.local_cookie)
        }
    },
    super_session: function() {
        if (Surveyio_flashDetect.installed && Surveyio_flashDetect.versionAtLeast(6)) {
            var b = "http://" + this.log_server + "/super.swf";
            b += "?survey_id=" + this.options.survey_id;
            b += "&token=" + this.options.token;
            var a = "callback=" + this.CONST.super_callback;
            var c = '<embed src="' + b + '" flashvars="' + a + '" allowscriptaccess="always" width="1" height="1" style="display:none;" />';
            document.write(c)
        }
    },
    log_callback: function(a) {
        if (a.success) {
            this.logged = true;
            if (a.completed != true) {
                this.show_action()
            } else {
                clearTimeout(this.action_timeout)
            }
        }
    },
    log: function() {
        if (this.logged != true) {
            var b = "http://" + this.log_server + "/log";
            b += "?survey_id=" + this.options.survey_id;
            b += "&token=" + this.options.token;
            b += "&local=" + this.local_session;
            b += "&callback=" + this.CONST.log.callback;
            b += "&url=" + encodeURIComponent(location.href);
            b += "&referrer=" + encodeURIComponent(document.referrer);
            b += "&title=" + ((document.title) ? this.base64(document.title.replace(/^\s*|\s*$/g, "").replace(/(\n\r|\n|\r)/gm, "").replace(/\t/g, "")) : "");
            b += "&t=" + new Date().getTime();
            var a = document.createElement("script");
            a.type = "text/javascript";
            a.charset = "utf-8";
            a.src = b;
            document.body.appendChild(a)
        }
    },
    place_action: function() {
        var a = document.createElement("a");
        a.href = "#";
        a.id = this.CONST.id_prepend + "action";
        a.onclick = function() {
            Surveyio.open();
            return false
        };
        a.className = "surveyio_" + this.options.banner.y + " surveyio_" + this.options.banner.x;
        document.body.appendChild(a)
    },
    show_action: function() {
        this.__("action").style.display = "block"
    },
    place_overlay: function() {
        var a = document.createElement("div");
        a.id = this.CONST.id_prepend + "overlay";
        a.style.display = "none";
        a.onclick = function() {
            Surveyio.close()
        };
        var b = '<div id="' + this.CONST.id_prepend + 'container"><a href="#" onclick="Surveyio.close(); return false;" id="' + this.CONST.id_prepend + 'close">Close</a><iframe src="" id="' + this.CONST.id_prepend + 'iframe" allowTransparency="true" scrolling="yes" frameborder="0"></iframe></div><div id="' + this.CONST.id_prepend + 'screen"></div>';
        a.innerHTML = b;
        document.body.appendChild(a)
    },
    css: function() {
        var a = this.options.banner;
        var c = this.CONST.cdn + "/" + a.x + "_" + a.y + "_" + a.color + ".png";
        var b = '<style type="text/css">a { outline: none; } a:focus { -moz-outline-style: none; } #surveyio_overlay { width: 100%; height: 100%; top:0; left:0; z-index: 10000; position: absolute; } #surveyio_screen { top:0; left:0; z-index:1; width: 100%; height: ' + this.viewport().y + 'px; position: absolute; background-color: #000; opacity: 0.85; -moz-opacity: 0.85; filter:alpha(opacity=85); } * html #surveyio_screen { width: expression(document.documentElement.clientWidth+"px"); } #surveyio_container { width: 680px; height: 640px; margin: 80px auto 0 auto; z-index:2; position:relative; background: #fff; border: solid 15px #fff; } #surveyio_container iframe { overflow-x: hidden; overflow-y: scroll; width: 680px; height: 100%; margin: 0; padding: 0; } #surveyio_close { position: absolute; display: block; top: -28px; left: -28px; width: 30px; height: 30px; text-indent: -2000px; background: url(' + this.CONST.cdn + "/close.png) no-repeat; } * html #surveyio_close { background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + this.CONST.cdn + "/close.png, sizingMethod=crop); cursor: pointer; } #surveyio_action { display: none; width: 105px; height: 105px; z-index: 9999; position: fixed; background: url(" + c + ") no-repeat; } * html #surveyio_action { position: absolute; background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + c + ", sizingMethod=crop); cursor: pointer; } .surveyio_right { right: 0; } .surveyio_top { top: 0px; _top:expression(eval(document.compatMode && document.compatMode=='CSS1Compat') ? document.documentElement.scrollTop : document.body.scrollTop); } .surveyio_bottom { bottom: 0px; _top:expression(eval(document.compatMode && document.compatMode=='CSS1Compat') ? document.documentElement.scrollTop + (document.documentElement.clientHeight-this.clientHeight) - 1 : document.body.scrollTop + (document.body.clientHeight-this.clientHeight) - 1); }</style>";
        document.write(b)
    },
    frame_source: function() {
        var a = this.CONST.frame_source + this.options.survey_id + "/embed";
        a += "?session=" + this.local_session;
        if (this.set_url_parts) {
            a += this.set_url_parts
        }
        return a
    },
    position_height: function() {
        var a = window.innerHeight || document.documentElement.clientHeight;
        Surveyio.__("screen").style.height = a + "px"
    },
    open: function() {
        this.__("iframe").src = this.frame_source();
        this.__("overlay").style.display = "block";
        this.__("container").style.top = this.viewport().y_scroll + "px"
    },
    close: function() {
        this.__("overlay").style.display = "none"
    },
    set_cookie: function(c, b, e) {
        var d = "";
        if (e > 0) {
            var a = new Date();
            a.setDate(a.getDate() + e);
            d = a.toGMTString()
        }
        cookieval = c + "=" + b + "; " + ((d) ? ("expires=" + d + " ;") : "") + "path=/";
        document.cookie = cookieval
    },
    get_cookie: function(b) {
        var e = "" + document.cookie;
        var d = e.indexOf(b);
        if (d == -1 || b == "") {
            return ""
        }
        var a = e.indexOf(";", d);
        if (a == -1) {
            a = e.length
        }
        return unescape(e.substring(d + b.length + 1, a))
    },
    viewport: function() {
        if (window.innerHeight && window.scrollMaxY) {
            var a = window.innerHeight + window.scrollMaxY;
            var c = window.innerWidth + window.scrollMaxX
        } else {
            if (document.body.scrollHeight > document.body.offsetHeight) {
                var a = document.body.scrollHeight;
                var c = document.body.scrollWidth
            } else {
                var a = document.body.offsetHeight;
                var c = document.body.offsetWidth
            }
        }
        if (!window.pageYOffset) {
            if (! (document.documentElement.scrollTop == 0)) {
                var b = document.documentElement.scrollTop
            } else {
                var b = document.body.scrollTop
            }
        } else {
            var b = window.pageYOffset
        }
        return {
            x: c,
            y: a,
            y_scroll: b
        }
    },
    utf8_encode: function(b) {
        b = b.replace(/\r\n/g, "\n");
        var a = "";
        for (var e = 0; e < b.length; e++) {
            var d = b.charCodeAt(e);
            if (d < 128) {
                a += String.fromCharCode(d)
            } else {
                if ((d > 127) && (d < 2048)) {
                    a += String.fromCharCode((d >> 6) | 192);
                    a += String.fromCharCode((d & 63) | 128)
                } else {
                    a += String.fromCharCode((d >> 12) | 224);
                    a += String.fromCharCode(((d >> 6) & 63) | 128);
                    a += String.fromCharCode((d & 63) | 128)
                }
            }
        }
        return a
    },
    base64: function(d) {
        var b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var a = "";
        var l,
        j,
        g,
        k,
        h,
        f,
        e;
        var c = 0;
        d = this.utf8_encode(d);
        while (c < d.length) {
            l = d.charCodeAt(c++);
            j = d.charCodeAt(c++);
            g = d.charCodeAt(c++);
            k = l >> 2;
            h = ((l & 3) << 4) | (j >> 4);
            f = ((j & 15) << 2) | (g >> 6);
            e = g & 63;
            if (isNaN(j)) {
                f = e = 64
            } else {
                if (isNaN(g)) {
                    e = 64
                }
            }
            a = a + b.charAt(k) + b.charAt(h) + b.charAt(f) + b.charAt(e)
        }
        return a
    },
    sha1: function(d) {
        function c(y, x) {
            var j = (y << x) | (y >>> (32 - x));
            return j
        }
        function r(z) {
            var y = "";
            var j;
            var A;
            var x;
            for (j = 0; j <= 6; j += 2) {
                A = (z >>> (j * 4 + 4)) & 15;
                x = (z >>> (j * 4)) & 15;
                y += A.toString(16) + x.toString(16)
            }
            return y
        }
        function t(z) {
            var y = "";
            var x;
            var j;
            for (x = 7; x >= 0; x--) {
                j = (z >>> (x * 4)) & 15;
                y += j.toString(16)
            }
            return y
        }
        var g;
        var v,
        u;
        var b = new Array(80);
        var m = 1732584193;
        var k = 4023233417;
        var h = 2562383102;
        var f = 271733878;
        var e = 3285377520;
        var s,
        q,
        p,
        o,
        n;
        var w;
        d = this.utf8_encode(d);
        var a = d.length;
        var l = new Array();
        for (v = 0; v < a - 3; v += 4) {
            u = d.charCodeAt(v) << 24 | d.charCodeAt(v + 1) << 16 | d.charCodeAt(v + 2) << 8 | d.charCodeAt(v + 3);
            l.push(u)
        }
        switch (a % 4) {
        case 0:
            v = 2147483648;
            break;
        case 1:
            v = d.charCodeAt(a - 1) << 24 | 8388608;
            break;
        case 2:
            v = d.charCodeAt(a - 2) << 24 | d.charCodeAt(a - 1) << 16 | 32768;
            break;
        case 3:
            v = d.charCodeAt(a - 3) << 24 | d.charCodeAt(a - 2) << 16 | d.charCodeAt(a - 1) << 8 | 128;
            break
        }
        l.push(v);
        while ((l.length % 16) != 14) {
            l.push(0)
        }
        l.push(a >>> 29);
        l.push((a << 3) & 4294967295);
        for (g = 0; g < l.length; g += 16) {
            for (v = 0; v < 16; v++) {
                b[v] = l[g + v]
            }
            for (v = 16; v <= 79; v++) {
                b[v] = c(b[v - 3] ^ b[v - 8] ^ b[v - 14] ^ b[v - 16], 1)
            }
            s = m;
            q = k;
            p = h;
            o = f;
            n = e;
            for (v = 0; v <= 19; v++) {
                w = (c(s, 5) + ((q & p) | (~q & o)) + n + b[v] + 1518500249) & 4294967295;
                n = o;
                o = p;
                p = c(q, 30);
                q = s;
                s = w
            }
            for (v = 20; v <= 39; v++) {
                w = (c(s, 5) + (q ^ p ^ o) + n + b[v] + 1859775393) & 4294967295;
                n = o;
                o = p;
                p = c(q, 30);
                q = s;
                s = w
            }
            for (v = 40; v <= 59; v++) {
                w = (c(s, 5) + ((q & p) | (q & o) | (p & o)) + n + b[v] + 2400959708) & 4294967295;
                n = o;
                o = p;
                p = c(q, 30);
                q = s;
                s = w
            }
            for (v = 60; v <= 79; v++) {
                w = (c(s, 5) + (q ^ p ^ o) + n + b[v] + 3395469782) & 4294967295;
                n = o;
                o = p;
                p = c(q, 30);
                q = s;
                s = w
            }
            m = (m + s) & 4294967295;
            k = (k + q) & 4294967295;
            h = (h + p) & 4294967295;
            f = (f + o) & 4294967295;
            e = (e + n) & 4294967295
        }
        var w = t(m) + t(k) + t(h) + t(f) + t(e);
        return w.toLowerCase()
    }
};
var Surveyio_flashDetect = new
function() {
    var a = this;
    a.installed = false;
    a.raw = "";
    a.major = -1;
    a.minor = -1;
    a.revision = -1;
    a.revisionStr = "";
    var b = [{
        name: "ShockwaveFlash.ShockwaveFlash.7",
        version: function(h) {
            return d(h)
        }
    },
    {
        name: "ShockwaveFlash.ShockwaveFlash.6",
        version: function(k) {
            var h = "6,0,21";
            try {
                k.AllowScriptAccess = "always";
                h = d(k)
            } catch(j) {}
            return h
        }
    },
    {
        name: "ShockwaveFlash.ShockwaveFlash",
        version: function(h) {
            return d(h)
        }
    }];
    var d = function(k) {
        var h = -1;
        try {
            h = k.GetVariable("$version")
        } catch(j) {}
        return h
    };
    var g = function(h) {
        var k = -1;
        try {
            k = new ActiveXObject(h)
        } catch(j) {
            k = {
                activeXError: true
            }
        }
        return k
    };
    var c = function(j) {
        var h = j.split(",");
        return {
            raw: j,
            major: parseInt(h[0].split(" ")[1], 10),
            minor: parseInt(h[1], 10),
            revision: parseInt(h[2], 10),
            revisionStr: h[2]
        }
    };
    var f = function(l) {
        var j = l.split(/ +/);
        var k = j[2].split(/\./);
        var h = j[3];
        return {
            raw: l,
            major: parseInt(k[0], 10),
            minor: parseInt(k[1], 10),
            revisionStr: h,
            revision: e(h)
        }
    };
    var e = function(h) {
        return parseInt(h.replace(/[a-zA-Z]/g, ""), 10) || a.revision
    };
    a.majorAtLeast = function(h) {
        return a.major >= h
    };
    a.minorAtLeast = function(h) {
        return a.minor >= h
    };
    a.revisionAtLeast = function(h) {
        return a.revision >= h
    };
    a.versionAtLeast = function(j) {
        var k = [a.major, a.minor, a.revision];
        var h = Math.min(k.length, arguments.length);
        for (i = 0; i < h; i++) {
            if (k[i] >= arguments[i]) {
                if (i + 1 < h && k[i] == arguments[i]) {
                    continue
                } else {
                    return true
                }
            } else {
                return false
            }
        }
    };
    a.FlashDetect = function() {
        if (navigator.plugins && navigator.plugins.length > 0) {
            var l = "application/x-shockwave-flash";
            var k = navigator.mimeTypes;
            if (k && k[l] && k[l].enabledPlugin && k[l].enabledPlugin.description) {
                var h = k[l].enabledPlugin.description;
                var m = f(h);
                a.raw = m.raw;
                a.major = m.major;
                a.minor = m.minor;
                a.revisionStr = m.revisionStr;
                a.revision = m.revision;
                a.installed = true
            }
        } else {
            if (navigator.appVersion.indexOf("Mac") == -1 && window.execScript) {
                var h = -1;
                for (var j = 0; j < b.length && h == -1; j++) {
                    var n = g(b[j].name);
                    if (!n.activeXError) {
                        a.installed = true;
                        h = b[j].version(n);
                        if (h != -1) {
                            var m = c(h);
                            a.raw = m.raw;
                            a.major = m.major;
                            a.minor = m.minor;
                            a.revision = m.revision;
                            a.revisionStr = m.revisionStr
                        }
                    }
                }
            }
        }
    } ()
};
