// var Autocompleter = Class.create({
//   initialize: function(element, autocomplete_url, key, initialFocus) {
//     this.form   = element.up('form')
//     this.tokens = element.down('.tokens')
//     this.hints  = element.down('.hints')
//     this.input  = element.down('input')
//     this.url    = autocomplete_url
//     this.key    = key
//   this.index  = 0
//  
//     this.input.observe('keyup', this.autocomplete.bind(this))
//     if (initialFocus) this.input.focus()
// 
//  this.input.observe('keydown', this.keypress.bind(this));
//    
//   },
//   onHover: function(e){
//  this.index=Event.findElement(e, 'DIV').id.split("div_hint_")[1]
//  this.render(e)
//    e.stop();
//   },
//   keypress: function(e){
//  
//    var cKeyCode = e.keyCode || e.which;
// 
//    switch(cKeyCode){
//      case Event.KEY_DOWN:
//          this.markNext(); 
//          this.render();
//          e.stop();
//          break;
//      case Event.KEY_UP: 
//          this.markPrevious(); 
//          this.render();
//          e.stop();
//          break;
//      case Event.KEY_RETURN:
//        this.addTokenOnEnter(e);
//        e.stop();
//        break;
//      }
//  },
// 
//   autocomplete: function(event) {
//     if (!event.metaKey && !event.altKey && !event.ctrlKey && event.keyCode >= 65 && event.keyCode <= 97 
//      || ( event.keyCode >= 48 && event.keyCode <= 57 ) || event.keyCode==8) {
//       new Ajax.Request(this.url, {'method': 'GET', 'parameters': {'q': $F(this.input)}, 'onComplete': this.compileHintsWithResultsFromRequest.bind(this)})
//     }
//   },
//   
//   compileHintsWithResultsFromRequest: function(transport) {
//     var result = transport.responseJSON
//     this.index = 0;
// 
//   this.entryCount=result.length
// 
//     if (result.length > 0) {
//       this.hints.update('')
//       for(var i=0; i<result.length; i++) {
//         var hint = this.createHint(result[i],i)
//         this.hints.insert(hint)
//       }
//    this.render()
//       this.hints.show()
//     } else {
//     this.hints.update('')
//       this.hints.hide()
//     }
//   },
// 
//   addTokenOnEnter: function() {
//   if (this.entryCount==0) return;
//  
//   var id    = this.getEntry(this.index).firstChild.readAttribute('rel')
//     var name  = this.getEntry(this.index).firstChild.innerHTML
//   this.addToken(id,name)
// 
//   },
//   addTokenOnClick: function(e) {
//     var id    = e.target.readAttribute('rel')
//     var name  = e.target.innerHTML
//   this.addToken(id,name)
//     e.stop()
//   },
//   
//   addToken: function(id,name) {
//     var token = this.createToken(id, name)
// 
//     this.tokens.insert(token)
//     this.form.insert(new Element('input', {'name': this.key+'[]', 'type': 'hidden', 'value': id, 'id': this.key+'_'+id}))
//     this.input.value = ''
//     this.input.focus()
//   this.hints.update("")
//     this.hints.hide()
//   this.entryCount=0
//   this.index=0
//   },
//   removeToken: function(e) {
//     var id = e.target.readAttribute('rel')
// 
//     $('token_'+id).remove()
//     $(this.key+'_'+id).remove()
//     this.input.focus()
//     e.stop()
//   },
//   
//   createToken: function(id, text) {
//     var token = new Element('span', {'id': 'token_'+id, 'class': 'token'}).update(text)
//     var close = new Element('img', {'class': 'x', 'src': '/images/icon-delete.png', 'rel': id, 'width': 12, 'height': 12})
// 
//     close.observe('click', this.removeToken.bind(this))
// 
//     token.insert(close)
// 
//     return token
//   },
//   
//   createHint: function(item, i) {
//     var hint = new Element('div', {'class': 'hint', 'id': 'div_hint_'+i})
//     var name = new Element('a', {'id': 'hint_'+item.id, 'href': '#', 'rel': item.id}).update(item.name)
// 
//     name.observe('click', this.addTokenOnClick.bind(this))
//   hint.observe("mouseover", this.onHover.bindAsEventListener(this));
//     hint.insert(name)
//     
//     return hint
//   },
// 
//   markPrevious: function() {
//  if(this.index > 0) {
//    this.index--
//  }
//   },
//   
//   markNext: function() {
//    if(this.index < this.entryCount -1){
//    this.index++
//  }
//  },
// 
//  render: function() {
//    if(this.entryCount > 0) {
//      for (var i = 0; i < this.entryCount; i++)
//        this.index==i ?
//          Element.addClassName(this.getEntry(i),"selected") :
//          Element.removeClassName(this.getEntry(i),"selected");
//    }
//  },
// 
//  getEntry: function(index) {
//  return $('div_hint_'+index);
//  }
// 
// })

