﻿var USER_DEFINED_COLORS;
var METABAR_MARGIN_VALUE = 30;

var METABAR_STYLE = "\
* {margin:0; padding:0;}\
html, body {border:none;}\
body {overflow:hidden;}\
.sitebar {height:30px; font:12px/29px 'Trebuchet MS', sans-serif !important; position:relative; overflow:hidden;}\
.sitebar__logo, .sitebar__grd {bottom:0; height:30px; position:absolute; top:0;}\
.sitebar__logo {background: 0 0 no-repeat; right:0; width:510px; z-index:98;}\
.sitebar__grd {background:url(http://%HOST%/static/i/sitebar5-sprite.png) 0 -60px repeat-x; left:0; width:100%; z-index:99;}\
.sitebar__close, .sitebar-button__l, .sitebar-button__r {background:url(http://%HOST%/static/i/sitebar5-sprite.png) 0 -30px no-repeat; display:block; overflow:hidden;}\
.sitebar__close {height:30px; position:absolute; right:0; text-indent:-9999px; top:0; width:27px; z-index:101;}\
.sitebar__cnt {padding:0 27px 0 20px; position:relative; white-space:nowrap; z-index:100;}\
.sitebar-label, .sitebar-button {display:-moz-inline-stack; display:inline-block;}\
.sitebar-label__t, .sitebar-button, .sitebar-button__r {font:normal 0.99em/250% 'Trebuchet MS', sans-serif !important;}\
.sitebar-label__t {color:#fff;}\
.sitebar a {cursor:pointer;}\
.sitebar-button {margin:0 7px 0 0; *margin:0 10px 0 0;}\
.sitebar-button__l {background-position:-400px -30px; padding:0 0 0 3px; text-decoration:none; color:#000 !important;}\
.sitebar-button__r {background-position:100% 0; padding:0 12px 0 9px;}\
.sitebar__cnt, .sitebar-label, .sitebar-button, .sitebar-button__l, .sitebar-button__r {height:30px; vertical-align:top; *display:inline; *zoom:1;}\
.sitebar img.stat {position:absolute;}\
/* Sitebar for IE */\
.sitebar__ie {background:#39c;}\
.sitebar__ie .sitebar__logo {background-image:url(http://%HOST%/static/i/sitebar5-ie.png);}\
/* Sitebar for FF */\
.sitebar__ff {background:#c30;}\
.sitebar__ff .sitebar__logo {background-image:url(http://%HOST%/static/i/sitebar5-ff.png);}\
/* Custom Sitebar */\
.sitebar__custom {background:#b3cb03;}\
.sitebar__custom .sitebar__logo {display:none;}\
.sitebar__custom .sitebar__close {background-position:-17px -30px;}";
var METABAR_HTML = '\
<div class="sitebar sitebar__%CLASS%" id="metabar_headcrab" style="%METABAR_BG_STYLE%">\
    <div class="sitebar__logo"></div>\
    <div class="sitebar__grd"></div>\
    <a title="Закрыть" class="sitebar__close" onclick="window.top.removeHeadCrab(); return false;" href="#">Закрыть</a>\
    <div class="sitebar__cnt">\
        <div class="sitebar-button"><a target="_top" class="sitebar-button__l" href="%DOWNLOAD_URL%"><span class="sitebar-button__r">%INSTALL_LABEL%</span></a></div>\
        <div class="sitebar-label"><span class="sitebar-label__t" style="%METABAR_COLOR_STYLE%">%DESCRIPTION_LABEL%</span></div>\
    </div>\
    <img class="stat" src="%STATS_IMG_SRC%" />\
</div>';

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset);
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return (setStr);
}


function removeHeadCrab(track) {
    if (track) {
        var image = document.createElement('img');
        image.setAttribute('src', 'http://' + HOST + "/stats/add_sitebar_close_stat/?toolbar_id=" + METABAR_TOOLBAR_ID);

        if (image.style.setAttribute) {
            image.style.setAttribute('cssText', 'display: block');
        }
        else {
            image.setAttribute('style', 'display: block');
        }

        document.getElementById('metabar_headcrab').appendChild(image);
    }

    setCookie('hide_metabar_headcrab', 'true', new Date(2100, 1, 1, 1, 1, 1, 1), '/', null, null);

    var iframe = document.getElementById("METABAR_IFRAME");
    if (iframe) {
        iframe.parentNode.removeChild(iframe);

        unfixTopMargin();
    }
}

function getIframeDocument(iframeNode) {
  if (iframeNode.contentDocument) return iframeNode.contentDocument
  if (iframeNode.contentWindow) return iframeNode.contentWindow.document
  return iframeNode.document
}

function getTopMargin(node) {
    if (window.getComputedStyle) {
        return Number(window.getComputedStyle(node, null).marginTop.replace("px", ""));
    }
    else if (node.currentStyle) {   //IE
        if (node.currentStyle["marginTop"]) {
            return Number(node.currentStyle["marginTop"].replace("px", ""));
        }
    }
    return 0;
}

function fixTopMargin() {
    var mode = (null == document.compatMode) ? "" : document.compatMode;

    var topMargin = METABAR_MARGIN_VALUE;
    switch (mode) {
        case "BackCompat":
            topMargin += getTopMargin(document.body);
            document.body.style.marginTop = "" + topMargin + "px";
            break;
        default:
            var html = document.body.parentNode;
            topMargin += getTopMargin(html);
            html.style.marginTop = "" + topMargin + "px";
            break;
    }
}

