$(function() {

    var BYobj = BYobj || {};

    BYobj.flip = {

        showData: function() {
            console.log('foo')
        },
        createGallery: function() {
            var z = 0,
                    inAnimation = false;

            $('#pictures img,#pictures iframe').each(function() {
                z++;
                $(this).css('z-index', z);
            });

            function swapFirstLast(isFirst) {
                if (inAnimation) return false;
                else inAnimation = true;

                var processZindex, direction, newZindex, inDeCrease;

                if (isFirst) {
                    var processZindex = z,
                            direction = '-',
                            newZindex = 1,
                            inDeCrease = 1;
                }
                else {
                    var processZindex = 1,
                            direction = '',
                            newZindex = z,
                            inDeCrease = -1;
                }

                $('#pictures img,#pictures iframe').each(function() {
                    if ($(this).css('z-index') == processZindex) {
                        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() {
                            $(this).css('z-index', newZindex)
                                    .animate({ 'top' : '0' }, 'fast', function() {
                                inAnimation = false;
                            });
                        });
                    } else {
                        $(this).animate({ 'top' : '0' }, 'fast', function() {
                            $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease);
                        });
                    }
                });

                return false;
            }

            $('#next a').click(function() {
                return swapFirstLast(true);
                BYobj.flip.showData();
            });

            $('#prev a').click(function() {
                return swapFirstLast(false);
                BYobj.flip.showData();
            });


        }
    };
    BYobj.flip.createGallery();


}());

