var USER_DEFINED_COLORS;

var IE_TOOLBAR = ' '+
'<style type="text/css"> html {margin-top:30px;}</style>'+
'<div id="metabar_headcrab" class="sitebar sitebar2" style="background:#39c; display:block; height:30px; font:12px/27px \'Trebuchet MS\', sans-serif !important; left:0;  min-width:600px; position:absolute; overflow:hidden; right:0; top:0; width:100%; z-index:10000;"> '+
'	<div style="background:url(http://%HOST%/static/i/sitebar-sprite-logo.png) 0 -30px repeat-x; bottom:0; display:block; height:30px; left:0; margin:0; padding:0; position:absolute; top:0; width:401px; z-index:98;"></div> '+
'	<div style="background:url(http://%HOST%/static/i/sitebar-sprite.png) 0 -60px repeat-x; bottom:0; display:block; height:30px; left:0; margin:0; padding:0; position:absolute; top:0; width:100%; z-index:99;"></div> '+
'	<a href="#" onclick="removeHeadCrab(true); return false;" style="background:url(http://%HOST%/static/i/sitebar-sprite.png) 0 -93px no-repeat; display:block; height:7px; margin:0; overflow:hidden; padding:0; position:absolute; right:12px; text-indent:-9999px; top:12px; width:7px; z-index:100;" title="Закрыть">Закрыть</a> '+
'	<table cellpadding="0" cellspacing="0" style="height:30px; float:right; font:0.99em/235% \'Trebuchet MS\', sans-serif !important; margin:0 23px 0 0; overflow:hidden; padding:0; position:relative; text-shadow:0 0 0 transparent !important; white-space:nowrap; z-index:100;"> '+
'		<tr> '+
'			<td style="padding-top:0px; text-align:right; vertical-align:top;"><span style="color:#fff !important; font:normal 0.99em/235% \'Trebuchet MS\', sans-serif !important; margin:0; padding:0; text-shadow:0 0 0 transparent !important;">' + DESCRIPTION_LABEL +'</span></td> '+
'			<td style="vertical-align:top;"> '+
'				<table cellpadding="0" cellspacing="0" style="color:#fff !important; cursor:pointer; float:right; font:italic 0.99em/235% \'Trebuchet MS\', sans-serif !important; height:30px; margin:0 2px 0 11px; min-width:100px; overflow:hidden; padding:0; position:relative; vertical-align:top; white-space:nowrap; z-index:100;"> '+
'					<tr> '+
'						<td style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) 0 0 no-repeat; height:30px; overflow:hidden; padding:0; width:13px; vertical-align:top;"></td> '+
'						<td  style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) 0 -33px repeat-x; height:30px; overflow:hidden; padding:0 5px; text-align:center; vertical-align:top;"><a target="_top" href="%DOWNLOAD_URL%" class="install_button" style="color:#fff !important; font:italic 0.99em/235% \'Trebuchet MS\', sans-serif !important; position:relative; text-decoration:none; text-shadow:0 0 0 transparent !important;">' + INSTALL_LABEL + '</a></td> '+
'						<td style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) -13px 0 no-repeat; height:30px; overflow:hidden; padding:0; width:17px; vertical-align:top;"></td> '+
'					</tr> '+
'				</table> '+
'			</td> '+
'		</tr> '+
'	</table> '+
'   <img src="%STATS_IMG_SRC%" />' +
'</div>';

