(function(jQ) {

getDirectoryTypeOption = function(locations_url, categories_url, features_url) {

  var type = jQ('#type').val();

  if (type != 0)
  {
    var data = { type : type };

    jQ.post(locations_url, data, function (data)
    {
      if (data != '')
      {
        if (jQ('#area_id1_box'))
        {
          jQ('#area_id1_box').remove();
        }
        if (jQ('#area_id2_box'))
        {
          jQ('#area_id2_box').remove();
        }
        if (jQ('#area_id3_box'))
        {
          jQ('#area_id3_box').remove();
        }

        jQ('#area_id0_box').replaceWith(data);
      }
    });

    jQ.post(categories_url, data, function (data)
    {
      if (data != '')
      {
        jQ('#categories').show();
        jQ('#directory_categories').html(data);
      }
      else
      {
        jQ('#categories').hide();
      }
    });

    jQ.post(features_url, data, function (data)
    {
      if (data != '')
      {
        jQ('#features').show();
        jQ('#directory_features').html(data);
      }
      else
      {
        jQ('#features').hide();
      }
    });
  }
}

wordCount = function ()
{
  var words = jQ('#words_allowed').html();
  var total_words = jQ('#directory_listing_description').val().split(' ').length;
  if (total_words > words)
  {
	var value = jQ('#directory_listing_description').val();
    jQ('#directory_listing_description').val(value.substring(0, value.length - 2));
  }

  jQ('#display_count').html(total_words);
}

changeSubscriptionType = function ()
{
  var st = jQ('#directory_listing_subscription_type_id').val();
  var words_allowed = jQ('#st_' + st).html();
  var words = jQ('#directory_listing_description').val().split(' ');
  var output = '';
  for (i = 0; i < words.length; i++)
  {
    if (i == words_allowed)
    {
      break;
    }
    output += words[i] + ' ';
  }
  jQ('#words_allowed').html(words_allowed);
  jQ('#directory_listing_description').val(output);
}

/* --- AREAS --- */

var initAreaRow = function (row) {
  jQ('.remove', row).hover(function () {
    row.addClass('remove');
  }, function () {
    row.removeClass('remove');
  }).bind('click', function () {
    row.remove();
    if (jQ('.directory-listing-areas li').length === 0) {
      jQ('.directory-listing-areas').append('<li class="empty">No specific areas associated</li>');
    }
  });
};

jQ(function () {
  
  jQ('.directory-listing-add .add').hover(function () {
    jQ(this).parents().addClass('add');
  }, function () {
    jQ(this).parents().removeClass('add');
  }).bind('click', function () {
    var add = jQ('.directory-listing-add');
    add.addClass('loading');
    jQ.get('/backend.php/directory_listings/getArea', { id: jQ('select', jQ(this).parents()).eq(0).val() }, function (response) {
      jQ('.directory-listing-areas .empty').remove();
      response = jQ(response);
      add.removeClass('loading');
      if (jQ('#' + response.attr('id')).length === 0) {
        jQ('.directory-listing-areas').append(response);
        initAreaRow(response);
      }
      else {
        alert('This area is already associated');
      }
    }, 'html');
  });

  jQ('.directory-listing-areas li').each(function () {
    var row = jQ(this);
    initAreaRow(row);
  });

});

})(jQuery);