﻿function addEvent(obj, type, fn) {
    if (obj.addEventListener)
        obj.addEventListener(type, fn, false);
    else if (obj.attachEvent) {
        obj["e" + type + fn] = fn;
        obj[type + fn] = function() { obj["e" + type + fn](window.event); }
        obj.attachEvent("on" + type, obj[type + fn]);
    }
}

function old_browser() {
    var ie6 = document.all && /MSIE\s?6/.test(navigator.userAgent)
    var ie7 = document.all && /MSIE\s?7/.test(navigator.userAgent)
    var quirksmode = document.compatMode == "BackCompat"
    return ie6 || (ie7 && quirksmode)
}

var kundo = {
    get_fixed_style: function() {
        style = ""
        if (old_browser()) {
            style = "position: absolute;";
        }
        else {
            style = "position: fixed;";
        }
        return style
    },
    slug: "http://kundo.se/embed/landingpage/karin-rask",
    create_box: function() {
        if (!document.getElementById("kundo_container")) {
            var body = document.getElementsByTagName("body")[0];
            var kundo_container = document.createElement('div');
            kundo_container.id = "kundo_container"
            body.appendChild(kundo_container);

            kundo.create_overlay(kundo_container, body);
            kundo.create_frame(kundo_container);
        }
        else {
            document.getElementById("kundo_container").style.display = "block";
        }
    },
    create_overlay: function(kundo_container, body) {
        var style =
			"top: 0; bottom:0; min-height:600px; left: 0;" +
			"background: #000; filter: alpha(opacity=50); opacity: 0.5; z-index: 1000; width: 100%;";
        style += kundo.get_fixed_style()
        if (old_browser()) {
            var body_height = Math.max(document.body.clientHeight,
									   document.documentElement.clientHeight);
            style += "height:" + body_height + "px;";
        }
        kundo_container.innerHTML +=
			'<div id="kundo_overlay" style="' + style + '"></div>';
    },
    create_button: function() {
        var body = document.getElementsByTagName("body")[0];
        var kundo_button_container = document.createElement('div');
        kundo_button_container.id = "kundo_button_container"
        body.appendChild(kundo_button_container);

        var style =
			"background: white; top: 200px; right: 0; cursor: pointer;";
        style += kundo.get_fixed_style()


        kundo_button_container.innerHTML +=
				'<img src="/images/kundo/tycktill-1-sv-right.png" ' +
				'alt="" id="kundo_feedback" style="' + style + '" onclick="kundo.create_box()">';


    },
    create_frame: function(kundo_container) {
        var style =
			"top: 19px; left: 50%; margin-left: 380px; " +
			"height: 22px; width: 23px; cursor: pointer; z-index:1100;";
        style += kundo.get_fixed_style()
        kundo_container.innerHTML +=
			'<img src="/images/kundo/close.png" alt="" ' +
			'id="kundo_close" style="' + style + '" onclick="kundo.hide_iframe()">';
        //kundo_container.innerHTML = '        <iframe src="http://kundo.se/embed/landingpage/karin-rask" id="kundo_iframe" scrolling="no" frameborder="no"    style="background: white;    border: none;    height: 510px;    left: 50%;    margin-left: -358px;    position: fixed;    top: 40px;    width: 760px;    z-index: 2000;"></iframe>'
        var iframeHTML = '        <iframe src="http://kundo.se/embed/landingpage/karin-rask" id="kundo_iframe" scrolling="no" frameborder="no"    style="background: white;    border: none;    height: 510px;    left: 50%;    margin-left: -358px;    position: fixed;    top: 40px;    width: 760px;    z-index: 2000;"></iframe>'
        if (old_browser()) {
            iframeHTML = iframeHTML.replace('position: fixed;', 'position:absolute !important;')
        }
        kundo_container.innerHTML += iframeHTML;
    },
    hide_iframe: function() {
        var element = document.getElementById("kundo_container");
        if (element) {
            element.parentNode.removeChild(element);
        }
    },
    init: function() {
        kundo.create_button();
    },

    check_close: function(e) {
        var evt = e || window.event;
        if (evt && evt.keyCode == 27) {
            kundo.hide_iframe();
        }
    }
}

addEvent(document, 'keydown', kundo.check_close)
