<div style="display: none;">
    <div id="customDealSetPopup" class="popup-attributes">
        <input type="hidden" id="customDealCategory">
        <input type="hidden" id="customDealMenu">
        <input type="hidden" id="customDealMenuSelection">
        <input type="hidden" id="customDealCtop">
        <input type="hidden" id="customDealCmod">
        <div class="popup-header toolbar-header">
            <div class="popup-title"><span id="customDealSetPopupTitle"></span></div>
            <div class="popup-close-btn" onclick="zamanEposPopupcloser()"><span class="icon icon-cancel"></span></div>
        </div>
        <div class="popup-content">
            <div class="toppings-part">
                <div id="DealSetList"></div>
            </div>
            <div class="toppings-part">
                <div id="setAttributesToppingHead"></div>
                <div id="setItemAttributeList"></div>
            </div>
            <div class="modifier-part">
                <div id="setAttributesModifierHead"></div>
                <div id="setItemModifierList"></div>
            </div>
            <div class="selectedDealSetItemPlaceHolder"></div>
            <div class="selectedToppingPlaceHolder"></div>
            <div class="selectedModifierPlaceHolder"></div>
        </div>
        <div class="tab-group">
            <div class="tab-item zaman-epos-nav quantity-digit" onclick="addDealAtCart()"><span class="icon icon-basket"> Add To Cart</span></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function zamanCustomDealControllerPopup(categoryObject, menuObject, selectionObject, AllSets) {
        $.colorbox({
            inline: true,
            scrolling: false,
            href: "#customDealSetPopup",
            width: '40%',
            height: '400px',
            open: true,
            closeButton: false,
            transition: 'elastic',
            overlayClose: false,
            className: 'transparent-overlay',
            onOpen: function () {
                $('#customDealSetPopup .popup-content').css({'height': '320', 'min-height': '300px'});
                $('#DealSetList').html('');
                $('#setAttributesToppingHead').text('').removeClass('caption');
                $('#setItemAttributeList').html('');
                $('#setAttributesModifierHead').text('').removeClass('caption');
                $('#setItemModifierList').html('');
                $('.selectedDealSetItemPlaceHolder').html('');
                $('.selectedToppingPlaceHolder').html('');
                $('.selectedModifierPlaceHolder').html('');
                $('#attributesToppingHead').text('').removeClass('caption');
                $('#itemAttributeList').html('');
                $('#attributesModifierHead').text('').removeClass('caption');
                $('#itemModifierList').html('');
            },
            onLoad: function () {
                $('#customDealSetPopupTitle').text(!jQuery.isEmptyObject(selectionObject) ? selectionObject.title + '-' + menuObject.title : menuObject.title);
                $('#customDealCategory').val(categoryObject.id);
                $('#customDealMenu').val(menuObject.id);
                $('#customDealMenuSelection').val(!jQuery.isEmptyObject(selectionObject) ? selectionObject.id : '');
                $('#customDealCtop').val(!jQuery.isEmptyObject(selectionObject) ? selectionObject.ctop : menuObject.ctop);
                $('#customDealCmod').val(!jQuery.isEmptyObject(selectionObject) ? selectionObject.cmod : menuObject.ctop);
            },
            onComplete: function () {
                var ctop = $('#customDealCtop').val();
                var cmod = $('#customDealCmod').val();
                if (AllSets.length > 0) {
                    var DealSetList = '';
                    var idList = '<div class="set-ids-list">';
                    AllSets.forEach(function (aSet) {
                        DealSetList += '<div class="deal-set-category-tab tab-group ' + aSet.rel + '' + aSet.rel_with + '" id="setCategory-' + aSet.id + '" onclick="getCustomDealSetItem(\'' + aSet.id + '\',\'' + aSet.title + '\',\'' + aSet.rel + '' + aSet.rel_with + '\',\'' + aSet.min + '\',\'' + aSet.max + '\')">';
                        DealSetList += '<div class="tab-item zaman-epos-nav quantity-digit">' + aSet.title + '</div>';
                        DealSetList += '</div>';
                        idList += '<input type="hidden" id="setCurrentQty-' + aSet.id + '" value="1">';
                    });
                    $('#DealSetList').html(DealSetList);
                    idList += '</div>';
                    $(idList).insertAfter('#DealSetList');
                    if (Number(ctop) !== 2) {
                        AllSets.forEach(function (aSet) {
                            manageSetToppings(categoryObject, menuObject, selectionObject, aSet);
                        });
                    }
                    if (Number(cmod) !== 2) {
                        AllSets.forEach(function (aSet) {
                            manageSetModifier(categoryObject, menuObject, selectionObject, aSet);
                        });
                    }
                }
                if (Number(ctop) === 2) {
                    manageDealToppings(categoryObject, menuObject, selectionObject);
                }
                if (Number(cmod) === 2) {
                    manageDealModifier(categoryObject, menuObject, selectionObject);
                }
                //$('#colorbox').colorbox.resize();
                $('#colorbox').colorbox.resize({height: '400px'});
            },
            onCleanup: function () {
                $('#customDealSetPopupTitle').text('');
                $('#customDealCategory').val('');
                $('#customDealMenu').val('');
                $('#customDealMenuSelection').val('');
                $('#customDealCtop').val('');
                $('#customDealCmod').val('');
            },
            onClosed: function () {
                $('#DealSetList').html('');
                $('#setAttributesToppingHead').text('').removeClass('caption');
                $('#setItemAttributeList').html('');
                $('#setAttributesModifierHead').text('').removeClass('caption');
                $('#setItemModifierList').html('');
                $('.selectedDealSetItemPlaceHolder').html('');
                $('.selectedToppingPlaceHolder').html('');
                $('.selectedModifierPlaceHolder').html('');
            }
        });
    }
    function getCustomDealSetItem(id, title, rel, min, max) {
        if (rel.indexOf('mono') >= 0) {
            if ($('.set-wise-topping-head').length > 0) {
                $('.top-cap-rel-' + rel).not('#setWiseTopingHead-' + id).remove();
            }
            if ($('.has-set-topping').length > 0) {
                $('.top-group-rel-' + rel).not('#ToppingOfTheSet-' + id).remove();
            }
            if ($('.deal-set-category-tab').length > 1) {
                $('.' + rel).not('#setCategory-' + id).remove();
            }
        }
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_menusetitem, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_menuattrcategory, zamanEposDb.epos_modifiercategory], function () {
            zamanEposDb.epos_menusetitem.where({'set': id}).filter(function (anItem) {
                return anItem.status === '3';
            }).toArray().then(function (allSetItems) {
                if ($('#elementsForSet-' + id).length > 0) {
                    $('#elementsForSet-' + id).remove();
                } else {
                    var caption = Number(max) > 0 ? max + ' ' + '<?php echo $this->lang->line("item_select") ?>' + ' ' + title : '<?php echo $this->lang->line("item_select") ?>' + ' ' + title;
                    var html = '<table class="attr-category-element-table deal-set-elements table-bg" id="elementsForSet-' + id + '"><caption>' + caption + '</caption></table>';
                    $(html).insertAfter('#setCategory-' + id);
                    allSetItems.forEach(function (aSetItem) {
                        zamanEposDb.epos_menu.get({'id': aSetItem.menu}, function (itemMenu) {
                            zamanEposDb.epos_menuselection.get({'id': aSetItem.selection}, function (itemSelection) {
                                var itemName = typeof (itemSelection) !== 'undefined' ? itemSelection.title + '-' + itemMenu.title : itemMenu.title;
                                var itemHtml = '<tr id="setCategory-' + id + '-setItem-' + aSetItem.id + '" class="set-item-row"><td><span id="setItemTitle-' + aSetItem.id + '">' + itemName + '<span></td><td class="text-center"><span class="icon icon-minus btn btn-large btn-negative" onclick="decrementSetItemQty(\'' + aSetItem.id + '\',\'' + aSetItem.set + '\',\'' + max + '\')"></span><span class="qty-box" id="setItemQuantity-' + aSetItem.id + '">' + aSetItem.qty + '</span><span class="icon icon-plus btn btn-large btn-positive" onclick="incremenSetItemQty(\'' + aSetItem.id + '\',\'' + aSetItem.set + '\',\'' + max + '\')"></span></td><td class="text-right"><span class="btn btn-large btn-negative" onclick="listingSelectedSetItem(\'' + aSetItem.id + '\',\'' + aSetItem.set + '\',\'' + itemName + '\',\'' + title + '\',\'' + max + '\')">Add</span></td></tr>';
                                $(itemHtml).appendTo('#elementsForSet-' + id);
                                $('#colorbox').colorbox.resize({height: '400px'});
                                //$('#colorbox').colorbox.resize();
                            });
                        });
                    });
                }
                $('#colorbox').colorbox.resize({height: '400px'});
                //$('#colorbox').colorbox.resize();
            });
        });
        // $('.deal-set-elements').not('#elementsForSet-' + id).remove();
    }
    function incremenSetItemQty(id, category, max) {
        var categoryCurrentQty = parseInt(Number($('#setCurrentQty-' + category).val()));
        var attrCurrentQty = parseInt(Number($('#setItemQuantity-' + id).text()));
        if (parseInt(Number(max)) > 0 && categoryCurrentQty < parseInt(Number(max))) {
            $('#setItemQuantity-' + id).text(attrCurrentQty + 1);
            $('#setCurrentQty-' + category).val(categoryCurrentQty + 1);
        }
        if (parseInt(Number(max)) === 0) {
            $('#setItemQuantity-' + id).text(attrCurrentQty + 1);
            $('#setCurrentQty-' + category).val(categoryCurrentQty + 1);
        }
        categoryCurrentQty = parseInt(Number($('#setCurrentQty-' + category).val()));
        if (parseInt(Number(max)) > 0 && categoryCurrentQty === parseInt(Number(max))) {
            $('#elementsForSet-' + category + ' .set-item-row').not('#setCategory-' + category + '-setItem-' + id).hide();
        } else {
            $('#elementsForSet-' + category + ' .set-item-row').show();
        }
        //$('#colorbox').colorbox.resize();
        $('#colorbox').colorbox.resize({height: '400px'});

    }
    function decrementSetItemQty(id, category, max) {
        var categoryCurrentQty = Number($('#setCurrentQty-' + category).val());
        var attrCurrentQty = Number($('#setItemQuantity-' + id).text());
        if (attrCurrentQty > 1) {
            $('#setItemQuantity-' + id).text(attrCurrentQty - 1);
            $('#setCurrentQty-' + category).val(categoryCurrentQty - 1);
        }
        categoryCurrentQty = parseInt(Number($('#setCurrentQty-' + category).val()));
        attrCurrentQty = parseInt(Number($('#setItemQuantity-' + id).text()));
        if (parseInt(Number(max)) > 0 && categoryCurrentQty === parseInt(Number(max))) {
            $('#elementsForSet-' + category + ' .set-item-row').not('#setCategory-' + category + '-setItem-' + id).hide();
        } else {
            $('#elementsForSet-' + category + ' .set-item-row').show();
        }
        //$('#colorbox').colorbox.resize();
        $('#colorbox').colorbox.resize({height: '400px'});
    }
    function listingSelectedSetItem(id, cat, title, catTitle, maxSelect) {
        var setItemArray = zamanEposCart.listSetItem();
        var setItemTotalQty = 0;
        if (setItemArray.length > 0) {
            for (var i in setItemArray) {
                if (cat === setItemArray[i].set) {
                    setItemTotalQty += parseInt(Number(setItemArray[i].qty));
                } else {
                    continue;
                }
            }
        }
        var currentItemQty = parseInt(Number($('#setItemQuantity-' + id).text()));
        if (maxSelect > 0) {
            if ((parseInt(Number(setItemTotalQty)) + currentItemQty) <= maxSelect) {
                if (id) {
                    var setItem = new Array();
                    setItem.id = id;
                    setItem.title = title;
                    setItem.qty = currentItemQty;
                    setItem.set = cat;
                    setItem.setTitle = catTitle;
                    zamanEposCart.addSetItem(setItem);
                    setItemTotalQty += currentItemQty;
                }
            } else {
                warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('from') ?>' + catTitle + ', <?php echo $this->lang->line('max_select_msg') ?>' + maxSelect + '&nbsp;<?php echo $this->lang->line('item') ?>', '<?php echo $this->lang->line('ok') ?>');
            }
        } else {
            if (id) {
                var setItem = new Array();
                setItem.id = id;
                setItem.title = title;
                setItem.qty = currentItemQty;
                setItem.set = cat;
                setItem.setTitle = catTitle;
                zamanEposCart.addSetItem(setItem);
            }
        }
        $('#setCategory-' + cat + '-setItem-' + id).remove();
        if (maxSelect > 0) {
            if (parseInt(Number(setItemTotalQty)) >= maxSelect) {
                $('#elementsForSet-' + cat).remove();
                $('#setCurrentQty-' + cat).remove();
                $('#setCategory-' + cat).remove();
                if ($('#DealSetList').children().length <= 0) {
                    $('.set-ids-list').remove();
                }
                $('#colorbox').colorbox.resize({height: '400px'});
                //$('#colorbox').colorbox.resize();
            }
        }
        displaySelectedSetItem();
        $('#colorbox').colorbox.resize({height: '400px'});
        //$('#colorbox').colorbox.resize();
    }
    function displaySelectedSetItem() {
        var AttrArray = zamanEposCart.listSetItem();
        if (AttrArray.length > 0) {
            var html = '<table class="display-attr-category-element-table collection-bg">';
            html += '<div class="caption">Selected Set Items</div>';
            for (var i in AttrArray) {
                html += '<tr><td>' + AttrArray[i].qty + '&nbsp;&nbsp;&nbsp;&nbsp;' + AttrArray[i].title + '</td></tr>';
            }
            html += '</table>';
            $('.selectedDealSetItemPlaceHolder').html(html);
            $('#colorbox').colorbox.resize({height: '400px'});
            //$('#colorbox').colorbox.resize();
        } else {
            $('.selectedDealSetItemPlaceHolder').html('');
            $('#colorbox').colorbox.resize({height: '400px'});
            //$('#colorbox').colorbox.resize();
        }
    }
    function manageSetToppings(categoryObject, menuObject, selectionObject, aSet) {
        if (jQuery.isEmptyObject(selectionObject)) {
            zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
                return attrCat.category === categoryObject.id && attrCat.menu === menuObject.id && attrCat.selection === '0' && attrCat.set === aSet.id;
            }).toArray().then(function (attrObject) {
                if (attrObject.length > 0) {
                    var setWisetoppingList = '<div id="setWiseTopingHead-' + aSet.id + '" class="set-wise-topping-head caption top-cap-rel-' + aSet.rel + '' + aSet.rel_with + '">' + aSet.title + '-' + '<?php echo $this->lang->line('toppings') ?>' + '</div>';
                    $(setWisetoppingList).insertAfter('#setCategory-' + aSet.id);
                    var topItem = '<div class="has-set-topping top-group-rel-' + aSet.rel + '' + aSet.rel_with + '" id="ToppingOfTheSet-' + aSet.id + '">';
                    attrObject.forEach(function (anAttr) {
                        topItem += '<div class="tab-group ' + anAttr.rel + '' + anAttr.rel_with + '" id="ToppingCategory-' + anAttr.id + '" onclick="zamanEposItemToppings(\'' + anAttr.id + '\',\'' + anAttr.title + '\',\'' + anAttr.rel + '' + anAttr.rel_with + '\',\'' + anAttr.min + '\',\'' + anAttr.max + '\',\'' + anAttr.free + '\',\'' + Number(aSet.id) + '\')">';
                        topItem += '<div class="tab-item zaman-epos-nav quantity-digit">' + anAttr.title + '</div>';
                        topItem += '</div>';
                    });
                    topItem += '</div>';
                    $(topItem).insertAfter('#setWiseTopingHead-' + aSet.id);
                    $('#colorbox').colorbox.resize({height: '400px'});
                    //$('#colorbox').colorbox.resize();
                }
            });
        } else {
            zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
                return attrCat.category === categoryObject.id && attrCat.menu === menuObject.id && attrCat.selection === selectionObject.id && attrCat.set === aSet.id;
            }).toArray().then(function (attrObject) {
                if (attrObject.length > 0) {
                    var setWisetoppingList = '<div id="setWiseTopingHead-' + aSet.id + '" class="set-wise-topping-head caption top-cap-rel-' + aSet.rel + '' + aSet.rel_with + '">' + aSet.title + '-' + '<?php echo $this->lang->line('toppings') ?>' + '</div>';
                    $(setWisetoppingList).insertAfter('#setCategory-' + aSet.id);
                    var topItem = '<div class="has-set-topping top-group-rel-' + aSet.rel + '' + aSet.rel_with + '" id="ToppingOfTheSet-' + aSet.id + '">';
                    attrObject.forEach(function (anAttr) {
                        topItem += '<div class="tab-group ' + anAttr.rel + '' + anAttr.rel_with + '" id="ToppingCategory-' + anAttr.id + '" onclick="zamanEposItemToppings(\'' + anAttr.id + '\',\'' + anAttr.title + '\',\'' + anAttr.rel + '' + anAttr.rel_with + '\',\'' + anAttr.min + '\',\'' + anAttr.max + '\',\'' + anAttr.free + '\',\'' + Number(aSet.id) + '\')">';
                        topItem += '<div class="tab-item zaman-epos-nav quantity-digit">' + anAttr.title + '</div>';
                        topItem += '</div>';
                    });
                    topItem += '</div>';
                    $(topItem).insertAfter('#setWiseTopingHead-' + aSet.id);
                    $('#colorbox').colorbox.resize({height: '400px'});
                    //$('#colorbox').colorbox.resize();
                }
            });
        }
    }
    function manageSetModifier(categoryObject, menuObject, selectionObject, aSet) {
        if (jQuery.isEmptyObject(selectionObject)) {
            zamanEposDb.epos_modifiercategory.where({'status': '3'}).filter(function (modCat) {
                return modCat.category === categoryObject.id && modCat.menu === menuObject.id && modCat.selection === '0' && modCat.set === aSet.id;
            }).toArray().then(function (modObject) {
                if (modObject.length > 0) {
                    var setWiseModofierList = '<div id="setWiseModiferHead-' + aSet.id + '" class="set-wise-modifier-head caption">' + aSet.title + '-' + '<?php echo $this->lang->line('modifiers') ?>' + '</div>';
                    if ($('#setWiseTopingHead-' + aSet.id).length > 0) {
                        $('#setCategory-' + aSet.id).closest('.has-set-topping').after(setWiseModofierList);
                    } else {
                        $(setWiseModofierList).insertAfter('#setCategory-' + aSet.id);
                    }
                    var modItem = '<div class="has-set-modifier">';
                    modObject.forEach(function (anModCat) {
                        modItem += '<div class="tab-group clearfix" id="modifierCategory-' + anModCat.id + '" onclick="zamanEposItemModifier(\'' + anModCat.id + '\',\'' + aSet.title + ' ' + anModCat.title + '\')">';
                        modItem += '<div class="tab-item zaman-epos-nav quantity-digit">' + anModCat.title + '</div>';
                        modItem += '</div>';
                    });
                    modItem += '</div><br/>';
                    $(modItem).insertAfter('#setWiseModiferHead-' + aSet.id);
                    $('#colorbox').colorbox.resize({height: '400px'});
                    //$('#colorbox').colorbox.resize();
                }
            });
        } else {
            zamanEposDb.epos_modifiercategory.where({'status': '3'}).filter(function (modCat) {
                return modCat.category === categoryObject.id && modCat.menu === menuObject.id && modCat.selection === selectionObject.id && modCat.set === '0';
            }).toArray().then(function (modObject) {
                if (modObject.length > 0) {
                    $('#setAttributesModifierHead').text(menuObject.title + '-' + '<?php echo $this->lang->line('modifiers') ?>');
                    var itemModifierList = '';
                    modObject.forEach(function (anModCat) {
                        itemModifierList += '<div class="tab-group clearfix" id="modifierCategory-' + anModCat.id + '" onclick="zamanEposItemModifier(\'' + anModCat.id + '\',\'' + anModCat.title + '\')">';
                        itemModifierList += '<div class="tab-item zaman-epos-nav quantity-digit">' + anModCat.title + '</div>';
                        itemModifierList += '</div>';
                    });
                    $('#setItemModifierList').html(itemModifierList);
                    $('#setAttributesModifierHead').addClass('caption');
                    $('#colorbox').colorbox.resize({height: '400px'});
                    //$('#colorbox').colorbox.resize();
                }
            });
        }
    }
    function  manageDealToppings(categoryObject, menuObject, selectionObject) {
        var selection = !jQuery.isEmptyObject(selectionObject) ? selectionObject.id : '0';
        zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
            return attrCat.category === categoryObject.id && attrCat.menu === menuObject.id && attrCat.selection === selection && attrCat.set === '0';
        }).toArray().then(function (attrObject) {
            if (attrObject.length > 0) {
                $('#setAttributesToppingHead').text(menuObject.title + '-' + '<?php echo $this->lang->line('toppings') ?>');
                var itemAttributeList = '';
                attrObject.forEach(function (anAttr) {
                    zamanEposDb.epos_menuattr.where('menuttrcategory').equals(anAttr.id).toArray().then(function (items) {
                        if (items.length > 0) {
                            itemAttributeList += '<div class="topping-cat-tab tab-group ' + anAttr.rel + '' + anAttr.rel_with + '" id="ToppingCategory-' + anAttr.id + '" onclick="zamanEposItemToppings(\'' + anAttr.id + '\',\'' + anAttr.title + '\',\'' + anAttr.rel + '' + anAttr.rel_with + '\',\'' + anAttr.min + '\',\'' + anAttr.max + '\',\'' + anAttr.free + '\',\'' + Number(0) + '\')">';
                            itemAttributeList += '<div class="tab-item zaman-epos-nav quantity-digit">' + anAttr.title + '</div>';
                            itemAttributeList += '</div>';
                        }
                        $('#setItemAttributeList').html(itemAttributeList);
                        $('#setAttributesToppingHead').addClass('caption');
                        $('#colorbox').colorbox.resize({height: '400px'});
                        //$('#colorbox').colorbox.resize();
                    });
                });
            }
        });
    }

    function manageDealModifier(categoryObject, menuObject, selectionObject) {
        if (jQuery.isEmptyObject(selectionObject)) {
            zamanEposDb.epos_modifiercategory.where({'status': '3'}).filter(function (modCat) {
                return modCat.category === categoryObject.id && modCat.menu === menuObject.id && modCat.selection === '0' && modCat.set === '0';
            }).toArray().then(function (modObject) {
                if (modObject.length > 0) {
                    $('#setAttributesModifierHead').text(menuObject.title + '-' + '<?php echo $this->lang->line('modifiers') ?>');
                    var itemModifierList = '';
                    modObject.forEach(function (anModCat) {
                        itemModifierList += '<div class="tab-group clearfix" id="modifierCategory-' + anModCat.id + '" onclick="zamanEposItemModifier(\'' + anModCat.id + '\',\'' + anModCat.title + '\')">';
                        itemModifierList += '<div class="tab-item zaman-epos-nav quantity-digit">' + anModCat.title + '</div>';
                        itemModifierList += '</div>';
                    });
                    $('#setItemModifierList').html(itemModifierList);
                    $('#setAttributesModifierHead').addClass('caption');
                    $('#colorbox').colorbox.resize({height: '400px'});
                    //$('#colorbox').colorbox.resize();
                }
            });
        } else {
            zamanEposDb.epos_modifiercategory.where({'status': '3'}).filter(function (modCat) {
                return modCat.category === categoryObject.id && modCat.menu === menuObject.id && modCat.selection === selectionObject.id && modCat.set === '0';
            }).toArray().then(function (modObject) {
                if (modObject.length > 0) {
                    $('#setAttributesModifierHead').text(menuObject.title + '-' + '<?php echo $this->lang->line('modifiers') ?>');
                    var itemModifierList = '';
                    modObject.forEach(function (anModCat) {
                        itemModifierList += '<div class="tab-group clearfix" id="modifierCategory-' + anModCat.id + '" onclick="zamanEposItemModifier(\'' + anModCat.id + '\',\'' + anModCat.title + '\')">';
                        itemModifierList += '<div class="tab-item zaman-epos-nav quantity-digit">' + anModCat.title + '</div>';
                        itemModifierList += '</div>';
                    });
                    $('#setItemModifierList').html(itemModifierList);
                    $('#setAttributesModifierHead').addClass('caption');
                    $('#colorbox').colorbox.resize({height: '400px'});
                    //$('#colorbox').colorbox.resize();
                }
            });
        }
    }

    function addDealAtCart() {
        var category = $('#customDealCategory').val();
        var menu = $('#customDealMenu').val();
        var selection = $('#customDealMenuSelection').val();
        if (Number(selection) > 0) {
            zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_menucategory, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_configuration, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti, zamanEposDb.epos_menuset, zamanEposDb.epos_menuattrcategory, zamanEposDb.epos_menuattr], function () {
                zamanEposDb.epos_menucategory.get({id: category}).then(function (categoryObject) {
                    zamanEposDb.epos_menu.get({id: menu}).then(function (menuObject) {
                        zamanEposDb.epos_menuselection.get({id: selection}).then(function (selectionObject) {
                            if (Number($('#customDealCtop').val()) !== 2) {
                                precheckAttributesMinmumRequirementsOnCustomDealWithCtop(categoryObject, menuObject, selectionObject);
                            } else {
                                precheckAttributesMinmumRequirementsOnCustomDealWithDealTop(categoryObject, menuObject, selectionObject);
                            }

                        });
                    });
                });
            });
        } else {
            zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_menucategory, zamanEposDb.epos_menu, zamanEposDb.epos_configuration, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti, zamanEposDb.epos_menuset, zamanEposDb.epos_menuattrcategory, zamanEposDb.epos_menuattr], function () {
                zamanEposDb.epos_menucategory.get({id: category}).then(function (categoryObject) {
                    zamanEposDb.epos_menu.get({id: menu}).then(function (menuObject) {
                        if (Number($('#customDealCtop').val()) !== 2) {
                            precheckAttributesMinmumRequirementsOnCustomDealWithCtop(categoryObject, menuObject, {});
                        } else {
                            precheckAttributesMinmumRequirementsOnCustomDealWithDealTop(categoryObject, menuObject, {});
                        }
                    });
                });
            });
        }
    }

    function precheckAttributesMinmumRequirementsOnCustomDealWithCtop(categoryObject, menuObject, selectionObject) {
        var selection = !jQuery.isEmptyObject(selectionObject) ? selectionObject.id : '0';
        var setSatisfaction = new Array();
        var toppingSatisfaction = new Array();
        zamanEposDb.epos_menuset.where({'status': '3'}).filter(function (set) {
            return set.category === categoryObject.id && set.menu === menuObject.id && set.selection === selection;
        }).toArray().then(function (allSets) {
            if (allSets.length > 0) {
                allSets.forEach(function (set) {
                    zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
                        return attrCat.category === categoryObject.id && attrCat.menu === menuObject.id && attrCat.selection === selection && attrCat.set === set.id && Number(attrCat.min) > 0;
                    }).toArray().then(function (setToppings) {
                        var status = manageCustomSetAndSetTopping(set, setToppings);
                        setSatisfaction.push(status.setSatisfaction);
                        toppingSatisfaction.push(status.toppingSatisfaction);
                    });
                });
            } else {
                setSatisfaction.push(true);
                toppingSatisfaction.push(true);
            }
        }).then(function () {
            setTimeout(function () {
                var hasSetDissatifaction = setSatisfaction.indexOf(false);
                var hasToppingDissatisfaction = toppingSatisfaction.indexOf(false);
                if (hasSetDissatifaction <= -1 && hasToppingDissatisfaction <= -1) {
                    addToCart(categoryObject, menuObject, selectionObject);
                }
            }, 50);
        });
    }

    function precheckAttributesMinmumRequirementsOnCustomDealWithDealTop(categoryObject, menuObject, selectionObject) {
        var selection = !jQuery.isEmptyObject(selectionObject) ? selectionObject.id : '0';
        var setSatisfaction = new Array();
        var toppingSatisfaction = new Array();
        zamanEposDb.epos_menuset.where({'status': '3'}).filter(function (set) {
            return set.category === categoryObject.id && set.menu === menuObject.id && set.selection === selection;
        }).toArray().then(function (allSets) {
            if (allSets.length > 0) {
                allSets.forEach(function (set) {
                    var status = manageCustomDealSet(set);
                    setSatisfaction.push(status);
                });
            } else {
                setSatisfaction.push(true);
            }
            setTimeout(function () {
                if (setSatisfaction.length > 0) {
                    var hasSetDissatifaction = setSatisfaction.indexOf(false);
                    if (hasSetDissatifaction <= -1) {
                        zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
                            return attrCat.category === categoryObject.id && attrCat.menu === menuObject.id && attrCat.selection === selection && attrCat.set === '0' && Number(attrCat.min) > 0;
                        }).toArray().then(function (cToppings) {
                            if (cToppings.length > 0) {
                                var status = manageToppionsMinRequirement(0, cToppings);
                                toppingSatisfaction.push(status);
                            } else {
                                toppingSatisfaction.push(true);
                            }
                        });
                        setTimeout(function () {
                            var hasToppingDissatisfaction = toppingSatisfaction.indexOf(false);
                            if (hasToppingDissatisfaction <= -1) {
                                addToCart(categoryObject, menuObject, selectionObject);
                            }
                        }, 20);
                    }
                }
            }, 20);
        });
    }



    function manageCustomSetAndSetTopping(set, setToppings) {
        var setSatisfaction = false;
        var setToppingsSatisfaction = false;
        setSatisfaction = manageCustomDealSet(set);
        setToppingsSatisfaction = manageCustomDealSetToppings(set, setToppings);
        var setAndTopSatifactionArr = new Array();
        setAndTopSatifactionArr.setSatisfaction = setSatisfaction;
        setAndTopSatifactionArr.toppingSatisfaction = setToppingsSatisfaction;
        return setAndTopSatifactionArr;
    }
    function manageCustomDealSet(set) {
        var setSatisfaction = false;
        if ($('#setCategory-' + set.id).length > 0 || $('#setCategory-' + set.id).children().length > 0) {
            var setMimimum = set.min;
            var setMimimumMsg = '<?php echo $this->lang->line('from') ?>' + set.title + ',&nbsp;<?php echo $this->lang->line('min_select_msg') ?>' + setMimimum + '&nbsp;<?php echo $this->lang->line('item') ?>';
            var setItemArray = zamanEposCart.listSetItem();
            var setItemQuantity = 0;
            setItemArray.forEach(function (itemSet) {
                if (itemSet.set === set.id) {
                    setItemQuantity += itemSet.qty;
                }
            });
            if (setMimimum > setItemQuantity) {
                warning('<?php echo $this->lang->line('warning') ?>', setMimimumMsg, '<?php echo $this->lang->line('ok') ?>');
            } else {
                setSatisfaction = true;
            }
        } else {
            setSatisfaction = true;
        }
        return setSatisfaction;
    }

    function manageCustomDealSetToppings(set, setToppings) {
        var result = new Array;
        if ($('#ToppingOfTheSet-' + set.id).length > 0 || $('#ToppingOfTheSet-' + set.id).children().length > 0) {
            setToppings.forEach(function (toppingCat) {
                if ($('#ToppingCategory-' + toppingCat.id).length > 0 && $('#ToppingCategory-' + toppingCat.id).children().length > 0) {
                    var setToppingMimimum = toppingCat.min;
                    var setToppingMimimumMsg = '<?php echo $this->lang->line('from') ?>' + set.title + '-' + toppingCat.title + ',&nbsp;<?php echo $this->lang->line('min_select_msg') ?>' + setToppingMimimum + '&nbsp;<?php echo $this->lang->line('topping') ?>';
                    var AttrArray = zamanEposCart.listItemTopping();
                    var totalToping = 0;
                    AttrArray.forEach(function (attr) {
                        if (Number(attr.set) === Number(set.id) && Number(attr.category) === Number(toppingCat.id)) {
                            totalToping += attr.qty;
                        }
                    });
                    if (setToppingMimimum > totalToping) {
                        result.push(toppingCat.id);
                        warning('<?php echo $this->lang->line('warning') ?>', setToppingMimimumMsg, '<?php echo $this->lang->line('ok') ?>');
                    }
                }
            });
        }
        return result.length > 0 ? false : true;
    }
    function manageToppionsMinRequirement(set, setToppings) {
        var result = new Array;
        setToppings.forEach(function (toppingCat) {
            if ($('#ToppingCategory-' + toppingCat.id).length > 0 && $('#ToppingCategory-' + toppingCat.id).children().length > 0) {
                var setToppingMimimum = toppingCat.min;
                var setToppingMimimumMsg = '<?php echo $this->lang->line('from') ?>' + toppingCat.title + ',&nbsp;<?php echo $this->lang->line('min_select_msg') ?>' + setToppingMimimum + '&nbsp;<?php echo $this->lang->line('topping') ?>';
                var AttrArray = zamanEposCart.listItemTopping();
                var totalToping = 0;
                AttrArray.forEach(function (attr) {
                    if (Number(attr.set) === Number(set) && Number(attr.category) === Number(toppingCat.id)) {
                        totalToping += attr.qty;
                    }
                });
                if (setToppingMimimum > totalToping) {
                    result.push(toppingCat.id);
                    warning('<?php echo $this->lang->line('warning') ?>', setToppingMimimumMsg, '<?php echo $this->lang->line('ok') ?>');
                }
            }
        });
        return result.length > 0 ? false : true;
    }
</script>