function page_size_changed(id, params) {
  var current_action = $('search_form').action;     
  assign_to_current_window_location( current_action, ( params + "&page_size=" + $F(id)) );    
}

function page_size_changed_using_window_location(id, params) {
  assign_to_current_window_location( window.location.href, ( params + "&page_size=" + $F(id)) );    
}

function page_sort_changed(id, params) {
  var current_action = $('search_form').action; 
  assign_to_current_window_location( current_action, ( params + "&sort=" + $F(id)) );
}

function assign_to_current_window_location( path, parameters ) {
  if( path.indexOf("?") > 0 ) {
    window.location = path + "&" + parameters;
  } else if (parameters.length > 0) {
    window.location = path + "?" + parameters;
  } else {
    window.location = path;
  }
}

function text_changed() {
  if (/^\d{5}(-\d{4})?$/.exec($F('text')) != null) {
    $('radius').enable();
  }
  else {
    $('radius').disable();
  }
}

function facebook_popup(url) {
  window.open(url, 'sharer', 'toolbar=0,status=0,width=626,height=436');
  return false;
}

function openNewWindow(location) {
  window.open(location, '_blank');
  return false;
}

function setupCategorySelection(object, method, subcategoryUrl) {
  var categorySelectId = method + '_categories',
      subcategorySelectId = method + '_subcategories',
      subcategoryWrapperId = method + '_subcategory_wrapper',
      listId = method + '_list',
      addButtonId = method + '_add',
      list = $(listId),
      categorySelect = $(categorySelectId),
      subcategoryWrapper = $(subcategoryWrapperId),
      addButton = $(addButtonId),
      categoryIds = {};

  $$('#' + listId + ' li').each(function(li) {
    var val = li.down('input').value,
        del = li.down('a');

    categoryIds[val] = li;

    Event.observe(del, 'click', function(e) {
      list.removeChild(li);
      delete categoryIds[val];
    });
  });

  Event.observe(categorySelect, 'change', function() {
    var val = Form.Element.getValue(categorySelect);

    addButton.hide();

    if (val != '0') {
      var url = subcategoryUrl;
      url += url.indexOf('?') ? '&' : '?';
      url += 'category_id=' + val;
      subcategoryWrapper.update('...');
      new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
          subcategoryWrapper.update('<select id="' + subcategorySelectId + '">' +
                '<option value="0">&lt;Any&gt;</option>' + transport.responseText + '</select>');
          addButton.show();
        }
      });
    } else {
      subcategoryWrapper.update('');
    }
  });

  Event.observe(addButton, 'click', function(e) {
	var subcategorySelect = $(subcategorySelectId);
	    subcategoryId = Form.Element.getValue(subcategorySelect),
        categoryId = Form.Element.getValue(categorySelect)
    var categoryName = categorySelect.options[categorySelect.selectedIndex].text;
    if (subcategoryId != '0') {
      var subcategoryName = categoryName + ': ' + subcategorySelect.options[subcategorySelect.selectedIndex].text;
    }
	buildCategoryInput(categoryId, '0', categoryName);
	if(subcategorySelect) {
      buildCategoryInput(categoryId, subcategoryId, subcategoryName);
    }
  });

  function buildCategoryInput(rootCategoryId, subcategoryId, name) {
    categoryId = subcategoryId == '0' ? rootCategoryId : subcategoryId,
    span = document.createElement('span'),
    input = document.createElement('input'),
    li = document.createElement('li'),
    del = document.createElement('a'),
    clear = document.createElement('div');

    if (categoryIds[categoryId]) {
      var li = categoryIds[categoryId],
          span = li.down('span');
      new Effect.Highlight(span);
      return;
    }

    categoryIds[categoryId] = li;

    del.innerHTML = 'x';
    del.title = 'Remove';
    del.href = 'javascript:;';
    li.appendChild(del);

    span.innerHTML = name;
    li.appendChild(span);

    input.type = 'hidden';
    input.value = categoryId;
    input.name = object + '[' + method + '][]';
    li.appendChild(input);

    clear.className = 'clear';
    li.appendChild(clear);

    Event.observe(del, 'click', function(e) {
      list.removeChild(li);
      delete categoryIds[categoryId];
    });

    list.appendChild(li);
    new Effect.Highlight(span);

    categorySelect.selectedIndex = 0;
    subcategoryWrapper.update('');
    addButton.hide();
  }
}


/* 
  Scripts that need to wait for the DOM to be loaded.
*/
document.observe("dom:loaded", function() {

  /* 
    Looks for clicks on the advertiser name on the Google Map popup.
    Scrolls the view done to the #directory element. This is handled
    through the event being bubbled up to the body.
  */
  Event.observe(document.body, "click", function(event) {
    var element = event.element();
    if( element && element.hasClassName('advertiser_name_link') ) {
      Effect.ScrollTo('directory');
      Event.stop(event);
    }
  });

});

