$(document).ready(function() {
    $('.entry_container').css('width', function () {
        return ($(this).children('div').length - 1) * 760;
    });
    $('.lightbocks .arrow-left').click(function () {
        if ($(this).siblings('.entry_window').children('.entry_container').position().left < 0) {
            $(this).siblings('.entry_window').children('.entry_container').animate({'left' : '+=760'});
        } else {
            $(this).siblings('.entry_window').children('.entry_container').css('left', '-=760');
            $(this).siblings('.entry_window').find('.entry').last().insertBefore($(this).siblings('.entry_window').find('.entry').first());
            //$(this).siblings('.entry_window').find('.entry').last().remove();
            $(this).siblings('.entry_window').children('.entry_container').animate({'left' : '+=760'});
        }
        return false;
    });
    $('.lightbocks .arrow-right').click(function () {
        if ($(this).siblings('.entry_window').children('.entry_container').position().left > -($(this).siblings('.entry_window').children('.entry_container').width() - 760)) {
            $(this).siblings('.entry_window').children('.entry_container').animate({'left' : '-=760'});
        } else {
            $(this).siblings('.entry_window').children('.entry_container').css('left', '+=760');
            $(this).siblings('.entry_window').find('.entry').first().insertAfter($(this).siblings('.entry_window').find('.entry').last());
            //$(this).siblings('.entry_window').find('.entry').last().remove();
            $(this).siblings('.entry_window').children('.entry_container').animate({'left' : '-=760'});
        }
        return false;
    });
    $('.lightbocks .close').click(function () {
        $(this).parent().hide();
        return false;
    });

    $('.service_button, .eng_button, .request_account_link').click(function () {
        parentLightbocks = $('#' + $(this).attr('rel')).parents('.lightbocks');
        if (parentLightbocks.not(':visible')) {
            parentLightbocks.css('top', ($(window).height() - parentLightbocks.height()) / 2 + $(window).scrollTop());
            parentLightbocks.find('.entry_container').css('left', -($(this).index() * 760));
            parentLightbocks.show();
            parentLightbocks.children('.arrow').css('top', function () {
                return (parentLightbocks.find('.entry_container').height() / 2) - 16;
            });
        }
        return false;
    });
});

