
/* Main Core JS Functions */
function submit_comment ( post_id ) {
/*
	Description:
		For use in submitting a comment via AJAX.
	
	Values:
		@string author 	=	Name of author of comment
		@string content	=	The comment text itself
		@string	id		= 	The ID of the post to which the comment belongs. NOTE: this is turned into INT server-side.
		
	Returns:
		@string msg		= 	A refreshed HTML view of the comments div, to be inserted into a comments_div holder.
*/
		var author 		= $("#name").val();
		var content		= $("#comment").val();
		var id			= post_id;
		var errors		= 0;
		
		$("#name_validation_messages").html(''); $("#content_validation_messages").html(''); $("#recaptcha_messages").html('');
		if ( author == '' )
		{
			$("#name_validation_messages").html('Please enter a name.');
			errors = 1;
		}
		if ( content == '' )
		{
			$("#content_validation_messages").html('Please enter a comment');
			errors = 1;
		}

		if ( errors == 0 )
		{
			$.ajax({
				type: "POST",
				url: base_url+"blog/check_captcha",
				data: "recaptcha_response_field="+$("#recaptcha_response_field").val()+"&recaptcha_challenge_field="+$("#recaptcha_challenge_field").val(),
				success: function(msg){
					if ( msg == "success" )
					{
						/* Ajax save the comment */

						$.ajax({
					   		type: "POST",
					   		url: base_url+"blog/submit_comment",
					   		data: "author="+author+"&content="+content+"&id="+id,
					   		success: function(msg){

								$("#comments_div").html(msg);

								$("#messages").html('<div class="message" id="message" class="success">Successfully submitted your comment!</div>');

								$("#name").val(null);
								$("#comment").val(null);
					   		}

					 	});
					}
					else
					{
						$("#recaptcha_messages").html('The captcha was invalid.');
					}
				}
			});
		}

		Recaptcha.reload();
		

}
function relative_time(time_value) {
   var parsed_date = Date.parse(time_value);

   var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
   var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

   if(delta < 60) {
       return 'less than a minute ago';
   } else if(delta < 120) {
       return 'about a minute ago';
   } else if(delta < (45*60)) {
       return (parseInt(delta / 60)).toString() + ' minutes ago';
   } else if(delta < (90*60)) {
           return 'about an hour ago';
       } else if(delta < (24*60*60)) {
       return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
   } else if(delta < (48*60*60)) {
       return '1 day ago';
   } else {
       return (parseInt(delta / 86400)).toString() + ' days ago';
   }
}
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && Math.ceil(diff) + " seconds ago" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var date = prettyDate(this.title);
			if ( date )
				jQuery(this).text( date );
		});
	};
	
function make_post_date(){
		
        var dates = document.getElementsByTagName("span");        
        for ( var i = 0; i < dates.length; i++ )
                if ( dates[i].title ) {
                        var date = prettyDate(dates[i].title);
                        if ( date )
                                dates[i].innerHTML = date;
                }
}
$(function(){
	make_post_date();
	
});
setInterval(make_post_date, 5000);