jQuery(document).ready(function() {

  set_region_init();

  /***************************
    DropDown menu :: Выбор страны
  ***************************/
  jQuery(".dropdown_region div a").click(function() {
    jQuery(".dropdown_region ul").toggle(); /* Открываем и закрываем по нажатию */
  });

  /* Меняем номер телефона справа в колонке с Контактами при выборе Регионального Офиса из Select-menu */
  jQuery(".dropdown_region ul li a").click(function() {
    var abbr_country = (jQuery(this).find('span').attr('class'));
    var text = $( this ).html();

    /* ставим куку */
    $.cookie( 'selected_region', abbr_country, { 'expires': '+1d', 'path': '/' } );

    set_region( abbr_country, text );
  });

  jQuery(document).bind('click', function(e) {
    var $clicked = jQuery(e.target);
    if (! $clicked.parents().hasClass("dropdown_region"))
      jQuery(".dropdown_region ul").hide();
  });

  function set_region_init () {

    var key = $.cookie( 'selected_region' ); 
    if ( !key ) {
      return 1;
    }
  
    var text = '';
    $.each( $( '.dropdown_region ul li a' ), function( elem, val ) {
      if( $( val ).find( 'span' ).attr( 'class' ) == key ) {
        text = $( val ).html();
      }
    } );

    set_region( key, text );
  }

  /***************************
    Set region :: Устанавливаем в правом списке выбора страны - телефон и email по региону.
  ***************************/
  function set_region ( abbr_country, text ) {
    /* Пробегаем массив по сокращениям региона */
    $.each(countryArr, function(key, value) {
      if (abbr_country == key)  {  /* Если выбранный Регион из селекта соответствует массиву, то... */
        $.each(value, function(country, phone) {
          /*... меняем в колонке с контактами информацию на новую. */
          $('#country').html(country);
          $.each(phone, function(phone, mail) {  /* Пробегаемся по списку phone : mail */
            $('#phone').html(phone);
            $('a#email').attr('class', 'email_'+abbr_country);
            $('a#email').attr('href', 'mailto:'+mail+'?subject='+mail_query+mail+idp);
          });/* --/ jQuery.each :: phone -- */
        });/* --/ jQuery.each :: country -- */
      }
    });/* --/ jQuery.each :: countryArr -- */
    
    $(".dropdown_region div a span").html(text);
    $(".dropdown_region ul").hide();
  }

  $.each(countryArr, function(key, value) {
    $.each( $( '.regionalOffices a#email_contacts' ), function( elem, val ) {
      if( $( val ).attr( 'class' ) == 'email_'+ key) {  /* Находим, где есть ссылка вхождения на email */
        $.each(value, function(country, phone) {
          /*... меняем в колонке с контактами информацию на новую. */
          $.each(phone, function(phone, mail) {  /* Пробегаемся по списку phone : mail */
            $( val ).attr('href', 'mailto:'+mail+'?subject='+mail_query+mail+idp);
            jQuery('#area_code_'+key).html(phone);  /* Вставляем коды городов из JS-массива */
          });
        });
      }
    });
  });

});
