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( '
' ); } else { $('#pid-'+ id ).css( 'color', '#007bff' ); $('#description-'+ id +' .title:first' ).css( 'color', '#007bff' ); $('#image-'+ id ).css( 'border-color', '#007bff' ); $('#image-'+ id ).css( 'border-style', 'dashed' ); $('#pid-'+ id ).html( '
' ); } $('#pid-'+ id +' .product-quantity:first').val( ko_products[id].quantity ); } function set_total_price(){ var sum_extra = 0; kviq.order = []; for ( var i = 0; i < ko_products.length; i++ ){ if ( ko_products[i].quantity > 0 ){ kviq.order.push( { id : ko_products[i].pid, q : ko_products[i].quantity, f: ko_products[i].features_selected } ); sum_extra += sum_features_extra_price( ko_products[i].features_selected ); } } kviq.tip_amount = Math.round( ( ( kviq.total_price + sum_extra ) / 100 ) * kviq.tip ); var price = kviq.total_price + sum_extra + kviq.tip_amount; if ( price > 0 && order_visible == 0 ){ $('#total-price').text( '(Kr '+ price +',-)' ); } else { $('#total-price').text( '' ); } $('#total-price').data( 'price', price ); generate_order(); kviq.set_cookie( 'kviq-order', JSON.stringify( kviq ), 30, '/' ); // console.log( kviq ); } function sum_features_extra_price( features ){ var sum_extra = 0; $.each( features, function( time, variants ){ $.each( variants, function( type, option ) { var match = 0; if ( match = option.match( / \(\+([0-9]+)kr\)$/i ) ){ sum_extra += parseInt( match[1] ); } }); }); return sum_extra; } function restore_order( _kviq ){ if ( _kviq ){ // Set values for ( var i = 0; i < _kviq.order.length; i++ ){ var id = $('#container-'+ _kviq.order[i].id ).data('id'); ko_products[id].pid = _kviq.order[i].id; ko_products[id].quantity = Math.max( 0, _kviq.order[i].q ); ko_products[id].features_selected = _kviq.order[i].f; adjust_buy_buttons( id, 'set' ); } kviq.total_price = Math.max( 0, _kviq.total_price ); kviq.total_quantity = Math.max( 0, _kviq.total_quantity ); kviq.tip = _kviq.tip; // Set price and refresh DOM set_total_price(); $('#menu').click(); } // console.log( kviq ); } function control_order_payment_overview(){ var price = $('#total-price').data( 'price' ); if ( order_visible == 1 && payment_options_visible == 0 ){ if ( kviq.selectable_pickup_time == '1' ){ set_pickup_times(); $( '#selectable_pickup_time' ).slideDown(); } payment_options_visible = 1; $( '#payment-options' ).slideDown(); $( '#order-visable-text').html('LUKK'); $( '.order-visable').css('border-color', 'gray' ); $( '.order-visable').css('background-color', 'gray' ); } else if ( payment_options_visible == 1 ){ 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(); } else if ( price > 0 ){ order_visible = 1; $( '.order-visable').css('border-color', 'green' ); $( '.order-visable').css('background-color', 'green' ); $( '#order-visable-text').html('TIL BETALING'); $( '#order' ).slideDown(); } $('#order').focus(); } function generate_order(){ var order_html = '
HANDLEKURVEN
'; var total_vat = 0; for ( var i = 0; i < ko_products.length; i++ ){ if ( ko_products[i].quantity > 0 ){ var features_extra_price = sum_features_extra_price( ko_products[i].features_selected ); order_html += '
'; order_html += '
'; order_html += '
'; order_html += '

'+ ko_products[i].quantity +' stk

'; order_html += '
'; order_html += '
'; order_html += '

'+ ko_products[i].title +'

'+ make_features_selected_string( ko_products[i].features_selected ) +'
'; order_html += '

Kr '+ ko_products[i].price +',- pr stk

'; order_html += '
'; order_html += '
'; order_html += '

kr '+ ( ( ko_products[i].quantity * ko_products[i].price ) + features_extra_price ) +',-

'; order_html += '
'; order_html += '
'; order_html += '
'; order_html += '
'; if ( ko_products[i].vat != undefined ){ var vat = parseFloat( ko_products[i].vat ); if ( vat > 0 ){ total_vat += ( ( ( ko_products[i].quantity * ko_products[i].price ) + features_extra_price ) / ( 100 + vat ) ) * vat; } } } } // Show if TIP if ( kviq.tip_visable == '1' ){ order_html += '
'; order_html += '
'; order_html += '
'; order_html += '

Tips:

'; order_html += '
'; order_html += ' '; order_html += '
'; order_html += '

kr '+ kviq.tip_amount +',-

'; order_html += '
'; order_html += '
'; order_html += '
'; order_html += ''; } order_html += '
'; order_html += '
'; order_html += '
'; order_html += '

Sum:

'; order_html += '
'; order_html += '
'+ ( kviq.vat_registered == '1' ? '( Herav MVA: kr '+ total_vat.toFixed(2).replace( '.', ',' ) +' )' : '' ) +'
'; order_html += '
'; order_html += '

kr '+ $('#total-price').data( 'price' ) +',-

'; order_html += '
'; order_html += '
'; order_html += '
'; order_html += ''; $('#order').css( 'max-height', bottom_pos ); $('#order').scrollTop( bottom_pos ); $('#order').html( order_html ); // console.log( 'tip: '+ kviq.tip ); $('#tip_buttons').find(`[data-tip='${kviq.tip}']`).css( { 'font-weight' : 'bold', 'background-color' : '#64ADFF', 'border' : '1px #64ADFF solid' } ); } function make_features_selected_string( obj ){ // console.log( obj ); var arr = []; $.each( obj, function( time, variants ){ // console.log( 'make_features_selected_string:' ); // console.log( variants ); $.each( variants, function( type, option ){ arr.push( type +': '+ option +', ' ); }); arr.push( ' | ' ); }); if ( arr.length > 0 ){ var s = arr.join('').replace( /, +\| /g, ' ]
[ ' ).replace( / \]\[ $/, '' ); return '[ '+ s +' ]'; } else { return ''; } } function set_pickup_times(){ var d = new Date(); start_ts = Math.round( Date.now() / 1000 ); var oh = kviq.openinghours.split(':'); for( var i = 0; i < oh.length; i++ ){ var start_end = oh[i].split('-'); var weekday = ( d.getDay() == 0 ? 6 : d.getDay() - 1 ); if ( weekday == i ){ console.log( ( d.getMonth() + 1 ) + '/' + d.getDate() + '/' + d.getFullYear() + " " + start_end[1].substr(0,2) +':'+ start_end[1].substr(2,2) +':00' ); var end_ts = Math.round( new Date( ( d.getMonth() + 1 ) + '/' + d.getDate() + '/' + d.getFullYear() + " " + start_end[1].substr(0,2) +':'+ start_end[1].substr(2,2) +':00' ).getTime() / 1000 ); break; } } console.log( start_ts ); console.log( end_ts ); var coeff = 1000 * 60 * 10; var timestamp = start_ts + ( 60 * 20 ); while( timestamp < end_ts ){ var rounded_ts = new Date( Math.ceil( ( timestamp * 1000 ) / coeff ) * coeff ); $("#selectable_pickup_time").append( new Option( $('