function init_pensamos(){

	// get logged_in cookie
	var logged_in = getCookie('logged_in');
	
	if (logged_in == 'true') {
		
		// disable form submit
		$('pensamos_comentario_form').observe('submit', function(event){
			// avoid form submit
			Event.stop(event);
		});
		
		// clear comment text on click
		var comentario = $('content_pensamos').down('div.comentario');
		// attach textarea click action
		comentario.down('textarea').observe('click', function(event){
			var ele = event.element();
			if (ele.value == lang.say('Click here to add a comment.')) {
				ele.value = '';
				ele.focus();
			}
		}).update(	// default value
		lang.say('Click here to add a comment.'));
		
		// set submit user comment action
		comentario.down('input.btn').observe('click', function(){
		
			$('pensamos_comentario_form').down('span.loading_comment').show();
			setTimeout(submitComentario, 1000);
			
		});
	
	} else {
		
		// if there is an add comment block, then hide it and show login options	
		var comment_div = $(document.body).down("div.comentario");
		if (typeof(comment_div)!='undefined') {
			
			comment_div.down('div.form').hide();
			
			comment_div.insert(
				new Element('h4').insert(lang.say('To comment you need to authenticate'))
			).insert(
				new Element('br')
			).insert(
				new Element('span',{id: 'login-provider-icons-comentario'}).insert(
					new Element('span', {title: lang.say('Login using your Twitter account'), className: 'twitter'}).insert('&nbsp;').observe('click',function(){
						$('login-provider-comentario-loading').show();
						setTimeout('window.location = "../auth/twitter/auth.php?goto="+encodeURIComponent(window.location+"#comentario");',1000);
					})
				).insert(
					new Element('span', {title: lang.say('Login using your Facebook account'), className: 'facebook'}).insert('&nbsp;').observe('click',function(){
						$('login-provider-comentario-loading').show();
						setTimeout('window.location = "../auth/facebook/index.php?goto="+encodeURIComponent(window.location+"#comentario");',1000);
					})
				).insert(
					new Element('span', {id: 'login-provider-comentario-loading', className: 'loading22x22'}).insert('&nbsp;').observe('click',function(){
						$('login-provider-loading').show();
					}).hide()
				)
			)
		}
		
	}
	
}


function submitComentario() {
	
	// send comment
	new Ajax.Request('ajax/blog_entry_comment/post.php', { 
		method: 'get',
		parameters: $('pensamos_comentario_form').serialize(),
		onCreate: function(){
			$('pensamos_comentario_form').disable();
		},
	  	onSuccess: function(transport){
			// before calling the call back function, check for syntax errors
			var response;
			try {
				response = transport.responseText.evalJSON();
			} catch (e) {
				alert(lang.say('Error submitting comment, invalid response.'));
				return false;
			};
			if (response.status=='error') {
				if (response.errors[0].reason == 'USER_NOT_LOGGED_IN') {
					alert(lang.say('Your session expired.'));
				} else {
					alert(lang.say('Errors found during ajax load') + ': ' + "\n" + response.errors[0].reason + ': ' + response.errors[0].message);
				};
				return;				
			}
			// when ok!
			// hide form and insert comment
			$('pensamos_comentario_form_div').hide();
			
			var comments = $('content_pensamos').down('div.comentarios').select('div.comment');
			var	comment = comments[comments.length-1];
			
			comment.hide();
			
			// update comment content
			comment.down('span.profile_image').show().down('img').src = response.comment.profile_image_url;
			comment.down('span.profile_name').update(response.comment.user_description).show();
			comment.down('span.profile_separation').show();								
			comment.down('span.comment_date').update(getLocalDate(response.comment.created_date,2)).insert('<br/>').show();
			comment.down('span.miscelanea2').show();
			comment.down('span.comment_text').update(response.comment.comment_text).show();
			
			comment.appear();
										
	    },
		onFailure: function() {
			alert(lang.say('Error submitting comment.'));
		},
		onComplete: function() {
			$('pensamos_comentario_form').enable();
			$('pensamos_comentario_form').down('span.loading_comment').hide();
		}
		
	 });
}

function follow_entry(id) {
	
	// get logged_in cookie
	var logged_in = getCookie('logged_in');
	
	if (logged_in == 'true') {
		
		var has_email = getCookie('logged_user_has_email');
		if (has_email != 'true') {
			alert(lang.say('This function requires and email. Go to Profile.'));
			return;
		}
		
		// start following a blog entry
		new Ajax.Request('ajax/blog_entry/follow.php', { 
			method: 'get',
			parameters: 'id='+id,
			onSuccess: function(transport){
				// before calling the call back function, check for syntax errors
				var response;
				try {
					response = transport.responseText.evalJSON();
				} catch (e) {
					alert(lang.say('Error following entry, invalid response.'));
					return false;
				};
				if (response.status=='error') {
					if (response.errors[0].reason == 'USER_NOT_LOGGED_IN') {
						alert(lang.say('Your session expired.'));
					} else {
						alert(lang.say('Errors found during ajax load') + ': ' + "\n" + lang.say(response.errors[0].reason));
					};
					return;				
				}
				
				// when ok!
				$('follow_blog_entry_link').href = 'javascript:unfollow_entry('+id+',this);';
				$('follow_blog_entry_link').update(lang.say('Stop following entry'));
											
		    },
			onFailure: function() {
				alert(lang.say('Error following entry.'));
			}
			
		 });
	} else {
		alert(lang.say('You need to login to follow an article.'));
	}
	
}

function unfollow_entry(id) {
	
	// stop following a blog entry
	new Ajax.Request('ajax/blog_entry/unfollow.php', { 
		method: 'get',
		parameters: 'id='+id,
		onSuccess: function(transport){
			// before calling the call back function, check for syntax errors
			var response;
			try {
				response = transport.responseText.evalJSON();
			} catch (e) {
				alert(lang.say('Error following entry, invalid response.'));
				return false;
			};
			if (response.status=='error') {
				if (response.errors[0].reason == 'USER_NOT_LOGGED_IN') {
					alert(lang.say('Your session expired.'));
				} else {
					alert(lang.say('Errors found during ajax load') + ': ' + "\n" + response.errors[0].reason + ': ' + response.errors[0].message);
				};
				return;				
			}
			
			// when ok!
			$('follow_blog_entry_link').href = 'javascript:follow_entry('+id+',this);';
			$('follow_blog_entry_link').update(lang.say('Follow entry'));
										
	    },
		onFailure: function() {
			alert(lang.say('Error following entry.'));
		}
		
	 });
	
}

