
var signupCurrentStep;

var MemberSignupController = Class.create({
  
  initialize:function()
  {
    // hide all steps but the current one
    this.goToStep(1);
    
    // step navigation links and buttons
    for (var i = 1; i <= 5; i++) {
      $('signup-jump-to-step-'+i).observe('click', this.goDirectlyToStep.bind(this));
    }
    $('member_signup_form').observe('submit', this.goToNextStep.bind(this));
    $('signup-steps-back').observe('click', this.goToPrevStep.bind(this));
    
    // setup 'other' position events
    $('position_title_field').observe('change', this.updatePositionOtherIsVisible.bind(this));
    this.updatePositionOtherIsVisible(false);
    
    // setup business description length calculation
    $('business_description_field').observe('keyup', this.updateBusinessDescriptionLength.bind(this));
    
    // setup events to keep track of the max number of business categories
    $$('input.business_category_option').each(function(businessCatEl) {
      businessCatEl.observe('change', this.businessCatsDidChange.bind(this));
    }.bind(this));
    
    // setup events to swap between individual/corporate specific fields
    $('member_type_field').observe('change', this.memberTypeDidChange.bind(this));
    this.memberTypeDidChange();
  },
  
  updatePositionOtherIsVisible:function(event)
  {
    var value = $('position_title_field').getValue();
    if (value == 'Other') {
      $('position_title_if_other_field').up().show();
      if (event) // only focus field if this is a genuine 'onchange' event
        $('position_title_if_other_field').focus();
    } else {
      $('position_title_if_other_field').up().hide();
      $('position_title_if_other_field').value = '';
    }
  },
  
  updateBusinessDescriptionLength:function(event)
  {
    var length = $('business_description_field').getValue().length;
    $('business_description_length').innerHTML = length;
    if (length > 250) {
      $('business_description_length').up().style.color = 'red';
      $('business_description_length').up().style.fontWeight = 'bold';
    } else {
      $('business_description_length').up().style.color = '';
      $('business_description_length').up().style.fontWeight = '';
    }
  },
  
  goToStep:function(stepNumber) // first step is 1, not 0.
  {
    // apply step
    signupCurrentStep = parseInt(stepNumber);
    
    // update page
    this.updateVisibleSteps();
    
    // scroll to the new step if the top of it (only if we will scroll up)
    var stepEl = $('signup-step-'+stepNumber);
    if (stepEl.viewportOffset().top < 0)
      stepEl.scrollTo();
  },
  
  goToNextStep:function(event)
  {
    // first kill the event. we will submit manually later if necessary
    event.stop();
    
    var nextStepNumber = signupCurrentStep + 1;
    
    if (nextStepNumber <= 5) { // go to next step
      var fieldsAreValid = this.validateFieldsInStep(signupCurrentStep);
      if (fieldsAreValid)
        this.goToStep(nextStepNumber);
    } else { // submit form
      var fieldsAreValid = this.validateFieldsInAllSteps();
      if (fieldsAreValid)
        $('member_signup_form').submit();
    }
  },
  
  goToPrevStep:function()
  {
    this.goToStep(signupCurrentStep - 1);
  },
  
  goDirectlyToStep:function(event)
  {
    var clickedDivId = event.findElement('div').id.toString();
    var stepNumber = clickedDivId.match(/[0-9]+$/)[0];
    this.goToStep(stepNumber);
  },
  
  validateFieldsInStep:function(stepNumber)
  {
    return formValidateController.validateFeildsInElement($('signup-step-'+stepNumber));
  },
  
  validateFieldsInAllSteps:function()
  {
    for (var i = 1; i <= 5; i++) {
      this.goToStep(i);
      var isValid = this.validateFieldsInStep(i);
      if (!isValid)
        return false;
    }
    
    return true;
  },
  
  updateVisibleSteps:function()
  {
    for (var i = 1; i <= 5; i++) {
      if (i == signupCurrentStep) {
        $('signup-step-'+i).show();
        $('signup-jump-to-step-'+i).setAttribute('class', 'step-on');
      } else {
        $('signup-step-'+i).hide();
        $('signup-jump-to-step-'+i).setAttribute('class', 'step');
      }
    }
    
    if (signupCurrentStep == 1)
      $('signup-steps-back').hide();
    else
      $('signup-steps-back').show();
  },
  
  businessCatsDidChange:function(event)
  {
    // count number of cats
    var countBusinessCats = 0;
    $$('input.business_category_option').each(function(businessCatEl) {
      if (businessCatEl.checked)
        countBusinessCats++;
    }.bind(this));
    
    if (countBusinessCats > 3)
      $('business_cats_invalid').show();
    else
      $('business_cats_invalid').hide();
  },
  
  memberTypeDidChange:function(event)
  {
    // are we a corporate member?
    var isCorporate = ($('member_type_field').getValue() == 'Corporate');
    
    // toggle elements which are only visible for individual members
    $$('.individual_membership_only').each(function(element) {
      if (isCorporate)
        element.hide();
      else
        element.show();
    }.bind(this));
    
    // toggle elements which are only visible for corporate members
    $$('.corporate_membership_only').each(function(element) {
      if (isCorporate)
        element.show();
      else
        element.hide();
    }.bind(this));
  }
  
});

document.observe('dom:loaded', function() {
  new MemberSignupController();
});
