$(function(){

  var operators = new Array();
  operators['ru'] = [
    ['77777+|1046', 'Билайн, Мегафон, остальные'],
    ['77777+|1046|135,12', 'МTC']
  ];
  operators['kz'] = [
    ['77777+|9193', 'K-Cell, НеоТелеком, КарТел (Билайн)']
  ];
  operators['be'] = [
    ['77777+|5555', 'VELCOM, DIALLOG, Life :), МТС']
  ];
  operators['ur'] = [
    ['77777+|5280', 'Все национальные GSM-операторы']
  ];
  operators['az'] = [
    ['77777+|8602', 'Азерсел']
  ];
  operators['ar'] = [
    ['77777+|9785', 'Vivacell-MTS, Beeline']
  ];
  operators['gr'] = [
    ['77777+|9778', 'Magticom, Beeline']
  ];
  operators['md'] = [
    ['77777+|4055', 'MOLDCELL']
  ];
  operators['es'] = [
    ['inf&nbsp;77777+|15154', 'EMT, Elisa, TELE2 Esti']
  ];
  operators['la'] = [
    ['inf&nbsp;1077777+|1863', 'LMT, Tele2 LV, BITE LV']
  ];
  operators['li'] = [
    ['nfo&nbsp;477777+|1378', 'BITE LT, Omnitel, TELE2 LT']
  ];

  $(".payment_select_country").change(function(){
    load_operators();
    update_sms_data();
    update_cookies();
  });

  $(".payment_select_type").change(function(){
    update_sms_data();
    update_cookies();
  });

  function load_operators() {
    var country = $('.payment_select_country option:selected').val();

    $(".payment_select_type option").remove();
    for(var i = 0; i < operators[country].length; i++){
      $(".payment_select_type").append('<option value="' + operators[country][i][0] + '">' + operators[country][i][1] + '</option>');
    }
    $(".payment_select_type option:first").attr('selected', 'yes');
  }

  function update_sms_data() {
    var country = $('.payment_select_country option:selected').val();
    var operator_string = $(".payment_select_type option:selected").val();

    if(country == 'ur') {
      $("#sms-support-number").text('+38 (044) 2000-430');
      $("#sms-provider").show();
    } else {
      $("#sms-support-number").text('+7 (812) 331 03 64');
      $("#sms-provider").hide();
    }

    var temp = operator_string.split("|");
    $("#sms_number").find("span").text(temp[1]);
    $("#sms_prefix").text(temp[0]);
    if(temp.length > 2) {
      $(".sms-cost").show();
      $("#sms-cost").text(temp[2]);
    } else {
      $(".sms-cost").hide();
    }
  }

  function update_cookies() {
    $.cookie('country', $('.payment_select_country option:selected').val());
    $.cookie('operator', $(".payment_select_type option:selected").text());
  }

  if($.cookie('country')) {
    $(".payment_select_country option[value='" + $.cookie('country') + "']").attr('selected', 'yes');
    load_operators();
    if($.cookie('operator')) {
      $(".payment_select_type option:contains('" + $.cookie('operator') + "')").attr('selected', 'yes')
    }
    update_sms_data();
  }


});