$(document).ready(function(){
	
	$('.openComments a').click(function(){
		var $commentContainer = $(this).parents('.openComments').next('.comments')
		if ($commentContainer.css('display') == 'none') {
			$commentContainer.slideDown().parent('.commentShell').ScrollTo(800);
		} else {
			$commentContainer.slideUp();
		}
		return false;
	});

	// when someone closes the comments section for a post
	$('.close').click(function(){
		$(this).parents('.comments').slideUp();
	});

	// submit the comment form via AJAX
	$('.speak').click(function() {
		var formBtn = $(this).parents('.commentForm');
		var postid = $(this).attr('id').split('_')[1];
		var form = '#form_' + postid;
		var actionPage = $(form).attr('action');
		var fVisitor = $('input[@name=visitor]',form).val();
		var fLink = $('input[@name=link]',form).val();
		var startTS = $('input[@name=startTS]',form).val();
		var fComment = $('textarea[@name=comment]',form).val();

		// only run the post if the visitor name and comments aren't blank
		if (fVisitor == '') {
			alert('Your name cannot be left blank');
		} else if (fComment == '') {
			alert('Comment cannot be left blank');
		} else {
			$.get( actionPage,
				{visitor: fVisitor, link: fLink, comment: fComment, fk_post_id: postid, startTS: startTS},
				function(data){
					//console.log(data);
					// place the returned string just above the form
					$(formBtn)
						.before(data.split('!!!')[1])
						.prev('.singleComment')
						.slideDown();

					// clear the form fields:
					$('input[@name=visitor]',form).val('');
					$('input[@name=link]',form).val('');
					$('textarea[@name=comment]',form).val('');
				}
			);

		}

		return false;
	}); 


/*
	$('.commentForm form').ajaxForm(function(data, status, $form) {
		// alert(data.split('!!!')[1]);
		// $(form).before(data.split('!!!')[1]);
		$('.commentForm').before(data.split('!!!')[1]).prev('.singleComment').slideDown();
	}); 
*/

});

function popUp(strURL,strType,strHeight,strWidth) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="fixedScrolling") strOptions="status,scrollbars,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	window.open(strURL, 'newWin', strOptions);
}
