Portfolio

jQuery.noConflict();

jQuery(document).ready(function($){

    mixitupCall();
    
    function mixitupCall() {

        $('#portfolio').mixitup({
            targetSelector: '.item',
            transitionSpeed: 450,
            onMixLoad: function(state){
                changeRow(state);
            },
            onMixEnd: function(state){
                changeRow(state);
            }
        });
    }

    function changeRow(state) {
        var $per_row = 3;
        var $count = 0;
        var $row = 0;
        $(state.origOrder).each(function (key, value) {
            $current = $(this);
            $current.attr('class', $current.attr('class').replace(/row-.*/, ''));
            if($current.is(':visible')) {
                if($count % $per_row == 0){
                    $row++
                }
                $count++;
                $current.addClass('row-'+$row);
                $current.attr('rel', $row);
            }
        });
    }

    var portfolio_top = $('#header').height();

    $(".single-portfolio a").live('click', function (e) {
        e.preventDefault();

        var $single_portfolio = $(this).parent();
        var row = $single_portfolio.attr('rel');
        var post_id = $single_portfolio.attr('data-post_id');

        $(".portfolio-description").remove();

        data = {
            action:'portfolio',
            post_id: post_id        }

        $.ajax({
            method:"POST",
            url:ajax_url,
            data: data,
            success: function (response) {
                var html = '<div class="col-sm-12 portfolio-description" style="display: none">'+response+'</div>';

                $( ".row-"+row).last().after(html);
                $(".portfolio-description").slideDown("slow");

                if($(".our-work-section-portfolio .portfolio-description").length > 1){
                    $(".our-work-section-portfolio .portfolio-description").not(":last").remove();
                }

                customScrollTo(jQuery(".portfolio-description").offset().top - portfolio_top, 500)
            }
        });
    });


    $(".portfolio-close").live('click', function (e) {
        e.preventDefault();
        var post_id = $(this).attr('rel');
        customScrollTo(jQuery(".portfolio-"+post_id).offset().top - portfolio_top, 500);
        $(".portfolio-description").slideUp("slow", function () {
            $(".portfolio-description").remove();
        });
    });

    $('.portfolio-more').click(function () {
        
        var current_page = parseInt($(".current_page").val());
        current_page = current_page + 1;
        var total_page = parseInt($(".total_page").val());
        var item_per_page = $(".item_per_page").val();
        var start = (current_page - 1) * item_per_page;

        $.ajax({
            method : "POST",
            url : ajax_url,
            data : {
                action : 'more_work',
                current_page : current_page,
                limit : item_per_page,
                start : start            },
            success : function (response) {
                $("#portfolio").append(response);
                $(".current_page").val(current_page);
                mixitupCall();

                if(total_page <= current_page){
                    $('.portfolio-more').remove();
                }
            }
        })
    });
});