/*
  Orange Skin designer resources site
*/

var DOM = {
  methodFormSel:          'form.delete input[type=submit]',
  passwordSel:            'a.password',
  imgZoomSel:             'a.zoom',
  formToValidateSel:      'form.validate',
  ajaxFormsSel:           'form.ajax'
}

$(document).ready(function() {
  // Confirmation dialogs
  $(DOM.methodFormSel).click(function(event) {
    event.preventDefault();
    var resourceType = $(this).attr('data-data-type').replace(/(.*):.*/, "$1");
    var resourceName = $(this).attr('data-data-type').replace(/.*:(.*)/, "$1");
    if (confirm("Delete " + resourceType + " " + resourceName + "?")) {
      $(this).parent('form').submit();
    };
  });

  // Password reveal
  $(DOM.passwordSel).click(function(event) {
    event.preventDefault();
    var pw = $(this).attr('data-password');
    $(this).parent().html(pw);
  });

  // image zooming
  $(DOM.imgZoomSel).each(function(event) {
    $(this).fancyZoom({
      scaleImg: true,
      closeOnClick: true
    });
  });

  // Form validation
  $(DOM.formToValidateSel).validate({
    errorClass: 'invalid',
    messages: {
      'signup[firstname]': 'Please enter your first name',
      'signup[lastname]': 'Please enter your last name',
      'signup[email]': 'Please enter a valid email address',
      'signup[company]': 'Please enter your company’s name',
      'signup[street]': 'Please enter your street address',
      'signup[city]': 'Please enter your city',
      'signup[state]': 'Please enter your state',
      'signup[zipcode]': 'Please enter your zip code',
      'signup[phone]': 'Please enter your phone number'
    // },
    // TODO: submit and update via AJAX
    // submitHandler: function(form) {
    //   $(form).children('input[type=submit]').hide()
    //          .after('<img id="submit-loading" src="/images/submit.gif">');
    //   $.ajax({
    //     url: $(form).attr('action'),
    //     type: "POST",
    //     dataType: "json",
    //     data: $.param( $(form).children('input') )
    //     // complete: function() {
    //     //   $(form).children('#submit-loading').hide()
    //     //          .siblings('input[type=submit]').show();
    //     // },
    //     // success: function() {
    //     //   alert("yay");
    //     // },
    //     // error: function() {
    //     //   alert("boo");
    //     // }
    //   });
    //   return false;
    }
  });
});
