"use strict";
/*global window, document*/
/*jslint white: true, devel: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true, indent: 2 */
var modal_dialog_properties = {
  close : false,
  escClose : false,
  overlayCss : {
    backgroundColor : 'black'
  },
  closeClass : 'close_modal_dialog_btn'
};

// ffc mailer form functionality
function ffc_steps_submit(frm) {
  frm = $('#' + frm);
  var data = {};
  var fields = [];

  $.each([ 'input', 'textarea', 'select' ], function (i, c) {
    $.each(frm.find(c), function (i, e) {
      if (e.name !== 'ffc-steps-url') {
        data[e.name] = e.value;
        if (e.name !== 'form-id' && e.name !== 'form-subject' && e.name !== 'form-subject-add') {
          fields.push(e.name);
        }
      }
    });
  });
  data['form-fields'] = $.toJSON(fields);

  var email_format_error = true;
  var email_validation_regexp = /^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.(?:[A-Za-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/;

  if (data.to && data.from) {
    if (data.to.match(email_validation_regexp) && data.from.match(email_validation_regexp)) {
      email_format_error = false;
    }
  } else {
    email_format_error = false;
  }

  if (!email_format_error) {
    $.ajax({
      url : frm.find('[name=ffc-steps-url]')[0].value,
      type : 'POST',
      data : data
    });
  } else {
    return false;
  }

  return true;
}

function ffc_steps_swap(x, y) {
  $('#' + x).hide();
  $('#' + y).show();
}

function ffc_steps_swap_timeout(x, y, timeout) {
  window.setTimeout(function () {
    ffc_steps_swap(x, y);
  }, timeout * 1000);
}

function wipe_after_submit() {
  $('.wipe_after_submit').val('');
}

$(document).ready(function () {
  // Search block
  $('#srch_keywords')
    .focus(function () {
      var input = $(this);
      if (!input.attr('data-active')) {
        input.val('');
        input.css('color', '#000000');
        input.attr('data-active', true);
      }
    })
    .blur(function () {
      var input = $(this);
      if (input.val() === '') {
        input.val('Keyword(s)').removeAttr('data-active');
        input.css('color', '#707070');
      }
    });

  // Cancel search if keywords are empty
  $('#providerDirectorySearchForm').submit(function (args) {
    var value = $('#srch_keywords').val();
    if (!value || !$.trim(value) || !$('#srch_keywords').attr('data-active')) {
      return false;
    }
    return true;
  });

  // Page open in popup window
  $('a.popup_window').click(function () {
    var link = $(this).attr('link');
    var title = $(this).attr('title');
    window.open(link, title, 'location=1,status=1,height=500,width=945,left=100,top=30,scrollbars=1');
  });

  // ffc mailer form functionality
  /*
  $('.ffc_mailer_form_submit').click(function () {
    if (ffc_steps_submit('req_form')) {
      ffc_steps_swap('req_step_01', 'req_step_02');
      var ffc_mailer_form_id = $('input#ffc_mailer_form_id').val();

      if (ffc_mailer_form_id === 'refer-a-friend') {
        ffc_steps_swap_timeout('refer_a_friend', 'req_step_00', 5);
        $('#popup_btns').addClass('oppc_display').hide();
        return false;
      }

      ffc_steps_swap_timeout('req_step_02', 'req_step_01', 4);
      wipe_after_submit();
    } else {
      ffc_steps_swap('req_step_01', 'req_step_error');
      ffc_steps_swap_timeout('req_step_error', 'req_step_01', 4);
    }
    return false;
  });
  */

  $('input.ffc_mailer_form_cancel').click(function () {
    $('#refer_a_friend').addClass('oppc_display').hide();
    return false;
  });

  // preload images
  jQuery.preloadImages = function () {
    jQuery.each(arguments, function (e) {
      jQuery("<img>").attr("src", this);
    });
  };

  // image popup
  $('.thumbnail').click(function () {
    $('.image_popup').modal(modal_dialog_properties);
  });
});