//var $comm = jQuery.noConflict();
var $comm = $;

var comm_ajax_timer_id = 0;

$comm.postJSON = function(url, data, callback) {
	$comm.post(url, data, callback, "json");
};

function hide_all()
{
	$comm("#comments").hide();
}

function click_checkbox(check_id)
{
	check_handle = document.getElementById(check_id);
	check_handle.checked = !check_handle.checked;
}

function update_preview()
{
	if (trim($comm("#comment-form-comment").val()) == '')
	{
		$comm("#comment-preview-div").hide();
	}
	else
	{
		$comm("#comment-preview-div").show();
	}
	$comm("#comment-preview").html(nl2br(strip_tags($comm("#comment-form-comment").val(), '<a><br><strong><b><em><i><blockquote><pre><code><img><ul><ol><li><del>')));
}

function ajax_error(page)
{
	$comm("#comments-loading").addClass("invisible");
	shownote('error', 'AJAX Error: There is something wrong with ' + page + '. Please contact website administrator!');
}

function quote_comment(commid,page)
{
	$comm("#comments-loading").removeClass("invisible");
	comm_ajax_timer_id = setTimeout('ajax_error("'+page+'");', 10000);
	$comm.postJSON(page, { id: commid }, function (data){hendluj_quote(data);});
}

function hendluj_quote(json)
{
	$comm("#comments-loading").addClass("invisible");
	clearTimeout(comm_ajax_timer_id);
	var old = $comm('#comment-form-comment').text();
	$comm('#comment-form-comment').text(old+"<blockquote>"+json.wrote+" <a href=\"#comment"+json.id+"\">#</a>\n"+json.comment+"</blockquote>\n\n");
	$comm.scrollTo('#comments-form-bubble', 1000);
}

function shownote(cssclass, note)
{
	$comm("#comments-note").removeClass("invisible");
	$comm("#comments-note").removeClass("warning");
	$comm("#comments-note").removeClass("info");
	$comm("#comments-note").removeClass("success");
	$comm("#comments-note").removeClass("error");

	$comm("#comments-note").addClass(cssclass);
	$comm("#comments-note").html(note);
	$comm("#comments-note").show("slow");
}

function hidenote()
{
	$comm("#comments-note").hide("slow");
}

function try_ajax_submit(page)
{

	$comm("#comments-loading").removeClass("invisible");

	comm_ajax_timer_id = setTimeout('ajax_error("'+page+'");', 30000);

	var what = document.getElementById("comment-form-what").value;
	var name = document.getElementById("comment-form-name").value;
	var email = document.getElementById("comment-form-email").value;
	var comment = document.getElementById("comment-form-comment").value;
	var captcha = document.getElementById("comment-form-captcha").value;
	var image = document.getElementById("comment-form-image").value;
	var notify = document.getElementById("comment-form-notify").checked;

 	$comm.postJSON(page, { ajax: "true", what: what, name: name, email: email, website: website, comment: comment, captcha: captcha, image: image, notify: notify }, function (data){hendluj(data);});

 	return false; //prevent classic form submit
}

function hendluj(json)
{

	$comm("#comments-loading").addClass("invisible");
	clearTimeout(comm_ajax_timer_id);
	shownote(json.status, json.note);
	if (json.comment != undefined) {
		window.location.reload();
		//$comm("#comments").append("<li class='new-comment' id='commentNEW'><div class='comment-author'>"+json.youwrote+"</div><div class='comments-gravatar'><img src='"+json.avatar+"' /></div><div class='comment-content'><p>"+json.comment+"</p></div></li>");
	}
}

function jqCheckAll( flag )
{
   $comm(":checkbox").attr('checked', flag);
}

/**
 *
 * @access public
 * @return void
 **/
function get_title(id, sURL)
{
	$comm.ajaxq ("comm_ajax_queue", {
	    url: "get-title.php?url="+sURL,
	    cache: false,
	    success: function(html)
	    {
	        $comm("#"+id).html(html);
	    }
	});

}

/**
 *
 * @access public
 * @return void
 **/
function check_batch_form()
{
	var value1 = document.getElementsByName("action")[0].value;
	var value2 = document.getElementsByName("action")[1].value;
	if (value1 == 'none' && value2 == 'none') {
			alert('You have not selected batch action!');
			return false;
	}
	else return true;
}
