$(function(){

// SNAPSHOT TOOLTIP (only for development homepage test)
	$('ul.gallery li a').mouseover(function(e) {

		// Fetch info/data of picture
			// Cache image tag
			var image = $(this).find('img:first');

			var src = image.attr('src');
			var vote = image.attr('data-vote');
			var size = image.attr('data-size');
			var id = image.attr('data-id');
			var alt = image.attr('alt');
			var width = image.attr('width');
			var height = image.attr('height');

			var href = $(this).attr('href');
			var title = $(this).attr('title');

		// 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';
			vote = vote.slice(1);
		}
		// Add link in title text
			// Find first index of ':'
			var title_first = title.indexOf(':');
			// find last index
			var title_last = title.lastIndexOf(':');
			// Grab text segments into array
			var title_text = [title.slice(0, title_first), title.slice(title_first + 1, title_last), title.slice(title_last + 1)];
			// Recreate title
			title = title_text[0] + '<a href="' + href + '" title="' + alt + '">' + title_text[1] + '</a>' + title_text[2];
		
		// Build contents
		var contents = '<div class="head"></div><div class="content clearfix2"><div class="title"><h2>' + alt + ' (' + vote_sign + vote + ')</h2><a href="#"></a></div><p class="info"><strong>File Size:</strong> ' + size + ' kb</p><div class="photo';
		if (width == 90) {
			contents += ' tall';
		}
		contents += '"><a href="' + href + '" title="' + alt + '"><img src="' + src + '" width="' + width + '" height="' + height + '" alt="' + alt + '" /></a></div><p class="status">' + title + '</p><ul><li class="count' + vote_class + '">' + vote_sign + vote + '</li><li class="vote"><a href="http://www.profiletwist.com/support/development/ajax-rater.php?id=' + id + '&type=gallery&vote=up" class="up" title="Thumbs Up"></a><a href="http://www.profiletwist.com/support/development/ajax-rater.php?id=' + id + '&type=gallery&vote=down" class="down" title="Thumbs Down"></a></li><li class="snapshot"><a href="' + href + '">View Snapshot</a></li></ul></div>';

		// Check if toolexpand div already exists
		if ($('#toolexpand').length == 0) {
			contents = '<div id="toolexpand" data-id="' + id + '">' + contents + '</div>';
			$('#main').append(contents);
		}
		else {
			// Dump new contents into div
			$('#toolexpand').html(contents);
			// Update id of toolexpand
			$('#toolexpand').attr('data-id', id);
		}

		// Recreate event handlers
			// Keep toolexpand visible when hoverd over
			$('#toolexpand').mouseover(function() {

				$(this).stop(true).css('opacity', 1);

			}).mouseout(function() {

				// Fade tooltip out  
				$('#toolexpand').stop(true).animate({opacity: 1.0}, 1000).fadeOut('normal');

			});
			// Collapse link
			$('#toolexpand .title a').click(function(e) {

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

				// Change tooltip display to none
				$('#toolexpand').css('display', 'none');

			});
			// Ajax vote links
			$('#toolexpand 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('=') + 1, query_string.indexOf('&'));
				ajax_vote = query_string.slice(query_string.indexOf('vote=') + 5);

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

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

						// Update running vote in the image's data-vote

							// Cache image object
							var image = $('#develop .gallery ul li img[data-id=' + ajax_id + ']');

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

						// Update in toolexpand as needed
						if ($('#toolexpand[data-id=' + ajax_id + ']').length == 1) {

							// Fetch new running vote
							var vote = image.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
							$('#toolexpand[data-id=' + ajax_id + '] ul li:first').attr('class', 'count' + vote_class).text(vote_sign + vote);
						}
					}
				}

			});

		// Temporarily empty title attributes to prevent browser default action
			// Link title
			toolexpand_title = $(this).attr('title');
			$(this).removeAttr('title');
			// Image title
			$(this).find('img:first').attr('title','');

		// Get x and y coordinates of mouse relative to parent #main
		var offset = $('#body').offset();
		var y = e.pageY - offset.top;
		var x = e.pageX - offset.left;

		// Find out whether toolexpand should go above or below link
		var height_up = e.pageY - $(window).scrollTop();
		var height_down = $(window).height() - height_up;

		// If more room above link
		if (height_up > height_down) {
			// Find height of #toolexpand
			var offset_height = $('#toolexpand').height() + 25;
			y = Math.round(y - offset_height);
		}
		// Else more room below link
		else {
			y = Math.round(y + 25);
		}
		
		// Calculate x coordiante
		if (x < 200) {
			x = 40;
		}
		else if (x > 720) {
			x = 520;
		}
		else {
			x = Math.round(x - 200);
		}

		// Update positioning of .calendar and reset opacity to 1
		$('#toolexpand').css({'top': y, 'left': x, 'opacity': 1});

		// Fade tooltip in
		$('#toolexpand').stop(true).fadeIn('normal');

	}).mouseout(function() {  

		// Fade tooltip out  
		$('#toolexpand').stop(true).animate({opacity: 1.0}, 1000).fadeOut('normal');

		// Add title attribute back into link
		$(this).attr('title', toolexpand_title);
		// Remove empty title attribute in image
		$(this).find('img:first').removeAttr('title');

	});

// 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('=') + 1, query_string.indexOf('&'));
	ajax_vote = query_string.slice(query_string.indexOf('vote=') + 5);

	// Send ajax request
	$.get('support/development/ajax-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-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-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-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 comment id
		var id = $(this).parents('div.comments:first').attr('data-id');

		// Generate query string for ajax request
		var query_string = 'id=' + id;

		// Send ajax request to fetch all comments
		$.post('support/development/ajax-view-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-id=' + 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');

	});

});