function insertSmiley(smiley)
{
    var currentText = document.getElementById("comment_form_area");
    var smileyWithPadding = " " + smiley + " ";
    currentText.value += smileyWithPadding;
    currentText.focus();
}
function hideCommentReplyForm(form_id)
{
	var div_id = "div_" + form_id;
	toggleVisibility(div_id, false);
}

function printCommentReplyForm(form_id, comment_parent_id) 
{
	var div_id = "div_" + form_id;
	var reply_comment_form = "comment_form" + form_id;
	
	var innerHTMLContent = '\
			<form name="' + reply_comment_form + '" id="' + reply_comment_form + '" onSubmit="return false;" >\
			<input type="hidden" name="media_id" value="' + media_id + '">\
			<input type="hidden" name="add_comment" value="">\
			<input type="hidden" name="form_id" value="' + reply_comment_form + '">\
			<input type="hidden" name="reply_parent_id" value="' + comment_parent_id + '">\
			<textarea name="comment" cols="45" rows="5" id="comment_form_area"></textarea>\
			<br/>\
			<div style="float:left;clear:left">\
				<input align="left" type="button" class="button" name="add_comment_button" value="Καταχώρηση σχολίου" onclick="postComment(\'' + reply_comment_form + '\');">\
			</div>\
			</form><br style="clear:both"><br>';
	
	setInnerHTML(div_id, innerHTMLContent);
}

var postCommentSuccess = function(t) 
{		
	response_str = t.responseText;
	response_code = response_str.substr(0, response_str.indexOf(" "));
	form_id = response_str.substr(response_str.indexOf(" ")+1);

	var form = document.forms[form_id];
	var dstDiv = form.add_comment_button;
	var commentDiv = form.comment;
	
	if (response_code == "OK") 
	{
		setInnerHTML('div_main_comment', "Το σχόλιό σας έχει καταχωρηθεί!");
		fetchComments(media_id, '1');
	}
	else
	{
		if(response_code == "NOPERM") 
		{
			alert("Δεν έχετε την άδεια να καταχωρήσετε σχόλια!");
			dstDiv.disabled = false;
		}
		else if(response_code == "TOOSHORT") 
		{
			alert("Το σχόλιο που εισάγατε είναι πολύ μικρό. Γράψτε κάποιο μεγαλύτερο και δοκιμάστε πάλι");
			dstDiv.disabled = false;
			commentDiv.disabled = false;
			commentDiv.focus();
		}
		else 
		{
			dstDiv.disabled = false;
		}
	
		dstDiv.value = "Καταχώρηση σχολίου";
	}
}

function postComment(comment_form_id) 
{
	/*if(!is_logged_in)
		return alert("You must login to post a comment!");*/

	var form = document.forms[comment_form_id];
		
	if (checkComment(form, comment_form_id)) 
	{
		var add_button = form.add_comment_button;
		add_button.value = "Το σχόλιο προστίθεται...";
		form.comment.disabled = true;
		add_button.disabled = true;
		
		comment_escaped = encodeURIComponent(document.forms[comment_form_id].comment.value);
		
		var url = base_url+'common/ajax/media_add_comment.php';
		//var pars = Form.serialize($(comment_form_id)) + '&comment=' + comment_escaped;
		var pars = $(comment_form_id).serialize() + '&comment=' + comment_escaped;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: postCommentSuccess });
	} 
}

function checkComment(comment_form, comment_form_id)
{
	var comment = comment_form.comment;
	var comment_button = comment_form.comment_button;
	
	if (comment.value.length == 0 || comment.value == null)
	{
		alert("Πρέπει να γράψετε κάποιο σχόλιο!");
		comment.disabled = false;
		comment.focus();
		return false;
	}

	if (comment.value.length > 4500)
	{
		alert("Το σχόλιό σας πρέπει να είναι μικρότερο από 4500 χαρακτήρες!");
		comment.disabled = false;
		comment.focus();
		return false;
	}
	
	return true;
}

function fetchComments(file_id, page)
{
	showLoading('div_display_comments');
	
	var url = base_url+'common/ajax/media_load_comments.php';
	var pars = 'file_id='+escape(file_id)+'&p='+escape(page);
	var target = 'div_display_comments';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}