function unfixTopMargin() {
    var mode = (null == document.compatMode) ? "" : document.compatMode;

    var topMargin;
    switch (mode) {
        case "BackCompat":
            topMargin = Math.max(0, getTopMargin(document.body) - METABAR_MARGIN_VALUE);
            document.body.style.marginTop = "" + topMargin + "px";
            break;
        default:
            var html = document.body.parentNode;
            topMargin = Math.max(0, getTopMargin(html) - METABAR_MARGIN_VALUE);
            html.style.marginTop = "" + topMargin + "px";
            break;
    }
}

function isQuirksMode() {
    return "BackCompat" == document.compatMode;
}

try {
    if (null == document.body) {
        document.write("<b id='metabar-loader'>M</b>");
        var loader = document.getElementById("metabar-loader");
        loader.parentNode.removeChild(loader);
    }
    METABAR_TOOLBAR_ID = METABAR_TOOLBAR_ID || null;

    var parts = METABAR_DOWNLOAD_URL.split("?");
    var href = parts[0];
    parts.splice(0, 1);
    var hash = parts.join();

    if (!/^(.+\&)?source\=/.test(hash)) {
        if (hash.length) {
            hash += "&"
        }
        hash += "source=sitebar";
        METABAR_DOWNLOAD_URL = href + "?" + hash;
    }

    var IS_IE = /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
    var IS_FF = /Firefox[\/\s](\d+\.\d+)/i.test(navigator.userAgent);

    POSITION = (this.METABAR_FIXED_POSITION && !(IS_IE && isQuirksMode())) ? "fixed" : "absolute";

    var METABAR_CLASS = 'custom';
    if (!USER_DEFINED_COLORS) {
        if (IS_IE) {
            METABAR_CLASS = 'ie';
        }

        if (IS_FF) {
            METABAR_CLASS = 'ff';
        }
    }
    if (null == window.DESCRIPTION_LABEL) {
        DESCRIPTION_LABEL = "Установить тулбар";
    }
    if (null == window.INSTALL_LABEL) {
        INSTALL_LABEL = "Установить";
    }
    METABAR_HTML = METABAR_HTML.replace('%INSTALL_LABEL%', INSTALL_LABEL);
    METABAR_HTML = METABAR_HTML.replace('%DESCRIPTION_LABEL%', DESCRIPTION_LABEL);
    METABAR_HTML = METABAR_HTML.replace('%DOWNLOAD_URL%', METABAR_DOWNLOAD_URL);
    METABAR_HTML = METABAR_HTML.replace('%CLASS%', METABAR_CLASS);

    if (USER_DEFINED_COLORS)  {
       if (window.METABAR_BG_COLOR) {
          var bgColor = ("" + window.METABAR_BG_COLOR).replace(/[\'\"\;]+/g, '');
          var bgStyle = 'background-color:' + bgColor + ';';
          while (METABAR_HTML.search('%METABAR_BG_STYLE%') != -1) {
              METABAR_HTML = METABAR_HTML.replace('%METABAR_BG_STYLE%', bgStyle);
          }
       }
       if (window.METABAR_TEXT_COLOR) {
          var textColor = ("" + window.METABAR_TEXT_COLOR).replace(/[\'\"\;]+/g, '');
          var colorStyle = 'color:' + textColor + ';';
          while (METABAR_HTML.search('%METABAR_COLOR_STYLE%') != -1) {
              METABAR_HTML = METABAR_HTML.replace('%METABAR_COLOR_STYLE%', colorStyle);
          }
       }
    }
    
    while (METABAR_STYLE.search('%HOST%') != -1) {
        METABAR_STYLE = METABAR_STYLE.replace('%HOST%', window.HOST);
    }
    while (METABAR_HTML.search('%HOST%') != -1) {
        METABAR_HTML = METABAR_HTML.replace('%HOST%', window.HOST);
    }

    var STATS_HOST = 'http://stats.' + HOST;
    while (METABAR_HTML.search('%STATS_IMG_SRC%') != -1) {
        METABAR_HTML = METABAR_HTML.replace('%STATS_IMG_SRC%', STATS_HOST + '/blank.gif?toolbar_id=' + METABAR_TOOLBAR_ID);
    }

    if ((IS_IE || IS_FF) && !getCookie('hide_metabar_headcrab')) {
        var iframe = document.createElement("iframe");

        iframe.style.position = POSITION;
        iframe.style.top = "0px";
        iframe.style.left = "0px";
        iframe.style.overflow = "hidden";
        iframe.style.zIndex = "10000";
        iframe.style.border = "0";

        iframe.setAttribute("frameborder", "0");
        iframe.setAttribute("marginwidth", "0");
        iframe.setAttribute("marginheight", "0");
        iframe.setAttribute("hspace", "0");
        iframe.setAttribute("vspace", "0");
        iframe.setAttribute("width", "100%");
        iframe.setAttribute("height", "30px");
        iframe.setAttribute("scrolling", "no");

        document.body.appendChild(iframe);

        var iframe_document = getIframeDocument(iframe);

        iframe.id = "METABAR_IFRAME";

        //this is the only way to dynamically inject stylesheet in IE8
        iframe_document.write(METABAR_HTML + "<style>" + METABAR_STYLE + "</style>");

        iframe_document.close();
        fixTopMargin();
    }
} catch (e) {
    console.log(e)
}

window.set_metabar_installed = function (toolbar_id, version, source_id) {
    if (toolbar_id == METABAR_TOOLBAR_ID || source_id == METABAR_TOOLBAR_ID) {
        removeHeadCrab(false);
    }
};
