var unit = "GB";
var light = "Редко";
var frequent = "Часто";
var heavy = "Много";
var veryheavy = "Очень много";

function init_calc() {

    var profiles = {
        "profile1": [2000, 210, 130, 3, 60, 20, 2],
        "profile2": [3200, 300, 180, 6, 90, 35, 5],
        "profile3": [4800, 480, 310, 9, 160, 60, 8],
        "profile4": [6000, 690, 440, 14, 220, 90, 14]
    };
    onstop = 0;

    //setting the 7 sliders
    jQuery("#slider1").slider({
        min: 0,
        max: 8000,
        step: 100,
        animate: true
    });
    jQuery("#slider2").slider({
        min: 0,
        max: 800,
        step: 3,
        animate: true
    });
    jQuery("#slider3").slider({
        min: 0,
        max: 500,
        step: 1,
        animate: true
    });
    jQuery("#slider4").slider({
        min: 0,
        max: 15,
        step: 1,
        animate: true
    });
    jQuery("#slider5").slider({
        min: 0,
        max: 250,
        step: 1,
        animate: true
    });
    jQuery("#slider6").slider({
        min: 0,
        max: 100,
        step: 1,
        animate: true
    });
    jQuery("#slider7").slider({
        min: 0,
        max: 15,
        step: 1,
        animate: true
    });

    // binding events to the listeners
    jQuery(".sliders div").bind("slidechange", function (event, ui) {
        refresh_handle(event, ui);
    }).bind("slide", function (event, ui) {
        refresh_handle(event, ui);
    }).bind("slidestop", function (event, ui) {
        refresh_total();
        onstop = 0;
    });

    //setting a anchor text to default value
    //$("#sliders div").trigger("slide");
    jQuery(".sliders div a").text("0");


    //settings defined profiles
    jQuery("#profiles a").each(function (index, domEle) {
        jQuery(domEle).click(function () {
            onstop = 0;
            $("span a").removeClass("encour");
            $(this).toggleClass("encour");

            jQuery(".sliders div").each(function (i, el) {
                jQuery(el).slider('value', profiles[domEle.id][i]);
            });
        });
    });

    //initializing grand total and bar graph on the first profile
}

function refresh_handle(event, ui) {
    jQuery(ui.handle).text(ui.value);
    if (event.type != "slidechange" && event.type != "slide") {
        refresh_total();
    } else if (event.type == "slidechange") {
        onstop++;
        if (parseInt(onstop, 10) == 7) {
            refresh_total();
            onstop = 0;
        }
    }
}

function refresh_total() {
    var max_profiles = []
    var max_bw = 22.6;
    var coef = [0.01, 0.333, 1, 300, 3, 60, 700];
    var total = 0;
    jQuery(".sliders div").each(function (i, el) {
        total += $(el).slider('value') * coef[i];
    });
    var total = (total / 1000);
    jQuery("div#total").text(total.toFixed(2) + ' ' + unit);
    if (total <= 3.995) {
        jQuery("div#profilcal").text(light);
        $("span a").removeClass("encour");
        jQuery('#profiles2 ul li#p1 a').addClass("encour");
    }
    if (total > 3.995 && total <= 7.995) {
        jQuery("div#profilcal").text(frequent);
        $("span a").removeClass("encour");
        jQuery('#profiles2 ul li#p2 a').addClass("encour");
    }
    if (total > 7.995 && total <= 12.995) {
        jQuery("div#profilcal").text(heavy);
        $("span a").removeClass("encour");
        jQuery('#profiles2 ul li#p3 a').addClass("encour");
    }
    if (total > 12.995) {
        jQuery("div#profilcal").text(veryheavy);
        $("span a").removeClass("encour");
        jQuery('#profiles2 ul li#p4 a').addClass("encour");
    }
    height = 236 - 236 * total / max_bw;
    //jQuery("#graph div:first").css("height",height);
    jQuery("#barre").animate({
        "height": height
    }, 'slow', 'swing');
}