var Publisher2 = Behavior.create({
	initialize: function() {
		var active = this.element.down('.active')
		if (active != null)
			this.current_panel_name = active.id.substring(4)
		else
			this.current_panel_name = 'status'
	},
	onclick: function(e) {
		var tab = e.findElement('li.tab')
		if (tab != null) {
			if (this.current_panel_name != null)
				this.hidePanel(this.current_panel_name)
			this.current_panel_name = tab.id.match(/^tab\-(\w+)/)[1]
	    this.showPanel(this.current_panel_name)
			e.stop()
		}
	},
  showPanel: function(panelName) {
     $('panel-' + panelName).addClassName('active')
     $('tab-' + panelName).addClassName('active')
  },
  hidePanel: function(panelName) {
     $('panel-' + panelName).removeClassName('active')
     $('tab-' + panelName).removeClassName('active')
  }
})

var Publisher = Class.create({
  initialize: function(element_id) {
    this.element = $(element_id);
    var active = this.element.down('.active')
    if (active != null) {
      this.current_panel_name = active.id.substring(4);
    } else {
      this.current_panel_name = "status";
    }
    this.setup();
  },
  
  setup: function() {
    this.element.select("li.tab").invoke("observe", "click", this.tabClickHandler.bind(this));
  },
  
  tabClickHandler: function(event) {
    event.stop();
    if (this.current_panel_name)
      this.hidePanel(this.current_panel_name);
    this.current_panel_name = event.currentTarget.id.match(/^tab\-(\w+)/)[1];
    this.showPanel(this.current_panel_name);
  },
  
  showPanel: function(panelName) {
     $("panel-" + panelName).addClassName("active");
     $("tab-" + panelName).addClassName("active");
  },
  
  hidePanel: function(panelName) {
     $("panel-" + panelName).removeClassName("active");
     $("tab-" + panelName).removeClassName("active");
  }
});

var VideoEmbed = Class.create({
  initialize: function(element) {
    this.element = element;
    this.thumb = this.element.down("img.thumb");
    this.embed = this.element.down("div.embed");
    this.setup();
  },
  
  setup: function() {
    this.thumb.observe("click", this.thumbClickHandler.bind(this));
  },
  
  thumbClickHandler: function(event) {
    this.thumb.hide();
    this.embed.show();
  }
});

VideoEmbed.init = function() {
  $$(".attachment.video").each(function(element) {
    new VideoEmbed(element);
  });
}

document.observe("dom:loaded", function() {
  // if ($("publisher")) new Publisher("publisher");
	Event.addBehavior({
		'#publisher': Publisher2
	})
  VideoEmbed.init();
});


var InlineComment = Behavior.create({
  initialize: function() {
    this.comment_id = this.element.id.match(/\d+$/)[0]
  },
  onclick: function(e) {
    e.stop()
    this.element.hide()
    $('expanded_comment_' + this.comment_id).show()
    return false
  }
})

var RemoveComment = Behavior.create({
  initialize: function(message) {
    this.message = message
  },
  
  onclick: function(e) {
    e.stop()
    if (confirm(this.message)) {
      new Ajax.Request(this.element.href, {method: "DELETE", parameters: {authenticity_token: AUTH_TOKEN}})
    }
  }
});