"use strict";
/*global Router, FormValidator, FormError*/
var EK = EK || {};
EK.EcommerceResources = {
  init: function () {
    EK.EcommerceResources.BudgetCalculatorStart.init();
    EK.EcommerceResources.BudgetCalculatorCommonForm.init();
    EK.EcommerceResources.HotTopics.init();
  }
};
EK.EcommerceResources.BudgetCalculatorStart = {
  init: function () {
    this.$form = $('#budget_calculator_start_form');
    this.bind();
  },
  bind: function () {
    this.$form.find('.start_btn:not(.guest_link)').live('click', $.proxy(function () {
      this.$form.trigger('submit');
      return false;
    }, this));
    this.$form.find('.sol-types_checkbox input').live('click', $.proxy(this.addCheckBoxes, this));
  },
  addCheckBoxes: function () {
    var $loginForm = $('#popup-login-form'),
        $signupForm = $('#popup-signup-form');

    this.createOrClearHidden($loginForm);
    this.createOrClearHidden($signupForm);
  },
  createOrClearHidden: function ($form) {
    var $hidden = $form.find('.budgetCalculator');
    if (!$hidden || !$hidden.length) {
      $form.append('<div class="budgetCalculator hidden"></div>');
      $hidden = $form.find('.budgetCalculator');
    } else {
      $hidden.html('');
    }
    var $checkboxes = this.$form.find('.sol-types_checkbox input:checked');
    if ($checkboxes.length) {
    	$checkboxes = $checkboxes.clone(false);
    	$checkboxes.attr('checked', 'checked');
    	$hidden.append($checkboxes);
    } else {
      $hidden.remove();
    }
  }
};
EK.EcommerceResources.BudgetCalculatorCommonForm = {
  errors: {
    'startFormEmptyTypes': 'Please select at least one solution type'
  },
  init: function () {
    this.$form = $('.budget-calculator-form');
    this.bind();
  },
  bind: function () {
    var t = this;
    this.$form.live('submit', function () {
      return t.validate.call(t);
    });
    this.$form.find('input[type=text]').live('keyup', function () {
      t.expandSubQuestion.call(t, $(this));
    });
  },
  expandSubQuestion: function ($el) {
    var val = $el.val() || '';
    val = parseInt(val, 10) || 0;

    var $next = $el.parents('.budget_calc-fields').next();
    if ($next.length && $next.hasClass('subquestions')) {
      if (val > 1) {
        $next.show();
      } else {
        $next.hide();
      }
    }
  },
  validate: function () {
    if (this.$form.attr('id') === 'budget_calculator_start_form') {
      return this.validateStartForm();
    } else {
      return this.validateCommonForm();
    }
  },
	validateCommonForm : function() {
		var fields = {};
		var regexps = {};
		this.$form.find('input[type=text]').each(function(i) {
			var input = $(this);
			var name 	= input.attr('name'), 
			    label 	= input.parents('.budget_calc-fields').find('.label').html() || input.parent().find('label').html(),
			    reg 	= input.parents('.budget_calc-fields').find('#validation').html();
			fields[name]	= label;
			regexps[name]	= reg;
		});
		try {
			FormValidator.checkFields.call(FormValidator, fields, '.budget-calculator-form');
			FormValidator.iterator.call(FormValidator, fields, '.budget-calculator-form', regexps);
			return true;
		} catch (e) {
			FormValidator.showErrors(e, '#budget_calculator_start_form_errors');
			return false;
		}
	},
  validateStartForm: function () {
    var checked = false;
    this.$form.find(':input').each(function (i) {
      if ($(this).attr('checked')) {
        checked = true;
      }
    });

    if (!checked){
      var e = new FormError(this.errors.startFormEmptyTypes);
      FormValidator.showErrors(e, '#budget_calculator_start_form_errors');
    } else {
      $('#budget_calculator_start_form_errors').hide();
    }

    return checked;
  }
};
EK.EcommerceResources.HotTopics = {
  init: function () {
    this.bind();
  },
  bind: function () {
    $('.top-level-topic .categ').live('click', function () {
      $(this).next().toggle();
    });
  }
};
$(function () {
  if (/ecommerce-resources/.test(window.location.pathname)) {
    EK.EcommerceResources.init();
  }
});

