/*---------------------------------------------------------------------+
| ExiteCMS Content Management System                                   |
+----------------------------------------------------------------------+
| Copyright 2006-2009 Exite BV, The Netherlands                        |
| for support, please visit http://www.exitecms.org                    |
+----------------------------------------------------------------------+
| Released under the terms & conditions of v2 of the GNU General Public|
| License. For details refer to the included gpl.txt file or visit     |
| http://gnu.org                                                       |
+---------------------------------------------------------------------*/
function tree_ToggleRows(rowset, rowid, name, type, state)
{
	// get all child elements
	if ( type == null) type = "TR";
	var elms = document.getElementById(rowset).getElementsByTagName(type);

	// determine the display style for the children
	if ( state == null)
	{
		var state = "none";
		for (var i = 0; i < elms.length; i++)
		{
			var r = elms[i];
			if (tree_MatchId(r.id, rowid+"-", true))
			{
				if (r.style.display == "none")
				{
					if (document.all || type != "TR")
						state = "block"; //IE4+ specific code
					else
						state = "table-row"; //Netscape and Mozilla
				}
				break;
			}
		}
	}

	// does this tree node has an image we need to swap?
	if (document.images['tree_'+rowid])
	{
		// swap the image
		image = document.images['tree_'+rowid].src;
		if (image.indexOf('collapse.png') != -1)
		{
			tmp = image.replace('collapse.png', 'expand.png');
		}
		else
		{
			tmp = image.replace('expand.png', 'collapse.png');
		}
		document.images['tree_'+rowid].src = tmp;
	}

	// flip the display state of the tree nodes children
	var descend = (state != "none");
	for (var j = 0; j < elms.length; j++)
	{
		var s = elms[j];
		if (tree_MatchId(s.id, rowid+"-", descend))
		{
			s.style.display = state;
			if ( !descend && document.images['tree_'+s.id])
			{
				image = document.images['tree_'+s.id].src;
				if (image.indexOf('collapse.png') != -1)
				{
					tmp = image.replace('collapse.png', 'expand.png');
					document.images['tree_'+s.id].src = tmp;
				}
			}
		}
	}

	if (state == "none")
	{
		createCookie(name+":"+rowid, 0, 0);
	}
	else
	{
		createCookie(name+":"+rowid, 1, 0);
	}

}

function tree_MatchId(target, pattern, descend) {
	var pos = target.indexOf(pattern);
	if (pos != 0) return false;
	if (!descend) return true;
	if (target.slice(pos + pattern.length, target.length).indexOf("-") >= 0) return false;
	return true;
}