var FF_TOOLBAR = ' '+
'<style type="text/css"> html {margin-top:30px;}</style>'+
'<div id="metabar_headcrab" class="sitebar" style="background:#c30; display:block; height:30px; font:12px/27px \'Trebuchet MS\', sans-serif !important; left:0;  min-width:600px; position:absolute; overflow:hidden; right:0; top:0; width:100%; z-index:10000;"> ' +
'	<div style="background:url(http://%HOST%/static/i/sitebar-sprite-logo.png) 0 0 repeat-x; bottom:0; display:block; height:30px; left:0; margin:0; padding:0; position:absolute; top:0; width:401px; z-index:98;"></div> ' +
'	<div style="background:url(http://%HOST%/static/i/sitebar-sprite.png) 0 -60px repeat-x; bottom:0; display:block; height:30px; left:0; margin:0; padding:0; position:absolute; top:0; width:100%; z-index:99;"></div> ' +
'	<a href="#" onclick="removeHeadCrab(true); return false;" style="background:url(http://%HOST%/static/i/sitebar-sprite.png) 0 -93px no-repeat; display:block; height:7px; margin:0; overflow:hidden; padding:0; position:absolute; right:12px; text-indent:-9999px; top:12px; width:7px; z-index:100;" title="Закрыть">Закрыть</a> ' +
'	<table cellpadding="0" cellspacing="0" style="height:30px; float:right; font:0.99em/235% \'Trebuchet MS\', sans-serif !important; margin:0 23px 0 0; overflow:hidden; padding:0; position:relative; text-shadow:0 0 0 transparent !important; white-space:nowrap; z-index:100;"> '+
'		<tr> '+
'			<td style="padding-top:0px; text-align:right; vertical-align:top;"><span style="color:#fff !important; font:normal 0.99em/235% \'Trebuchet MS\', sans-serif !important; margin:0; padding:0; text-shadow:0 0 0 transparent !important;">' + DESCRIPTION_LABEL +'</span></td> '+
'			<td style="vertical-align:top;"> '+
'				<table cellpadding="0" cellspacing="0" style="color:#fff !important; cursor:pointer; float:right; font:italic 0.99em/235% \'Trebuchet MS\', sans-serif !important; height:30px; margin:0 2px 0 11px; min-width:100px; overflow:hidden; padding:0; position:relative; vertical-align:top; white-space:nowrap; z-index:100;"> '+
'					<tr> '+
'						<td style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) 0 0 no-repeat; height:30px; overflow:hidden; padding:0; width:13px; vertical-align:top;"></td> '+
'						<td  style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) 0 -33px repeat-x; height:30px; overflow:hidden; padding:0 5px; text-align:center; vertical-align:top;"><a target="_top" href="%DOWNLOAD_URL%" class="install_button" style="color:#fff !important; font:italic 0.99em/235% \'Trebuchet MS\', sans-serif !important; position:relative; text-decoration:none; text-shadow:0 0 0 transparent !important;">' + INSTALL_LABEL + '</a></td> ' +
'						<td style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) -13px 0 no-repeat; height:30px; overflow:hidden; padding:0; width:17px; vertical-align:top;"></td> '+
'					</tr> '+
'				</table> '+
'			</td> '+
'		</tr> '+
'	</table> '+
'   <img src="%STATS_IMG_SRC%" />' +
'</div>';

