/*global oAccountModule $*/
/**
 * Account Module Object. Transitional states for login account module.
 */
oAccountModule =  {

  /* @var boolean Used to store whether module is open */
  bModuleOpen: null,
  /* @var object Stores which HTML element is currently open */
  oOpenEl: null,
  /* @var object Key/Value pairs for specific types of module displays */
  oOptTypes: { stnd:'standard', mrtg:'mortgage',bnk:'banking',nf:'nf',nfbus:'nfbus',comm:'commercial',agri:'agribusiness',db:'definedbenefit',mutfnds:'mutfnds',brokerage:'brokerage',farm:'farm' },
  /* @var object Key/Value pairs for specific Login URLs */
  oLoginURLs: {
    stnd:'https://www.nationwide.com/access/web/login-control.x?pageType=standard-login',
    bnk:'https://bankonline.nationwidebank.com/bankonline/externalLogin.do',
    nf:'https://www.nationwide.com/access/web/login-control.x?pageType=standard-login',
    nfbus:'https://myplan.nwservicecenter.com/iApp/isc/cmd/Login',
    mrtg:'https://www.nationwidebankmortgage.com/custserv/Form-login.aspx',
    comm:'http://www.nationwide.com/commercial-billing.jsp',
    agri:'https://agportal.nationwideagribusiness.com/ag/insurance/!ut/p/kcxml/04_Sj9SPykssy0xPLMnMz0vM0Y_QjzKLN4j3DALJgFiO-pFQgXD9KLhkFEwySN9b39cjPzdVP0C_IDfCIMvEUREAkUJ5Fw!!/delta/base64xml/L0lDU0NUTzdvSko3dWFDU1lKQ2dwUkEhIS9vSG9RQUFJUUpBQU1ZeGpHTVVwakdLWXd4bUljRklVdUNBISEvNEpGaUNPc1RsRTZDdUEySnlpZEJYZnJDRlpzT2ZybUUvN19BXzM0OS8yL3dwcy5wb3J0bGV0cy5sb2dpbg!!',
    db:'https://nwprov.nwservicecenter.com/webapp/tdw/login.do',
    mutfnds:'https://nationwide.olaccess.com',
    brokerage:'http://nationwide.netxselect.com/'
},
  /* @var object Key/Value pairs for specific SignUp URLs */
  oSignUpURLs: {
    base:'http://www.nationwide.com/account-access/sign-up.jsp',
    stnd:'https://www.nationwide.com/access/register/signon-standard.htm',
    bnk:'/online-bank-services.jsp',
    nf:'/account-access/sign-up.jsp',
    nfbus:'https://myplan.nwservicecenter.com/iApp/isc/cmd/Login',
    mrtg:'https://www.nationwideadvantagemortgage.com/custserv/form-new-login.aspx',
    comm:'http://www.nationwide.com/commercial-billing.jsp',
    agri:'http://www.nationwideagribusiness.com/portal/portalLogin.htm',
    db:'https://nwprov.nwservicecenter.com/webapp/tdw/',
    mutfnds:'https://nationwide.olaccess.com',
    brokerage:'http://nationwide.netxselect.com/',
    farm:'http://farm.nationwideagribusiness.com'
},

  /**
   * Method
   * @param object to bind
   */
  init : function(oEl) {
    $('div.accountModWrapper').css({"display":"block"});
    oEl.bind('change', function(){oAccountModule.decide(this);});
    // Next two lines added for close button.
    oAccountModule.closeButton = $('#loginClose');
    oAccountModule.closeButton.bind('click', function() {
		$("select.fsOptions option:first-child").attr('selected','true');
		oAccountModule.optReset(); return false;
	});
  },

  /**
   * Method
   * Sets display appropriate modules based on LI clicked.
   * If the value is unrecognized, assume it is a URL and attempt to go to it.
   * @param object LI element of that clicked
   */
  decide:function(oEl) {
    // Next two lines added for close button.
    $("#myAccount input.req").val('');
    oAccountModule.closeButton.fadeIn('normal');
    this.optResetForm();
    switch(oEl.value) {
      case '1':
        this.optToggle(this.oOptTypes.nf, this.oLoginURLs.nf);
        this.optChangeSignUp(this.oSignUpURLs.nf);
        break;
      case '2':
        this.optToggle(this.oOptTypes.bnk, this.oLoginURLs.bnk);
        this.optChangeSignUp(this.oSignUpURLs.bnk);
        break;
      case '3':
        this.optToggle(this.oOptTypes.stnd, this.oLoginURLs.stnd);
        this.optChangeSignUp(this.oSignUpURLs.stnd);
        break;
      case '4':
        this.optToggle(this.oOptTypes.mrtg, this.oLoginURLs.mrtg);
        this.optChangeSignUp(this.oSignUpURLs.mrtg);
        break;
      case '5':
        this.optToggle(this.oOptTypes.mutfnds, this.oLoginURLs.mutfnds);
        this.optChangeSignUp(this.oSignUpURLs.mutfnds);
        break;
      case '9':
      	this.optToggle(this.oOptTypes.comm, this.oLoginURLs.comm);
      	this.optChangeSignUp(this.oSignUpURLs.comm);
      	break;
      case '10':
        this.optToggle(this.oOptTypes.agri, this.oLoginURLs.agri);
        this.optChangeSignUp(this.oSignUpURLs.agri);
        break;
      case '11':
        this.optToggle(this.oOptTypes.db, this.oLoginURLs.db);
        this.optChangeSignUp(this.oSignUpURLs.db);
        break;
      case '12':
        this.optToggle(this.oOptTypes.nfbus, this.oLoginURLs.nfbus);
        this.optChangeSignUp(this.oSignUpURLs.nfbus);
        break;
      case '13':
        this.optToggle(this.oOptTypes.brokerage, this.oLoginURLs.brokerage);
        this.optChangeSignUp(this.oSignUpURLs.brokerage);
        break;
      case '14':
        this.optToggle(this.oOptTypes.farm, this.oLoginURLs.farm);
        this.optChangeSignUp(this.oSignUpURLs.farm);
        break;
      case '-1':
        /*
         * For the disabled divider, in case it is selected in IE
         * (where 'disabled' doesn't work on OPTIONs).
         * Fall through, to treat the same as 0 ('Select');
         * If gave the divider option value='0',
         * Firefox 2 sometimes defaulted to it on load.
         * TODO: Since IE doesn't support the disabled attribute on OPTIONs,
         * we may want to consider something like the JavaScript workaround at
         * http://elmicoxcodes.blogspot.com/2007/05/activating-option-disabled-in-ie.html
         */
      case '0':
        this.optReset();
       break;
      default:
        // Assume the value is a URL to go directly to.
        window.location.href = oEl.value;
        break;
    }
  },

  /**
   * Method
   * Closes open options and resets appropriate params.
   */
  optReset:function() {
    // If there is no boolean found, return, logic not needed.
    this.optChangeSignUp(this.oSignUpURLs.base);
    if (!this.oOpenEl) { return; }
    this.oOpenEl.slideUp("fast");
    this.oOpenEl = null;
    this.bModuleOpen = null;
        oAccountModule.closeButton.fadeOut('normal');
        $('div.accountModWrapper > div.uiWrap > div.formElms').slideUp("fast",function(){
			$('div.accountModWrapper > div.uiWrap').removeClass('setUI');
            $("select.fsOptions option:first-child").attr('selected','true');
		});
    $('form.myAccountForm').attr('action','');
  },

  /**
   * Method
   * Clears all form fields when user switches to a different login type.
   */
  optResetForm:function() {
    for (var i in this.oOptTypes) {
      $('div.accountModWrapper > div.uiWrap > div.formElms > div.' + this.oOptTypes[i] + 'Expand > input.text').attr('value','');
    }
  },

  /**
   * Method
   */
  optChangeSignUp:function(signUpType) {
    $("li.signUp > a").attr("href",signUpType);
  },

  /**
   * Method
   * @param string Type of module to display
   * @param string URL of Form action
   */
  optToggle:function(sType,acctType) {
    $('div.' + sType + 'Expand > form.myAccountForm').attr('action',acctType);
    if (this.bModuleOpen && $('div.'+sType+'Expand').attr('class') === this.oOpenEl.attr('class')) {
      return;
    } else if(this.bModuleOpen) {
      this.oOpenEl.slideUp("fast");
    } else {
      $('div.accountModWrapper > div.uiWrap > div.formElms').css({"display":"block"});
      $('div.accountModWrapper > div.uiWrap > div.formElms > div.formButton').css({"display":"block"});
    }
    $('div.accountModWrapper > div.uiWrap').addClass('setUI');
    $('div.'+sType+'Expand').slideDown("fast");
    // Which module is open
    this.oOpenEl = $('div.'+sType+'Expand');
    // Set boolean for open module
    this.bModuleOpen = 1;
  }
};

$(document).ready(function() {
  // Loop through select nodes of options of Account Module
  oAccountModule.init($('select.fsOptions'));
});