// vote.inc.php v1.23 +4.1 (http://hsus.xrea.jp/wiki/PC/SoftWare/PukiWiki/#vote-plus) 用
_d = document;
isMSIE = !!(window.attachEvent && !window.opera);

function create_pie(i_pie_zone, mode) {
	// 一つ上のFORMを探す
	var tr = _d.getElementById('flashcontent' +i_pie_zone);  while (tr !== null && tr.tagName !== 'FORM')  tr = tr.previousSibling;
	// その子供のTABLEを探す
	tr = tr.firstChild;  while (tr !== null && tr.tagName !== 'TABLE')  tr = tr.nextSibling;
	// その子供のTBODYを探す
	tr = tr.firstChild;  while (tr !== null && tr.tagName !== 'TBODY')  tr = tr.nextSibling;
	// その子供の2つめのTRを探す
	tr = tr.firstChild;  while (tr !== null && tr.tagName !== 'TR')  tr = tr.nextSibling;
	tr = tr.nextSibling; while (tr !== null && tr.tagName !== 'TR')  tr = tr.nextSibling;
	
	var param = '';
	var gid = 0;
	for (var i=1; tr; ++i) {
		// 1つ目のTDを探す
		tr = tr.firstChild;
		while (tr !== null && tr.tagName !== 'TD')
			tr = tr.nextSibling;
		var td1 = tr.innerHTML.replace(/<[^>]*>/g, '');
		// 2つ目のTDを探す
		tr = tr.nextSibling;
		while (tr !== null && tr.tagName !== 'TD')
			tr = tr.nextSibling;
		var td2 = tr.innerHTML.match(/^\d+/);
		
		if (mode === 'pie')
			param += td1 + ';' + td2 + '\n';
		else if (mode === 'column') {
			param = '<graph gid="' +gid+ '" title="' +td1+ '"><value xid="0">' +td2+ '</value></graph>' + param;
			++gid;
		} else
			// JAVA
			param += '<param name="category' +i+ '-per" value="' +td2+ '"><param name="category' +i+ '" value="' +td1+ '">';
		
		// 次のTRを探す
		tr = tr.parentNode.nextSibling;
		while (tr !== null && tr.tagName !== 'TR')
			tr = tr.nextSibling;
	}
	
	if (mode === 'pie' || mode === 'column') {
		/* Flash */
		var height = i > 9
			? _d.getElementById('vote'+i_pie_zone).offsetHeight
			: 280;  // pie
		var so = new SWFObject('/swf/am'+mode+'.swf', 'am'+mode+i_pie_zone, 600, height, 8, '#FFFFFF');
		so.addVariable('path', '/swf/');
		//so.addVariable('settings_file', encodeURIComponent('/swf/vote_'+mode+'.xml'));
		so.addVariable('chart_settings',
			mode === 'pie'
			? encodeURIComponent('<settings>'
			+ '<width></width><height></height><data_type>csv</data_type><text_size>12</text_size><decimals_separator>.</decimals_separator><thousands_separator>,</thousands_separator><precision>1</precision>'
			+ '<export_image_file>/swf/export.php</export_image_file>'
			+ '<pie><x>380</x><y>130</y><radius>80</radius><inner_radius>0</inner_radius><height>0</height><angle>0</angle></pie>'
			+ '<animation><start_time>2</start_time><start_effect>strong</start_effect><start_radius>150</start_radius><pull_out_time>1</pull_out_time></animation>'
			+ '<data_labels><radius>20</radius><max_width>80</max_width><show><![CDATA[{title} : {value} ({percents}%)]]></show><hide_labels_percent>3</hide_labels_percent></data_labels>'
			+ '<balloon><show><![CDATA[{title} : {value} ({percents}%)]]></show></balloon>'
			//+ '<balloon><text_color>#000000</text_color><alpha>0</alpha><show><![CDATA[{title} : {value} ({percents}%)]]></show></balloon>'  // SS用
			+ '<legend><x>0</x><y>25</y><max_columns>1</max_columns><spacing>5</spacing><key><border_color>#000000</border_color></key></legend>'
			+ '</settings>'
			)
			: encodeURIComponent('<settings>'
			+ '<type>bar</type><width></width><height></height><data_type>xml</data_type><text_size>13</text_size><decimals_separator>.</decimals_separator><thousands_separator>,</thousands_separator><angle>0</angle>'
			+ '<export_image_file>/swf/export.php</export_image_file>'
			+ '<column><width>100</width><spacing>0</spacing><grow_time>6</grow_time><alpha>60</alpha><data_labels><![CDATA[{title} : {value} ({percents}%)]]></data_labels><data_labels_text_color>#222222</data_labels_text_color><data_labels_position>inside</data_labels_position><balloon_text><![CDATA[{title} : <b>{value} / {total} ({percents}%)</b>]]></balloon_text></column>'
			+ '<line><balloon_text><![CDATA[{value}]]></balloon_text></line>'
			+ '<background><border_color>#000000</border_color><border_alpha>15</border_alpha></background>'
			+ '<plot_area><margins><left>0</left><top>20</top><right>0</right><bottom>0</bottom></margins></plot_area>'
			+ '<grid><category><alpha>0</alpha></category><value><alpha>0</alpha></value></grid>'
			+ '<values><category><enabled>false</enabled><color>#222222</color></category><value><enabled>false</enabled><min>0</min><frequency>1</frequency></value></values>'
			+ '<axes><category><alpha>0</alpha><width>1</width></category><value><alpha>0</alpha><width>1</width><tick_length>7</tick_length></value></axes>'
			+ '<balloon><text_size>13</text_size></balloon>'
			+ '<legend><enabled>false</enabled></legend>'
			+ '</settings>'
			)
		);
		if (mode === 'pie')
			so.addVariable('chart_data', encodeURIComponent(/;(?!0\s)\d/.test(param) ? param : 'Dummy;1'));
		else
			so.addVariable('chart_data', encodeURIComponent('<chart><series><value xid="0">Unknown</value></series><graphs>' + param + '</graphs></chart>'));
		so.addVariable('preloader_color', '#000000');
		so.write('flashcontent'+i_pie_zone);
	} else {
		/* Java */
		_d.getElementById('flashcontent' +i_pie_zone).innerHTML
		= '<applet codebase="/java/" archive="jfreechart-1.0.1.jar" code="PieChart.class" name="pie' +i_pie_zone+ '" id="pie' +i_pie_zone+ '" width="690" height="430">'
		+ param
		+ (i < 20 ? '<param name="title" value="' +_d.title.replace(/ - .*$/, '')+ '"><param name="legend">' : '')
		+ '<em>JavaAppletが無効になっているか、使用できないブラウザのようです。JavaAppletを開始できませんでした。</em><br /></applet>表示欠けが発生する場合、グラフを右クリックしてみてください。'
		;
		
		// 読み込み終わってから実行しないと意味なさそう
		// 何故かFireFox 1.5.0.4 だとCPUを食い潰すので処理させない
		// JavaAppletからPanelの大きさを取得し、Appletタグの大きさをそれに合わせる
		try {
			_d.getElementsByName('pie' +i_pie_zone)[0].width = eval('_d.pie' +i_pie_zone+ '.getX() + 10');
			_d.getElementsByName('pie' +i_pie_zone)[0].height = eval('_d.pie' +i_pie_zone+ '.getY() + 10');
		} catch(e) {}
	}
}


function write_java_pie_btn(i_pie_zone, mode) {
	// Flashが使えないことがわかった場合、Javaによるグラフ表示ボタンを出力
	setTimeout(function(){
		try {
			_d.getElementById('am'+mode+i_pie_zone).FlashVersion();
		} catch(e) {
			// Flash 使用不可
			if (navigator.javaEnabled())
				// Java 使用可（信用ならず）
				_d.getElementById('flashcontent' +i_pie_zone).innerHTML = '<input type="button" value="Flashが実行出来ないので、JavaAppletで円グラフを表示する　※初回起動は非常に遅いです" onclick="this.style.display=\'none\'; create_pie(' +i_pie_zone+ ')" />';
			else
				//_d.write('<b>JavaAppletによるグラフの描写も出来ません。Javaを有効にしてください。</b>');
				_d.getElementById('flashcontent' +i_pie_zone).innerHTML += '<b>JavaAppletによるグラフの描写も出来ません。Javaを有効にしてください。</b>';
		}
	}, 1000);
}
