// http://blog.livedoor.jp/takoashi/
// created by takoashi
// 2006/02/27 14:34:29
//	ツリーの葉を逆順で表示できる機能を追加
// 2005/08/11 04:56:47
//	改良版公開

// 
// クラス名が等しいかどうかチェックする
// 
function IsClassName(tag,classname)
{
	if( tag.getAttribute("className")	== classname )
		return true;
	if( tag.getAttribute("class")		== classname )
		return true;
	return false;
}

// 
// Livedoor系ブログのプラグインをタイトルから取得する
// 
function GetPluginByTitle(PlugInTitle,LinksId)
{
	var divs;
	if( LinksId == null )
		divs = document.getElementsByTagName("div");
	else
		divs = document.getElementById(LinksId).getElementsByTagName("div");

	for(i=0 ; i<divs.length ; i++ )
	{
		if(	IsClassName( divs[i], "sidetitle" )	&&
			divs[i].innerHTML == PlugInTitle	)
		{
			// リニューアル対応
			if(	IsClassName( divs[i+1], "sidetop" ) )
				return divs[i+2];

			// 旧版
			return divs[i+1];
		}
	}
}


// 
// ConvertTreeに必要なパラメータ
// 
function TreeDataObj()
{
	this.KeyAS		= '<div class="sidebody">';
	this.KeyAE		= '</div>';
	this.KeyTS		= '<div class="key">';
	this.KeyTE		= '</div>';
	this.KeyTSN		= '<div class="key">';
	this.KeyTEN		= '</div>';
	this.LeafAS		= '<div class="leaf_area">';
	this.LeafAE		= '</div>';
	this.LeafTS		= '<div class="leaf">';
	this.LeafTE		= '</div>';
	this.LeafTSL	= '<div class="leaf_last">';
	this.LeafTEL	= '</div>';

	this.OutKey		= '';
	this.LeafOut	= '';
	this.Reverse	= 0;
}

// プラグインの高さを設定しスクロールさせる
function SetPluginScroll(h, sidediv)
{
	if( sidediv == null )
		return;
	if( h == null )
		h = "100px";

	sidediv.style.overflow = "auto";
	sidediv.style.height   = h;
}

// 
// ツリー化されたデータを表示する
// 

var id_count = 0;
function ConvertTree(TREEDATA,DOC,TDO)
{
	DOC.innerHTML = "";
	for( i=0 ; i<TREEDATA["___key___"].length ; i++ )
	{
		var	leafsdata = TREEDATA[TREEDATA["___key___"][i]];
		var	leafarea = "";
		if( leafsdata.length != 0 )
		{
			// リーフが存在する
			if( TDO.Reverse == 0 )
			{
				for( j=0 ; j<leafsdata.length ; j++ )
				{
					if( j!=leafsdata.length-1)
						leafarea += TDO.LeafTS;
					else
						leafarea += TDO.LeafTSL;

					leafarea += leafsdata[j];

					if( TDO.OutKey!=leafsdata[j] )
						leafarea += TDO.LeafOut;

					if( j!=leafsdata.length-1)
						leafarea += TDO.LeafTE;
					else
						leafarea += TDO.LeafTEL;
				}
			}
			else
			{
				for( j=leafsdata.length-1 ; j>=0 ; j-- )
				{
					if(j!=0)
						leafarea += TDO.LeafTS;
					else
						leafarea += TDO.LeafTSL;

					leafarea += leafsdata[j];

					if( TDO.OutKey!=leafsdata[j] )
						leafarea += TDO.LeafOut;

					if(j!=0)
						leafarea += TDO.LeafTE;
					else
						leafarea += TDO.LeafTEL;
				}
			}

			leafarea = TDO.LeafAS + leafarea + TDO.LeafAE;
			var sidebody = TDO.KeyAS + TDO.KeyTS + TREEDATA["___key___"][i] + TDO.KeyTE + leafarea + TDO.KeyAE;
			sidebody = sidebody.replace( /##ID##/g, "LeafArea"+id_count );

			DOC.innerHTML += sidebody;
		}
		else
		{
			// リーフが存在しない
			DOC.innerHTML += TDO.KeyAS + TDO.KeyTSN + TREEDATA["___key___"][i] + TDO.KeyTEN + TDO.KeyAE;
		}

		id_count++;
	}
}

