kviq.total_price = 0; kviq.total_quantity = 0; kviq.tip = 0; kviq.tip_amount = 0; var order_visible = 0; var payment_options_visible = 0; var isApp = false; var tip_visable = 0; var nrOfVariants = 0; var features_selected_temp; var new_category_splide_pos = 0; $( document ).ready(function() { // console.log( ko_products ); max_category_slide = parseInt( parseInt( $('#categories-menu a:last').offset().left ) + parseInt( $('#categories-menu a:last').innerWidth() ) - parseInt( $('#categories-menu').width() ) ); top_pos = $('#categories').offset().top; bottom_pos = ( $('#button-bottom').offset().top + $('#button-bottom').height() ); display_height = ( bottom_pos - top_pos ); if ( kviq.get_cookie( 'kviq-order' ) ){ var _kviq = JSON.parse( kviq.get_cookie( 'kviq-order' ) ); restore_order( _kviq ); } menu_height = $('#menu').height(); logo_height = $('#logo').height(); max_menu_slide = ( menu_height - display_height - logo_height ); $(window).on("scroll resize", function(){ top_pos = $('#categories').offset().top - logo_height; max_menu_slide = ( menu_height - display_height - logo_height - 50 ); product_table_offset = $('#menu-table').offset().top - $(window).scrollTop() - 50; if ( product_table_offset <= 0 ){ menu_slide_pst = ( 100 / max_menu_slide ) * top_pos; new_category_splide_pos = ( ( max_category_slide / 100 ) * menu_slide_pst ); if ( new_category_splide_pos > 0 ){ $('#categories-menu').scrollLeft( parseInt( new_category_splide_pos ) ); } } if ( $('#categories2').length !== 0 ){ max_menu_slide =+ $('#categories2').height(); if ( product_table_offset >= 0 ){ $('#categories').slideUp(); $('#panelmenu-button').slideUp(); $('#categories2').slideDown(); } else { $('#categories').slideDown(); $('#panelmenu-button').slideDown(); $('#categories2').hide(); } } }); if ( $('#categories2').length !== 0 ){ $('#logo').css('margin-top', -25 ); var product_table_offset = $('#menu-table').offset().top - $(window).scrollTop() - 50; if ( product_table_offset >= 0 ){ $('#categories').hide(); $('#panelmenu-button').hide(); $('#categories2').show(); } } if ( window.location.hash.match( /^#order_payment_overview/g ) ){ window.history.pushState( 'menu', 'Meny', window.location.pathname ); control_order_payment_overview(); } }); $( document ).on( 'click', '#panelmenu-button', function( event ){ $('#panelmenu').html( panelmenu_content ) $('#panelmenu').slideToggle(); }); $( document ).on( 'click', '#categories a, #categories2 a', function( event ){ event.preventDefault(); var catid = event.currentTarget.getAttribute('data-id'); var addToTop = $('#categories').height() + 1; $([document.documentElement, document.body]).animate({ scrollTop: $("#category-"+ catid ).offset().top-addToTop }, 800 ); }); $( document ).on( 'click', '.product-add', function( event ){ event.preventDefault(); var id = event.currentTarget.parentNode.id.replace( /^pid\-/, '' ); adjust_buy_buttons( id, 'add' ); set_total_price(); }); $( document ).on( 'click', '.product-sub', function( event ){ event.preventDefault(); var id = event.currentTarget.parentNode.id.replace( /^pid\-/, '' ); adjust_buy_buttons( id, 'sub' ); set_total_price(); }); $( document ).on( 'click', '#menu, #order-top', function( event ){ order_visible = 0; payment_options_visible = 0; $( '#order' ).slideUp(); $( '#selectable_pickup_time' ).slideUp(); $( '#payment-options' ).slideUp(); if ( kviq.total_quantity == 0 ){ $( '#order-visable-text').html('VELG FRA MENYEN'); } else { $( '#order-visable-text').html('BESTILL'); } $( '.order-visable').css('border-color', '' ); $( '.order-visable').css('background-color', '' ); set_total_price(); }); $( document ).on( 'click', '.customer-support', function( event ){ event.preventDefault(); $.alert( 'Kontakt kundeservice på kundeservice@kviq.no' ); }); $( document ).on( 'click', '.tip', function( event ){ event.preventDefault(); kviq.tip = $(this).data('tip'); set_total_price(); }); $( document ).on( 'click', '.order-visable', function( event ){ control_order_payment_overview(); set_total_price(); }); $( document ).on( 'click', '.orderline', function( event ){ var pid = event.currentTarget.getAttribute('data-pid'); // console.log( pid ); var addToTop = $('#categories').height() + 1; $([document.documentElement, document.body]).animate({ scrollTop: $("#container-"+ pid ).offset().top-addToTop }, 400 ); $('#menu').click(); }); $( document ).on( 'click', '#payment-vipps', function( event ){ event.preventDefault(); var price = $('#total-price').data( 'price' ); var pickup_time = $('#selectable_pickup_time').val(); if ( price > 0 ){ // $.alert( 'Kr '+ price.toString() +',-', 'Totalbeløpet' ); var url = '/ajax'; isApp = ( kviq.mobilecheck() && !isApp ? true : false ); var parameters = 'method=vipps-payment&tip='+ kviq.tip +'&order='+ encodeURIComponent( JSON.stringify( ko_products ) ) +"&pickup_time="+ pickup_time +"&token_i="+ kviq.token_i +"&is_app="+ isApp; // console.log( parameters ); kviq.ajax( url, parameters, kviq.submit_response, 1 ); } }); // Respone kviq.submit_response = function( id, response ){ // console.log( response ); // Buy if ( id == 1 ){ if ( obj = JSON.parse( response.responseText ) ){ if ( obj.status == 0 ){ if ( obj.url_data.status == 0 && obj.url_data.url.length >= 12 ){ window.top.location.href = obj.url_data.url; } else if ( obj.url_data.status == 100 ){ $.alert( 'Denne er i test-modus', 'DEMO' ); } } else if ( obj.status == 10 ){ $.alert( obj.status_text ); } else { $.alert( 'Ukjent feil. Kontakt kundeservice' ); } // console.log( obj ); } } } /// Functions ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// function adjust_buy_buttons( id, type ){ var price = $('#price-'+ id ).data('price'); var time = Date.now(); nrOfVariants = 0; features_selected_temp = []; // Add if ( type == 'add' ){ if ( ko_products[id].features && ko_products[id].features.length > 0 ){ var features = JSON.parse( atob( ko_products[id].features ) ); // console.log( features ); if ( features && features.variants ){ $.each( features.variants, function( type, features ){ description = features.description; options = features.options; nrOfVariants++; if ( options && options.length > 0 && options[0] != '' ){ var buttons = {}; for( i=0; i < options.length; i++ ){ buttons[i] = { btnClass: 'btn-blue', time: time, text: options[i], action: function( data ){ set_feature_selected( id, type, data.text, 'add', data.time ) } }; } // console.log( 'bottons:' ); // console.log( buttons ); $.confirm({ title: type, boxwidthmax: '500px', useBootstrap: false, content: ''+ description +'', draggable: true, buttons: buttons, closeIcon: true }); } else { $.confirm({ title: type, boxwidthmax: '500px', useBootstrap: false, content: ''+ description +'', draggable: true, buttons: { OK: function( key ){ var input_value = $('.confirm_input_value' ).last().val(); if ( $.trim( input_value ).length == 0 ){ return false } else { set_feature_selected( id, type, input_value, 'add', time ) } } }, closeIcon: true }); } }); } } if ( nrOfVariants == 0 ){ ko_products[id].quantity++; kviq.total_price += parseInt( price ); kviq.total_quantity++; } // Sub } else if ( type == 'sub' && ko_products[id].quantity >= 1 ){ if ( ko_products[id].features_selected ){ var features_selected = ko_products[id].features_selected; // console.log( features_selected ); var features_feature = 'Hvilken skal fjernes'; var buttons = {}; for( var time in features_selected ){ nrOfVariants++; var arr = []; $.each( ko_products[id].features_selected[time], function( type, option ){ arr.push( type +': '+ option ); }); var btn_text = arr.join( ', ' ); buttons[btn_text] = { btnClass: 'btn-blue', time: time, text: btn_text, action: function( data ){ set_feature_selected( id, features_feature, data.text, 'sub', data.time ) } }; } if ( kviq.object_count( buttons ) > 1 ){ $.confirm({ title: '', boxwidthmax: '500px', useBootstrap: false, content: features_feature, draggable: true, buttons: buttons }); } else if ( kviq.object_count( buttons ) == 1 ){ // console.log( time ); set_feature_selected( id, features_feature, time, 'sub', time ); } } if ( nrOfVariants == 0 ){ ko_products[id].quantity--; kviq.total_price -= parseInt( price ); kviq.total_quantity--; } } set_buttons( id ); } function set_feature_selected( id, type, option_selected, method, time ){ var selectedFeature = time +'|'+ type; var price = $('#price-'+ id ).data('price'); // console.log( 'time: '+ time +' id: '+ id +' type:'+ type +' option_selected: '+ option_selected +' method: '+ method +' nrOfVariants: '+ nrOfVariants ); // console.log( time ); if ( method == 'add' ){ if ( !features_selected_temp[time] ){ features_selected_temp[time] = {}; } features_selected_temp[time][type] = option_selected; if ( nrOfVariants == Object.keys(features_selected_temp[time]).length ){ ko_products[id].features_selected[time] = features_selected_temp[time]; ko_products[id].quantity++; kviq.total_price += parseInt( price ); kviq.total_quantity++; } } else { delete ko_products[id].features_selected[time]; ko_products[id].quantity--; kviq.total_price -= parseInt( price ); kviq.total_quantity--; } set_total_price(); // console.log( ko_products ); set_buttons( id ); } function set_buttons( id ){ if ( ko_products[id].quantity == 0 ){ $('#pid-'+ id ).css( 'color', '#000000' ); $('#description-'+ id +' .title:first' ).css( 'color', '#000000' ); $('#image-'+ id ).css( 'border-color', '#ffffff' ); $('#image-'+ id ).css( 'border-style', 'solid' ); $('#pid-'+ id ).html( '
Kr '+ ko_products[i].price +',- pr stk
'; order_html += '