/*******************************************************************************
*  Базовые функции  ************************************************************
*******************************************************************************/


// проверяем правильность формата эл. адреса

function check_email_format (email)
{ 
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return filter.test(email);
}



// показываем поп-ап форму

function show_popup_form(frm_name)
{
  $('#popup-frm-'+frm_name+'-wrap').removeClass('hidden');
  if ($('#popup-frm-'+frm_name+'-wrap .txt').size() > 0) $('#popup-frm-'+frm_name+'-wrap .txt')[0].focus();
  $(document).bind('keydown', 'Esc', function() { hide_popup_form(frm_name) } );
}


// прячем поп-ап форму

function hide_popup_form(frm_name)
{
  $('#popup-frm-'+frm_name+'-wrap').addClass('hidden');
}


// показываем / скрываем что-нибудь



	
// select to input

jQuery(function($) {
    $('select.leader').change(function()
    {
      var $select = $(this), $input = $select.next();

      $select.val() == 'другое'
        ? $input.attr('name', $select.attr('name'))
                    .show()
        : $input.removeAttr('name')
                    .hide();
    }).triggerHandler('change');
});


/* проверяем адрес */

var invitation_email_checked = false;
function check_invitation_email (email)
{
  if ( ! check_email_format( $('#invitation-form .email').val() ) )
  {
    $('#invitation-form .email').addClass('text-red');
    invitation_email_checked = false;
  }
  else
  {
    $('#invitation-form .email').removeClass('text-red');
    invitation_email_checked = true;
  }
}

function send_invitation ()
{
  if (invitation_email_checked)
  {
    $('#invitation-form .load').removeClass('hidden');
    $.post('/ajax/send_invitation', {inv_email: $('#invitation-form .email').val() }, function(data) {
      if (data.status)
      {
        Alert.show('', 'Ваше приглашение отправлено', 'message');
        $('#invitation-form .email').val('');
      }
      else
      {
        Alert.show('', 'Произошла какая-то ошибка', 'error');
      }
      $('#invitation-form .load').addClass('hidden');
    }, "json");
  }
}



function activate_textarea_buttons (textarea)
{
  var ts = textarea;
  $('.textarea-buttons .bold').click( function() { DocumentSelection.wrapSelection(ts, "[b]","[/b]"); } );
  $('.textarea-buttons .italic').click( function() { DocumentSelection.wrapSelection(ts, "[i]","[/i]"); } );
  $('.textarea-buttons .under').click( function() { DocumentSelection.wrapSelection(ts, "[u]","[/u]"); } );
  $('.textarea-buttons .cross').click( function() { DocumentSelection.wrapSelection(ts, "[s]","[/s]"); } );
  $('.textarea-buttons .url').click( function() {
    var s = prompt("Введите ссылку");
    if (s != null)
    {
      var s2 = prompt("Введите текст для ссылки");
      if (s2 != null)
        DocumentSelection.wrapSelection(ts, "[url="+s+"]"+s2,"[/url]");
    }
  } );
  if ( $('.textarea-buttons .img').size() > 0)
  {
    $('.textarea-buttons .img').click( function() {
      var s = prompt("Введите ссылку");
      if (s != null)
      {
        DocumentSelection.wrapSelection(ts, "[img]"+s,"[/img]");
      }
    } );
  }
  if ( $('.textarea-buttons .cut').size() > 0)
    $('.textarea-buttons .cut').click( function() { DocumentSelection.wrapSelection(ts, "[cut]","[/cut]"); } );
}