var the_target = null;
var post_article_id = null;

function post(target, article_id) {
	the_target = target;
	post_article_id = article_id;
	jQuery("#post_dialog").empty().dialog('open');
	jQuery("#post_dialog").load("/copy?article_id="+article_id+"&portlet=1&dialog=1 #postmainbody",post_init);
}

function post_init() {
	init_tags();
}

// this "overrides" the dummy function in post_known.adp.
function do_post() {
	if (!post_article_id) {
		return false;
	}
	var data = jQuery("#frm").serializeArray();
	data["callback"]="?";
	data["article_id"]=post_article_id;
	jQuery.post("/do_post_known_ajax", data, function(x) {}, "jsonp" );
	jQuery("#post_dialog").dialog('close');

	jQuery(the_target).closest("tr").remove();

	showTopRecs();

	var c = jQuery("#recs_list_table tr").length;
	if (c==1) {
		jQuery("#article_count").text("1 recommendation");
	} else {
		jQuery("#article_count").text(c+" recommendations");
	}
	if (c <= max_items) {
		jQuery("#top_only").hide();
	}
	post_article_id = null;

	return false;
}



jQuery(function(){
	jQuery("#post_dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		width: "960px",
		position: ["middle", "top"],
		resizable: false,
		draggable: false,
		minHeight: 0
	});
});


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function clexpand(basename) {
	var d = document.getElementById(basename+"-body");
	if (d.style.display=="none") {
		clexpand_open(basename);
	} else {
		clexpand_collapse(basename);
	}
}

function clexpand_open(basename) {
	var i = document.getElementById(basename+"-arrow");
	var d = document.getElementById(basename+"-body");
	d.style.display = 'block';
	i.src = staticRoot+"/img/arrow-open.gif";
	createCookie("clexpand-article-"+basename, "open", 1000);
}

function clexpand_collapse(basename) {
	var i = document.getElementById(basename+"-arrow");
	var d = document.getElementById(basename+"-body");
	d.style.display = 'none';
	i.src = staticRoot+"/img/arrow-collapsed.gif";
	createCookie("clexpand-article-"+basename, "collapsed", 1000);
}

function clexpand_to_cookie(basename) {
	var d = document.getElementById(basename+"-body");
	if (d==null)
		return;
	var c = readCookie("clexpand-article-"+basename);
	if (c==null)
		return;
	if (c=="open") {
		clexpand_open(basename);
	} else if (c=="collapsed") {
		clexpand_collapse(basename);
	}
}
