$(function(){

// ADD SMILEY CODE TO MESSAGE
	$('#comments .post.comment ul li a:not(.extra-smileys)').click(function(e) {

		e.preventDefault(); // don't follow the link

		// Fetch class name from parent li
		var class_name = $(this).parents('li:first').attr('class');

		// Initialize symbol variable
		var symbol;
		
		// Cycle through and find matching text symbol representation
		switch (class_name) {
			case 'smile':
				symbol = ':)';
				break;
			case 'grin':
				symbol = ':D';
				break;
			case 'frown':
				symbol = ':(';
				break;
			case 'wink':
				symbol = ';)';
				break;
			case 'tongue':
				symbol = ':P';
				break;
			case 'shocked':
				symbol = '=O';
				break;
			case 'cool':
				symbol = '8)';
				break;
			case 'undecided':
				symbol = ':-/';
				break;
			case 'angel':
				symbol = '0=)';
				break;
			case 'helmet':
				symbol = ':helmet:';
				break;
			case 'monkey':
				symbol = ':monkey:';
				break;
			case 'angry':
				symbol = '>:(';
				break;
			case 'dog':
				symbol = ':o3';
				break;
			case 'cat':
				symbol = '=^.^=';
				break;
			case 'confused':
				symbol = ':-S';
				break;
			case 'cry':
				symbol = ":'(";
				break;
			case 'kiss':
				symbol = ':-*';
				break;
			case 'in_love':
				symbol = '<3';
				break;
			case 'present':
				symbol = ':present:';
				break;
			case 'flower':
				symbol = '@-}—';
				break;
			case 'cake':
				symbol = ':cake:';
				break;
			case 'camera':
				symbol = ':camera:';
				break;
			case 'music':
				symbol = ':music:';
				break;
			case 'afro':
				symbol = 'Oo';
				break;
			case 'facepalm':
				symbol = ':facepalm:';
				break;
			case 'reading':
				symbol = ':reading:';
				break;
			case 'rules':
				symbol = ':rules:';
				break;
			case 'devilish':
				symbol = '>:)';
				break;
			case 'examine':
				symbol = ':examine:';
				break;
			case 'face_wall':
				symbol = ':facewall:';
				break;
			case 'fail':
				symbol = ':fail:';
				break;
			case 'geek':
				symbol = ':geek:';
				break;
			case 'hammer_time':
				symbol = ':hammertime:';
				break;
			case 'idea':
				symbol = ':idea:';
				break;
			case 'keep_quiet':
				symbol = ':shh:';
				break;
			case 'ninja':
				symbol = ':ninja:';
				break;
			case 'party':
				symbol = ':party:';
				break;
			case 'pirate':
				symbol = ':pirate:';
				break;
			case 'praise':
				symbol = ":praise:";
				break;
			case 'quoted_for_truth':
				symbol = ':qft:';
				break;
			case 'sleeping':
				symbol = ':sleeping:';
				break;
			case 'stop':
				symbol = ':stop:';
				break;
			case 'talkative':
				symbol = ':talkative:';
				break;
			case 'thumb_up':
				symbol = ':thumbsup:';
				break;
			case 'touche':
				symbol = ':touche:';
				break;
		}

		// Pad symbol with spaces
		symbol = ' ' + symbol + ' ';

		// Add symbol to textarea
			// Cache textarea
			textarea = document.getElementById('textboxer');

			// IE support
			if (document.selection) {
				textarea.focus();
				sel = document.selection.createRange();
				sel.text = symbol;
			}
			//MOZILLA/NETSCAPE support
			else if (textarea.selectionStart || textarea.selectionStart == '0') {
				textarea.focus();
				var startPos = textarea.selectionStart;
				var endPos = textarea.selectionEnd;
				textarea.value = textarea.value.substring(0, startPos) + symbol + textarea.value.substring(endPos, textarea.value.length);
			}
			// Default to appending the smiley at the end
			else {
				textarea.value += symbol;
			}

	});

// EXPAND - MINIMIZE ADDITIONAL SMILEYS
	$('#comments .post.comment ul li.more a').click(function(e) {

		e.preventDefault(); // don't follow the link

		// Cache parent li
			var parent = $(this).parents('li:first');

		// Change title and span contents as needed
			// Change to "maxmize smileys and more"
			if (parent.hasClass('min')) {
				$(this).attr('title', 'More smiley icons');
				$(this).find('span:first').text('More');
			}
			// Change to "minimize smilieys and less"
			else {
				$(this).attr('title', 'Less smiley icons');
				$(this).find('span:first').text('Less');
			}

		// Toggle class to change icon picture
			parent.toggleClass('min');

		// Toogle more smileys list to display
		$('#comments .post.comment ul.more-icons').toggleClass('hidden');


	});

// COMMENT RATING THUMBS UP/DOWN
$('#comments .forum .group ul li.vote a').click(function(e) {

	e.preventDefault(); // don't follow the link

	// Extract query string
	var href = $(this).attr('href');
	var query_string = href.slice(href.indexOf('?') + 1);

	// Extract and save id and type of vote for ajax process_data function
	ajax_id = query_string.slice(query_string.indexOf('comment_id=') + 11, query_string.indexOf('&vote'));
	ajax_vote = query_string.slice(query_string.indexOf('vote=') + 5);

	// Send ajax request
	$.get('lib/ajax/comment-rater.php', query_string, process_data);

	// Function to process data returned
	function process_data(data) {
		if (data == 'success') {

			// Update running vote in data-vote count

				// Cache count_holder object
				var count_holder = $('#comments .forum .group[data-comment-id=' + ajax_id + '] ul li.count');

				// If vote was up, increment
				if (ajax_vote == 'up') {
					count_holder.attr('data-vote', parseInt(count_holder.attr('data-vote')) + 1);
				}
				// Else decrement
				else {
					count_holder.attr('data-vote', parseInt(count_holder.attr('data-vote')) - 1);
				}

			// Update count display (visible counter for user)

				// Fetch new running vote
				var vote = count_holder.attr('data-vote');

				// Determine class and sign of vote
				if (vote > 0) {
					var vote_sign = '+';
					var vote_class = ' green';
				}
				else if (vote == 0) {
					var vote_sign = '';
					var vote_class = '';
				}
				else {
					var vote_sign = '';
					var vote_class = ' red';
				}

				// Update the running vote with appropriate classes
				count_holder.attr('class', 'count' + vote_class).text(vote_sign + vote);
		}
	}

});

// SHOW/HIDE REPLY COMMENTS
	$('#comments .forum > .group ul li.comments a').click(function(e) {

		e.preventDefault(); // don't follow the link

		// Fetch data - number of comments and id
		var num_comments = $(this).attr('data-comments');
		var comment_id = $(this).attr('data-comment-id');

		// If reply comments exist
		if (num_comments > 0) {

			// Cache .comments box
			var comments_box = $('#comments .forum div.comments.id-' + comment_id);

			// Determine whether reply comments are shown
				// Hidden
				if (comments_box.hasClass('hidden')) {
					// If respond div doesn't exist, create one
					if (comments_box.find('div.respond').length == 0) {
						var contents = '<div class="respond"><form action="" method="post"><div class="photo"><a href="#" title="Caption Text"><img src="/images/gallery/80x60/user_comment.png" width="80" height="60" alt="User Comment"></a></div><label for="form_comment-name">Name:</label><input type="text" id="form_comment-name" name="name" /><label for="form_comment-gender">Gender:</label><select id="form_comment-gender" name="gender"><option value="empty" selected="selected">Select Gender</option><option value="male">Male</option><option value="female">Female</option></select><textarea name="message">Write a comment...</textarea><input type="hidden" name="comment_id" value="' + comment_id + '" /><input type="submit" name="post_reply" value="Post Comment" /></form></div>';
						comments_box.append(contents);
					}
					// Display comments box
					comments_box.removeClass('hidden');
				}
				// Shown
				else {
					// Hide comments box
					comments_box.addClass('hidden');
				}
		}
		// No reply comments exist, but comments box may have been already created
		else {
			// If comments box doesn't exist, create one
			if ($('#comments .forum div.comments.id-' + comment_id).length == 0) {

				// Create comments box
				var contents = '<div class="comments id-' + comment_id + '"><ul><li class="view-all" data-comment-id="' + comment_id + '>No reply comments</li><li class="cancel"><a href="#" title="Click to collapse"></a></li></ul><div class="respond"><form action="" method="post"><div class="photo"><a href="#" title="Caption Text"><img src="/images/gallery/80x60/user_comment.png" width="80" height="60" alt="User Comment"></a></div><label for="form_comment-name">Name:</label><input type="text" id="form_comment-name" name="name" /><label for="form_comment-gender">Gender:</label><select id="form_comment-gender" name="gender"><option value="empty" selected="selected">Select Gender</option><option value="male">Male</option><option value="female">Female</option></select><textarea name="message">Write a comment...</textarea><input type="hidden" name="comment_id" value="' + comment_id + '" /><input type="submit" name="post_reply" value="Post Comment" /></form></div></div>';
				$(this).parents('div.group:first').after(contents);

				// Recreate event handlers
				$('#comments .forum div.comments.id-' + comment_id + ' ul li.cancel a').click(function(e) {

					e.preventDefault(); // don't follow the link

					// Hide comments box
					$(this).parents('div.comments:first').addClass('hidden');

				});
			}
			// Comment box already exist
			else {
				// Cache .comments box
				var comments_box = $('#comments .forum div.comments.id-' + comment_id);
				// Determine whether reply comments are shown
					// Hidden
					if (comments_box.hasClass('hidden')) {
						// Display comments box
						comments_box.removeClass('hidden');
					}
					// Shown
					else {
						// Hide comments box
						comments_box.addClass('hidden');
					}
			}
		}

	});

// VIEW ALL REPLY COMMENTS
	$('#comments .forum div.comments ul li.view-all a').click(function(e) {

		e.preventDefault(); // don't follow the link

		// Extract board id and comment id
		var board_id = $(this).parents('div.comments:first').attr('data-board-id');
		var comment_id = $(this).parents('div.comments:first').attr('data-comment-id');

		// Generate query string for ajax request
		var query_string = 'board_id=' + board_id + '&comment_id=' + comment_id;

		// Send ajax request to fetch all comments
		$.post('lib/ajax/view-reply-comments.php', query_string, processData);

		// Function to process data returned
		function processData(data) {

			// If comments fetch was successful
			if (data != 'error') {

				// Cache comments box
				var comments_box = $('#comments .forum .comments[data-comment-id=' + comment_id + ']');

				// Remove all previous reply comments
				comments_box.find('div.group').remove();

				// Add fetched reply comments, directly after first ul list (display comments/collapse button)
				comments_box.find('ul:first').after(data);

				// Fetch the total number of reply comments
				var comments = comments_box.attr('data-comments');

				// Change the display paragraph
				comments_box.find('ul li.view-all').text('Viewing all - ' + comments + ' of ' + comments + ' comments');
			}
		}

	});

// COLLAPSE REPLY COMMENTS
	$('#comments .forum div.comments ul li.cancel a').click(function(e) {

		e.preventDefault(); // don't follow the link

		// Hide comments box
		$(this).parents('div.comments:first').addClass('hidden');

	});

});