$(document).ready(function(){
	
	if ($('div.comment').length > 0) {
		$('div.comment p').jTruncate({length: 190});
	}
	
	$('.comments_popup').click(function(){
			comment_window = window.open($(this).attr('href'),'comment_window','toolbar=no, scrollbars=yes, menubar=no, width=480, height=500');
			return false;
			if (window.focus) { comment_window.focus(); }
		});
	
});

function addCommentToggle()
{
	if ($('#add-comment-form').css('display') == 'block') {
		$('#add-comment-form').slideUp();
	} else {
		$('#add-comment-form').slideDown();
	}
}

function hideAddComment()
{
	$('#add-comment-form').slideUp();
	$('#comment-username').val("");
	$('#comment-text').val("");
	$('#comment-captcha').val("");
}

function processCommentForm()
{
	$('#comment-form-errors').fadeOut();
	$('#comment-form-success').fadeOut();
	$('#comment-username').removeClass('comment-form-error');
	$('#comment-text').removeClass('comment-form-error');
	$('#comment-captcha').removeClass('comment-form-error');
	
	$.post("/comments-post.php", { 
		username: escape($('#comment-username').val()), 
		commenttext: escape($('#comment-text').val()), 
		captcha: escape($('#comment-captcha').val()),
		sip: escape($('#sip').val()),
		content_id: escape($('#comment-content_id').val()), 
		content_type: escape($('#comment-content_type').val()), 
		cdo: escape($('#comment-do').val()),
		comment_title: escape($('#comment-title').val()) },
		function(data){
			if (data.success == '1') {
				msg = '<p><strong>Your comment has been successfully submitted!</strong><br />We moderate each comment that gets posted onto our site, your comment will show up when a moderator approves your comment.</p>';
				$('#comment-form-success').html(msg).fadeIn();
				
				setTimeout("hideAddComment();$('#comment-form-success').fadeOut();",10000);
			} else {
				msg = '<p><strong>There were errors in your form, please review them below.</strong><br />';
				if (data.username == '1') {
					$('#comment-username').addClass('comment-form-error');
					msg += '&bull; Please enter a name to post with.<br />';
				}
				if (data.commenttext == '1') {
					$('#comment-text').addClass('comment-form-error');
					msg += '&bull; Please your comment.<br />';
				}
				if (data.captcha == '1') {
					$('#comment-captcha').addClass('comment-form-error');
					msg += '&bull; The image verification text you entered is invalid.<br />';
				}
				if (data.badwords == '1') {
					$('#comment-text').addClass('comment-form-error');
					msg += '&bull; Your comment contains foul language, please rephrase your comment.<br />';
				}
				msg += '</p>';
				$('#comment-form-errors').html(msg).fadeIn();
			}
		}, "json");
		
	return false;
}

/* jTruncate */
(function($){$.fn.jTruncate=function(h){var i={length:300,minTrail:20,moreText:"more",lessText:"less",ellipsisText:"...",moreAni:"",lessAni:""};var h=$.extend(i,h);return this.each(function(){obj=$(this);var a=obj.html();if(a.length>h.length+h.minTrail){var b=a.indexOf(' ',h.length);if(b!=-1){var b=a.indexOf(' ',h.length);var c=a.substring(0,b);var d=a.substring(b,a.length-1);obj.html(c+'<span class="truncate_ellipsis">'+h.ellipsisText+'</span>'+'<span class="truncate_more">'+d+'</span>');obj.find('.truncate_more').css("display","none");obj.append('<div class="clear_both">'+'<a href="#" class="truncate_more_link">'+h.moreText+'</a>'+'</div>');var e=$('.truncate_more_link',obj);var f=$('.truncate_more',obj);var g=$('.truncate_ellipsis',obj);e.click(function(){if(e.text()==h.moreText){f.show(h.moreAni);e.text(h.lessText);g.css("display","none")}else{f.hide(h.lessAni);e.text(h.moreText);g.css("display","inline")}return false})}}})}})(jQuery);