﻿
var _location = "";
var _b = "";

$(function() {
    //$('#' + _b).addClass('active');
    //calc();
})

function toggleMap(id) {
    $('.pricecalcmap').attr('style', 'display:none;');
    $('#' + id).attr('style', 'display:block;');
    _location = id;
    calc();
}

function typeToggle(item) {
    _b = item.id;
    $('.pricecalcform li').removeClass();
    $('#' + _b).addClass('active');

    calc();
}

function showResultPane() {
    if (_location.length > 0 && _b.length > 0) {
        $("#pricecalcresult").attr('style', 'display:block;');
    }
    else {
        $("#pricecalcresult").attr('style', 'display:none;');
    }
}

function calc() {
    var b = getConsumption(); // consumption, kWh
    var a = getPrice("AveragePrices", _location); //average price for selected area
    var fee = getPrice("Green", _location);

    var standartFast = getLowestPrice("Fast", _location);
    var standartFlex = getLowestPrice("Flex", _location);
    var greenFast = standartFast + fee;
    var greenFlex = standartFlex + fee;

    doCalc(a, b, standartFast, "#standartFast");
    doCalc(a, b, standartFlex, "#standartFlex");
    doCalc(a, b, greenFast, "#greenFast");
    doCalc(a, b, greenFlex, "#greenFlex");

    showResultPane();
}

function doCalc(a, b, c, outp) {
    var d = (a * b) - (c * b);
    $(outp).html(Math.round(d / 100)); //100 øre = kr 
    //$(outp).html(Math.round(d));
}


function getLowestPrice(type, loc) {
    var res = -1;
    $.each(priceCalculator, function(n, val) {
        if (val[2] == type) {
            var tmp = loc == "DK1" ? val[0] : val[1];
            tmp = parseFloat(tmp);
            if (res > tmp || res < 0)
                res = tmp;
        }
    });
    return res;
}

function getPrice(name, loc) {
    var res = -1;
    $.each(priceCalculator, function(n, val) {
        if (val[2] == name) {
            res = loc == "DK1" ? val[0] : val[1];
        }
    });
    return parseFloat(res);
}

function getConsumption() {
    var res = -1;
    $.each(consumptions, function(n, val) {
        if (val[0] == _b) {
            res = val[1];
        }
    });
    return parseFloat(res);
}
