var booking_cart = {
    'add': function(product_id, quantity) {
        $.ajax({
            url: 'index.php?route=checkout/booking_cart/add',
            type: 'post',
            data: 'product_id=' + product_id + '&quantity=1',
            dataType: 'json',
            beforeSend: function() {
                //$('#cart > button').button('loading');
            },
            complete: function() {
                //$('#cart > button').button('reset');
            },
            success: function(json) {
                $('.alert, .text-danger').remove();

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['error']) {
                    $('.notification .noti_content').html("<div class='text_wrap full_wrap'>"+json['error']+"</div>");
                    $('.notification').show();

                    //$('html, body').animate({ scrollTop: 0 }, 'slow');
                }
                if(json['warning']!=''){
                    $('.notification .noti_content').html("<div class='text_wrap full_wrap'>"+json['warning']+"</div>");
                    $('.notification').show();
                }
                if(json['show_plan']==1){
                    $.magnificPopup.open({
                        type: 'ajax',
                        items: {
                            src: 'index.php?route=account/pricing/ajax'
                        },
                    });
                }
                if (json['success']) {
                    $('.notification .noti_content').html("<div class='text_wrap full_wrap'>"+json['success']+"</div>");
                    $('.notification .noti_content').append('<div class="full_wrap btn_wrap"><a href="index.php?route=checkout/booking_cart" class="single_cart_btn">View Shopping Cart</a></div>');
                    $('.notification').show();

                    // Need to set timeout otherwise it wont update the total
                   // setTimeout(function () {
                        //$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
                    //}, 100);

                    //$('html, body').animate({ scrollTop: 0 }, 'slow');

                    //$('#cart > ul').load('index.php?route=common/cart/info ul li');
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    },
    'remove': function(product_id) {
        $.ajax({
            url: 'index.php?route=checkout/booking_cart/remove',
            type: 'post',
            data: 'product_id=' + product_id,
            dataType: 'json',
            beforeSend: function() {
                //$('#cart > button').button('loading');
            },
            complete: function() {
                //$('#cart > button').button('reset');
            },
            success: function(json) {
                // Need to set timeout otherwise it wont update the total
               window.location = 'index.php?route=checkout/booking_cart';
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }
}


var wishlist = {
    'add': function(product_id) {
        $.ajax({
            url: 'index.php?route=account/wishlist/add',
            type: 'post',
            data: 'product_id=' + product_id,
            dataType: 'json',
            success: function(json) {
                $('.alert').remove();

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['success']) {
                    $('.notification .noti_content').html("<div class='text_wrap full_wrap'>"+json['success']+"</div>");
                    $('.notification .noti_content').append('<div class="full_wrap btn_wrap"><a href="index.php?route=account/wishlist" class="single_cart_btn">View my wishlist</a></div>');
                    $('.notification').show();
                    //$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

                $('#wishlist-total span').html(json['total']);
                $('#wishlist-total').attr('title', json['total']);

               // $('html, body').animate({ scrollTop: 0 }, 'slow');
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    },
    'remove': function() {

    }
}

var customer_bookinglist = {
    'add': function(product_id) {
        $.ajax({
            url: 'index.php?route=account/customer_bookinglist/add',
            type: 'post',
            data: 'product_id=' + product_id,
            dataType: 'json',
            success: function(json) {
                $('.alert').remove();

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['success']) {
                    $('.notification .noti_content').html("<div class='text_wrap full_wrap'>"+json['success']+"</div>");
                    $('.notification .noti_content').append('<div class="full_wrap btn_wrap"><a href="index.php?route=account/customer_bookinglist" class="single_cart_btn">View my booking list</a></div>');
                    $('.notification').show();
                    //$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

                // $('html, body').animate({ scrollTop: 0 }, 'slow');
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    },
    'remove': function() {

    }
}

function getURLVar(key) {
    var value = [];

    var query = String(document.location).split('?');

    if (query[1]) {
        var part = query[1].split('&');

        for (i = 0; i < part.length; i++) {
            var data = part[i].split('=');

            if (data[0] && data[1]) {
                value[data[0]] = data[1];
            }
        }

        if (value[key]) {
            return value[key];
        } else {
            return '';
        }
    }
}

function init_search(){
    search_url = 'index.php?route=product/search';
    search_txt = $.trim(($('#search [name="search"]').val()));
    if(search_txt) {
        search_url += '&search='+search_txt;
    }
    /*if($('#top_cat_dp ul li a.active').length) {
        search_url += '&category_id='+$('#top_cat_dp ul li a.active').attr("data-id");
        search_url += '&sub_category=true';
    }
    if($('#top_age_dp ul li a.active').length) {
        search_url += '&filter='+$('#top_age_dp ul li a.active').attr("data-id");
    }*/

    window.location = search_url;
}


function equal_gallery_height(){
    if($('.gallery_ul').length){
        //if($(window).innerWidth()>480) {
            max_height = 0;
            $('.gallery_ul li').css("height", "");
            $('.gallery_ul li').each(function (index, element) {
                if ($(this).innerHeight() > max_height) {
                    max_height = $(this).innerHeight();
                }
            });
            if (max_height > 0) {
                $('.gallery_ul li').css("height", max_height + "px");
            }
        //}
    }
}

function copy_div_text(element_id) {
    /* Get the text field */
    var copyText = document.getElementById("ref_span");

    /* Select the text field */
    copyText.select();

    /* Copy the text inside the text field */
    document.execCommand("copy");

    /* Alert the copied text */
    alert("Copied the text: " + copyText.value);
}

/* Agree to Terms */
$(document).delegate('.agree', 'click', function(e) {
    e.preventDefault();

    $('#modal-agree').remove();

    var element = this;

    $.ajax({
        url: $(element).attr('href'),
        type: 'get',
        dataType: 'html',
        success: function(data) {
            html  = '<div id="modal-agree" class="modal">';
            html += '  <div class="modal-dialog">';
            html += '    <div class="modal-content">';
            html += '      <div class="modal-header">';
            html += '        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
            html += '        <h4 class="modal-title">' + $(element).text() + '</h4>';
            html += '      </div>';
            html += '      <div class="modal-body">' + data + '</div>';
            html += '    </div';
            html += '  </div>';
            html += '</div>';

            $('body').append(html);

            $('#modal-agree').modal('show');
        }
    });
});

$(window).load(function(){
    AOS.init({easing: 'ease-out-back'});
	equal_gallery_height();
});

$(document).ready(function(){



    $('[data-toggle="tooltip"]').tooltip();

    $('.submit_form_anchor').click(function(){
        $(this).parents("form").submit();
    });
    $('.trigger_enter_form input[type="text"],.trigger_enter_form input[type="password"]').keypress(function(evt_form_trigger){
        if(evt_form_trigger.keyCode==13){
            $(this).parents(".trigger_enter_form").submit();
        }
    });

    $('.notification .notification_close_btn,.notification .noti_overlay_wrap').click(function(){
        $('.notification').hide();
    });
    // Highlight any found errors
    $('.text-danger').each(function() {
        var element = $(this).parent().parent();

        if (element.hasClass('form-group')) {
            element.addClass('has-error');
        }
    });
    $('.dropdown-menu.single_select_dp>li>a').click(function(){
        if($(this).hasClass("active")){
            $(this).removeClass("active");
            data_title = $(this).parents(".dropdown").attr("data-title");
            $(this).parents(".dropdown").find(".dropdown-toggle .selected_dp_span").text(data_title);
        }else{
            $(this).parents(".dropdown-menu").find("a").removeClass("active");
            $(this).addClass("active");
            data_title = $(this).html();

            $(this).parents(".dropdown").find(".dropdown-toggle .selected_dp_span").text(data_title);
        }
    });

    $('.home_tabs_ul li').click(function(){
        if(!$(this).hasClass("active")){
            $('.home_tabs_ul li').removeClass("active");
            $(this).addClass("active");
            active_id = $(this).attr("data-id");
            $('.home_tab_content').hide();
            $('.home_tab_content[data-id="'+active_id+'"]').show();
        }
    });
    if($(window).innerWidth()<=786) {
        $('.main_menu_wrap .menu_ul > li.has_children > a').click(function () {
            if($(this).parents("li.has_children").find(".submenu_ul:visible").length){
                $(this).find(".submenu_open_icon").removeClass("active");
                $(this).parents("li.has_children").find(".submenu_ul").slideUp(600);
            }else{
                $(this).find(".submenu_open_icon").addClass("active");
                $(this).parents("li.has_children").find(".submenu_ul").slideDown(600);
            }


        });
    }

    $('.menu_bar_wrap').click(function(){
        if($('.main_menu_wrap .menu_ul:visible').length){
            $('.main_menu_wrap .menu_ul').fadeOut(600);
            $('.menu_bar_wrap').removeClass("active");
        }else{
            $('.main_menu_wrap .menu_ul').fadeIn(600);
            $('.menu_bar_wrap').addClass("active");
        }
    });

    $('.search_go_wrap').click(function(){
        init_search();
    });

    $('#search [name="search"]').keypress(function(evt_srch){
        if(evt_srch.keyCode==13){
            init_search();
        }
    });

    $('.filter_button').click(function(){
        $('.gallery_data_wrap .filter_wrap.overlay').fadeIn(600);
    });
    $('.filter_close_btn').click(function(){
        $('.gallery_data_wrap .filter_wrap.overlay').fadeOut(600);
    });

    $('#footer_carousel_wrap').owlCarousel({
        loop: true,
        navText: ["<i class='fa fa-angle-left testi_left_arrow'></i>", "<i class='fa fa-angle-right testi_right_arrow'></i>"],
        margin: 0,
        items: 1,
        smartSpeed: 800,
        autoplayTimeout: 6000,
        nav: true,
        dots: false,
        autoplay: true
    });


});