// Standard jQuery header
;(function($) {

$(document).ready(function() {	
	// Hide Form Elemets
	// Function to show Comment Form and Hide Comments
	var commentNewPostTop;
	$("#CommentFormInit").click(function () { 
		// Hide Comments
		commentNewPostTop=$("#CommentNewPost").css('top');
		//alert(commentNewPostTop);
		$("#CommentNewPost").css({'top' : '0'});
		$("#CommentNewPost").css({'z-index' : '5'});
		$("#PageComments").hide();
		$("#CommentFormInit").hide();
		$("div.jScrollPaneContainer").hide();
		// Show form 
		$("#PageCommentInterface_Form_PageCommentsPostCommentForm fieldset").show();
		$("#CommentFormClose").show(); 
		$("#PageCommentInterface_Form_PageCommentsPostCommentForm_action_postcomment").show();
		
	});
	
	$("#PageCommentInterface_Form_PageCommentsPostCommentForm_action_postcomment").click(function () { 
		// Form
		var title=$("#PageCommentInterface_Form_PageCommentsPostCommentForm input[name=Title]").val();
		var name=$("#PageCommentInterface_Form_PageCommentsPostCommentForm input[name=Name]").val();
		var txt=$("#PageCommentInterface_Form_PageCommentsPostCommentForm input[name=Text]").val();
				
		// Check user inputs
		if(title && name && txt) {
			// Show Comments
			$("#PageComments").show();
			$("#CommentFormInit").show();
			$("#CommentNewPost").css({'z-index' : '-1', 'top' : commentNewPostTop});
			$("div.jScrollPaneContainer").show();
			// Hide Form
			$("#PageCommentInterface_Form_PageCommentsPostCommentForm fieldset").hide();
			$("#CommentFormClose").hide(); 
			$("#PageCommentInterface_Form_PageCommentsPostCommentForm_action_postcomment").hide();
		}else{
			$('PageCommentInterface_Form_PageCommentsPostCommentForm_error').show();
		}	
	});
		
	$("#CommentFormClose").click(function () { 
		// Show Comments
		$("#CommentNewPost").css({'top' : commentNewPostTop, 'z-index' : '-1'});
		$("#PageComments").show();
		$("#CommentFormInit").show();
		$("div.jScrollPaneContainer").show();
		// Hide Form
		$("#PageCommentInterface_Form_PageCommentsPostCommentForm fieldset").hide();
		$("#PageCommentInterface_Form_PageCommentsPostCommentForm_action_closecomment").hide(); 
		$("#CommentFormClose").hide();
		$("#PageCommentInterface_Form_PageCommentsPostCommentForm_action_postcomment").hide();
		$("#PageCommentInterface_Form_PageCommentsPostCommentForm_error").hide();
	});
		 	 	   	 
	// show / hide the facebook comment forms on klick on the link
	$('#PageComments a.addcommentlink').click(function () { 
		// Get rel Attribute of the link
	 	var rel=this.rel;
	 	// Form visibility
	 	if ($("div[rel$='"+rel+"']").is(":visible")){
	 		// Hide Form
	 		$("div[rel$='"+rel+"']").hide(200, function() {
	 			$('#PageComments').jScrollPane({showArrows:true, scrollbarWidth:11, arrowSize:11});
	 		});
	 	}else{
	 		// Show Form
	 		$("div[rel$='"+rel+"']").show(200, function() {
	 			// Reinitialize Scrollbar width open Form
	 			$('#PageComments').jScrollPane({showArrows:true, scrollbarWidth:11, arrowSize:11});
	 			// Measure Pixels to top of the div
		 		var divpos=$('#PageComments').position();
		 		var pos=$(this).position();
	 			// Scroll up, to make form visible, if it might be hidden
	 			if (divpos.top <= 0){
	 				document.getElementById('PageComments').scrollTo(pos.top);
	 			}
	 		});
	 	}
	});
	
	 // Add Scrollbars to Comment
	 $('#PageComments').jScrollPane({showArrows:true, scrollbarWidth:11, arrowSize:11});
	  
	// Standard jQuery footer
})
})(jQuery);
