
$(document).ready(function() {

  if ( isInteger( window.location.hash.substr(1) )  ){
    $('select#id_server').val( window.location.hash.substr(1) );
  }


	recount();
  init_vd();
  init_cd();
  
  show_srv_price();
  $('select#id_server').bind("change keyup", function(){
      show_srv_price();
      recount(); 
  });
  
	$('select#id_payment_type, #id_qty').bind("change keyup", function(){ recount() } );
  $('input#id_amount').bind("change keyup", recount_qty ); 
  
  show_delivery_tip();
  $('select#id_delivery_type').bind("change keyup", function(){
      show_delivery_tip();
  } );  
  
});

function show_delivery_tip(){
  var delivery_method_id = $('select#id_delivery_type').val();
  if (delivery_method_id) {
      tip = delivery_methods[delivery_method_id];
      $('span#dlvr-tip').html(tip);
  } else {
      $('span#dlvr-tip').html('');
  }
}

function show_srv_price(){
  var server_id = $("select#id_server").val();
  if (server_id != '') {
      var server_price = prices[server_id];
      $("span#srv-price").html("Цена: <b>{server_price} руб. за {price_per} {price_per_name}</b>".supplant({'server_price' : server_price.toFixed(2), 'price_per' : price_per, 'price_per_name' : price_per_name}));
  } else {
      $("span#srv-price").html("Выберите сервер, чтобы узнать цену");
  }
}

function show_dscd_srv_price(discount, server_price){
  // Показываем цену золота на сервере с учетом уже посчитанной скидки
  if (discount > 0) {
    var dsc_server_price = server_price * (1 - discount/100); 
    $("span#srv-price").html(
        "Цена: <strike>{server_price} руб.</strike> С учетом скидки - <b>{dsc_server_price} руб.  за {price_per} {price_per_name}</b>".supplant(
            {
                'server_price' : server_price.toFixed(2),
                'dsc_server_price': dsc_server_price.toFixed(2), 
                'price_per' : price_per, 
                'price_per_name' : price_per_name
            }
        )
    )
  } else {
      show_srv_price();
  }
}

function recount(must_set_amount){
  must_set_amount = typeof(must_set_amount) != 'undefined' ? must_set_amount : true;
  var server_id = $("select#id_server").val();
  var server_price = prices[server_id];
	var method = $("select#id_payment_type").val();
	var qty = $("#id_qty").val();
	var amount = server_price*payment_systems[method]['multiplier']*qty/price_per;
	var discount = get_discount(amount, payment_systems[method]['currency'] ); 
	show_result(amount, discount, payment_systems[method]['unit'], must_set_amount);
	
	// показываем варнинг
	show_warning(method);
	// Показываем цену золота с учетом скидки
	show_dscd_srv_price(discount,server_price);
	
}

function recount_qty(){
  var amount = parseFloat($("input#id_amount").val());
  
  var server_id = $("select#id_server").val();
  var server_price = prices[server_id];
	var method = $("select#id_payment_type").val();
  
  var raw = get_raw(amount, payment_systems[method]['currency']);
    
  var qty = parseFloat(price_per*raw/(server_price*payment_systems[method]['multiplier']) ).toFixed(2);
  
  if (!isNaN(qty)){
      $('input#id_qty').val(qty);
  } else {
      $('input#id_qty').val('');
  }
  recount(false);
}


function get_raw(amount, currency_code){
  var raw = 0;
  d2 = (cd > fixed_discount) ? cd : fixed_discount;
  if (currency_code == 'RUR') {
      user_volume = v;
      table = discounts;
  } else if (currency_code == 'USD') {
      user_volume = u;
      table = usd_discounts;
  }
  
  for (var volume in table) {
      raw = amount/(1-(table[volume]/100))
      if ((raw + user_volume) >= volume) {
          return (table[volume] > d2) ? raw: amount/(1-(d2/100));
      }
  }  
  return amount/(1-(d2/100));
}









