
var bibleAjaxPrototype = Class.create();


bibleAjaxPrototype.prototype =
{
    book : 0,
    navigatonBook : 0,
    chapter : 0,
    timer : null,

    chapterCount : $w('50 40 27 36 34 24 21 4 31 24 22 25 29 36 10 13 10 42 150 31 12 8 66 52 5 48 12 14 3 9 1 4 7 3 3 3 2 14 4 28 16 24 21 28 5 5 3 5 1 1 1 16 16 13 6 6 4 4 5 3 6 4 3 1 13 22'),

    initialize : function()
    {
    },

    hideContent : function()
    {
        if ($('x'))
        {
            $('x').hide();
        }
        return false;
    },

    hideSubmenu : function()
    {
        clearInterval(this.timer)

        if($('submenu'))
        {
            $('submenu').parentNode.removeChild($('submenu'))
        }
    },

    showContent : function()
    {
        this.hideSubmenu()

        $('x').style.zIndex = -100;
        $('x').show()

        this.getWindowDimensions()
        $('x').style.left = ((this.widthPlusScrollbar - $('x').clientWidth) / 2) + "px"
        $('x').style.top = ((this.heightPlusScrollbar - $('x').clientHeight) / 2) + document.body.scrollTop +"px"
        $('x').style.zIndex = 100;
        $('x').hide()


        Effect.Appear('x')
    },

    loadChapter : function(tmp_book, tmp_chapter)
    {
        var tmp_book = tmp_book > 0  ? tmp_book : 1
        var tmp_chapter = tmp_chapter > 0 ? tmp_chapter : 1

        bibleAjax.hideSubmenu()
        bibleAjax.hideContent()

        if($('navigation'))
        {
            if(this.navigatonBook != tmp_book)
            {
                $('navigation').hide();
            }
        }


        new Ajax.Updater('read', 'bibleDispatcher.php',
        {
            parameters: {
                chapter: tmp_chapter,
                book: tmp_book
            },

            onComplete:  function()
            {
                bibleAjax.book = tmp_book
                bibleAjax.chapter = tmp_chapter
                bibleAjax.showChapterNavigation()

                $$(".adjustableTable").each(function(e) {
                    e.style.width = $('read').offsetWidth - 120 + 'px';
                })
            }
        });
    },

    showChapterNavigation :  function()
    {
        if ($('navigation'))
        {
            if(this.navigatonBook != this.book)
            {
                this.navigatonBook = this.book
                for(i = $('navigation').childNodes.length - 1; i>=0; i--)
                {
                    $('navigation').removeChild($('navigation').childNodes[i]);
                }

                cc = this.chapterCount[this.book - 1]
                if(cc == 1)
                {
                    $('navigation').innerHTML = 'В этой книге всего одна единственная глава'
                }
                else if (cc < 15)
                {
                    $R(1, parseInt(cc) + 1, true).each(function(value)
                    {
                        bibleAjax.addNewMark(value)
                    })
                }
                else
                {
                    $R(1, parseInt(cc) + 1, true).each(function(value)
                    {
                        if (value == 1 || value % 10 == 0)
                        {
                            if(cc == value)
                            {
                                bibleAjax.addNewMark(value)
                            }
                            else
                            {
                                bibleAjax.addNewMarkMenu(value)
                            }
                        }
                    })
                }
            }
            $('navigation').show()
        }
    },

    addNewMark : function(text,target)
    {
        if(!target)
        {
            target = 'navigation'
        }
        var t = document.createTextNode("" + text)
        var a = document.createElement("a")
        a.href = 'javascript:bibleAjax.loadChapter('+this.book+','+parseInt(text)+')';
        a.style.position = ''
        a.appendChild(t)

        $(target).appendChild(a)
        $(target).appendChild(document.createTextNode(" "))
    },

    addNewMarkMenu : function(text,target)
    {
        if(!target)
        {
            target = 'navigation'
        }

        var t = document.createTextNode("" + text + ">")
        var a = document.createElement("a")

        var id = "" + text
        a.id = id
        a.href = '#';
        a.style.position = ''

        a.onmouseover = bibleAjax.showMenu;
        a.onmouseout = function() {
            bibleAjax.timer = setInterval('bibleAjax.hideSubmenu()', 2000);
        };
        a.appendChild(t)

        $(target).appendChild(document.createTextNode(" "))
        $(target).appendChild(a)
        $(target).appendChild(document.createTextNode(" "))
    },

    showMenu : function()
    {
        bibleAjax.hideSubmenu();

        var d = document.createElement("div")
        d.id = "submenu"
        d.style.position = 'absolute'
        d.style.top = bibleAjax.getGlobalOffsetTop(this) + this.offsetHeight +  'px'
        d.style.left = bibleAjax.getGlobalOffsetLeft(this) + 'px'
        $(d).hide()
        d.onmouseover = function() {
            if(bibleAjax.timer)
            {
                clearInterval(bibleAjax.timer)
            }
        };

        d.onmouseout = function() {
            bibleAjax.timer = setInterval('bibleAjax.hideSubmenu()', 2000);
        }

        document.body.appendChild(d)

        var i = parseInt(this.id)
        var cc = parseInt(bibleAjax.chapterCount[bibleAjax.book - 1])

        $R(i, i+10, true).each(function(value){
            if( value <= cc)
            {
                bibleAjax.addNewMark(value, d.id)
            }
        });

        $(d).show()
    },

    getGlobalOffsetLeft : function (e)
    {
        var thisLeft = 0;
        if (e && e.x > 0)
        {
            thisLeft = e.x;
        }
        else if (e.offsetParent)
        {
            thisLeft = e.offsetLeft
            while (e.offsetParent)
            {
                e = e.offsetParent
                if (e.offsetLeft > 0)
                {
                    thisLeft += e.offsetLeft
                }
            }
        }
        return thisLeft;
    },

    getGlobalOffsetTop : function (e)
    {
        var thisTop = 0;
        if (e.y > 0)
        {
            thisTop = e.y;
        }
        else if (e.offsetParent)
        {
            thisTop = e.offsetTop
            while (e.offsetParent)
            {
                e = e.offsetParent
                if (e.offsetTop > 0)
                {
                    thisTop += e.offsetTop
                }
            }
        }
        return thisTop;
    },

    getWindowDimensions : function()
    {
        if (typeof window.innerWidth != "undefined")
        {
            this.widthPlusScrollbar = window.innerWidth ;
            this.heightPlusScrollbar = window.innerHeight ;
        }
        else if (document.documentElement && typeof document.documentElement.offsetWidth != "undefined" && document.documentElement.offsetWidth != 0)
        {
            this.widthPlusScrollbar = document.documentElement.offsetWidth ;
            this.heightPlusScrollbar = document.documentElement.offsetHeight ;
        }
        else if (document.body && typeof document.body.offsetWidth != "undefined")
        {
            this.widthPlusScrollbar = document.body.offsetWidth ;
            this.heightPlusScrollbar = document.body.offsetHeight ;
        }
    }

};

bibleAjax = new bibleAjaxPrototype();

if (window.opera)
{
    window.addEventListener("resize" , bibleAjax.loadChapter(1,1), false) ;
}
else if(window.navigator.userAgent.match(/MSIE/))
{
    window.onload = bibleAjax.loadChapter
}
else
{
    window.onload = bibleAjax.loadChapter
}

