// jquery ready
$(document).ready(function () {
  // show on load
  $('.switchFriendDropdown').show();
  
  // stripe table
  $('.editForm tr:odd').removeClass('alt');
  $('.editForm tr:even').addClass('alt');
  
  // request friendship
  $('.switchFriendDropdown').click(function(e) {
    var pp = $(this).parent().parent();
    var dropdown = $('.friendDropdown', pp);
    
    if (dropdown.is(':hidden')) {
      $('.friendDropdownText', pp).hide();
      dropdown.show();
      $('.friendDropdownManual', pp).attr('value', '0');
    } else {
      dropdown.hide();
      var text = $('.friendDropdownText', pp);
      if (!text.attr('value')) {
        text.attr('value', dropdown.children(':first').attr('value')+((dropdown.children(':checkbox').is(':checked'))?' met':''));
      }
      text.show();
      $('.friendDropdownManual', pp).attr('value', '1');
    }
  });
  
  bindLabel();
  
});

function bindLabel() {
  // bind the export_u dropdown
  $('.export_u').bind('change', function(e) {
    var v = $(this).attr('value');
    if (v == '1') {
      $(this).next().show();
    }
    else {
      $(this).next().hide();
    }
  });
  // bind the label itself
  $('.export_u_label').bind('focus', function(e) {
    if ($(this).attr('value') == 'label...') {
      $(this).attr('value', '');
      $(this).css('color', '#000000');
    }
  });
  $('.export_u_label').bind('blur', function(e) {
    if ($(this).attr('value') == '' || !$(this).attr('value')) {
      $(this).attr('value', 'label...');
      $(this).css('color', '#dddddd');
    }
  });
  $('.export_u_label').bind('keyup', function(e) {
    var val = $(this).attr('value');
    var val2 = check_label(val);
    if (val != val2)
      $(this).attr('value', val2);
  });
  
  $('.export_u_label').focus().blur();
}

function check_label(str) {
  if (str.match(/^[a-z\d\-_,]{1,250}$/i)) {
    return str;
  }
  var ret = '';
  for (var item in str) {
    if (str[item].match(/^[a-z\d\-_,]{1,250}$/i)) {
      ret += str[item];
      if (str[item] == ',')
        ret += ' ';
    }
  }
  return ret;
}