/*
 * @object TysonCadenhead
 * @description This object holds all of the TysonCadenhead functions and config options
 */
var TysonCadenhead = {
	
	/*
	 * @function AddListeners
	 * @description Executed on page load, this function adds all the event listeners
	 */
	AddListeners: function(){

		$('.social [title]').tooltip({
			fadeInSpeed: 300
		});
		
		// Fires when a comment is submitted (for validation and Ajax submission)
		if ($('#_wp_unfiltered_html_comment').length === 0){
			$('.post-a-comment #submit').click(function(){
				TysonCadenhead.CommentSubmit();
				return false;
			});
		}
		
		// Gets the random portfolio image to show on the side
		portfolio.getRandom($('.portfolio-random'));
	},
	
	/*
	 * @function CommentSubmit
	 * @description Fires when the form is submitted for comments
	 * @param form The form to be submitted
	 */
	CommentSubmit: function(){
		
		// If the comment validates, it is submitted
		if (TysonCadenhead.CommentValidate()){
			$.ajax({
				url: location.protocol + '//' + location.host + '/wp-comments-post.php',
				data: {
					author: $('#author').val(),
					email: $('#email').val(),
					url: $('#url').val(),
					comment: $('#comment').val(),
					comment_post_ID: $('input[name=comment_post_ID]').val()
				},
				type: 'POST',
				success: function(){
					var personLink, date = new Date();
					if ($('#commentform #website').val()){
						personLink = $('#commentform #website').val();
					}
					else {
						personLink = 'mailto:' + $('#commentform #email').val();
					}
					$('#commentlist').append(
						'<li class="comment even thread-even depth-1">' + 
							'<p>' + $('#commentform #comment').val() + '</p>' +
							'<p>' +
								'<cite>' +
									'Comment by <a href="' + personLink + '>' + $('#commentform #author').val() + '</a>' +
								'</cite>' +
							'</p>' +
						'</li>'
					);
					$('#commentform input').each(function(){
						$(this).val('');
					});
					$('#commentform textarea').each(function(){
						$(this).val('');
					});
					$('#submit').val('Submit Comment');
					$('#commentform').prepend('<div class="form-message success-message">Your comment has been submitted!  Thanks for your feedback!</div>');
				},
				error: function(e){
					if (e.responseText.indexOf('Duplicate') !== -1){
						$('#commentform').prepend('<div class="form-message error-message">Oops!  It looks like this is a duplicate comment... maybe you have already said that?</div>');
					}
					else {
						$('#commentform').prepend('<div class="form-message error-message">There was a problem submitting your comment... sorry about that!</div>');
					}
				}
			});
		}
		
	},
	
	/*
	 * @function CommentValidate
	 * @decription Validates the comments to see if they can be submitted.  Adds the error classes and error messages if they are needed
	 */
	CommentValidate: function(){
		
		// This is an array of the fields to validate
		var fields = ['author', 'email', 'comment'], flagError = false;
		
		// Remove all of the old error markers
		$('#commentform p').each(function(){
			$(this).removeClass('error');
		});
		$('.form-message').remove();
		
		// Loop through the fields and flag anything that doesn't have a value
		for (var i = 0; i < fields.length; i++){
			if (!$('#' + fields[i]).val()) {
				$('#' + fields[i]).parent().addClass('error').keyup(function(){
					TysonCadenhead.CommentValidate();
				});
				flagError = true;
			}
		}
		
		// Do some client-side validation
		if (flagError) {
			
			// Add the error message
			$('#commentform').prepend('<div class="form-message error-message">Oh no!  It looks like you missed part of the form.  Please fix it and we can get this thing submitted!</div>');
			
			return false;
		
		}
		
		// If we pass the validation...
		else {
			
			return true;
			
		}
	},
	
	
	/*
	 * @function GetTwitter
	 * @description Renders the Twitter display to the page
	 */
	GetTwitter: function(){
		
		return new TWTR.Widget({
		  version: 2,
		  type: 'profile',
		  rpp: 3,
		  interval: 6000,
		  height: 107,
		  width: 250,
		  theme: {
		    shell: {
		      background: 'none',
		      color: '#3c1b14'
		    },
		    tweets: {
		      background: 'none',
		      color: '#3c1b14',
		      links: '#000'
		    }
		  },
		  features: {
		    scrollbar: false,
		    loop: false,
		    live: false,
		    hashtags: false,
		    timestamp: false,
		    avatars: false,
		    behavior: 'all'
		  }
		});
		
	}
	
};

/*
 * @function document.ready
 * @description Fires when the document is fully loaded
 */
$(document).ready(function(){
	TysonCadenhead.AddListeners();
});

