function initWikify(){
	var theSource = document.getElementById("source");
	var theSourceR = document.getElementById("contentRight");
//	var tiddlerText = "こんにちは。http://test.com";
	var tiddlerText = getTiddlerText(theSource) + getTiddlerText(theSourceR) ;

	var theOutput = document.getElementById("out");
	createTiddlerBody(theOutput,tiddlerText);
//	theTest.parentNode.replaceChild(theNew,theTest);

}

function getTitlePatternText(){
	var text = "";
	for(i=0;i<myJSONObject.array.length;i++){
		if (i > 0){
			text += "|";
		}
		text += myJSONObject.array[i];
	}
//	alert (text);
	return text;

}

function walk(node){
	for (var i=0;i<node.childNodes.length;i++)
	{
//		alert(node.childNodes.item(i).nodeValue);
		if (node.childNodes.item(i).hasChildNodes()){
			walk(node.childNodes.item(i));
		}
		if (node.childNodes.item(i).nodeType == 3){
			text += node.childNodes.item(i).nodeValue;
		}
	}
}

var text = "";
function getTiddlerText(place)
{
	// 子ノードの数だけ処理を繰り返す
	text = "";
	if (place != undefined && place.hasChildNodes()){
		walk(place)
	}
/*
	for (i=0;i<place.childNodes.length;i++)
	{
		// ノードタイプをダイアログ表示する
		if (place.childNodes.item(i).nodeType == 3){
			text += place.childNodes.item(i).nodeValue;
		}
	}
*/
//	alert(text);
	return text;
}

// Create the body section of a read-only tiddler
function createTiddlerBody(place,tiddlerText,highlightText,highlightCaseSensitive)
{
	// Get the body of the tiddler
	var tiddlerExists = (tiddlerText != null);
	if(!tiddlerExists)
		tiddlerText = "This tiddler doesn't yet exist. Double-click to create it";
	// Create the body
	var theBody = createTiddlyElement(place,"div",null,null);
//	theBody.setAttribute("id","body" + title);
	if(!tiddlerExists)
		theBody.style.fontStyle = "italic";
	// Add the body text wikifing the links
	wikify(tiddlerText,theBody,highlightText,highlightCaseSensitive,false);

	return theBody;
}


// Create child text nodes and link elements to represent a wiki-fied version of some text
function wikify(text,parent,highlightText,highlightCaseSensitive,single)
{
	// Link patterns
	var dm = "http://uemachi.cotocoto.jp/plus/jump.php?title=";
	var hitTexts = new Object();
	//Rakusai  正規表現によって、作成済みのタイトルに自動でリンク
	var breakPattern = "(¶|\r\n|\n)";
	var upperLetter = "[A-Z]";
	var lowerLetter = "[a-z]";
	var anyLetter = "[A-Za-z_0-9]";
	var linkPattern = "|(" + upperLetter + "+" + lowerLetter + "+" + upperLetter + anyLetter + "*)";
	var titlePattern = "|(" + getTitlePatternText() + ")";
	var urlPattern = "|((?:http|https|mailto|ftp):[^(¶|\r|\n|\t| |　)]*)";
	// Create the RegExp object for matching formatting
	var formatRegExp = new RegExp(breakPattern + titlePattern + linkPattern + urlPattern,"mg");
	// The start of the fragment of the text being considered
	var nextPos = 0;
	// Loop through the bits of the body text
	var i=0;
	do {
		// Get the next formatting match
		var formatMatch = formatRegExp.exec(text);
		var matchPos = formatMatch ? formatMatch.index : text.length;
		// Dump out the formatted match
		if(formatMatch)
		{
			// Dump out the link itself in the appropriate format
			if(formatMatch[1])
			{
			}
			else if(formatMatch[2])
			{
				//ダブりがないときのみ
				if (hitTexts[formatMatch[0]] == undefined){
					var theLink = createExternalLink(parent,dm + formatMatch[0],false,single);
					theLink.appendChild(document.createTextNode(formatMatch[0]));
					parent.appendChild(theLink);
//					parent.appendChild(document.createTextNode("　"));
					hitTexts[formatMatch[0]] = 1;
				}
			}
			else if(formatMatch[3])
			{
			}
			else if(formatMatch[4])
			{
			}
		}
		// Move the next position past the formatting match
		nextPos = formatRegExp.lastIndex;
		i = i + 1;
	} while(formatMatch);
}

function createTiddlyElement(theParent,theElement,theClass,theText)
{
	var e = document.createElement(theElement);
	if(theClass != null)
		e.className = theClass;
	if(theText != null)
		e.appendChild(document.createTextNode(theText));
	if(theParent != null)
		theParent.appendChild(e);
	return(e);
}

// Create an external link
function createExternalLink(place,url)
{
	var theLink = document.createElement("a");
	theLink.href = url;
	theLink.title = "External link to " + url;
	theLink.target = "_blank";
	place.appendChild(theLink);
	return(theLink);
}

function createTiddlyButton(theParent,theText,theTooltip,theAction)
{
	var theButton = document.createElement("a");
	if(theAction)
		{
		theButton.onclick = theAction;
		theButton.setAttribute("href","JavaScript:;");
		}
	theButton.setAttribute("title",theTooltip);
	if(theText)
		{
		theButton.appendChild(document.createTextNode(theText));
		}
	theParent.appendChild(theButton);
	return(theButton);
}

function createTiddlyLink(place,title,includeText,single)
{
	var btn;
	var text = includeText ? title : null;
	var subTitle = "";//getTiddlerSubtitle(title);
	var theClass = subTitle ? "tiddlyLinkExisting" : "tiddlyLinkNonExisting";
	if(!subTitle)
		subTitle = title + " doesn't yet exist";
	var btn = createTiddlyButton(place,text,subTitle,null);
	btn.className = theClass;
	btn.setAttribute("tiddlyLink",title);
	btn.setAttribute("tiddlySingle",single ? "true" : "false");
	return(btn);
}


// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(initWikify);	// run init onLoad


