/**
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 * We offer the best and most useful modules PrestaShop and modifications for your online store.
 *
 * @author    knowband.com <support@knowband.com>
 * @copyright 2017 Knowband
 * @license   see file: LICENSE.txt
 * @category  PrestaShop Module
 *
 *
 * Description
 *
 * Updates quantity in the cart
 */

$(document).ready(function() {
    $('.back-to-top').click(function(){
        $("html, body").animate({ scrollTop: 0 }, 800);
        return false;
    });
    
    
    if ($(selector_pagination).length != 0) {
        if (version == 6 && ismobile==0) {
            if (layout_type == 1) {
                if($.totalStorage('display')!=='list') {
                    $.totalStorage('display', 'list');
                    location.reload();
                }
            } else {
                if($.totalStorage('display')!=='grid') {
                    $.totalStorage('display', 'grid');
                    location.reload();
                }
            }
        }
        $(".back-to-top").mouseover(function() {
            var top_bg_color = ColorLuminance(background_color_top_link, -0.2);
            $(this).css("background", 'url(' + image_url + 'top-link.png) 50% 43% no-repeat ' + top_bg_color);
        });
        $(".back-to-top").mouseout(function() {
            $(this).css("background", 'url(' + image_url + 'top-link.png) 50% 43% no-repeat ' + background_color_top_link);
        });
        endlessScroll(0, true, 100);
    }
});

function endlessScroll(pageno, trigger_event, scroll) {
    var endlessscrollias = jQuery.ias({
        item: selector_item,
        container: selector_container,
        next: selector_next,
        pagination: selector_pagination,
        negativeMargin: 150
    });
    endlessscrollias.extension(new IASPagingExtension());
    if (display_loading_message == 1) {
        endlessscrollias.extension(new IASSpinnerExtension({
            src: image_url + 'loader.gif',
            html: "<div class='ias-spinner' style='text-align: center;'><img style='display:inline' src='{src}'/></div><div class='ias-spinner' style='text-align:center'>" + loading_message + "</div>",
        }));
    }
    if (display_end_message == 1) {
        endlessscrollias.extension(new IASNoneLeftExtension({
            text: end_page_message,
            html: '<div class="ias-noneleft message-box" style="text-align: center;background-color:' + background_color + ';color:' + text_color + ';border-color:' + border_color + '">{text}</div>'
        }));
    }
    if (trigger_event) {
        if (scroll_type == 1) {
            endlessscrollias.extension(new IASTriggerExtension({
                text: load_more_link_page,
                html: '<div  class="ias-trigger ias-trigger-next vss-more-products" style="display: block;"><div class="inner-text">{text}</div><div class="vss-down-icon"></div></div>',
                offset: load_more_link_frequency,
            }));
        }
    }
    endlessscrollias.getNextUrl = function(container) {
        if (!container) {
            container = endlessscrollias.$container;
        }
        var nexturl = $(endlessscrollias.nextSelector, container).find('a').attr('href');
        if (typeof nexturl !== "undefined") {
            nexturl = nexturl.replace("#/page-", "?p=");
            if (window.location.protocol == 'https:') {
                nexturl = nexturl.replace('http:', window.location.protocol);
            } else {
                nexturl = nexturl.replace('https:', window.location.protocol);
            }
        }
        return nexturl;
    };
    var amountScrolled = 600;
    $(window).scroll(function() {
        if ($(window).scrollTop() > amountScrolled) {
            $('a.back-to-top').fadeIn('slow');
        } else {
            $('a.back-to-top').fadeOut('slow');
        }
    });
}

$(document).ajaxComplete(function(event, xhr, settings) {
    if (typeof (settings.url) != "undefined") {
            if (removeURLParameter(settings.url, 'p') != undefined && version == 6 && ismobile==0) {
                if ($.totalStorage('display') == 'list') {
                    $('ul.product_list').removeClass('grid').addClass('list row');
                    $('.product_list > li').removeClass('col-xs-12 col-sm-6 col-md-4').addClass('col-xs-12');
                    $('.product_list > li').each(function(index, element) {
                        var html = '';
                        html = '<div class="product-container"><div class="row">';
                        html += '<div class="left-block col-xs-4 col-sm-5 col-md-4">' + $(element).find('.left-block').html() + '</div>';
                        html += '<div class="center-block col-xs-4 col-sm-7 col-md-4">';
                        html += '<div class="product-flags">' + $(element).find('.product-flags').html() + '</div>';
                        html += '<h5 itemprop="name">' + $(element).find('h5').html() + '</h5>';
                        var hookReviews = $(element).find('.hook-reviews');
                        if (hookReviews.length) {
                            html += hookReviews.clone().wrap('<div>').parent().html();
                        }
                        html += '<p class="product-desc">' + $(element).find('.product-desc').html() + '</p>';
                        var colorList = $(element).find('.color-list-container').html();
                        if (colorList != null) {
                            html += '<div class="color-list-container">' + colorList + '</div>';
                        }
                        var availability = $(element).find('.availability').html();	// check : catalog mode is enabled
                        if (availability != null) {
                            html += '<span class="availability">' + availability + '</span>';
                        }
                        html += '</div>';
                        html += '<div class="right-block col-xs-4 col-sm-12 col-md-4"><div class="right-block-content row">';
                        var price = $(element).find('.content_price').html();       // check : catalog mode is enabled
                        if (price != null) {
                            html += '<div class="content_price col-xs-5 col-md-12">' + price + '</div>';
                        }
                        html += '<div class="button-container col-xs-7 col-md-12">' + $(element).find('.button-container').html() + '</div>';
                        html += '<div class="functional-buttons clearfix col-sm-12">' + $(element).find('.functional-buttons').html() + '</div>';
                        html += '</div>';
                        html += '</div></div>';
                        $(element).html(html);
                    });
                    $('.display').find('li#list').addClass('selected');
                    $('.display').find('li#grid').removeAttr('class');
                    $.totalStorage('display', 'list');
                }
            }
    }
});

function removeURLParameter(url, parameter) {
    //prefer to use l.search if you have a location/link object
    var urlparts = url.split('?');
    if (urlparts.length >= 2) {

        var prefix = encodeURIComponent(parameter) + '=';
        var pars = urlparts[1].split(/[&;]/g);

        //reverse iteration as may be destructive
        for (var i = pars.length; i-- > 0; ) {
            //idiom for string.startsWith
            if (pars[i].lastIndexOf(prefix, 0) !== -1) {
                return pars[i];
            }
        }
    }
}
function ColorLuminance(hex, lum) {

    // validate hex string
    hex = String(hex).replace(/[^0-9a-f]/gi, '');
    if (hex.length < 6) {
        hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
    }
    lum = lum || 0;

    // convert to decimal and change luminosity
    var rgb = "#", c, i;
    for (i = 0; i < 3; i++) {
        c = parseInt(hex.substr(i * 2, 2), 16);
        c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
        rgb += ("00" + c).substr(c.length);
    }

    return rgb;
}