

$(function() {

    var projectsGallery = new ProjectsGallery();

    $(window).resize(function(e) {
        projectsGallery.render();
    })

    $('.galleryProjectNext').on('click',function(e) { projectsGallery.next(e) });
    $('.galleryProjectPrev').on('click',function(e) { projectsGallery.prev(e) });

});


ProjectsGallery = function() {

    this.render();
    this.index = $('#galleryProjects .active').attr('data-id');
    this.length = $('#galleryProjects .image').length;
};

ProjectsGallery.prototype.render =  function() {
    var position = $('#galleryProjects .active').position().left;
    $('#galleryProjects').css('left',-position);

    if (this.index >= this.length-1) {
        $('.galleryProjectNext').addClass('inactive');
    }
    else {
        $('.galleryProjectNext').removeClass('inactive');

    }

    if (this.index <= 0) {
        $('.galleryProjectPrev').addClass('inactive');
    }
    else {
        $('.galleryProjectPrev').removeClass('inactive');

    }


};

ProjectsGallery.prototype.next =  function(e) {
    e.preventDefault();
    var self = this;
    if (this.index < this.length-1) {
        this.index++;
    }


    $('#galleryProjects .image').removeClass('active');
    $('#galleryProjects .image[data-id="'+this.index+'"]').addClass('active').one("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend",
        function(event) {
            self.render();


        });



};


ProjectsGallery.prototype.prev =  function(e) {
    e.preventDefault();
    var self = this;
    if (this.index > 0) {
        this.index--;
    }
    $('#galleryProjects .image').removeClass('active');
    $('#galleryProjects .image[data-id="'+this.index+'"]').addClass('active').one("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend",
        function(event) {
            self.render();


        });
};