/*** Effects ***/

// Custom script.aculo.us effects
Effect.AppearDown=function(el,opts){
  el=$(el);
  return new Effect.Parallel([
    new Effect.Appear(el,opts),
    new Effect.BlindDown(el,opts)
  ]);
};
Effect.FadeUp=function(el,opts){
  el=$(el);
  return new Effect.Parallel([
    new Effect.Fade(el,opts),
    new Effect.BlindUp(el,opts)
  ]);
}

// Modify script.aculo.us defaults
Effect.DefaultOptions.duration=0.25;



/*** Utilities ***/

var DefaultValueTextField=Class.create({
  // Uses the Prototype JS framework.
  // For generating text input fields that are pre-filled with default values,
  // which disappear and reappear based on user clicks and input.
  
  // Usage (two steps):
  // 1. Add HTML: <input id="users-new-username" title="username" />
  // 2. Add JS:   new DefaultValueTextField($('users-new-username'));
  // This automatically uses the input's title attribute as the default value.
  
  initialize:function(input){
    this.input=input;
    this.value=input.title;
    this.input.title='';
    this.reset();
    this.input.observe('focus',this.onfocus.bindAsEventListener(this));
    this.input.observe('blur',this.onblur.bindAsEventListener(this));
  },
  reset:function(){
    this.input.setStyle({color:'#aaaaaa'}).value=this.value;
  },
  onfocus:function(){
    if($F(this.input)==this.value){
      this.input.setStyle({color:'#000000'}).value='';
      this.input.focus();
    }
  },
  onblur:function(){
    if($F(this.input).blank()){ this.reset(); }
  }
});

var MaxLengthTextField=Class.create({
  // Uses the Prototype JS framework.
  // For generating text input fields that have maximum lengths.
  
  // Usage (two steps):
  // 1. Add HTML: <textarea id="new-comment-text"></textarea>
  // 2. Add JS:   new MaxLengthTextField($('new-comment-text),200);
  
  initialize:function(input,limit){
    this.input=input;
    this.limit=limit;
    this.truncate();
    this.input.observe('keyup',this.truncate.bindAsEventListener(this));
    this.input.observe('keydown',this.truncate.bindAsEventListener(this));
    this.input.observe('blur',this.truncate.bindAsEventListener(this));
  },
  truncate:function(){
    this.input.value=$F(this.input).truncate(this.limit,'');
  }
});

var DynamicSubmitButton=Class.create({
  // Uses the Prototype JS framework.
  // For generating form submit buttons that are disabled and change to an
  // alternate text value when the form is submitted. For example, a button
  // labeled "Save" can be disabled and changed to "Saving..." on submit.
  
  initialize:function(button){
    this.button=button;
    if(this.form=this.button.up('form')){
      this.form.observe('submit',this.changeButton.bindAsEventListener(this));
    }
  },
  changeButton:function(ev){
    var newText='';
    switch($F(this.button)){
      case 'Login':
        newText='Logging in...';break;
      case 'Save':
      case 'Save changes':
        newText='Saving...';break;
    }
    if(newText!=''){this.button.value=newText}
    this.button.disabled='disabled';
    if(this.alt=this.button.next('span.alt')){this.alt.fade()}
  }
});

var PopUpControl = Class.create({
  // Pass the element that should trigger the popup when clicked;
  //   the url of the page that should be opened in a new window
  //   a hash of options that can be passed to window.open.
  //   The two keys that we look for specifically at this point
  //   are title and params. Title is the title of the popup page,
  //   and params is a string containing comma separated key value
  //   pairs that window.open accepts.
  initialize:function(cntrl, opts){
    this.cntrl=cntrl;
    this.opts=$H(opts) || $H({});
    this.cntrl.observe('click',this.pop.bindAsEventListener(this));
  },
  pop:function(event){
    event.stop();
    window.open(this.href(), this.name(), this.params());
  },
  href:function(){ return this.cntrl.readAttribute('href') },
  name:function(){ return this.opts.get('name') || '_blank' },
  params:function(){ return this.opts.get('params') || '' }
});

var RemoteLink = Class.create({
  initialize: function(a) {
    this.a = a;
    this.a.observe('click', this.act.bindAsEventListener(this));
  },
  act:function(event) {
    event.stop();
    
    new Ajax.Request(this.href(), {
      parameters: this.params(),
      method: this.method(),
      onSuccess: this.renderSuccess.bind(this),
      onFailure: this.renderFailure.bind(this),
      onComplete: this.renderComplete.bind(this)
    });
  },
  // not really much to do for default callbacks in this case....
  renderSuccess: function(xhr) {
    return;
  },
  renderFailure: function(xhr) {
    return;
  },
  renderComplete: function(xhr) {
    return;
  },
  href: function() {
    return this.a.readAttribute('href');
  },
  params: function() {
    return 'authenticity_token=' + encodeURIComponent(authenticityToken);
  },
  method: function() {
    return 'get';
  }
});

var VotingLink = Class.create(RemoteLink,{
  initialize: function($super,a) {
    $super(a);
    this.yourRating = $('your_osama_rating');
    this.a.observe('mouseover', this.mouseoverrating.bindAsEventListener(this));
    this.a.observe('mouseout', this.mouseoutrating.bindAsEventListener(this));
  },
  act: function($super, event) {
    this.a.up('ul').hide();
    $super(event);
  },
  mouseoverrating: function(ev) {
    this.yourRating.update(this.a.title[0]);
  },
  mouseoutrating: function(ev) {
    this.yourRating.update('?');
  },
  method: function() {
    return 'post';
  }
})

var DisabledVotingLink = Class.create({
  initialize: function(a) {
    this.a = a;
    this.a.observe('click', function(ev){ev.stop();});
  }
});



/*** Miscellaneous ***/

document.observe('dom:loaded',function(){
  $$('input[type=submit], input[type=image]').each(function(input){
    new DynamicSubmitButton(input);
  });
  
  $$('a[rel=external]').each(function(a){
    new PopUpControl(a);
  });
  
  $$('input.embed-code, textarea.embed-code').invoke('observe','click',function(ev){
    ev.element().activate();
  });
  
  $$('body#osamas-show div.main div.medium ul.enabled li a').each(function(link) {
    new VotingLink(link);
  });
  
  $$('body#osamas-show div.main div.medium ul.disabled li a').each(function(link) {
    new DisabledVotingLink(link);
  });

  // Put any additional JavaScript here that doesn't fit the sections above.
});
