var comments;
function genComments()
{
	comments=document.getElementsByClassName('comment');
	ids=new Array();
	for (i=0;i<comments.length;i++)
	{
		t=comments[i].href
		ids.push(t.substr(t.lastIndexOf('=')+1));
	}
	new Ajax.Request('/comments/lookup.php',{parameters: 'ids='+escape(ids.join(',')), onComplete: function(req) { addComments(req.responseText); }});

	if (registration.user.loggedin)
		new Ajax.Request('/flags/lookup.php',{onComplete: function(req) { addFlags(req.responseText); }});
}
function addComments(req)
{
	stories=new Object;
	if (req!='' && req!=null)
	{
		lines=req.split("\n");
		for (i=0;i<lines.length;i++)
		{
			if (lines[i]=='') continue;
			parts=lines[i].split("|");
			stories['x'+parts[0]]=parts[1];
		}
	}
	for (i=0;i<comments.length;i++)
	{
		t=comments[i].href
		id=t.substr(t.lastIndexOf('=')+1);
		if (stories['x'+id]==undefined)
			comments[i].innerHTML="0";
		else
			comments[i].innerHTML=stories['x'+id]
	}
}
function addFlags(req)
{
	var flags=document.getElementsByClassName('flag');
	var imgs=eval('('+req+')');
	for (i=0;i<flags.length;i++)
	{
		id=flags[i].href.match(/\/(\d+)(\.php)?$/)[1];
		onoff="off";
		if (imgs['x'+id]!=undefined) onoff="on";
		flags[i].innerHTML='<img src="http://gfx.tucsoncitizen.com/global/flag_'+onoff+'.gif" border="0" />';
		addEventObj(flags[i],'click',toggleFlag);
	}
}
function toggleFlag(e)
{
	if (!e) e=window.event;
	stopEvent(e);

	var o=e.target;
	if (!o) o=e.srcElement;
	while (o && o.nodeName!="A") o=o.parentNode;
	if (o.nodeName!="A") return false;
	var id=o.href.match(/\/(\d+)(\.php)?$/)[1];
	new Ajax.Request('/flags/toggle.php',{parameters: 'id='+escape(id), onComplete: function(req) { o.innerHTML=req.responseText; }});
	return false;
}
