function total_price()
{
  var total_cost = 9.75;
  
  // add up prices of all sliders
  $(".slider").each(function()
  {
    // get slider value
    var val = $(this).slider("value");
    
    // unlimited?
    if(val == this.max_val) cost = this.cost_unlimited;
    else var cost = Math.round(((val/this.interval)*this.cost_per_unit)*100)/100;

    total_cost+= cost;
  });
  
  // add bool prices
  $(".bool-option").each(function()
  {
    // is it checked?
    if(this.checked) total_cost+=parseFloat(this.value);
  });
  
  var raw_cost = total_cost;
  
  total_cost = Math.round(total_cost * 100) / 100;
  if(!((total_cost*10) % 10)) total_cost+=".00";
  else if(!((total_cost*10) % 1)) total_cost+="0";
  
  $("#custom_cost").text(total_cost);
  
  return raw_cost;
}

$("document").ready(function()
{
  // show the form
  $("form#create-custom-form").css({display:"block"});  
  
  // remove the hidden 'get_price' input
  $("#get_price_hidden").remove();
  
  // take out the update price buttons
  // remove order link
  $("a.custom_order_link").remove();
  
  // change update price text to 'Order'
  $("input.cv-submit").val("Order");
  
  // change new order button style
  $("input.cv-submit").addClass("hosting-buy-button");
  $("input.cv-submit").css("width", "80px");
  $("input.cv-submit").removeClass("cv-submit");
  
  // go through each select.slider, get relevant info and make a slider
  $("select.slider").each(function()
  {
    // get interval
    var interval = this.options[1].value - this.options[0].value;
    var min_val = this.options[0].value;
    var max_val = this.options[this.options.length-1].value;
    var selected_val = this.options[this.selectedIndex].value;
    var cost_per_unit = this.id;
    var cost_unlimited = this.title;
    var name = this.name;
    
    // in megabytes?
    if($(this).hasClass("MB")) var mb = true;
    else var mb = false;

    // deal with unlimited options
    if(max_val < 0) var max_val = this.options[this.options.length-2].value;

    if(selected_val < 0) selected_val = parseInt(max_val) + parseInt(interval);

    $(this).replaceWith("<div class='slider new'></div>");
    $("div.new").get(0).interval = parseInt(interval);
    $("div.new").get(0).min_val = parseInt(min_val);
    $("div.new").get(0).max_val = parseInt(max_val) + parseInt(interval);
    $("div.new").get(0).selected_val = parseInt(selected_val);
    $("div.new").get(0).cost_per_unit = parseFloat(cost_per_unit);
    $("div.new").get(0).cost_unlimited = parseFloat(cost_unlimited);
    $("div.new").get(0).mb = mb;
    $("div.new").get(0).name = name;
    $("div.new").removeClass("new");
  });
  
  $("div.slider").each(function()
  {
    // create the readout divs
    $(this).after("<div class='readout'><div>");

    // create sliders
    $(this).slider(
    {
      min: this.min_val,
      max: this.max_val,
      step: this.interval,
      value: this.selected_val,
      slide: function(e, ui)
      {
        // megbytes
        if(this.mb)
        {
          if(ui.value >= 1000)var disp_val = (ui.value/1000)+" GB";
          else var disp_val = ui.value+" MB";
        }
        else var disp_val = ui.value;
        if(ui.value == this.max_val) $(this).next(".readout").text("Unlimited");
        else $(this).next(".readout").text(disp_val);

        // one per email fields
        if($(this).parent().get(0).id=="email-master")
        {
          $(".one-per-email").text($(this).next(".readout").text());
        }
        
        total_price();
      },
      stop: total_price
    });
    
    // update the labels on initialise
    // megbytes
    if(this.mb)
    {
      if(this.selected_val >= 1000)var disp_val = (this.selected_val/1000)+" GB";
      else var disp_val = this.selected_val+" MB";
    }
    else var disp_val = this.selected_val;
    if(this.selected_val == this.max_val) $(this).next(".readout").text("Unlimited");
    else $(this).next(".readout").text(disp_val);

    // one per email fields
    if($(this).parent().get(0).id=="email-master")
    {
      $(".one-per-email").text($(this).next(".readout").text());
    }
  });
  
  // total cost on checkbox click
  $(".bool-option").click(function(){
    total_price();
  });
  
  // form submit action
  $("#create-custom-form").submit(function()
  {
    // create get string
    var get_string="";
    
    // sliders
    $(".slider").each(function()
    {
      // get slider value
      var val = $(this).slider("value");

      // unlimited?
      if(val == this.max_val) val = -1;

      get_string+=this.name+"="+val+"&";
    });

    // add bool prices
    $(".bool-option").each(function()
    {
      // is it checked?
      if(this.checked) get_string+=this.name+"=1&";
      else get_string+=this.name+"=0&";
    });
    
    var cost = total_price();

    document.location = "hosting-custom.php?h=0&"+get_string+"cost="+cost;
    
    return false;
  });
  
  total_price();
});