var errorfields = {};

var contact = {
    send: function(obj) {
        $('#widget-contact-form .form-control').removeClass('is-invalid');

        jQuery.post("/contact-send", jQuery(obj).serialize(), function (data) {
            if (data.error) {
                swal("", data.msg, "error");
                for(e in data.errors) {
                    $('#' + e).addClass('is-invalid');
                }
            } else {
                window.location = data.url;
            }
        }, "json");
    }
};
var basket = {
    add: function (obj) {
        jQuery.post("/basket-add", jQuery(obj).serialize(), function (data) {
            if (data.error) {
                swal("", data.msg, "error");
            }
            else {
                window.location.href = data.url;
            }
        }, "json");
    },
    modify: function (obj) {
        jQuery.post("/basket-modify", jQuery(obj).serialize(), function (data) {
            if (data.error) {
            }
            else {
                window.location.href = data.url;
            }
        }, "json");
    },
    deleteItem: function (product_id) {
        jQuery.post("/basket-delete-item", 'product_id=' + product_id, function (data) {
            if (data.error) {
            }
            else {
                window.location.href = data.url;
            }
        }, "json");
    },
    deleteAll: function () {
        jQuery.post("/basket-delete-all", '', function (data) {
            if (data.error) {
            }
            else {
                window.location.href = data.url;
            }
        }, "json");
    },
};

var wishlist = {
    add: function(pid) {
        jQuery.post("/wishlist-add", {product_id:pid}, function (data) {
            if (data.error) {
                swal("", data.msg, "error");
            } else {
                swal("", data.msg, "success");
            }
        }, "json");
    },
    deleteitem: function(pid) {
        jQuery.post("/wishlist-delete", {product_id:pid}, function (data) {
            if (data.error) {
            } else {
                window.location.reload();
            }
        }, "json");
    }
};

var comments = {
    send: function(obj) {
        jQuery.post("/comments-add", jQuery(obj).serialize(), function (data) {
            if (data.error) {
                swal("", data.msg, "error");
            } else {
                if( window.location.hash == '' ) {
                    window.location = window.location + '#comments';
                }
                window.location.reload();
            }
        }, "json");
    },
    delete: function(cid) {
        swal({
            title: "",
            text: "Biztos hogy törlöd ezt a véleményt?",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "Mégse",
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Igen, törlöm!",
            closeOnConfirm: false
        }, function() {
            jQuery.post('/comments-delete', {comment_id:cid}, function (data) {
                if( data.error ) {

                } else {
                    if( window.location.hash == '' ) {
                        window.location = window.location + '#comments';
                    }
                    window.location.reload();
                }
            });
        });

    }
};

var ww = {
    closeErrors: function () {
        jQuery('.errors').hide();
    }
};

let newsletter = {
    subscribe: function (f){
        $('.is-invalid').removeClass('is-invalid');
        $.post('/newslettersubscribe', $(f).serialize(), function(data){
            if( data.error ) {
                swal('', data.message, 'error');

                for(let i of data.invalid) {
                    $('#' + i).addClass('is-invalid');
                }

            } else {
                swal('', data.message, 'success');
                f.reset();
            }
        }, 'json');
    }
};

function send_request(f) {
    $('.has-error', f).removeClass('has-error');
    $('.help-block', f).remove();
    $.post('/request-send', $(f).serialize(), function(data){
        if( data.error ) {
            show_errors(data.errors);
        } else {
            swal('', data.msg, 'success');
            f.reset();
        }
    }, 'json');
}

function show_errors(d) {
    if( d ) {
        for(var i in d) {
            //$('#'+i).parents('form').addClass('was-validated');
            for(var j in d[i]) {
                $('#'+i).addClass('is-invalid');
                $('#'+i).parent().append('<span class="invalid-feedback">' + d[i][j] + '</span>');
                break;
            }
        }
    }
}

$(document).ready(function (){

    var lightBoxSettings = {
        buttons: [
            "zoom",
            //"share",
            //"slideShow",
            //"fullScreen",
            //"download",
            //"thumbs",
            "close"
        ],
        protect: true,
        infobar: false,
        loop: true,
        animationEffect: "fade",
        transitionEffect: "slide",
        lang: "de",
        hash: false,
        touch: {
            vertical: false
        }
    };

    $('[data-fancybox="gallery"]').fancybox(lightBoxSettings);
    $('a.lb').fancybox(lightBoxSettings);

    /*
    $.each(errorfields, function(i, val){
        $('#' + errorfields[i].field).addClass('error');
        $('#' + errorfields[i].field + '-error').html('<a class="south" href="#" title="' + errorfields[i].message + '"></a>').css('display', 'inline-block');
    });

    $('.south').tipsy({
        gravity: 's'
    });
    */

/*    $('nav#offmenu').mmenu({
        "classes": "mm-slide mm-light",
        "offCanvas": {
            "position": "right"
        }
    }).find(".mm-subopen").addClass( "mm-fullsubopen" );
    $('nav#offcat').mmenu({
        "classes": "mm-slide mm-light",
        "offCanvas": {
            "position": "left"
        }
    }).find(".mm-subopen").addClass( "mm-fullsubopen" );*/

});


$(window).load(function() {

});