function setwiki(){

	$j('#wikitabs ul li a').click(function(){clickTag(this);return false;});
	$j(".descb .wikimore a").click(function(){clickMore(this);return false;});
	$j(".descb .wikiclose a").click(function(){clickClose(this);});

}

var wikiNowTab = 'wiki01';
var wikiTabCount = 0;
var wikiTabNo = '';
var exludeWikiTab = new Array('脚注' ,'関連項目' ,'外部リンク');

function getwiki(){
	var keyword = wikiWord;
	if(keyword == ''){
		keyword = word;
	}

	$j.ajax({
		dataType: "jsonp",
		data: {
			"keyword": keyword
		},
		url: wikiUrl +'/getwiki.php',
		timeout: 3000,
		success: function(data){
			if(data == null){
				return;
			}
			wikiTabCount = 0;
			wikiNowTab = 'wiki01';
			//getLead(data);
			getChapter(data.chapter);

			$j('#wikitabs ul li a').click(function(){clickTag(this);return false;});
			$j(".descb .wikimore a").click(function(){clickMore(this);return false;});
			$j(".descb .wikiclose a").click(function(){clickClose(this);});

		}
	});
}

//続きを読むイベント
function clickMore(obj){
	$j("#" +wikiNowTab + " .wikimore").hide();
	$j("#" +wikiNowTab + " .wikibody").show();
	$j(".descb .wikiclose").show();
}

//閉じるイベント
function clickClose(obj){
	$j("#" +wikiNowTab + " .wikimore").show();
	$j("#" +wikiNowTab + " .wikibody").hide();
	$j(".descb .wikiclose").hide();
	

}

function clickTag(obj){
	$j(".descb .wikimore").show();
	$j(".descb .wikiclose").hide();
	$j("#" +wikiNowTab + " .wikibody").hide();
	$j('#' + wikiNowTab).hide();

//	$j('#wikitabs ul li').className='ui-state-default ui-corner-top';
	for(i=0 ;i< $j('#wikitabs ul li').length;i++ ){
		$j('#wikitabs ul li')[i].className='ui-state-default ui-corner-top';
	}

	wikiNowTab = obj.name;

//	obj.parentNode.className='ui-state-default ui-corner-top ui-tabs-selected ui-state-active';
	var tmp = $j(obj);
	tmp.parent()[0].className='ui-state-default ui-corner-top ui-tabs-selected ui-state-active';

	$j('#' + wikiNowTab).show();

}
/*
function getLead(wiki){
	var html = ''
	if(wiki['lead'] != undefined){
		html += wiki['lead'];
		$j(".intro").append(html);
	}
}
*/
//タブ部作成
function setTab(title){
	for(i=0;i<exludeWikiTab.length;i++){
		if(exludeWikiTab[i] == $j.trim(title)){
			return false;
		}
	}

	wikiTabCount++;
	if(wikiTabCount < 10){
		wikiTabNo = '0' + wikiTabCount.toString();
	}else{
		wikiTabNo = wikiTabCount.toString();
	}

		// li 要素の生成
		var li = document.createElement('li');
		if(wikiTabCount == 1){
			li.className = 'ui-state-default ui-corner-top ui-tabs-selected ui-state-active';
		}
		// a 要素の生成
		var atag = document.createElement('a');
		atag.href = '#wiki'+ wikiTabNo;
		atag.name = 'wiki'+ wikiTabNo;
//		atag.appendChild(document.createTextNode(title));

		title = title.replace(/^\[\[/,'');
		title = title.replace(/\]\]$/,'');

		$j(atag).html(title);

		li.appendChild(atag);
		$j("#wikitabs ul").append(li);

		return true;

}
//本文作成
function setTabContents(contentHtml){
/*
	var html = '';
	//文字分割
	var split = "<br />";
	var pos = contentHtml.indexOf(split, 60);
	var content1 = contentHtml.substr(0 ,pos + split.length);
	var content2 = contentHtml.substr(pos + split.length);
*/

	var splitMax = 60;
	var html = '';
	var content1 = '';
	var content2 = '';

	//先頭のBRタグ削除
	contentHtml = contentHtml.replace(/^(<br \/>)*/, '');

	if(contentHtml.length > splitMax){
		//文字分割
		var split = "<br />";
		var pos = contentHtml.indexOf(split, splitMax);

		if(pos == -1){
			pos = contentHtml.lastIndexOf(split);
		}
		if(pos != -1){
			content1 = contentHtml.substr(0 ,pos + split.length);
			content2 = contentHtml.substr(pos + split.length);
		}else{
			content1 = contentHtml;
		}

	}else{
		content1 = contentHtml;
	}

	content1 = content1.replace(/(<a.*?>)/gi, "");
	content1 = content1.replace(/(<\/a.*?>)/gi, "");

	content2 = content2.replace(/(<a.*?>)/gi, "");
	content2 = content2.replace(/(<\/a.*?>)/gi, "");

	// div 要素の生成
	var div = document.createElement('div');
	div.id = 'wiki' + wikiTabNo;
	if(wikiTabCount >1){
		div.style.display = "none";
	}else{
		div.style.display = "block";
	}
//	div.appendChild(document.createTextNode(contentHtml));

	var div1 = document.createElement('div');
	div1.innerHTML = content1;
	div1.className = 'wikiteaser';
	div.appendChild(div1);

	if($j.trim(content2) != "" && $j.trim(content2) != "<br />"){
		var divMore = document.createElement('div');
		divMore.className = 'wikimore';
		divMore.innerHTML = '<a href="#">続きを読む</a>';
		div.appendChild(divMore);

		var div2 = document.createElement('div');

		div2.innerHTML = content2;
		div2.className = 'wikibody';
		div2.style.display = "none";
		div.appendChild(div2);
	}
//	$j(".descb").append(div);
	$j("#wikiclose").after(div);


}

