/* START COMMENT FUNCTIONS */

var comments_max_chars_allowed = 1000;

function toggle_display(tag_id)
{
	if ( $(tag_id).style.display == "none" )
	{
		$(tag_id).style.display = "block";
	} else
	{
		$(tag_id).style.display = "none";
	}
}

function limit_character_input(id, max_chars, feedback_div_id)
{
	var char_left = max_chars-$(id).value.length;
	if (char_left >= 0)
	{
		$(feedback_div_id).innerHTML = "(" + char_left +" "+ CHARACTERS_LEFT + ")";
	} else
	{
		
		$(feedback_div_id).innerHTML = "(<font color=red>" + Math.abs(char_left) +" "+ CHARACTERS_TOO_MUCH + "</font>)";
	}
}


function post_comment(message_id, comment, type)
{
	comment_div_id = 'comments_'+type+'_'+message_id;
	nr_comments_div_id = 'nr_comments_'+type+'_'+message_id;
	comment_form_div_id = 'comment_form_'+type+'_'+message_id;
	feedback_div_id = 'comment_feedback_'+type+'_'+message_id;
	textarea_name = 'comment_textarea'+message_id;
	input_btn = 'input_field_post'+message_id;
	char_div = 'char_left_div'+message_id;
	input_field = 'input_field'+message_id;

	$(textarea_name).style.display = 'none';
	$(input_btn).style.display = 'none';
	var comment_text = $(textarea_name).value;
	
    $(input_field).innerHTML = comment_text;
	$(char_div).style.display = 'none';
	
	var errors = new Array();
	
	if ( !Object.isNumber(message_id) )
	{
		errors.push(ERROR_COMMENT_MESSAGE_NOTEXIST);
	}

	if ( comment.blank() )
	{
		errors.push(ERROR_NO_COMMENT);
	}

	if ( comment.length > comments_max_chars_allowed )
	{
		errors.push(ERROR_MAX_COMMENT);
	}

	if ( type<4 || type>6 )
	{
		errors.push(ERROR_NOT_COMMENT);
	}
	
	if ( !$(feedback_div_id) )
	{
		errors.push(ERROR_NO_CONTAINER);
	}
	
	//escape special characters in comment
	comment = encodeURIComponent(comment);//escape(comment);


	//check if there are any errors
	if (errors.length > 0)
	{
		$(feedback_div_id).className = "feedback_div red_border";
		$(feedback_div_id).innerHTML = "";
		errors.each(
			function(error)
			{
				$(feedback_div_id).innerHTML += error + "<br />";
			}
		);
		if ( $(feedback_div_id).style.display == "none" )
		{
			Effect.BlindDown(feedback_div_id, { duration: 0.5 });
		}
		return;
	}

	//show loading
	$(feedback_div_id).className = 'feedback_div';
	$(feedback_div_id).update('<img src="/images/loaders/ajax-loader_snake.gif" alt="loading..." />');
	if ( $(feedback_div_id).style.display == "none" )
	{
		Effect.BlindDown(feedback_div_id, { duration: 0.5 });
	}

	//process comment through ajax
	var url = "/ajax.php";
	var mode = "post_comment";
	var pars = 'mode=' + mode + '&message_id=' + message_id + '&comment=' + comment + '&type=' + type;

	
	//$(feedback_div_id).style.display = "none";
	//update
	new Ajax.Updater(feedback_div_id, url,
		{
			method:'post',
			parameters: pars,
			//insertion: 'top',
			onComplete: function()
				{
					$(feedback_div_id).className = 'feedback_div green_border';
					if ( $(feedback_div_id).style.display == "none" )
					{
						Effect.BlindDown(feedback_div_id, { duration: 0.5 });
					}
					Effect.toggle(comment_form_div_id, 'blind', { duration: 0.5 });
					update_nr_comments(message_id, type, nr_comments_div_id);
					if( $('2' + nr_comments_div_id) )
					{
						update_nr_comments(message_id, type, '2' + nr_comments_div_id);
					}
					
					//toggle_comments(message_id, type, comment_div_id, comment_form_div_id);
					get_comments(message_id, type, comment_div_id);
				}
		}
	);
}


function update_nr_comments(message_id, type, update_div_id)
{
	var url = "/ajax.php";
	var mode = "get_nr_comments";
	var pars = 'mode=' + mode + '&message_id=' + message_id + '&type=' + type;
	
	//update
	new Ajax.Updater(update_div_id, url,
		{
			method:'post',
			parameters: pars
		}
	);
}


function toggle_comments(message_id, type, comment_div_id, comment_form_div_id)
{
	if ( $(comment_div_id).style.display=='none' )
	{
		get_comments(message_id, type, comment_div_id);
		if ( $(comment_form_div_id).style.display=='none' )
		{
			//Effect.BlindDown(comment_form_div_id, { duration: 0.5 });
		}

	} else
	{
		Effect.BlindUp(comment_div_id, { duration: 0.5 });
		if ( $(comment_form_div_id).style.display!='none' )
		{
			//Effect.BlindUp(comment_form_div_id, { duration: 0.5 });
		}

	}
}

function deploy_textfield(comment_textarea, input) 
{	
	//Adjust the input field and adds the submit link
	$(comment_textarea).style.height = '68px';
		
		if(($(comment_textarea).value==COMMENT_ON_MESSAGE) || ($(comment_textarea).value==ENTER_NOTE))
		{
			$(comment_textarea).value='';
		}
	$(input).style.display = 'inline';
}

function small_textfield(comment_textarea, input) 
{	
	//Adjust the input field and adds the submit link
	$(comment_textarea).style.height = '34px';
	if($(comment_textarea).value=='')
		{
			$(comment_textarea).value=COMMENT_ON_MESSAGE;
		}
}


function get_comments(message_id, type, update_div_id)
{
	var errors = new Array();
	
	if ( !Object.isNumber(message_id) )
	{
		errors.push(ERROR_COMMENT_MESSAGE_NOTEXIST);
	}

	var url = "/ajax.php";
	var mode = "get_comments";
	var pars = 'mode=' + mode + '&message_id=' + message_id + '&type=' + type;


	new Ajax.Updater(update_div_id, url,
		{
			method:'post',
			parameters: pars,
			onComplete: function()
				{
					Effect.BlindDown(update_div_id, { duration: 0.5 });
				}
		}
	);
}


function delete_comment(comment_id, comment_type, comment_on_id, user_id, comment_options_div_id, feedback_div_id)
{
	//alert("deleting now");
	nr_comments_div_id = 'nr_comments_'+comment_type+'_'+comment_on_id;
	
	var url = "/ajax.php";
	var mode = "delete_comment";
	var pars = 'mode=' + mode + '&comment_id=' + comment_id + '&comment_type=' + comment_type + '&comment_on_id=' + comment_on_id + '&user_id=' + user_id;

	

	new Ajax.Updater(feedback_div_id, url,
		{
			method:'post',
			parameters: pars,
			onComplete: function()
				{
					$(comment_options_div_id).hide();
					Effect.BlindDown(feedback_div_id, { duration: 0.5 });
					update_nr_comments(comment_on_id, comment_type, nr_comments_div_id);
					if( $('2' + nr_comments_div_id) )
					{
						update_nr_comments(comment_on_id, comment_type, '2' + nr_comments_div_id);
					}
					
				}
		}
	);

	
}




/* END COMMENT FUNCTIONS */




