(function ($) {
  Drupal.behaviors.receiver = {
    attach: function(context) {
      var receiver = new Drupal.receiver();
      receiver.init();
    } 
  }
  
  Drupal.receiver = function() {
    this.init = function() {
      // Don't run if we ain't got it
      if ($('#receiver').length == 0) return;
      this.scorebox = $('#lcd');
      this.score = Drupal.settings.ctr.score;
      this.has_score = this.score > 0;
      this.format_form();
      this.animate_lcd();
      this.animate_meter();
    }
    
    // Form fanciness
    this.format_form = function() {
      //Replace label
      this.label = $('#go-form label[for="edit-name"]');
      var text = $.trim(this.label.html());
      var input = $('#edit-name');
      
      _this = this;
      
      input.bind('focus', {_this: _this}, function(event) {
        event.data._this._format_form(input, text, 'focus');
      });
      
      input.bind('blur', {_this: _this}, function(event) {
        event.data._this._format_form(input, text, 'blur');
      });
      
      this.label.hide();
      input.blur();
      
      $('#go-form form').bind('submit', function() {
        var val = $(this).find('#edit-name').attr('value');
        if (val == text || val == text + Drupal.settings.ctr_cursor || val == '') {
          alert(Drupal.t('Please enter a valid Drupal.org username'));
          return false;
        }
      });
      
    }
    
    // Toggle the label text as the value of the form for focus/blur
    this._format_form = function(input, text, action) {
      var default_value = $(input).attr('value');
      if (action == 'focus' && (default_value == text || default_value == text + Drupal.settings.ctr_cursor)) {
        $(input).attr('value', '').removeClass('blur').addClass('focus');
        this.cursorStop();
        this.label.show();
        return;
      }
      else if (action == 'blur' && default_value == '') {
        $(input).attr('value', text).addClass('blur').removeClass('focus');
        this.cursorStart(input);
        this.label.hide();
        return;
      }
      input.select();
      this.label.show();
    }
    
    // Start the flashing cursor @todo refactor this as <blink></blink> :)
    this.cursorStart = function(element) {
      this.cursorStop();
      this.cursor_element = element;
      this.cursor_orig = $(element).attr('value');
      _this = this;
      this.t = setTimeout('_this._cursor()', 500);
    }
    
    // Stop the flashing cursor
    this.cursorStop = function() {
      clearTimeout(this.t);
    }
    
    // Switch the cursor state
    this._cursor = function() {
      this.state = this.state == 'on' ? 'off':'on';
      if (this.state == 'on') {
        $(this.cursor_element).attr('value', this.cursor_orig + Drupal.settings.ctr_cursor);
      }
      else {
        $(this.cursor_element).attr('value', this.cursor_orig);
      }
      this.t = setTimeout('_this._cursor()', 500);
    }
    
    // The LCD display
    this.animate_lcd = function() {
      if (this.has_score) {
        this.scorebox.hide().fadeIn(800);
      };
    }
    
    // Meter animation init
    this.animate_meter = function() {
      this.meter_increment = 204;
      this.meter_x_start = 0;
      this.meter_y_start = -2244;
      this.meter_step = 0;
      if (this.has_score) {
        this.meter = $('div.meter');
        this._meter_move();
      };
    }
    
    // Callback to set background-position attr
    this._meter_pos = function(x,y) {
      return x + 'px ' + y + 'px';
    }
    
    // Do the animation
    this._meter_move = function() {
      if (this.meter_step == parseInt(this.score) + 0.5) {
        clearTimeout(this.meter_timeout);
        return;
      }
      x = this.meter_x_start;
      y =  this.meter_y_start + this.meter_step*this.meter_increment;
      this.meter.css('background-position', this._meter_pos(x, y));
      this.meter_step += 0.5;
      _this = this;
      this.meter_timeout = setTimeout('_this._meter_move()', this._meter_ease(15,10,this.meter_step/this.score));
    }
    
    // Adjust for easing
    this._meter_ease = function(speed, factor, done) {
      max = speed * factor;
      return Math.abs(0.5-done)*max + speed;
    }
    
  }
})(jQuery);;

