if (typeof(BCNTRY) === 'undefined') { BCNTRY = {}; }

BCNTRY.setXsellMaskDisplay = function (state) {
    if (state === 'off') {
        yd.setStyle($('extra'), 'z-index', 0);
	yd.setStyle($('extra_item'), 'z-index', 200);
        yd.setStyle($('extra_item_mask'), 'display', 'none');
    }
    else {
        yd.setStyle($('extra'), 'z-index', -200);
	yd.setStyle($('extra_item'), 'z-index', -100);
        yd.setStyle($('extra_item_mask'), 'display', 'block');
    }
};


BCNTRY.xSellItem = function (sku, xsell_id, is_conveyable) {
    this.sku = sku;
    this.sku_class = sku.split('-')[0];
    this.xsell_id = xsell_id;
    
    this.addToCart = function () {
        yc.asyncRequest(
            'POST',
            '/' + BCNTRY.site.catalog + '/cart/add.html',
            this.callback,
            'item_code=' + this.sku_class +
            '&mv_order_item=' + this.sku_class +
            '&mv_sku=' + this.sku +
            '&xsell_variant_id=' + this.xsell_id +
            '&is_conveyable=1' +
            '&max_quantity=1' +
            '&mv_form_profile=odat_price_fix' +
            '&mv_javascript=1' +
            '&mv_order_group=1' +
            '&mv_order_quantity=1' +
            '&mv_todo=refresh'
        );
    };

    this.callback = {
        success: BCNTRY.xSellSuccess,
        failure: BCNTRY.xSellFailure,
        timeout: 15000
    };

    this.addListener = function ( element, instance ) {
        ye.addListener(element, "click", function () {
            instance.addToCart();
        });
    };

    return this;
};

BCNTRY.xSellSuccess = function () {
    redraw_all();
    document.body.style.cursor = 'default';
    BCNTRY.setXsellMaskDisplay('on');
};

BCNTRY.xSellFailure = function () {
    alert('There was a problem adding the extra item to your cart. Please try again.');
};

BCNTRY.getCartItemCount = function () {
    var cart_items = $('line_items');
    var item_count = 0;
    for (var row in cart_items.rows) {
        if (row === '0') { continue; }
        if (typeof cart_items.rows[row].id !== 'undefined') {
            if (cart_items.rows[row].id.match('item_row_')) {
                item_count++;
            }
        }
    }
    return item_count;
};

BCNTRY.getCartXsellItemCount = function () {
    return yd.getElementsByClassName('xsell_item_row').length;
};

BCNTRY.shouldRemoveXsell = function () {
    if (BCNTRY.getCartItemCount() === 2 && BCNTRY.getCartXsellItemCount() === 1) { 
        return 1;
    }
    else {
        return 0;
    }
};

BCNTRY.removeFromCartXsell = function (item_id, sub_item_id, code) {
    if (BCNTRY.shouldRemoveXsell()) {
        BCNTRY.customConfirmInstance =  new BCNTRY.customConfirm(
            'remove_item_popup',
            function () {
                remove_from_cart(item_id, sub_item_id, code);
                yd.setStyle('extra', 'display', 'none');
            },
            function () {
                BCNTRY.restoreSavedIndex(item_id, sub_item_id);
                return false;
            }
        );
        BCNTRY.customConfirmInstance.toggleDisplay();
    }
    else {
        if (BCNTRY.getCartItemCount() === 1 && BCNTRY.getCartXsellItemCount() === 0) {
            yd.setStyle('extra', 'display', 'none');
        }
        remove_from_cart(item_id, sub_item_id, code);
    }
};

BCNTRY.recalculateItem = function (item_id, subitem_id, row_qty, item_sku) {
    if ($(row_qty).value === '0') {
        BCNTRY.removeFromCartXsell(item_id, subitem_id, item_sku);
    }
    else {
        recalculate_item(item_id, subitem_id, row_qty, item_sku);
    }
};

BCNTRY.customConfirm = function ( confirmElm, ycallback, ncallback ) {
    this.decision = '';
    this.yesCallback = ycallback;
    this.noCallback  = ncallback;

    this.toggleDisplay = function () {
        if (yd.getStyle(confirmElm, 'display') === 'block') {
            yd.setStyle(confirmElm, 'display', 'none');
        }
        else if (yd.getStyle(confirmElm, 'display') === 'none') {
            yd.setStyle(confirmElm, 'display', 'block');
        }
        else {
            return false;
        }
    };

    this.setDecision = function (decision) {
        if (decision === 'yes') {
            this.decision = 'yes';
            this.toggleDisplay();
            this.yesCallback();
            return true;
        }
        else {
            this.decision = 'no';
            this.toggleDisplay();
            this.noCallback();
            return false;
        }
    };

    return this;
};

BCNTRY.saveSelectedIndex = function (item_id, sub_item_id) {
    var select =$('quantity_'+item_id+'_'+sub_item_id); 
    select.prevIndex = select.selectedIndex;
};
BCNTRY.restoreSavedIndex = function (item_id, sub_item_id) {
    var select =$('quantity_'+item_id+'_'+sub_item_id); 
    select.selectedIndex = select.prevIndex;
};
