$(document).ready(function(){

  // remove bottom border from previous anchor on current selected side nav
  $('#nav ul li.current').prev().addClass('no-border');

  // add first and last classes to all lists, table rows, table heads and table cells in the DOM
  $('ul li:first-child,table tr:first-child,tr td:first-child, tr th:first-child, fieldset .field:first-child, #content article:first-child').addClass('first');
  $('ul li:last-child,table tr:last-child,tr td:last-child, tr th:last-child, fieldset .field:last-child, #content article:last-child').addClass('last');

  // prepare buttons/links for icon embelishment
  $('.button-link, button').wrapInner('<span>');
 
 
  // display example input info or field names within input
  $('.input-example').example(function() {
      return $(this).attr('title');
  });


  // Turn legends into h3s.
  $('legend').each(function(index) {
    $(this).replaceWith('<h3>' + $(this).text() + '</h3>');
  });


  // validation of form submissions
	$.validator.addMethod("password",function(value,element){
    return this.optional(element) || /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/i.test(value);  
  },"It is recommended as web professionals that you use a secure password with a minimum length of 8 characters, and at least one uppercase, a number and a lowercase character. Basically the longer and more random the better, you know the drill.");

  $('.validate-form, #comment_form, #entryform').validate({
		rules: {
			firstname: "required",
			lastname: "required",
			"category[]": "required",
			username: {
				required: true,
				minlength: 6
			},
			password: "required password",
			password_confirm: {
				required: true,
				equalTo: "#password"
			},
      telephone: {
        required: function(element) {
          return $('#mobile').val() == '';
        }
      },
      mobile: {
        required: function(element) {
          return $('#telephone').val() == '';
        }
      }
		},
		messages: {
      username: {
				// required: "Please provide your email address (will be used for your login username)",
				minlength: "Your username must be at least 6 characters long"
			},
      password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 8 characters long"
			},
			password_confirm: {
				required: "Please provide a confirmation password",
				equalTo: "Please enter the same password as above."	
			},
      telephone: {
        required: 'One phone number is required.'
      },
      mobile: {
        required: 'One phone number is required.'
      }
	  },
		errorPlacement: function(error, element) {
			if ( element.is(":checkbox") )
				error.insertAfter( element.closest("ul") );
			else
				error.insertAfter( element );
		}  
	});

	$('form#change-email-password-form').validate({
		rules: {
			password: "password",
			password_confirm: {
				equalTo: "#password"
			}
		},
		messages: {
      password: {
				minlength: "Your password must be at least 8 characters long"
			},
			password_confirm: {
				equalTo: "Please enter the same password as above."	
			}
	  }  
	});
	
	$('#profile_organisation').bind('keyup keypress blur', function() {  
    $('#profile_title').val($(this).val()); 
  });
	
  // show postal address in profile

  if ($('#profile_street_as_postal, #profile_street_as_postal input').is(':checked')) {
	  $('#postal-address').slideUp('fast');
	};

  $('#profile_street_as_postal, #profile_street_as_postal input').click(function() {
   if ($('#profile_street_as_postal, #profile_street_as_postal input').is(':checked')) {
     $('#postal-address').slideUp('fast',function(){
      $('#profile_postal_address').removeClass('required');
      $('#profile_postal_suburb').removeClass('required');
      $('#profile_postal_state').removeClass('required');
      $('#profile_postal_postcode').removeClass('required');
     });
   } else {
     $('#postal-address').slideDown('fast',function(){
      $('#profile_postal_address').addClass('required');
      $('#profile_postal_suburb').addClass('required');
      $('#profile_postal_state').addClass('required');
      $('#profile_postal_postcode').addClass('required');
     });
   }
  });

  
});
