/*
 * Author: Addo
 */
jQuery(function() {
	replyComment();
	changInfo();
	ajaxComment();
});

function replyComment() {
	jQuery('a.reply').click(function() {
		var id = jQuery(this).parents('li.comment').attr('id').split('-')[2];
		var authorId = 'author-' + id;
		var commentId = 'comment-' + id;
		var commentBoxId = jQuery('#comment').attr('id');
		reply(authorId, commentId, commentBoxId);
	});
	jQuery('li.comment').hover(function() {
		if (jQuery(this).find('#respond').length == 0) {
			jQuery(this).find('a.reply').slideDown('normal');
		}
	}, function() {
		jQuery(this).find('a.reply').slideUp('fast');
	});
}

function changInfo() {
	jQuery('a#change_info').click(function() {
		if (jQuery(this).text() == 'change') {
			jQuery(this).text('close');
			jQuery('#author_info').slideDown('fast');
		} else {
			jQuery(this).text('change');
			jQuery('#author_info').slideUp('fast');
		}
	});
}

function reply(authorId, commentId, commentBoxId) {
	var author = jQuery('#' + authorId).text();
	var linkStr = '<a href="#' + commentId + '">@'
			+ author.replace(/\t|\r|\n/g, '') + ': </a>';
	appendStr(linkStr, commentBoxId);
}

function appendStr(linkStr, commentBoxId) {
	var $commentBox = jQuery('#' + commentBoxId);
	if ($commentBox.val().indexOf(linkStr) > -1) {
		alert('You have already replyed this person!');
		return;
	}
	if ($commentBox.val().replace(/\t|\s|\r|\n/g, '').length == 0) {
		$commentBox.val(linkStr + '\n');
	} else {
		$commentBox.val($commentBox.val().replace(/[\n]*$/g, '') + '\n\n'
				+ linkStr + '\n');
	}
	$commentBox[0].focus();
}

function ajaxComment() {
	jQuery('#commentform').submit(function() {
		postData();
		return false;
	});
}

function postData() {
	var url = jQuery('#commentform').attr('action');
	var data = jQuery('#commentform').serialize();

	// ajax before
	var before = function() {
		jQuery('#submit').attr('disabled', 'true');
		jQuery('#ajaxerror').slideUp('normal');

		jQuery('#commentform')
				.append('<div id="overlay"/>')
				.append('<div id="ajaxerror" style=" display: none;padding: 5px;margin-top: 15px;color: #790E11;"/>');
		jQuery('#overlay').css( {
			display : 'none',
			position : 'absolute',
			top : 0,
			left : 0,
			width : '100%',
			height : '100%',
			opacity : 0.6,
			zIndex : 1000
		}).fadeIn('normal');
	};

	// ajax success
	var success = function(data, textStatus) {
		if (!/<li/.test(data)) {
			// textStatus is an error message.
			jQuery('#ajaxerror').text(data).slideDown('normal');
		} else {
			if (jQuery('ol#commentlist').length == 0) {
				// no comments yet.
				jQuery('#respond').before(
						'<ol id="commentlist" class="commentlist"></ol>');
			}
			jQuery('ol#commentlist').append(data);
			var $new = jQuery('ol#commentlist li:last');
			$new.find('.avatar').load(function() {
				$new.find('.pic').html(this);
			});
			if (jQuery('ol#commentlist li').length % 2 == 0) {
				$new.removeClass('even').addClass('odd');
			} else {
				$new.removeClass('odd').addClass('even');
			}
			$new.hide();
			$new.slideDown(1000);

			jQuery('#comment').val('');
			jQuery('#submit').removeAttr('disabled');

			// update comments count
			var count = jQuery('#comments_head .comments_count').html();
			jQuery('#comments_head .comments_count').html(
					jQuery('ol#commentlist li').length + ' '
							+ count.split(' ')[1]);

			// make this link scrolling smoothly
			$new.find('a[href^="#"]').click(function() {
				var target = jQuery(this).attr('href');
				jQuery.scrollTo(jQuery(target).offset().top, 800);
				return false;
			});
		}
	};

	// ajax error
	var error = function(request, status) {
		var message = request.responseText;
		jQuery('#ajaxerror').html(
				message.substring(message.indexOf('<p>') + 3, message
						.indexOf('</p>')));
		jQuery('#ajaxerror').slideDown('normal');
		jQuery('#submit').removeAttr('disabled');
	};

	// ajax complete
	var complete = function(request, status) {
		jQuery('#overlay').fadeOut(500, function() {
			jQuery(this).remove();
		});
	};
	// process ajax
	jQuery.ajax( {
		url : url,
		type : 'POST',
		data : data,
		beforeSend : before,
		success : success,
		error : error,
		complete : complete
	});
}
