<div class="order-menu-list" style="padding-top: 5px; ">
    <div class="menupage-category-info">
        <div id="categoryTitle"><p class="sidebar-heading text-uppercase"><?php echo $this->lang->line('gkpos_click_category_mesage') ?></p></div>
        <!--<div id="categoryDescription"></div>-->
    </div>
    <div id="menuListArea">
        <?php echo $this->load->view('order_info', array('order_info' => $currentOrderObj, 'first_taken' => !empty($firstTaken) ? $firstTaken[0]->first_taken : '', 'first_taken_by' => !empty($firstTaken) ? $firstTaken[0]->created_by : '', 'last_taken' => !empty($lastTaken) ? $lastTaken[0]->last_taken : '', 'last_taken_by' => !empty($lastTaken) ? $lastTaken[0]->created_by : '')) ?>
    </div>
    <div class="clearfix"></div>
    <div id="operationStatus" class="alert alert-success alert-dismissable" style="display: none;">
        <button type="button" class="close" data-dismiss="alert">x</button>
        <?php echo $this->lang->line('gkpos_operation_sucess') ?>
    </div>
</div>
<script>
    $(document).ready(function() {
        manageWindowHeight();
        manageMenuAreaHeight();
    });

    //load menus of a category 
    function getMenuByCategory(category, pageBtn, index) {
        var categoryColorIndex = 1;
        
        var categoryOrder = $('#category_' + category).attr('data-order');
        var categoryTitle = $('#category_' + category).text();
        $(".menuPrevBtn").attr('id', category);
        $(".menuNextBtn").attr('id', category);
        var menuFirstOrder = '0';
        var menuLastOrder = '0';
        var menuNextBtnCounter = '0';
        var menuPrevBtnCounter = '0';
        if (pageBtn == '') {
            $("#menuFirstOrder").val('0');
            $("#menuLastOrder").val('0');
            $("#menuNextBtnCounter").val("0");
            $("#menuPrevBtnCounter").val("0");
            categoryColorIndex = index;
            $('#categoryColorIndex').val(categoryColorIndex);
        } else {
            if (pageBtn == 'menuNextBtn') {
                categoryColorIndex = $('#categoryColorIndex').val();
                menuNextBtnCounter = parseInt($("#menuNextBtnCounter").val()) + 1;
                $("#menuNextBtnCounter").val(menuNextBtnCounter);
                menuPrevBtnCounter = parseInt($("#menuPrevBtnCounter").val());
                if (menuPrevBtnCounter > 0) {
                    $("#menuPrevBtnCounter").val(menuPrevBtnCounter - 1);
                }

            }
            if (pageBtn == 'menuPrevBtn') {
                categoryColorIndex = $('#categoryColorIndex').val();
                menuPrevBtnCounter = parseInt($("#menuPrevBtnCounter").val()) + 1;
                $("#menuPrevBtnCounter").val(menuPrevBtnCounter);
                menuNextBtnCounter = parseInt($("#menuNextBtnCounter").val());
                if (menuNextBtnCounter > 0) {
                    $("#menuNextBtnCounter").val(menuNextBtnCounter - 1);
                }
            }
            menuFirstOrder = $("#menuFirstOrder").val();
            menuLastOrder = $("#menuLastOrder").val();
        }
        $.ajax({
            url: "<?php echo site_url('gkpos/orders/getmenuorder') ?>",
            data: {
                category: category,
                pageBtn: pageBtn,
                menuFirstOrder: menuFirstOrder,
                menuLastOrder: menuLastOrder,
                menuNextBtnCounter: menuNextBtnCounter,
                menuPrevBtnCounter: menuPrevBtnCounter
            },
            type: "POST",
            dataType: "json",
            success: function(output) {
                if (true == output.status) {
                    var objectLength = output.menus.length;
                    if (objectLength > 0) {
                        var btnColorClass = "item-btn item-btn-" + categoryColorIndex + " ";
                        getmenus(category, pageBtn, menuFirstOrder, menuLastOrder, btnColorClass, menuNextBtnCounter, menuPrevBtnCounter);
                    }
                } else {
                    $("#menuNextBtnCounter").val("0");
                    $("#menuPrevBtnCounter").val("0");
                    $('#categoryTitle').html('');
                    $('#categoryTitle').append(' <p class="sidebar-heading text-uppercase">' + categoryTitle + '</p>');
                    $('#menuListArea').html('');
                    $('#categoryDescription').html('');
                    $('#categoryDescription').append('<p class="' + btnColorClass + 'info text-uppercase"><?php echo $this->lang->line("gkpos_menu_not_found"); ?></p>');
                }
                manageWindowHeight();
                manageMenuAreaHeight();
            }
        });
    }
    function getmenus(category, pageBtn, menuFirstOrder, menuLastOrder, btnColorClass, menuNextBtnCounter, menuPrevBtnCounter) {
        $.ajax({
            url: "<?php echo site_url('gkpos/orders/get_menus_by_category') ?>",
            data: {
                category: category,
                menuFirstOrder: menuFirstOrder,
                menuLastOrder: menuLastOrder,
                pageBtn: pageBtn,
                menuNextBtnCounter: menuNextBtnCounter,
                menuPrevBtnCounter: menuPrevBtnCounter
            },
            type: "POST",
            dataType: "json",
            success: function(output) {
                if (true === output.status) {
                    var objectLength = output.menus.length;
                    if (objectLength > 0) {
                        var firstObject = output.menus[0];
                        var lastObject = output.menus[objectLength - 1];
                        menuFirstOrder = firstObject.order;
                        $("#menuFirstOrder").val(menuFirstOrder);
                        menuLastOrder = lastObject.order;
                        $("#menuLastOrder").val(menuLastOrder);
                        var menuIndex = 1;
                        $('#menuListArea').html('');
                        $.each(output.menus, function() {
                            if (menuIndex == 1) {
                                $('#categoryTitle').html('');
                                $('#categoryDescription').html('');
                                $('#categoryTitle').append(' <p class="sidebar-heading text-uppercase">' + this.categoryTitle + '</p>');
                                var description = this.categoryContent;
                                if (description) {
                                    //$('#categoryDescription').append(' <p class="' + btnColorClass + 'info text-uppercase">' + this.categoryContent + '</p>');
                                }
                            }
                            //$('#menuListArea').append('<div id="' + this.categoryId + "_" + this.id + '" class="' + btnColorClass + 'col-lg-4 col-md-4 col-sm-4 col-xs-4 menu-item text-center text-uppercase" title="' + this.content + '" onclick=checkSelection(\'' + this.categoryId + '\',\'' + this.id + '\',\'' + false + '\')>' + this.title + '<br/><span class="price-tag">TB-' + getPrice(this.base_price) + ' IN-' + getPrice(this.in_price) + ' OUT-' + getPrice(this.out_price) + '<span></div>');
                            $('#menuListArea').append('<div id="' + this.categoryId + "_" + this.id + '" class="' + btnColorClass + 'col-lg-4 col-md-4 col-sm-4 col-xs-4 menu-item text-center text-capitalize" title="' + this.content + '" onclick=checkSelection(\'' + this.categoryId + '\',\'' + this.id + '\',\'' + false + '\')>' + this.title + '</div>');
                            menuIndex++;
                        });
                    }
                    manageWindowHeight();
                    manageMenuAreaHeight();
                }
                manageWindowHeight();
                manageMenuAreaHeight();
            }
        });
    }
    function checkSelection(category, menu, sel) {

        var menuTitle = $("#" + category + "_" + menu).text();
        $.ajax({
            url: "<?php echo site_url('gkpos/orders/getmenuselection') ?>",
            data: {
                category: category,
                menu: menu
            },
            type: "POST",
            dataType: "json",
            success: function(output) {
                if (true == output.status) {
                    $('#warningPopupContent').html('');
                    $.each(output.selections, function() {
                        var index = $('#categoryColorIndex').val();
                        var btnColorClass = "item-btn item-btn-" + index + " ";
                        // $('#warningPopupContent').append('<div id="' + this.selMenuCategoryId + "_" + this.selMenuId + "_" + this.id + '" class="' + btnColorClass + 'col-lg-4 col-md-4 col-sm-4 col-xs-4 menu-item text-center text-uppercase" title="' + this.content + '" onclick=addtocart(\'' + this.selMenuCategoryId + '\',\'' + this.selMenuId + '\',\'' + this.id + '\')>' + this.title + '<br/><span class="price-tag">TB-' + getPrice(this.base_price) + ' IN-' + getPrice(this.in_price) + ' OUT-' + getPrice(this.out_price) + '<span></div>');
                        $('#warningPopupContent').append('<div id="' + this.selMenuCategoryId + "_" + this.selMenuId + "_" + this.id + '" class="' + btnColorClass + 'col-lg-4 col-md-4 col-sm-4 col-xs-4 menu-item text-center text-uppercase" title="' + this.content + '" onclick=addtocart(\'' + this.selMenuCategoryId + '\',\'' + this.selMenuId + '\',\'' + this.id + '\')>' + this.title + '</div>');
                    });
                    jQuery("#warningPopupHeader").text(menuTitle + ' ' + "<?php echo $this->lang->line('gkpos_selection') ?>");
                    jQuery(".warningPopup").colorbox({inline: true, slideshow: false, scrolling: false, height: "auto", className: 'menu-selection-popup', open: true, width: '100%', maxWidth: '550px'});
                } else {
                    addtocart(category, menu, 'no');
                }
            }
        });
    }
    function manageMenuAreaHeight() {
        var normalHeight = $(window).height();
        var resizeHeight = 0;
        if (normalHeight <= 768) {
            $(window).bind('resize', function() {
                resizeHeight = $(window).height();
                if (resizeHeight < 700) {
                    $('.order-menu-list').css({"min-height": "330px", "overflow-y": "auto", "overflow-x": "hidden"});
                } else {
                    $('.order-menu-list').css({"min-height": "430px", "overflow-y": "auto", "overflow-x": "hidden"});
                }
            });
            if (normalHeight < 700) {
                $('.order-menu-list').css({"min-height": "330px", "overflow-y": "auto", "overflow-x": "hidden"});
            } else {
                $('.order-menu-list').css({"min-height": "430px", "overflow-y": "auto", "overflow-x": "hidden"});
            }

        } else {
            $(window).bind('resize', function() {
                var resizeHeight = $(window).height();
                if (resizeHeight > normalHeight) {
                    $('.order-menu-list').css({"min-height": "512px", "overflow-y": "auto", "overflow-x": "hidden"});
                } else {
                    $('.order-menu-list').css({"min-height": "512px", "overflow-y": "auto", "overflow-x": "hidden"});
                }
            });
            $('.order-menu-list').css({"min-height": "512px", "overflow-y": "auto", "overflow-x": "hidden"});
        }
    }
    function getPrice(price) {
        var currencySymbol = '<?php echo $this->config->item("currency_symbol") ?>';
        return currencySymbol + price;
    }
</script>