/*----------------------------*/
/* Web Public Member Profiles */
/*----------------------------*/

jQuery(document).ready(function($) {
  
  // create short reference to the page element
  var pg = $('div#rpr-pg-web-public-profile');
  
  // create a short reference to
  // the member profile on the page
  // and cache original height of 
  // information box for animations
  var p = $('.rpr-member-profile');
  if (!p) return;
  p.data('information-height', p.find('div.information').css('height'));
  
  // expand the height of the information box when the mini contact
  // form is display and collapse the height when it is closed
  p.find('.rpr-rounded-button.make-contact').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    var target = '635px';
    var numbers = p.find('div.contact-details p.phone-numbers');
    if (numbers.size()) target = '755px';
    p.find('div.information').animate({ height: target }, 1000, null, function() { });        
  });
  p.find('.rpr-rounded-button.view-profile').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    p.find('div.information').animate({ height: p.data('information-height') }, 1000, null, function() { });
    p.find('table.send div.ajax-status').hide();
  });
  
  // enable the special approved coach link to show 
  // an explanation to the general public about approval
  p.find('a.approval-explanation').click(function(e) {
    e.preventDefault();
    
    // make a clone of the basic lightbox content we will display
    var lb = pg.find('div.rpr-pg-lightbox-approval-explanation').clone();
    lb.removeClass('rpr-hidden');
    
    // add the proper closing behavior to the cloned link
    lb.find('.rpr-lightbox-closer').click(function(e) {
      e.preventDefault(); rpr__lightbox.close(); });
    
    // open the lightbox with
    // the html approval message
    rpr__lightbox.open(lb);
  });
  
  // provide functionality for the send message button
  p.find('table.send .send-message').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    
    // use a stored flag to prevent
    // multiple clicks from sending
    if (p.data('sending')) return;
    p.data('sending', true);
    
    // locate the enclosing form
    var f = $(this).parents('form');
    
    // the first task in our attempt to 
    // send the message is to show status
    var ajs = f.find('div.ajax-status');
    var ajsa = ajs.find('.activity');
    var ajss = ajs.find('.success');
    var ajsf = ajs.find('.failure');
    ajsa.show();
    ajss.hide();
    ajsf.hide();
    ajs.show();
    
    // hide all previous errors as well
    f.find('.rpr-form-error').hide();
    
    // define a function which will handle
    // both success and failure of our post
    var complete = function(data, status) {
      if (!(data instanceof Object)) data = new Object();
      
      // in all cases
      // activity is 
      // completed
      ajsa.hide();
      
      // first check for communiation problems
      // by checking the result and ajax status
      if ((!data.ok) || (status != 'success'))
        
        // show failure
        ajsf.show();
      
      // check for form field
      // errors reported in response
      else if (data.form_error)
      {
        // hide ajax status
        // as errors will be
        // shown on the form
        ajs.hide();
        
        // we allow only one error at a time show determine which is indicated and show it
        if (data.form_error == 'first_name') f.find('td.first_name .rpr-form-error').show();
        if (data.form_error == 'last_name') f.find('td.last_name .rpr-form-error').show();
        if (data.form_error == 'email') f.find('td.email .rpr-form-error').show();
        if (data.form_error == 'emailc') f.find('td.emailc .rpr-form-error').show();
        if (data.form_error == 'message') f.find('td.message .rpr-form-error').show();
      }
      
      // when a specific code
      // is returned show it
      else if (data.error)
      {
        // set the error code and show text
        ajsf.find('.code').text(data.error);
        ajsf.show();
      }
      
      // when the sent flag
      // indicates success
      else if (data.sent)
      {
        // show success
        // message
        ajss.show();
        ajsf.hide();
        
        // clear the form contents
        f.find('input').val('');
        f.find('select').val('');
        f.find('textarea').val('');
      }
      
      // unknown response
      // show the error
      else ajsf.show();
      
      // result the flag to allow
      // another sending action
      p.data('sending', false);
    };
    
    // compose and execute ajax request
    $.ajax({ url: 'web_profile.php',
             type: "POST",
             cache: false,               
             data: { action: 'send_message',
                     m: f.find('input[name=m]').val(),
                     first_name: f.find('input[name=first_name]').val(),
                     last_name: f.find('input[name=last_name]').val(),
                     city: f.find('input[name=city]').val(),
                     country: f.find('select[name=country]').val(),
                     timezone: f.find('select[name=timezone]').val(),
                     email: f.find('input[name=email]').val(),
                     emailc: f.find('input[name=emailc]').val(),
                     home_phone: f.find('input[name=home_phone]').val(),
                     work_phone: f.find('input[name=work_phone]').val(),
                     mobile_phone: f.find('input[name=mobile_phone]').val(),
                     preferred_method: f.find('select[name=preferred_method]').val(),
                     message: f.find('textarea[name=message]').val()
                   },
             dataType: 'json',
             success: complete,
             error: complete });
  });
    
  // enable the clear link within the ajax status window to clear
  p.find('table.send div.ajax-status a.clear').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    var ajs = $(this).parents('div.ajax-status');
    ajs.hide();
  });
  
});
