$(document).ready(function() {

    document.createAttribute('header');
    document.createAttribute('blog');
    document.createAttribute('email');
    document.createAttribute('site');    

    $.easing.custom = function (x, t, b, c, d) {
        var s = 1.70158;
        if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
        return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
    }

    $('#pathslide').html('');
    $('#blog').hide();
    $('#email').hide();
    $('#site').hide();
    $('#comment').hide();
    $('#send').hide();
    $('#bricks').hide();
    $('.next1').hide();

    if($('div.slide').length>0) {
        $('#pathslide').html($('#slide1').attr('header'));
        if($('#slide1').attr('blog')!='') {
            $('#ablog').attr('href', $('#slide1').attr('blog'));
            $('#blog').show();
        }
        if($('#slide1').attr('email')!='') {
            $('#aemail').attr('href', 'mailto:'+$('#slide1').attr('email'));
            $('#email').show();
        }
        if($('#slide1').attr('site')!='') {
            $('#asite').attr('href', $('#slide1').attr('site'));
            $('#site').show();
        }
        var ofsarr=new Array(129, 117, 108, 97, 87, 77, 67, 53, 43, 31, 18, 10);
        var ofs=0;
        if($('div.slide').length<=12) ofs=ofsarr[$('div.slide').length];
        $('#bricks').attr('style', 'background-position: 0 0; width: '+(129-ofs)+'px; margin-left: '+(ofs)+'px');
        $('#bricks').show();
        $('#acomments').html('комментарии: '+$('#comment1 .comm').length);
        $('#comment').show();
        $('#send').show();
        $('.next1').show();
    //$('#asendlink').attr('href', 'pathtosite/'+'0');
    //$('#asendlink').html('pathtosite/'+'0');
    }

    $('.next2').hide();
    $('.prev2').hide();
    $('#pagenavsep').hide();
    if($('.slide2').length>0) {
        $('.next2').show();
    }
    $('#pagenavsep').hide();
    if($('.next2').is(':visible') && $('.prev2').is(':visible')) $('#pagenavsep').show();
    $('div.pages').scrollable({
        size: 1,
        clickable: false,
        onSeek: function() {
            $('.next2').show();
            $('.prev2').show();
            if($('.slide2').length==this.getPageIndex()+1) {
                $('.next2').hide();
            }
            if(0==this.getPageIndex()) {
                $('.prev2').hide();
            }
            $('#pagenavsep').hide();
            if($('.next2').is(':visible') && $('.prev2').is(':visible')) $('#pagenavsep').show();

            if($('.next2').is(':visible')) {
                shake();
            }

        },
        items: '.items2',
        prevPage: '.prev2',
        nextPage: '.next2',
        easing: 'swing',
        speed: 600,
        keyboard: false
    });

    $('div.scrollable').scrollable({
        size:1,
        clickable:false,
        onBeforeSeek: function() {
            // перелистываем фрейм на первую страницу
            var a=this.getPageIndex()+1;
            try {
                document.getElementById('iframe'+a).contentWindow.rollbackToStart();
            }
            catch(err) {};
        },
        onSeek: function() {
            $('#acomments').click(function(){
                var api=$('div.scrollable').scrollable({
                    //api: true
                    });
                var a=api.getPageIndex()+1;
                $('#comment'+a).fadeIn();
            })
            $('#asend').click(function(){
                var api=$('div.scrollable').scrollable({
                    //api: true
                    });
                var a=api.getPageIndex()+1;
                $('#sendform'+a).fadeIn();
            })

            var a=this.getPageIndex()+1;

            if(a == 12) {
                $('.next1').click(function(){
                    document.location.href=$('#bricks-next').attr('href');
                })
            }
            if(a == 1) {
                $('.prev1').click(function(){
                    document.location.href=$('#bricks-prev').attr('href');
                })
            }

            //$('#asendlink').attr('href', '/'+(a-1));
            //$('#asendlink').html(''+(a-1));

            $('#pathslide').html($('#slide'+a).attr('header'));
            if($('#slide'+a).attr('blog')!='') {
                $('#ablog').attr('href', $('#slide'+a).attr('blog'));
                $('#blog').show();
            } else {
                $('#blog').hide();
            }
            if($('#slide'+a).attr('email')!='') {
                $('#aemail').attr('href', 'mailto:'+$('#slide'+a).attr('email'));
                $('#email').show();
            } else {
                $('#email').hide();
            }
            if($('#slide'+a).attr('site')!='') {
                $('#asite').attr('href', $('#slide'+a).attr('site'));
                $('#site').show();
            } else {
                $('#site').hide();
            }
            $('#bricks').attr('style', 'background-position: 0 -'+(14*this.getPageIndex())+'px; width: '+(129-ofs)+'px; margin-left: '+(ofs)+'px');
            $('#acomments').html('комментарии: '+$('#comment'+a+' .comm').length);

            $('iframe').hide();
            $('iframe').show();

            try {
                document.getElementById('iframe'+a).contentWindow.shake();
            }
            catch(err) {};

            return false;
        },
        vertical: false,
        //        prevPage: '.prev1',
        //        nextPage: '.next1',
        easing: 'custom',
        speed: 1000
    });

    $('.prev1').click(function(){
        var api=$('div.scrollable').scrollable({
            //api: true
            });
        var pagenum=api.getPageIndex()-1;
        if(pagenum<0) pagenum=0;
        api.seekTo(pagenum);
        return false;
    })

    $('.next1').click(function(){
        var api=$('div.scrollable').scrollable({
            //api: true
            });
        var pagenum=api.getPageIndex()+1;
        if(pagenum>api.getPageAmount()) pagenum=api.getPageAmount();
        api.seekTo(pagenum);
        return false;
    })

    $('#acomments').click(function(){
        $('#comment1').fadeIn();
    })
    $('.closecomments').click(function(){
        $('.comments').fadeOut();
    })
    $('#asend').click(function(){
        $('#sendform1').fadeIn();
    })
    $('.closesendform').click(function(){
        $('.sendform').fadeOut();
    })

    $('#bricks a').attr('href', '#');

    $('#bricks a').click(function() {
        var api=$('div.scrollable').scrollable({
            //api: true
            });
        var pagenum=this.id.substring(5, this.id.length);
        api.seekTo(pagenum);
        return false;
    })

    if($('.next2').is(':visible')) {
        shake();
    }

    $('#showlist').click(function(){
        $('#list').fadeIn();
    })
    $('.closelist').click(function(){
        $('#list').fadeOut();
    })

});

function move_to(slidenum) {
    var api=$('div.scrollable').scrollable({
        //api: true
        });
    if(api!=undefined) api.seekTo(slidenum);
    return false;
}
