function toggle_categories() {
  $('#' + $('#ad_type').attr('value').toLowerCase() + '-categories').show().siblings('.categories').hide();
}

function insert_category(category) {
  var q = $('#q').attr('value');
  if (q.indexOf(category) == -1) {
    $('#q').attr('value', $.trim(q + ' ' + category));
  } else {
    $('#q').attr('value', $.trim(q.replace(category, '')));
  }
}

$(function() {
  
  $('#ad').corner('8px');
  $('#comment-post').corner('8px');
  
  $('#stars').corner('4px');
  
  $('#comment-post textarea:not(.processed)').textareaResizer();
  
  // Advanced search
	 	
 if($('.advanced').get(0)) {
	 	
   if (typeof TextboxList != 'undefined') {
	 	
     toggle_categories();
	 	
     t = new TextboxList('#ad_tags', {
       unique: true,
       encode: function(o){ return o ? o.join(' ') : ''; },
       decode: function(o){ return o ? o.split(' ') : []; }
     });
	 	
     $('#ad_type').change(function() {
       toggle_categories();
     });
	 	
     $('.values input[type="checkbox"]').click(function() {
       insert_category($(this).attr('value'));
     });
	 	
     $('#tag-cloud a').click(function() {
       t.add($(this).text());	 	
       return false;
     });
   }
 }
  
  $('#ads-navigation > ul').bind('tabsselect', function(event, ui) {
    $('#secondary .filter a').attr('href', ui.tab.rel);
  });
    
});


function voteLoading() {
  $('#voting #score a').addClass('disabled');
  $('#voting .spinner').show();
}

function voteComplete() {
  $('#voting .spinner').hide();
  $('#voting #score a').removeClass('disabled');
}