function getChapter(chapters){

	for(chapterIndex in chapters){
		var html = ''
		var chapter = chapters[chapterIndex];
		if(typeof(chapter.title) != 'undefined'){
			if(setTab(chapter.title) == false){
				continue;
			}
		}
		if(typeof(chapter.article) != 'undefined'){
			var article = chapter.article;
			//コメント削除
			article = delComment(article);
			//先頭のBRタグ削除
			article = article.replace(/^(\n)*/, '');
			html += nl2br(article);
		}

		if(typeof(chapter.table) != 'undefined'){
			html += makeTable(chapter.table);
		}

		for(subIndex in chapter.sub){
			var title = chapter.sub[subIndex].title;
			var article = chapter.sub[subIndex].article;
			html += title + '<br />';
			html += nl2br(article);
/*
			for(tableIndex in chapter.sub[subIndex].table){
				var table = chapter.sub[subIndex].table[tableIndex];
				for(table2Index in table){
					html += table2Index.toString() +":"+ table[table2Index];
				}
			}
*/
			if(typeof(chapter.sub[subIndex].table) != 'undefined'){
				html += makeTable(chapter.sub[subIndex].table);
			}

			for(sub2Index in chapter.sub[subIndex].sub){
				var title = chapter.sub[subIndex].sub[sub2Index].title;
				var article = chapter.sub[subIndex].sub[sub2Index].article;
				html += nl2br(title);
				html += nl2br( article);
/*
				for(tableIndex in  chapter.sub[subIndex].sub[sub2Index].table){
					var table =  chapter.sub[subIndex].sub[sub2Index].table[tableIndex];
					for(table2Index in table){
						html += table2Index.toString() +":"+ nl2br(table[table2Index]);
					}
				}
*/
				if(typeof(chapter.sub[subIndex].sub[sub2Index].table) != 'undefined'){
					html += makeTable(chapter.sub[subIndex].sub[sub2Index].table);
				}

			}
		}

		setTabContents(html);
	}

	return html;
}

function makeTable(table){

	var html = '';
	var isFirst = true;
	for(row in table){
		var tableRow = table[row];
		for(rowIndex in tableRow){
			if(isFirst == true){
				isFirst = false;
//				html += rowIndex +'<br />';
			}
			html += tableRow[rowIndex].toString() +'<br />';
		}
	}

/*
	var html = '<table border="1">';
	var isFirst = true;
	for(row in table){
		var tableRow = table[row];
		for(rowIndex in tableRow){
			html += '<tr>';
			if(isFirst == true){
				isFirst = false;
				html += makeTableTd(rowIndex ,true);
				html += '</tr><tr>';

			}
			html += makeTableTd(tableRow[rowIndex].toString() ,false);
		}
		html += '</tr>';
	}
*/
	html += '</table>';

	return html;
}

function makeTableTd(text ,isHead){
	var html = '';
	var tmp='';
	if(isHead == true){
		tmp = text.replace(/!!/g,'||').split('||');
	}else{
		tmp = text.split('||');
	}

	for(colIndex in tmp){
		var col = tmp[colIndex];
		var ret = col.match('(row|col)span="?(.*?)"?\\|(.*)');
		var span = '';
		if(ret != null){
			span = " " +ret[1] +'span="' + ret[2] + '"';
			html += '<td' + span + '>' +ret[3] +'</td>';
		}else{
			if(col == ''){
				html += '<td>&nbsp;</td>';
			}else{
				html += '<td>' +col +'</td>';
			}
		}
	}

	return html;
}

//コメント削除
function delComment(txt){
	txt = txt.replace(/<!--(.*?)-->/g, '');

	return txt;
}

//改行変換
function nl2br(txt){
	if(typeof(txt) == 'undefined'){
		return '';
	}
	txt = txt.replace(/\n{2,}/g, '\n');
	txt = txt.replace(/\n/g, '<br />');
//	txt = txt.replace(/\r\n/g, '<br />');
//	txt = txt.replace(/(\n|\r)/g, '<br />');
	return txt;
}