// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
remove_field = function(element, item) {
  element.up(item).remove();
}

replace_ids = function(s){
  var new_id = new Date().getTime();
  return s.replace(/NEW_RECORD/g, new_id);
}

var SearchSelector = Class.create({
  initialize: function(form)
  {
    this.form = form;
    
    this.order   = form.down("select[name=order]");
    this.license = form.down("select[name=license]");
    this.search  = form.down("input[name=search_for]")
    this.bind();
  },
  
  bind: function()
  {
    this.order.observe("change",this.submit.bind(this));
    this.license.observe("change",this.submit.bind(this));
  },

  submit: function(event)
  {
    event.stop();
    this.form.submit();
  }
});

function ShowBanner(banners)
{
  var imageToShow = banners[Math.floor(Math.random()*banners.length)];
  var imgElement = new Element("img", {'src': "/images/banners/" + imageToShow.img, 'alt': "Banner Image for the site"});
  if (imageToShow.href)
  {
    var link = new Element("a", {'href':imageToShow.href}).update(imgElement)
   $('bannerRotation').update(link);
  }
  else
  {
    $('bannerRotation').update(imgElement);  			        
  }

}

var load_category_selector = function(categories)
{
    $j.each(categories, function(id, category)
    {
      $j('#category_select').append($j("<option>"+ category.name + "</option>").attr("value", id));
    });

    $j('#category_select').change(function()
    {
      var selectedValue = $j('#category_select').val();

      var subCategorySelect = $j("#sub_category_select");
      subCategorySelect.children("option").remove();

      subCategorySelect.append($j("<option>All</option>").attr("value", selectedValue));
      $j.each(categories[selectedValue].children, function(idx, category)
      {
        var el = $j("<option></option>");
        el.attr("value", category.id);
        el.html(category.name);

        subCategorySelect.append(el)
      });
    });

    $j("#category_select").trigger("change");

}
