// #############################################################################
// Collapsible element handlers

//{{{

/**
* Toggles the collapse state of an object, and saves state to 'iddb_collapse' cookie
*
* @param        string  Unique ID for the collapse group
*
* @return       boolean false
*/
function iddb_toggle_collapse(objid)
{
	if (!is_regexp)
	{
		return false;
	}

	obj = fetch_object('collapseobj_' + objid);
	img = fetch_object('collapseimg_' + objid);

	if (!obj)
	{
		// nothing to collapse!
		if (img)
		{
			// hide the clicky image if there is one
			img.style.display = 'none';
		}
		return false;
	}

	if (obj.style.display == 'none')
	{
		obj.style.display = '';
		iddb_save_collapsed(objid, false);
		if (img)
		{
			img_re = new RegExp("_collapsed\\.gif$");
			img.src = img.src.replace(img_re, '.gif');
		}
	}
	else
	{
		obj.style.display = 'none';
		iddb_save_collapsed(objid, true);
		if (img)
		{
			img_re = new RegExp("\\.gif$");
			img.src = img.src.replace(img_re, '_collapsed.gif');
		}
	}
	return false;
}
//}}}
//{{{
/**
* Updates iddb_collapse cookie with collapse preferences
*
* @param        string  Unique ID for the collapse group
* @param        boolean Add a cookie
*/
function iddb_save_collapsed(objid, addcollapsed)
{
	var collapsed = fetch_cookie('iddb_collapse');
	var tmp = new Array();

	if (collapsed != null)
	{
		collapsed = collapsed.split('\n');

		for (var i in collapsed)
		{
			if (collapsed[i] != objid && collapsed[i] != '')
			{
				tmp[tmp.length] = collapsed[i];
			}
		}
	}

	if (addcollapsed)
	{
		tmp[tmp.length] = objid;
	}

	expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	set_cookie('iddb_collapse', tmp.join('\n'), expires);
}
//}}}
//{{{
/**
* Updates iddb_collapse cookie with collapse preferences
*
* @param        string  Unique ID for the collapse group
* @param        boolean Add a cookie
*/
function iddb_init()
{
	if (!is_regexp)
	{
		return false;
	}

	var collapsed = fetch_cookie('iddb_collapse');

	if (collapsed != null)
	{
		collapsed = collapsed.split('\n');

		for (var i in collapsed)
		{
			obj = fetch_object('collapseobj_' + collapsed[i]);
			img = fetch_object('collapseimg_' + collapsed[i]);

			if (obj)
			{
				obj.style.display = 'none';
				if (img)
				{
					img_re = new RegExp("\\.gif$");
					img.src = img.src.replace(img_re, '_collapsed.gif');
				}
			}
		}
	}
}
//}}}

