/* $Id: gamertags.js,v 1.1.2.2 2010/01/24 23:16:48 pobster Exp $ */

Drupal.behaviors.gamertagsPSNSelectList = function(context) {
  $("input[name=psn_gamertag]", context).each(function() {
    Drupal.actNAOption(this.value);
    Drupal.toggleGamertags(this.value, true);
  }).keyup(function () {
    Drupal.toggleGamertags(this.value, false);
  });
}

Drupal.toggleGamertags = function(text, justHide) {
  // What to do if textfield is empty
  if (text == '') {
    switch (justHide) {
      // If this is the first time this form is displayed we just want to hide the selection box without any effects
      case true:
         $('div#edit-psn-region-wrapper').css('display', 'none', function() {
         });
        break;
      default: 
        $('div#edit-psn-region-wrapper').hide('slow', function() {
          Drupal.actNAOption(text);
        });
    }
  }
  // Else, the text contains something so check to see if it's hidden - if so, SHOW it
  else if ($('div#edit-psn-region-wrapper').is(':hidden') == true) {
    Drupal.actNAOption(text);
    $('div#edit-psn-region-wrapper').show('slow');
  }
}

Drupal.actNAOption = function(text) {
  // If the textfield IS empty then prepend N/A (and select it) if it doesn't already exist
  if (text == '' && $("select[name=psn_region]:eq(0)").val() != 'na') {
    $("select[name=psn_region]").prepend('<option value="na">N/A</option>');
    $("select[name=psn_region] option:eq(0)").attr("selected", "selected");
  }
  else if (text != '') {
    // Else remove it when the textfield does actually contain *something*
    $("select[name=psn_region] option[value='na']").remove();
  }
}