

function nofrag(href){
    var i = href.indexOf("#");
    return i == -1 ? href : href.substr(0,i)
}
  
function doScroll(){
    var y=0,el = document.getElementById(embed);
    while (el){
        y += el.offsetTop || 0;
        el = el.offsetParent;
    } 
    document.body.scrollTop = y;
}
      
function mwrite(){
    document.write('<'+'a hr'+'ef="ma'+'il'+'to:wm'+'aster-pubm'+'ail-2007' + String.fromCharCode(64) + 'rusbible.ru">wma'+'ster-pubm'+'ail-2007'+String.fromCharCode(64)+'rusbible.ru</a>');
}      


/*
    если сайт расположен на зеркале, пишем стили для показа (или скрытия) ссылок
    на главный сайт (rusbible.ru)
*/
if (window.location.host == 'rusbible.ru' || window.location.host == 'www.rusbible.ru' || window.location.href.indexOf("#notamirror") != -1)
    document.write('<style type="text/css">.mirror {display: none}</style>');
else
    document.write('<style type="text/css">.nomirror {display: none}</style>');

/*
    рисуем стили для скрытия возможностей, недоступных при выключенных скриптах
*/
document.write('<style type="text/css">.scriptcheck { display: none }</style>');
    
/*
    если страница показывается в окошке, используем подходящую таблицу стилей
*/

var embed;
var parloc = null, myloc = nofrag(location.href);
try {
if (self != parent)
    parloc=nofrag(parent.location.href);
} catch (e){}

if (parloc == myloc){
   embed = parent.BibleLinks.embed;
   BibleFilesRoot = parent.BibleFilesRoot;
}
else {
   embed = window.location.hash;
   if (embed.substring(0,1)=='#') embed = embed.substring(1);
   if (embed.indexOf("embed=") == 0) 
     embed = embed.substring(6);
   else
      embed = false;
}
if (embed)
    document.write('<link rel="stylesheet" type="text/css" href="'+BibleFilesRoot+'css/iframe.css" />');

//прокручиваемся к заданному элементу
function scrollToItem(el){
    document.body.scrollTop = $(el).offset().top;
}        
    
var curgl = false,curst="";    
if (window.location.hash && (m = /^\#?([0-9]+)([\:\.][0-9]+)?$/.exec(window.location.hash))){
    curgl = m[1];
    curst = m[2] ? m[2] : '';
    if (window.location.hash.indexOf(".")>-1){
        $(function(){
            scrollToItem(document.getElementById(curgl + ":" + curst.substr(1)));
        });
    }
}

if (curgl){
    document.write('<style type="text/css">'+"\r\n"+'.bar'+curgl+'{font-weight:bold}'+"\r\n"+'</style>');
    $(function(){
        $(".updch a").attr("href",function(){
            var href = this.href;
            if (m = /\.([0-9]+)\.html(#[^#]*)?/.exec(href))
                return this.href.replace(/\.([0-9]+)\.html(#[^#]*)?$/,'.'+curgl+'.html#'+curgl+curst);
            else 
                return this.href.replace(/\.html#[^#]*$/,'.html#'+curgl+curst);
            });
    });
}

//подсвечиваем блоки, на которые указывают внутренние ссылки
$(function(){

    $('a.inner').click( 
        function(){ 
            var y,pel,el,id,loc = nofrag(location.href);
            if (this.href.indexOf(loc + "#") == 0){
                id = '#'+this.href.substr(loc.length+1);
                pel = el = $(id).get(0);
                while (pel){
                    y += pel.offsetTop || 0;
                    pel = pel.offsetParent;
                }
                document.body.scrollTop = y - parseInt($(el).css("margin-top"));
                $(el).addClass('highlited');
                setTimeout("$('"+id+"').removeClass('highlited');",2000);
                return false;
            }
        } 
    );
    
    if (embed) setTimeout('doScroll()',10);
    
});     

//ходим по примечаниям, расположенным на других страницах
$(function(){
    $('a.prim').click(
        function(){
            var m;
            if (

                !(m = window.location.pathname.match(/(.*books\/[^.]+\.[^.]+\.)([^.]+)\.html$/))
             && !(m = window.location.pathname.match(/(.*sinodal\/[^.]+\.)([^.]+)\.html$/))

            ) return true;
        
            var id = $(this).attr("id"),href = $(this).attr("href");
            if (id.indexOf("primsrc") === 0)
                location.href = m[1] + "notes.html" + href.substr(href.indexOf("#"));
            else {
                if (window.location.href.indexOf("#" + id) != -1)
                    history.go(-1);
                else
                    alert("Возврат к исходному тексту примечания в многостраничном режиме не поддерживается");
            }
            return false;
            
        }
    );
});

if (location.protocol == "mk:"){
    document.write('<link rel="stylesheet" type="text/css" href="'+BibleFilesRoot+'css/chm.css" />');
    document.write('<script type="text/javascript" language="javascript" src="'+BibleFilesRoot+'scripts/chm.js"></script>');
}

