Event.addBehavior({
  '#simple_search' : function(){ 
    if(is_advanced_search()){
      return false;
    }
    else{
      this.show();      
    }

  },
  '#simple_search a.search' : function(){this.show()},
  '#simple_search a.search:click' : function(){
    $('advanced_search').show()
    $('simple_search').hide()
    return false;
  },
  '#advanced_search' : function(){ 
    if(is_advanced_search()){
      this.show() 
    }
    else{
      this.hide()
    }
  },
  '#advanced_search a.search' : function(){this.show()},
  '#advanced_search a.search:click' : function(){
    $('simple_search').show()
    $('advanced_search').hide()
    return false;
  },
  'input#go_button' : function() {
    this.remove()
  },
  'select.nav:change' : function() {
    window.location = "/discussions/categories/" + this.value
  },
  'table .discussion_comments' : function() {
    var regex = /discussion_message_(\d+)_content/
    var matches = this.id.match(regex)
    
    if(matches){
      var discussion_message_id = matches[1]

      var content_title = this.childElements().first()
      var title = content_title.cloneNode(true)

      title_div = $div({id: 'discussion_message_'+ discussion_message_id +'_title', style:'display: none'}, title)
      title_div.addClassName(this.classNames())
      this.parentNode.insertBefore(title_div, this)

      var shrink = $a({style:'cursor:default;'},"[-]")
      shrink.addClassName("shrink_expand")

      var expand = $a({style:'cursor:default;'},"[+]")
      expand.addClassName("shrink_expand")
      Element.insert(content_title, { after: shrink})
      Element.insert(title, {after: expand})
    }
  },
  'table .discussion_comments a.shrink_expand:click' : function() {
    var parentDiv = this.parentNode
    
    if (parentDiv.id.match(/content/)) {
      var id = parentDiv.id.replace(/content/,'title')
      parentDiv.hide()
      $(id).toggle()
    } else{
      var id = parentDiv.id.replace(/title/,'content')
      parentDiv.hide()
      $(id).toggle()
    }
    return false;
  }
})

function is_advanced_search(){
  var queryString = window.top.location.search.substring(1);
  var other_params = Object.keys(queryString.toQueryParams()).without('x').without('y').without('query').length
  if (other_params > 0) {
    return true
  }
  else{
    return false
  }
}