//this script determines how close to the pagiDiv the mouse gets, 
//if the distance gets to be less than 400 px we'll call the function to load more records
window.addEvent('domready', function() {
	var myAutoPagiCheck = function(e){
		var scrollPos = document.body.scrollTop ? document.body.scrollTop : window.pageYOffset;
		var elePos = $('pagiDiv').getPosition();
		bDiff = (scrollPos * 1) - (elePos.y * 1);
		if(Math.abs(bDiff) < 1000){
			if($('fGetMoreRows').value != 'true' && $('postID').value == ''){
				getMoreRows();
			}
		}
	}.periodical(1500);
});


//this function opens the siteNav div
function siteNav(){
	
}

function expandComments(postID){
	var myCommentTR = $('blogComments'+postID);
	
	if(myCommentTR.innerHTML != ''){
		closeComment(postID);
	}else{
		openComment(postID)
	}
}

function closeComment(postID){
	var myCommentTR = $('blogComments'+postID);
	myCommentTR.innerHTML = '';
}

function openComment(postID){
	var fillComments = new Request({
		method : 'post',
		url : '/index/fill-comment',
		data: {'postID' : postID},
		onComplete: function(json){
			var myCommentTD = $('blogComments'+postID);
			eval("var result = " + json);
			myCommentTD.innerHTML = result.html;
		}
	}).send();
}

function postComment(postID){
	var myForm = $('postComment'+postID);
	if(myForm.commentName.value.trim() && myForm.commentText.value.trim()){
		var postComment = new Request({
			method : 'post',
			url : '/index/post-comment',
			data : {
				'postID' : postID,	
				'commentName' : myForm.commentName.value.trim(),
				'commentEmail' : myForm.commentEmail.value.trim(),
				'commentText' : myForm.commentText.value.trim()
			},
			onComplete : function(json){
				eval("var result = " + json);
				var myForm = $('postComment'+result.postID);
				openComment(result.postID);
				for(x=0; x<myForm.elements.length; x++){
					myForm.elements[x].value = '';
				}
				$('iComment'+result.postID).innerHTML = result.iComments;
				$('commentDivError'+result.postID).style.display = "none";
			} 
		}).send();
	}else{
		$('commentDivError'+postID).innerHTML = "Please Enter a Name and Comment";
		$('commentDivError'+postID).style.display = "block";
	}
}

function toggleEnterComment(postID){
	if($('enterCommentDiv'+postID).style.display != 'block'){
		$('enterCommentDiv'+postID).style.display = 'block';
		$('enterCommentToggle'+postID).style.display = "none";
	}
}

function getMoreRows(){
	if($('fGetMoreRows').value != 'true'){
		$('fGetMoreRows').value = 'true';
		var getMoreRows = new Request({
			method : 'post',
			url : '/index/get-more-posts',
			data : {
				'postCount' : $('postCount').value,
				'category' : $('category').value
			},
			onComplete : function(json){
				eval("var result = " + json);
				if(!result.noPosts){
					var newDiv = new Element('div',{
						'id' : 'content'+$('postCount').value,
						'html' : result.html
					});
					$('morePosts').adopt(newDiv);
					$('postCount').value = result.newPostCount;
					$('fGetMoreRows').value = '';
				}
				loadAllMce();
			} 
		}).send();
	}
}