$( function() {
    $('#k1 input').bind('change', {}, reCalculateK1);
    $('#k2 input').bind('change', {}, reCalculateK2);
    $('#k3 input').bind('change', {}, reCalculateK3);
} );

var printPrice = {
    'bw': {
        'A4': {
            'single': [0.45, 0.28, 0.14, 0.13, 0.12, 0.10],
            'duplex': [0.60, 0.50, 0.30, 0.26, 0.24, 0.20]
        },
        'A3': {
            'single': [0.60, 0.50, 0.30, 0.26, 0.24, 0.20],
            'duplex': [0.90, 0.73, 0.50, 0.45, 0.40, 0.36]
        }
    },
    'color': {
        'A4': {
            'single': [1.80, 1.64, 1.40, 1.20, 1.00, 0.80],
            'duplex': [3.50, 3.20, 2.62, 2.20, 1.90, 1.56]
        },
        'A3': {
            'single': [3.50, 3.20, 2.62, 2.20, 1.90, 1.56],
            'duplex': [6.90, 6.30, 5.10, 4.30, 3.60, 3.10]
        }
    }
};

var paperAddFee = {
    'satyna': {
        'A4': [0, 0.15, 0.20, 0.35, 0.40, 0.45, 0.50],
        'A3': [0, 0.30, 0.40, 0.70, 0.80, 0.90, 1.00]
    },
    'kreda': {
        'A4': [0, 0.12, 0.15, 0.23, 0, 0.25, 0],
        'A3': [0, 0.24, 0.30, 0.46, 0, 0.50, 0]
    }
};

var bCardPrice = {
    'bw': {
        'single': [25, 40],
        'duplex': [38, 62]
    },
    'color': {
        'single': [33, 54],
        'duplex': [50, 70]
    }
};

function reCalculateK1(event) {
    disableK1();
    var sum = 0;
    var range = 0;
    var pagePrice = 0;
    var pageAddFeeIndex = 0;
    var pages = Number( $('#k1pages').attr('value') );
    var color = $('#k1color_bw').attr('checked') ? 'bw' : 'color';
    var size  = $('#k1size_a4').attr('checked') ? 'A4' : 'A3';
    var sides = $('#k1duplex_0').attr('checked') ? 'single' : 'duplex';
    var paper = $('#k1paper_s').attr('checked') ? 'satyna' : 'kreda';
    if (pages > 0) {
        if (pages > 10) {
            range = 1;
        }
        if (pages > 30) {
            range = 2;
        }
        if (pages > 100) {
            range = 3;
        }
        if (pages > 500) {
            range = 4;
        }
        if (pages > 1000) {
            range = 5;
        }
        if ($('#k1weight_80').attr('checked')) {
            pageAddFeeIndex = 0;
        }
        if ($('#k1weight_120').attr('checked')) {
            pageAddFeeIndex = 1;
        }
        if ($('#k1weight_160').attr('checked')) {
            pageAddFeeIndex = 2;
        }
        if ($('#k1weight_250').attr('checked')) {
            pageAddFeeIndex = 3;
        }
        if ($('#k1weight_280').attr('checked')) {
            pageAddFeeIndex = 4;
        }
        if ($('#k1weight_300').attr('checked')) {
            pageAddFeeIndex = 5;
        }
        if ($('#k1weight_350').attr('checked')) {
            pageAddFeeIndex = 6;
        }
        // koszt druku
        sum += pages * (printPrice[color][size][sides][range] + paperAddFee[paper][size][pageAddFeeIndex]);
        // bindowanie
        if ($('#k1bind').attr('checked')) {
            if (pages > 0 && pages <=50) {
                sum += 2.5;
            }
            if (pages > 50 && pages <=100) {
                sum += 3;
            }
            if (pages > 100 && pages <=150) {
                sum += 3.5;
            }
            if (pages > 150 && pages <=200) {
                sum += 4.5;
            }
            if (pages > 200) {
                sum += 5;
            }
        }
        // foliowanie
        if ($('#k1folia').attr('checked')) {
            var fPrice = size == 'A4' ? 0.20 : 0.40;
            sum += fPrice * pages;
        }
        // falcowanie
        if ($('#k1falc').attr('checked')) {
            sum += 0.1 * pages;
        }
    }
    sum *= 100;
    sum = Math.round(sum) / 100;
    $('#k1sum').text( sum + ' z\u0142' );
}

function disableK1() {
    // dla kredy 3 gramatury są niedostępne: 80g, 280g i 350g
    if ($('#k1paper_k').attr('checked')) {
        if ($('#k1weight_80').attr('checked') || $('#k1weight_280').attr('checked') || $('#k1weight_350').attr('checked')) {
            $('#k1weight_120').attr('checked', true);
        }
        $('#k1weight_80').attr('disabled', true);
        $('#k1weight_280').attr('disabled', true);
        $('#k1weight_350').attr('disabled', true);
    } else {
        $('#k1weight_80').attr('disabled', false);
        $('#k1weight_280').attr('disabled', false);
        $('#k1weight_350').attr('disabled', false);
    }
    // jeśli ilość >= 100 to aktywujemy opcje dodatkowe
    var pages = Number( $('#k1pages').attr('value') );
    if (pages >= 100) {
        $('#k1folia').attr('disabled', false);
        $('#k1falc').attr('disabled', false);
    } else {
        $('#k1folia').attr('checked', false);
        $('#k1falc').attr('checked', false);
        $('#k1folia').attr('disabled', true);
        $('#k1falc').attr('disabled', true);
    }
}


function reCalculateK2(event) {
    var sum = 0;
    var color = $('#k2color_bw').attr('checked') ? 'bw' : 'color';
    var sides = $('#k2duplex_0').attr('checked') ? 'single' : 'duplex';
    var numerIndex = $('#k2count_100').attr('checked') ? 0 : 1;
    sum += bCardPrice[color][sides][numerIndex];
    $('#k2sum').text( sum + ' z\u0142' );
}

function reCalculateK3(event) {
    var sum = 0;
    var priceList = [65, 36, 1.65];
    var calType = 0;
    var calCount = Number( $('#k3count').attr('value') );
    if ($('#k3size_a4').attr('checked')) {
        calType = 1;
    }
    if ($('#k3size_8x5').attr('checked')) {
        calType = 2;
        // min. 25 szt.
        if (calCount < 25) {
            calCount = 25;
            $('#k3count').attr('value', calCount);
        }
    }
    sum += calCount * priceList[calType];
    $('#k3sum').text( sum + ' z\u0142' );
}

