var versoDiscounted = $('#verso_discounted').is(':checked');
var logoDiscounted = $('#logo_discounted').is(':checked');

var discountMap = {"none":0,"logo":0.05, "verso":0.25};

/**
 * Update the price of a job in the page
 * @param $jobLine The jobLine tr jquery object
 * @param productRef the reference of the selected product
 * @param formatRef the reference of the selected format
 */
function updateJobPrice($jobLine)
{
    var price = 0;
    var qty = $('input.quantity_hidden', $jobLine).val();
    var productRef = $('select.product_reference', $jobLine).val();
    var formatRef = $('select.format_reference', $jobLine).val();
    var $optionRefList = $('span.option_reference:not(.default) input:checked', $jobLine);

    if (productRef != 'null' && formatRef != 'null')
    {
        var formatPrice = computeFormatPrice(qty, productRef, formatRef);
        var optionsPrice = computeOptionsPrice(qty, productRef, $optionRefList);
        price = formatPrice + optionsPrice;
        if (versoDiscounted && productMap[productRef].versoDiscountable) price = Math.round(price * (1.0 - discountMap["verso"]));
        else if (logoDiscounted && productMap[productRef].logoDiscountable) price = Math.round(price * (1.0 - discountMap["logo"]));
    }
    price = applyOptionModulation(price, productRef, $optionRefList);
    $('input.price', $jobLine).val(price);
    $('span.price_display', $jobLine).html(new Number(price / 100).toFixed(2).toString().replace(".", ","));
    updateTotalPrice();
}

/**
 * Compute the price of a format
 * @param qty  the quantity of product ordered
 * @param productRef the reference of the selected product
 * @param formatRef the reference of the selected format
 */
function computeFormatPrice(qty, productRef, formatRef)
{
    var price = 0;
    var product = productMap[productRef];
    var extensionQty = product.extensionQty;
    var format;
    for (var i in product.formats)
    {
        if (product.formats[i].reference == formatRef)
        {
            format = product.formats[i];
            break;
        }
    }
    var basePrices = format.basePrices;
    var extensionPrice = format.extensionPrice;
    price = computeQtyPrice(basePrices, qty, extensionQty, extensionPrice);

    return price;
}

/**
 * Compute the price of an optionList
 * @param qty  the quantity of product ordered
 * @param productRef the reference of the selected product
 * @param optionRefList the list of references of the selected options
 */
function computeOptionsPrice(qty, productRef, $optionRefList)
{
    var price = 0;
    var product = productMap[productRef];

    $optionRefList.each(function()
    {
        var optionRef = $(this).val();
        var option;
        for (var i in product.options)
        {
            if (product.options[i].reference == optionRef)
            {
                option = product.options[i];
                break;
            }
        }
        var extensionQty = product.extensionQty;
        var basePrices = option.basePrices;
        var extensionPrice = option.extensionPrice;
        price = price + computeQtyPrice(basePrices, qty, extensionQty, extensionPrice);
    });

    return price;
}

/**
 * apply the price modulation of selected options on this job price
 * @param price  the original price of the job
 * @param productRef the reference of the selected product
 * @param $optionRefList the list of references of the selected options
 */
function applyOptionModulation(price, productRef, $optionRefList)
{
    var product = productMap[productRef];

    $optionRefList.each(function()
    {
        var optionRef = $(this).val();
        var option;
        for (var i in product.options)
        {
            if (product.options[i].reference == optionRef)
            {
                option = product.options[i];
                break;
            }
        }
        var priceModulationPercent = option.priceModulationPercent;
        price = Math.round(price * (priceModulationPercent / 100));
    });

    return price;
}

/**
 * Compute the price based on the quantity
 * @param basePrices An array of quantityPrices, defining the price for a quantity
 * @param qty  the quantity of product ordered
 * @param extensionQty the "quantum" of quantity when we move over the max quantity in basePrices
 * @param extensionPrice the "quantum" of price when we move over the max price in basePrices
 */
function computeQtyPrice(basePrices, qty, extensionQty, extensionPrice)
{
    var basePrice = 0;
    var baseQty = 0;
    var price = 0;
    var isBasePrice = false;
    for (var i in basePrices)
    {
        if (basePrices[i].quantity > baseQty)
        {
            baseQty = basePrices[i].quantity;
            basePrice = basePrices[i].price;
        }
        if (basePrices[i].quantity == qty)
        {
            price = basePrices[i].price;
            isBasePrice = true;
            break;
        }
    }
    if (!isBasePrice)
    {
        price = basePrice + ((qty / extensionQty) - 1) * extensionPrice;
    }
    return price;
}

function updateShippingPrice()
{
    var boxCount = 0;
    var price = 0;

    if (currentShippingMethod != undefined)
    {
        var shippingPrice = currentShippingMethod.shippingPrice == undefined ? 0 : currentShippingMethod.shippingPrice;
        var pricePerBox = currentShippingMethod.pricePerBox == undefined ? 0 : currentShippingMethod.pricePerBox;
        for (i in boxCountList)
        {
            boxCount = boxCount + boxCountList[i];
        }
        price = shippingPrice + (boxCount * pricePerBox);
    }
    $('#shipping_method_price').val(price);
    $('#shipping_method_price_display').html(new Number(price == 0 ? price : price / 100).toFixed(2).toString().replace(".", ","));
    updateTotalPrice();
}

function updateTotalPrice()
{
    var totalPrice = 0;
    $('div.job_concerns').each(function()
    {
        totalPrice = totalPrice + parseInt($('tr.job_line input.price', $(this)).val());
    });
    totalPrice = totalPrice + parseInt($('#shipping_method_price').val());
    $('#total_price').val(totalPrice);
    $('#total_price_display').html(new Number(totalPrice == 0 ? totalPrice : totalPrice / 100).toFixed(2).toString().replace(".", ","));
}