var SmallGallery = {

    handle : null,
    lastImageId : 0,
    firstDisplayed: 1,
    arrowLeft : null,
    arrowRight : null,

    init : function() {
        if ($('.gallery_small').length && $('.gallery_small').children().length) {
            SmallGallery.handle = $('.gallery_small');
            SmallGallery.lastImageId = $('.gallery_small').children().length - 2;
            SmallGallery.arrowLeft = $('.gallery_small li.prev a');
            SmallGallery.arrowLeft.click(SmallGallery.movePrev);
            SmallGallery.arrowRight = $('.gallery_small li.next a');
            SmallGallery.arrowRight.click(SmallGallery.moveNext)
            SmallGallery.refresh();

        }
        $('#gallery-more').click(function(){
            $("#profile-nav a:last").click();
        });
    },

    moveNext : function(e) {
        $(e).stop();
        SmallGallery.handle.children(':lt('+(SmallGallery.firstDisplayed+3)+')').hide();
        SmallGallery.handle.children(':gt('+(SmallGallery.firstDisplayed+2)+'):lt(3)').show();
        SmallGallery.firstDisplayed += 3;
        SmallGallery.refresh();
        return false;
    },

    movePrev : function(e) {
        $(e).stop();
        SmallGallery.handle.children(':gt('+(SmallGallery.firstDisplayed-1)+')').hide();
        SmallGallery.handle.children(':gt('+(SmallGallery.firstDisplayed-4)+'):lt(3)').show();
        SmallGallery.firstDisplayed -= 3;
        SmallGallery.refresh();
        return false;
    },

    refresh: function() {
        if (!SmallGallery.haveLeftArrow()) {
            SmallGallery.handle.children(':first').css('visibility','hidden');
        } else {
            SmallGallery.handle.children(':first').css('visibility','visible');
        }
        if (!SmallGallery.haveRightArrow()) {
            SmallGallery.handle.children(':last').css('visibility','hidden');
        } else {
            SmallGallery.handle.children(':last').css('visibility','visible');
        }
        SmallGallery.handle.children(':last').show();
        SmallGallery.handle.children(':first').show();

    },

    haveLeftArrow: function() {
        return (SmallGallery.handle.children(':eq(1)').is(':hidden'));
    },

    haveRightArrow: function() {
        return (SmallGallery.handle.children(':eq(' + SmallGallery.lastImageId + ')').is(':hidden'));
    }
}

$(document).ready(function(){
    SmallGallery.init();
});
