var unit= "GB";
var light= "Light";
var frequent= "Frequent";
var heavy= "Heavy";
var veryheavy= "Very Heavy";
function init_calc(){

    var profiles = {
            "profile1": [2000,200,50,6,60,20,1],
            "profile2": [5000,500,150,10,100,25,4],
            "profile3": [8000,800,250,18,120,40,6],
            "profile4": [10000,1000,400,24,150,100,15]
            };
    onstop = 0;

    //setting the 7 sliders
    jQuery("#slider1").slider({min: 0, max: 10000, step:100, animate: true});
    jQuery("#slider2").slider({min: 0, max: 1000, step:3, animate: true});
    jQuery("#slider3").slider({min: 0, max: 400, step:1, animate: true});
    jQuery("#slider4").slider({min: 0, max: 24, step:1, animate: true});
    jQuery("#slider5").slider({min: 0, max: 150, 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 = 11.06;
      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 <= 1.805)
      {
    	  jQuery("div#profilcal").text(light);
    	  $("span a").removeClass("encour");
    	  jQuery('#profiles2 ul li#p1 a').addClass("encour");
      }
      if(total > 1.805 && total <= 3.005)
      {
    	  jQuery("div#profilcal").text(frequent);
    	  $("span a").removeClass("encour");
    	  jQuery('#profiles2 ul li#p2 a').addClass("encour");
      }
      if(total > 3.005 && total <= 4.505)
      {
    	  jQuery("div#profilcal").text(heavy);
    	  $("span a").removeClass("encour");
    	  jQuery('#profiles2 ul li#p3 a').addClass("encour");
      }
      if(total > 4.505)
      {
    	  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');
  }