var USER_TOOLBAR = ''+
'<style type="text/css"> html {margin-top:30px;}</style>'+
'<div id="metabar_headcrab" class="sitebar" style="background:%METABAR_BG_COLOR%; display:block; height:30px; font:12px/27px \'Trebuchet MS\', sans-serif !important; left:0;  min-width:600px; position:absolute; overflow:hidden; right:0; top:0; width:100%; z-index:10000;"> '+
'	<div style="background:url(http://%HOST%/static/i/sitebar-sprite.png) 0 -60px repeat-x; bottom:0; display:block; height:30px; left:0; margin:0; padding:0; position:absolute; top:0; width:100%; z-index:99;"></div> '+
'	<a href="#" onclick="removeHeadCrab(true); return false;" style="background:url(http://%HOST%/static/i/sitebar-sprite.png) 0 -93px no-repeat; display:block; height:7px; margin:0; overflow:hidden; padding:0; position:absolute; right:12px; text-indent:-9999px; top:12px; width:7px; z-index:100;" title="Закрыть">Закрыть</a> '+
'	<table cellpadding="0" cellspacing="0" style="height:30px; float:right; font:0.99em/235% \'Trebuchet MS\', sans-serif !important; margin:0 23px 0 0; overflow:hidden; padding:0; position:relative; text-shadow:0 0 0 transparent !important; white-space:nowrap; z-index:100;"> '+
'		<tr> '+
'			<td style="padding-top:0px; text-align:right; vertical-align:top;"><span style="color:%METABAR_TEXT_COLOR% !important; font:normal 0.99em/235% \'Trebuchet MS\', sans-serif !important; margin:0; padding:0; text-shadow:0 0 0 transparent !important;">' + DESCRIPTION_LABEL +'</span></td> '+
'			<td style="vertical-align:top;"> '+
'				<table cellpadding="0" cellspacing="0" style="color:%METABAR_TEXT_COLOR% !important; cursor:pointer; float:right; font:italic 0.99em/235% \'Trebuchet MS\', sans-serif !important; height:30px; margin:0 2px 0 11px; min-width:100px; overflow:hidden; padding:0; position:relative; vertical-align:top; white-space:nowrap; z-index:100;"> '+
'					<tr> '+
'						<td style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) 0 0 no-repeat; height:30px; overflow:hidden; padding:0; width:13px; vertical-align:top;"></td> '+
'						<td  style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) 0 -33px repeat-x; height:30px; overflow:hidden; padding:0 5px; text-align:center; vertical-align:top;"><a target="_top" href="%DOWNLOAD_URL%" class="install_button" style="color:%METABAR_TEXT_COLOR% !important; font:italic 0.99em/235% \'Trebuchet MS\', sans-serif !important; position:relative; text-decoration:none; text-shadow:0 0 0 transparent !important;">' + INSTALL_LABEL + '</a></td> ' +
'						<td style="background:url(http://%HOST%/static/i/sitebar-sprite2.png) -13px 0 no-repeat; height:30px; overflow:hidden; padding:0; width:17px; vertical-align:top;"></td> '+
'					</tr> '+
'				</table> '+
'			</td> '+
'		</tr> '+
'	</table> '+
'   <img 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 headcrab = document.getElementById('metabar_headcrab');
    if (headcrab.style.setAttribute) {
        headcrab.style.setAttribute('cssText', 'display: none');
    }
    else {
        headcrab.setAttribute('style', 'display: none');
    }

}
try {
    METABAR_TOOLBAR_ID = METABAR_TOOLBAR_ID || null;
    METABAR_DOWNLOAD_URL = METABAR_DOWNLOAD_URL || "http://metabar.ru/toolbar/download/197/";
    
    METABAR_DOWNLOAD_URL = METABAR_DOWNLOAD_URL + '?source=sitebar';

    var IS_IE = /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
    var IS_FF = /Firefox[\/\s](\d+\.\d+)/i.test(navigator.userAgent);

    var TOOLBAR = '';
    if (USER_DEFINED_COLORS) {
        TOOLBAR = USER_TOOLBAR;
    } else {
        if (IS_IE) {
            TOOLBAR = IE_TOOLBAR;
        }
        
        if (IS_FF) {
            TOOLBAR = FF_TOOLBAR;
        }
    }
    TOOLBAR = TOOLBAR.replace('%DOWNLOAD_URL%', METABAR_DOWNLOAD_URL);
    while (TOOLBAR.search('%METABAR_BG_COLOR%')!=-1) {
        TOOLBAR = TOOLBAR.replace('%METABAR_BG_COLOR%', window.METABAR_BG_COLOR || '#b7d7fe');
    }
    while (TOOLBAR.search('%METABAR_TEXT_COLOR%')!=-1) {
        TOOLBAR = TOOLBAR.replace('%METABAR_TEXT_COLOR%', window.METABAR_TEXT_COLOR || '#222324');
    }
    
    while (TOOLBAR.search('%HOST%')!=-1) {
        TOOLBAR = TOOLBAR.replace('%HOST%', window.HOST);
    }

    var STATS_HOST = 'http://stats.' + HOST;
    while (TOOLBAR.search('%STATS_IMG_SRC%')!=-1) {
        TOOLBAR = TOOLBAR.replace('%STATS_IMG_SRC%', STATS_HOST + '/blank.gif?toolbar_id=' + METABAR_TOOLBAR_ID);
    }

    if ((IS_IE || IS_FF) && !getCookie('hide_metabar_headcrab') ) {
        document.write(TOOLBAR);
    }
} 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);
    }
};

