function supportUho(Options){
    "use strict";

    function closeFrame(frame, close, toWnd, noRestore) {
        frame.style.display = "none";
        close.style.display = "none";
        toWnd.style.display = "none";

        if (uho) uho.style.display = "block";

        frame.src = "";

        if (noRestore) window.localStorage.removeItem("restoreMyChatFrame");
    }

    function openChat(who, w, h, url, id, params){
        var miniWEBMC = window.open(url + '/support.html?' + who + '&id=' + id + (params.length ? "&" + params.join("&") : ""), '',
            'menubar=no,resizable=0,directories=no,location=no,toolbar=no,status=no,scrollbars=0,screenX=100,screenY=100,width=' + w + ',height=' + h);

        setTimeout(function(){
            miniWEBMC.focus();
        }, 0);
    }

    function openInFrame(who, w, h, url, id, params) {
        var frame = document.getElementById(Options.inFrame);
        var close = document.getElementById("close_" + Options.inFrame);
        var toWnd = document.getElementById("toWindow_" + Options.inFrame);
        var restoreWho = window.localStorage.getItem("restoreWho");

        if (restoreWho === "uin=0" || restoreWho === "group=0"){
            restoreWho = null;
        }

        if ((who !== "uin=0" && who !== "group=0") || !restoreWho) {
            window.localStorage.setItem("restoreWho", who);
        } else

        if (restoreWho){
            who = restoreWho;
        }

        if (frame){
            var lnk = (location.pathname + location.search).replace("/", "");

            frame.src           = url + '/support.html?' + who + '&id=' + id + (params.length ? "&" + params.join("&") : "") + "&ref=" + lnk;
            close.style.display = "block";
            toWnd.style.display = "block";
            frame.style.display = "block";

            if (uho) uho.style.display = "none";

            window.localStorage.setItem("restoreMyChatFrame", "true");

            if (close){
                close.onclick = function () {
                    closeFrame(frame, close, toWnd, true);
                };

                toWnd.onclick = function () {
                    openChat(who, w, h, url, id, params);

                    closeFrame(frame, close, toWnd, true);
                }
            }
        } else {
            console.error("Can't find iFrame: " + Options.inFrame);
        }
    }

    var restore  = window.localStorage.getItem("restoreMyChatFrame");
    var text     = Options.text     || "Online Support";
    var position = Options.position || 1;
    var uin      = Options.uin      || 0;
    var height   = Options.height   || 500;
    var width    = Options.width    || 550;
    var btnID    = Options.btnID    || null;
    var hideMain = Options.hideMain || false;
    var group    = Options._type ? Options.group : 0;
    var chatUrl  =(Options.secure ? "https://" : "http://") + (Options.url || "localhost");
    var phone    = document.body.offsetWidth <= 500;
    var params   = []
        .concat(Options.select  ? "select=true" : [])
        .concat(Options.inFrame && !phone ? "inFrame=true" : [])
        .concat(Options.custom  ? "custom=" + encodeURI(Options.custom) : [])
        .concat(Options.fields  ? "fields=" + Options.fields : []);

    var css = document.createElement("link");
    var inFrame = Options.inFrame && !phone;

    var msgClose     = "mcMsgCloseWnd";
    var eventMethod  = window.addEventListener ? "addEventListener" : "attachEvent";
    var eventer      = window[eventMethod];
    var messageEvent = eventMethod === "attachEvent" ? "onmessage" : "message";

    eventer(messageEvent, function (e) {
        if (e.data === msgClose || e.message === msgClose){
            var frame = document.getElementById(Options.inFrame);
            var close = document.getElementById("close_" + Options.inFrame);
            var toWnd = document.getElementById("toWindow_" + Options.inFrame);

            closeFrame(frame, close, toWnd);
        }
    });

    css.type = "text/css";
    css.href = chatUrl + "/chat/uho/uho.css";
    css.rel  = "stylesheet";

    var uho = document.createElement("a");

    if (!hideMain){
        uho.id        = "supportUhoMyChat";
        uho.className = "supportUho noAnimation uhoPosition_" + position;
        uho.innerHTML = "<img class='supportUhoIMG' src='" + chatUrl + "/chat/uho/mc.png'>" + text +
            "<a href='https://nsoft-s.com?supportUho&" + Options.id + "' style='font-size: 1px; opacity: 0;'>.</a>";
    }

    if (Object.prototype.toString.call( uin ) === '[object Array]'){
        if (uin.length === 1){
            uin = uin[0];
        }
    }

    if (inFrame){
        var frameView = document.createElement("iframe");

        frameView.id          = Options.inFrame;
        frameView.src         = "";
        frameView.allow       = "microphone; camera";
        frameView.frameBorder = "0";
        frameView.style.display         = "none";
        frameView.style.width           = "370px";
        frameView.style.height          = "400px";
        frameView.style.position        = "fixed";
        frameView.style.right           = "30px";
        frameView.style.bottom          = "0";
        frameView.style.backgroundColor = "#ffffff";
        frameView.style.zIndex          = "9999";
        frameView.style.boxShadow       = "0 -1px 1px 2px rgba(203,203,203,0.59)";


        var closeView = document.createElement("div");

        closeView.id        = "close_" + Options.inFrame;
        closeView.title     = "Close";
        closeView.innerText = "+";
        closeView.style.display         = "none";
        closeView.style.position        = "fixed";
        closeView.style.right           = "410px";
        closeView.style.bottom          = "370px";
        closeView.style.backgroundColor = "rgba(216,216,216,0.46)";
        closeView.style.borderRadius    = "50%";
        closeView.style.zIndex          = "9999";
        closeView.style.width           = "30px";
        closeView.style.height          = "30px";
        closeView.style.textAlign       = "center";
        closeView.style.lineHeight      = "30px";
        closeView.style.cursor          = "pointer";
        closeView.style.fontWeight      = "400";
        closeView.style.fontSize        = "20pt";
        closeView.style.border          = "1px solid #494949";
        closeView.style.transform       = "rotate(45deg)";
        closeView.style.fontFamily      = "Arial";

        var toWindow = document.createElement("div");

        toWindow.id         = "toWindow_" + Options.inFrame;
        toWindow.title      = "To window";
        toWindow.className  = "fa fa-clone";
        toWindow.style.display         = "none";
        toWindow.style.position        = "fixed";
        toWindow.style.right           = "410px";
        toWindow.style.bottom          = "330px";
        toWindow.style.backgroundColor = "rgba(216,216,216,0.46)";
        toWindow.style.borderRadius    = "50%";
        toWindow.style.zIndex          = "9999";
        toWindow.style.width           = "30px";
        toWindow.style.height          = "30px";
        toWindow.style.textAlign       = "center";
        toWindow.style.lineHeight      = "30px";
        toWindow.style.cursor          = "pointer";
        toWindow.style.fontWeight      = "400";
        toWindow.style.fontSize        = "13pt";
        toWindow.style.border          = "1px solid #494949";

        document.body.appendChild(frameView);
        document.body.appendChild(closeView);
        document.body.appendChild(toWindow);
    }

    if (btnID){
        btnID.forEach(function (id) {
            var el = document.getElementById(id);

            if (el) {
                el.addEventListener('click', function () {
                    var who = this.getAttribute("group")
                        ? "group=" + this.getAttribute("group")
                        : group && !this.getAttribute("uin")
                            ? "group=" + group
                            : "uin="   + this.getAttribute("uin");

                    if (inFrame) {
                        openInFrame(who, width, height, chatUrl, Options.id, params);
                    } else {
                        openChat(who, width, height, chatUrl, Options.id, params);
                    }
                }, false);
            }
        });
    }

    if (!hideMain){
        document.body.appendChild(css);
        document.body.appendChild(uho);

        document.getElementById(uho.id).addEventListener("click", function(){
            if (inFrame){
                openInFrame(group ? "group=" + group : "uin=" + uin, width, height, chatUrl, Options.id, params);
            } else{
                openChat(group ? "group=" + group : "uin=" + uin, width, height, chatUrl, Options.id, params);
            }
        }, false);
    }

    if (restore && inFrame){
        openInFrame(group ? "group=" + group : "uin=" + uin, width, height, chatUrl, Options.id, params);
    }
}

supportUho(mc_uho_opt);
