﻿// Arekore-textArea. last-update: 2005.01.02.
	// (c) snow-materia "http://sm.useyan.com/"


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	menu setting
var are = {
	// menu
		id   : 'ArekoreArea',	// メニューのID
		clas : 'hover',	// メニューの各リスト部分にオンマウスしたときのクラス名
		attr : 'at:title',	// title属性が何かしらの理由で使えない場合の代替属性

	// interval
		wait  : 2000,	// 最初にメニューを出してから消えるまでの時間 [ms]
		limit : 250,	// マウスがメニューから離れてから消えるまでの時間 [ms]

	// style
		height : 16,	// メニューの各リスト部分の高さ [px]
		border : 1,	// メニューの各リスト部分の枠の幅 [px]
		x : 0,	// menu-base-position-absolute .. 0:left, 1:right
		y : 0	// menu-base-position-attribute .. 0:top, 1:bottom
};


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	arekore setting
function are_tag_object() {
	// デフォルトのコンテキストメニューを設定.
	are.context_title = 'Context-Menu';	// コンテキストメニュータイトル
	are.context_id    = 'context-menu';	// コンテキストメニューのID
	// コンテキストメニューの名前 { 'キー' : '名前', ... }
	are.context_obj   = {
		_cut        : '切り取り',	// 切り取り
		_copy       : 'コピー',	// コピー
		_paste      : '貼り付け',	// 貼り付け
		_delete     : '削除',	// 削除
		all_select : 'すべて選択',	// すべて選択
		all_cut    : 'すべて切り取り',	// すべて切り取り
		all_copy   : 'すべてコピー',	// すべてコピー
		all_paste  : 'すべて貼り付け',	// すべて貼り付け
		all_delete : 'すべて削除'	// すべて削除
	};

	return {
	// メニューはいくらでも追加・変換して設定できます.
		// tag_list['グループ名'] = { 'メニュー' : '↓のルールに沿ったタグとか', ... };

	// テキスト選択部分に挿入するタグ・テキスト
		// A: テキスト選択部分のテキスト中の文字列-前半, B:-後半
	// `^ A`    で始まる場合 .. 行頭にそのテキストを付加
	// `B $`    で終わる場合 .. 行末にそのテキストを付加
	// `= A`    で始まる場合 .. その部分をそのテキストに置き換える
	// `A | B`  と書いた場合 .. A と B のテキストで挟む
	// `/A/, B` と書いた場合 .. A で正規表現させて B に置き換える
	// `/A|C|E/, B|D|F` と書いた場合 .. 順番に A,C,E で正規表現させて B,D,F に置き換える
	// `B /`    で終わる場合 .. <B> (or <B />) として、行末にそのタグを追加
	// `A` or `A B`   の場合 .. テキストを <A B> ～ </A> とタグで囲む

	//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-
	// COMMON
	'よく使うの' : {
		'p (段落)' : 'p',
		'br (強制改行)' : 'br /',
		'img (イメージ)' : 'img src="" alt="" /',
	
		'a (リンク)' : 'a href=""',
		'a title (タイトルつき)' : 'a href="" title=""',
		'a blank (別窓つき)' : 'a href="" title="" target="_blank"',
	
		'strong (強調)' : 'strong',
		'em (やや強調)' : 'em',
	
		'span (汎用インライン)' : 'span class=""',
		'div (汎用ブロック)' : 'div class=""'
	},
	// QUOTE
	'引用系' : {
		'q (行内の引用)' : 'q',
		'q cite (参照URLも)' : 'q cite=""',
		'blockquote (段落で引用)' : 'blockquote',
		'blockquote cite (参照URLも)' : 'blockquote cite=""',
		'cite (参照元)' : 'cite'
	},
	// CODE
	'コード系' : {
		'pre (テキスト的)' : 'pre',
		'code (ソースコード)' : 'code',
		'var (変数)' : 'var',
		'dfn (定義)' : 'dfn',
		'kbd (キーボード)' : 'kbd',
		'samp (サンプル)' : 'samp'
	},
	// INS, DEL
	'追加・削除・省略' : {
		'ins (追加)' : 'ins',
		'ins date (更新日付つき)' : 'ins datetime=""',
		'del (削除)' : 'del',
		'del date (更新日付つき)' : 'del datetime=""',
		'abbr (省略語)' : 'abbr title=""',
		'acronym (頭文字語)' : 'acronym title=""',
		'sub (下付き文字)' : 'sub',
		'sup (上付き文字)' : 'sup'
	},
	// LIST
	'リスト系' : {
		'ol (番号付リスト)' : 'ol',
		'ul (箇条書きリスト)' : 'ul',
		'li (～のリスト項目)' : 'li',
		'dl (定義型リスト)' : 'dl',
		'dt (～の項目部分)' : 'dt',
		'dd (～の説明部分)' : 'dd'
	},
	// HEADER-LEVEL
	'見出し系' : {
		'h1 (レベル 1)' : 'h1',
		'h2 (レベル 2)' : 'h2',
		'h3 (レベル 3)' : 'h3',
		'h4 (レベル 4)' : 'h4',
		'h5 (レベル 5)' : 'h5',
		'h6 (レベル 6)' : 'h6'
	},
	// TABLE
	'テーブル系' : {
		'table (表)' : 'table summary=""',
		'caption (表のタイトル)' : 'caption',
		'colgroup (列の構造的グループ化)' : 'colgroup',
		'colgroup span (複数列で)' : 'colgroup span=""',
		'col (列の属性共有グループ化)' : 'col',
		'col span (複数列で)' : 'col span=""',
		'thead (ヘッダ部分)' : 'thead',
		'tfoot (フッタ部分)' : 'tfoot',
		'tbody (本体部分)' : 'tbody',
		'tr (行部分)' : 'tr',
		'th (ヘッダセル部分)' : 'th',
		'td (セル部分)' : 'td',
		'td colspan (複数列)' : 'td colspan=""',
		'td rowspan (複数行)' : 'td rowspan=""',
		'tables (まとめて)' : '<table summary=""><caption></caption><tbody><tr><td>|</td></tr></tbody></table>'
	},
	// FORM
	'フォーム系' : {
		'form (フォーム)' : 'form action="" method="post"',
		'fieldset (グループ化)' : 'fieldset',
		'legend (グループのタイトル)' : 'legend',
		'label (ラベル)' : 'label',
		'label for (関連付け)' : 'label for=""',
		'textarea (複数行テキストエリア)' : 'textarea name="" cols="" rows=""',
		'select (セレクトメニュー)' : 'select name=""',
		'optgroup (～項目グループ化)' : 'optgroup',
		'option (～項目)' : 'option value=""',
		'option on (～セレクト)' : 'option value="" selected="selected"',
		'input text (1行テキスト)' : 'input type="text" name="" value="" /',
		'input radio (ラジオボタン)' : 'input type="radio" name="" value="" /',
		'input radio on (～チェック)' : 'input type="radio" name="" value="" checked="checked" /',
		'input checkbox (チェックボックス)' : 'input type="checkbox" name="" value="" /',
		'input checkbox on (～チェック)' : 'input type="checkbox" name="" value="" checked="checked" /',
		'input pass (パスワード)' : 'input type="password" name="" value="" /',
		'input hidden (隠し要素)' : 'input type="hidden" name="" value="" /',
		'input file (ファイル参照)' : 'input type="file" name="" /',
		'input button (ボタン)' : 'input type="button" value="" /',
		'input submit (送信)' : 'input type="submit" value="submit" /',
		'input reset (リセット)' : 'input type="reset" value="reset" /'
	},
	// OBJECT, SCRIPT
	'オブジェクトとか' : {
		'object (オブジェクト)' : 'object type=""',
		'style (スタイルシート)' : 'style type="text/css"',
		'script (JavaScript)' : 'script type="text/javascript"',
		'script src (外部JS)' : 'script type="text/javascript" src=""',
		'noscript (JS-Offのとき)' : 'noscript'
	},
	// HTML, META, LINK, BODY
	'HTML基礎' : {
		'html (HTML)' : 'html lang="ja"',
		'html xml (XHTML)' : 'html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"',
		'head (ヘッダ領域)' : 'head',

		'meta (メタ情報)' : 'meta name="" content="" /',
		'meta type (文書型と文字コード)' : 'meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /',
		'meta type sjis (～シフトJIS)' : 'meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" /',
		'meta type euc (～EUC)' : 'meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" /',
		'meta style (CSSのタイプ)' : 'meta http-equiv="Content-Style-Type" content="text/css" /',
		'meta script (Scriptタイプ)' : 'meta http-equiv="Content-Script-Type" content="text/javascript" /',

		'title (タイトル)' : 'title',

		'link (関連付け)' : 'link rel="" title="" href="" /',
		'link css (外部CSS)' : 'link rel="stylesheet" type="text/css" media="screen,tv" href="" /',
		'link rss (RSS)' : 'link rel="alternate" type="application/rss+xml" title="" href="" /',
		'link favicon (お気にアイコン)' : 'link rel="shortcut icon" title="" href="/favicon.ico" /',

		'body (本文領域)' : 'body',
		'address (問い合わせ先)' : 'address',
		'hr (罫線)' : 'hr /',
		'!---- (コメント文)' : '<!--|-->'
	},
	// DOCTYPE
	'DOCTYPE宣言' : {
		'?xml?' : '= <?xml version="1.0" encoding="UTF-8"?>',
		'!doctype xhtml1.1' : '= <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
		'!doctype xhtml1.0-strict' : '= <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
		'!doctype xhtml1.0-transitional' : '= <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
		'!doctype xhtml1.0-frameset' : '= <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
		'!doctype html4.01-strict' : '= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
		'!doctype html4.01-transitional' : '= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
		'!doctype html4.01-none' : '= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">',
		'!doctype html4.01-frameset' : '= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
		'!doctype compact-html1.0' : '= <!DOCTYPE HTML PUBLIC "-//W3C//DTD Compact HTML 1.0 Draft//EN">'
	},

	// ABBREVIATION, ACRONYM
	'省略語・頭文字語' : {
		'[Win]' : '=<abbr title="Windows-Operating-System">Win</abbr>',
		'[Mac]' : '=<abbr title="Macintosh-Operating-System">Mac</abbr>',
		'[MS]' : '=<acronym title="MicroSoft">MS</acronym>',
		'[IE]' : '=<acronym title="Internet Explorer">IE</acronym>',
		'[NN]' : '=<acronym title="Netscape Navigator">NN</acronym>',

		'[WWW]' : '=<acronym title="World Wide Web">WWW</acronym>',
		'[W3C]' : '=<acronym title="World Wide Web Consortium">W3C</acronym>',

		'[HTML]' : '=<acronym title="HyperText Markup Language">HTML</acronym>',
		'[XHTML]' : '=<acronym title="eXtensible HyperText Markup Language">XHTML</acronym>',
		'[XML]' : '=<acronym title="eXtensible Markup Language">XML</acronym>',
		'[SGML]' : '=<acronym title="Standard Generalized Markup Language">SGML</acronym>',
		'[DHTML]' : '=<acronym title="Dynamic HyperText Markup Language">DHTML</acronym>',

		'[DOM]' : '=<acronym title="Document Object Model">DOM</acronym>',
		'[CSS]' : '=<acronym title="Cascading Style Sheets">CSS</acronym>',
		'[JS]' : '=<acronym title="JavaScript">JS</acronym>',

		'[CGI]' : '=<acronym title="Common Gateway Interface">CGI</acronym>',
		'[PHP]' : '=<acronym title="Hypertext PreProcessor">PHP</acronym>',

		'[CMYK]' : '=<acronym title="Cyan Magenta Yellow blacK">CMYK</acronym>',
		'[RGB]' : '=<acronym title="Red Green Blue">RGB</acronym>',
		'[HSB]' : '=<acronym title="Hue Saturation Brightness">HSB</acronym>',
		'[HSV]' : '=<acronym title="Hue Saturation Value">HSV</acronym>'
	},
	// CHARACTER-REFERENCES
	'文字参照' : {
		'&' : '=&amp;',
		'"' : '=&quot;',
		'<' : '=&lt;',
		'>' : '=&gt;',
		'\\space' : '=&nbsp;',
		'(c)' : '=&copy;',
		'$' : '=&#36;',
		'@' : '=&#64;',
		'%' : '=&#37;',
		'\\' : '=&#92;',
		'\uff5e' : '=&sim;',
		'\u2026' : '=&hellip;',
		'\u2022' : '=&bull;',
		'\u2013' : '=&ndash;',
		'\u2014' : '=&mdash;',
		'\u2191' : '=&uarr;',
		'\u2193' : '=&darr;',
		'\u2190' : '=&larr;',
		'\u2192' : '=&rarr;',
		'\u2194' : '=&harr;',
		'\u21d4' : '=&hArr;'
	},

	// LINE-MARK
	'行頭・行末にマーク' : {
		'\u30fb .. ' : '^\u30fb',
		'> .. ' : '^> ',
		'# .. ' : '^# ',
		'\\t .. ' : '^\t',
		' .. !' : ' !$',
		' .. ?' : ' ?$',
		'&lt; .. &gt;' : '&lt;|&gt;',
		'&quot; .. &quot;' : '&quot;|&quot;'
	},
	// REGULAR-EXPRESSION
	'正規表現で置換' : {
		'& → &amp;' : '/&/, &amp;',
		'" → &quot;' : '/"/, &quot;',
		'< → &lt;' : '/</, &lt;',
		'> → &gt;' : '/>/, &gt;',
		'&|"|<|>|\\t → &amp;|&quot;|&lt;|&gt;|\\sp*4' : '/&|"|<|>|\t/, &amp;|&quot;|&lt;|&gt;|    ',
		'\\t → (\\space * 4)' : '/\t/,    ',
		'(\\space * 4) → &nbsp; &nbsp; ' : '/    /,&nbsp; &nbsp; ',
		'&amp; → &' : '/&amp;/,&',
		'&quot; → "' : '/&quot;/,"',
		'&lt; → <' : '/&lt;/,<',
		'&gt; → >' : '/&gt;/,>',
		'&gt;|&lt;|&quot;|&amp;|\\sp*4 → >|<|"|&|\\t' : '/&gt;|&lt;|&quot;|&amp;|    /, >|<|"|&|\t',
		'(\\space * 4) → \\t' : '/    /,\t',
		'&nbsp; &nbsp;  → (\\space * 4)' : '/&nbsp; &nbsp; /,    '
	},

	//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-
	'':''};
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	script
// text-transform
function are_text_transform(tag) {
    if (!tag) return false;
    var ds = document.selection;
    if (typeof(ds.createRange()) != 'object' || 
    	typeof(ds.createRange().duplicate()) != 'object') return false;
	var dupli = ds.createRange().duplicate();
	// not textarea
	if (typeof(dupli.parentElement()) != 'object' ||
		dupli.parentElement().nodeName != 'TEXTAREA') {
		are['text_focus'] = new Object();
		return false;
	}

	// text
    var str = (dupli.text) ? dupli.text.replace(/\r\n|\r|\n/g, "\n") : '';
    var all = false;
    var copy = false;

	// replace-text	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-
	// clipboard [cut, copy, paste, ...]
	if (tag.match(/^(_|all_)(cut|copy|paste|delete|select)$/)) {
		// all-select
		if (tag.match(/^all_/)) {
			are['busy'] = true;
			dupli.parentElement().select();
			dupli = document.selection.createRange().duplicate();
		    str = (dupli.text) ? dupli.text.replace(/\r\n|\r|\n/g, "\n") : '';
	    	all = true;
		}
		// cut, copy
		if (tag.match(/(cut|copy)$/)) {
			window.clipboardData.setData('text', str);
		}
		// paste
		if (tag.match(/paste$/)) {
			str = window.clipboardData.getData('text');
		}
		// cut, delete
		if (tag.match(/(cut|delete)$/)) {
			str = '';
		}
		// no transform
		if (tag.match(/(copy|select)$/)) copy = true;
	}
	// line-start
	else if (tag.match(/^\^/)) {
		tag = tag.replace(/^\^/, '');
		str = str.replace(/(\n|^)/g, "$1" + tag);
	}
	// line-end
	else if (tag.match(/\$$/)) {
		tag = tag.replace(/\$$/, '');
		str = str.replace(/(\n|$)/g, tag + "$1");
	}
	// tag only
	else if (tag.match(/^\=/)) {
		str = tag.replace(/^\=\s*/, '');
	}
	// some replace
	else if (tag.match(/^\/.+\|.+\/\s*\,/)) {
		var reg = tag.match(/^\/(.+\|.+)\/\s*\,(.*)$/);
		var key = reg[1].split('|');
		var value = reg[2].split('|');
		for (var i=0; i<key.length; i++) {
			if (!key[i]) next;
			var re = new RegExp(key[i], 'g');
			var vs = value[i] || '';
			str = str.replace(re, vs);
		}
	}
	// replace
	else if (tag.match(/^\/.+\/\s*\,/)) {
		var reg = tag.match(/^\/(.+)\/\s*\,(.*)$/);
		var re = new RegExp(reg[1], 'g');
		str = str.replace(re, reg[2]);
	}
	// cut in
	else if (tag.match(/\|/)) {
		var tags = tag.split(/\|/);
		str = tags[0] + str + tags[1];
	}
	// space-element
	else if (tag.match(/\/$/) || !tag.match(/^\w+/)) {
		if (!are['xhtml']) tag = tag.replace(/\s*\/$/, '');
		str += '<' + tag + '>';
	}
	// normal-element
	else {
		var tags = tag.match(/^\w+/);
	    str = '<' + tag + '>' + str + '</' + tags[0] + '>';
	}

	// not all-select
	if (!copy) dupli.text = str;

	// set this object to global
	are['text_focus'] = dupli;
	are['text_focus'].index = str.length;
	are['text_focus'].on = true;
	are['text_focus'].all = all;

	return true;
}


// begin text-transform
function are_begin_transform(e) {
	var obj = are_this_object(this, e);
	if (!obj) return;

	var item = (are['attr']) ? obj.getAttribute(are['attr']) : obj.title;
	if (!item && are['attr']) item = obj.title;
	if (item) are_text_transform(item);
	return false;
}


// begin text-transform, text-select
function are_end_transform() {
	are['busy'] = true;
	var obj = are['text_focus'];
	if (typeof(obj) == 'object' && obj.on) {
		obj.parentElement().focus();
		if (obj.all) {
			obj.parentElement().select();
		} else if (obj.index) {
			obj.moveStart('character', - obj.index);
			obj.moveEnd('character', 0);
			obj.select();
		}
	}
	are['busy'] = false;
	return false;
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	create menu
// check this-object
function are_this_object(obj, e) {
	if (!obj.nodeName && typeof(e) == 'object' && e.srcElement) obj = e.srcElement;	// IE
	if (typeof(obj) != 'object' || !obj.nodeName || obj.nodeType != 1) return 0;
	return obj;
}


// create-menu-object
function are_create_menu() {
	var d = document;

	// textareas
	var areas = d.getElementsByTagName('TEXTAREA');
	if (!areas || !areas.length) return;

	// are_tag_objectect
	var tag_list = are_tag_object();
	if (typeof(tag_list) != 'object') return;

	// div-elements
	var div = d.createElement('DIV');
	d.body.appendChild(div);
	div.setAttribute('id', are['id']);

	// first-list
	var ul = d.createElement('UL');
	div.appendChild(ul);

	// context-menu
	if (are['context_obj'] && typeof(are['context_obj']) == 'object') {
		// list
		var li = d.createElement('LI');
		ul.appendChild(li);
		if (are['context_id']) li.setAttribute('id', are['context_id']);
		if (are['context_title']) li.appendChild(d.createTextNode(are['context_title']));

		// list ->
		var li_ul = d.createElement('UL');
		li.appendChild(li_ul);

		// children
		for (var k in are['context_obj']) {
			if (!k || !tag_list[k]) continue;
			var li_li = d.createElement('LI');
			li_ul.appendChild(li_li);
			are_create_list(li_li, are['context_obj'][k], k);	// to are_create_list()
		}
	}

	// format
	are['context_title'] = null;
	are['context_id']  = null;
	are['context_obj'] = null;

	// tag-menu
	for (var k in tag_list) {
		if (!k || !tag_list[k]) continue;
		var li = d.createElement('LI');
		ul.appendChild(li);
		are_create_list(li, k, tag_list[k]);	// to are_create_list()
	}

	// format
	tag_list = null;

	// context-menu-store
	are_menu_begin();

	// link textarea
	for (var i=0; i < areas.length; i++) {
		areas[i].attachEvent('oncontextmenu', are_menu_open);
		areas[i].attachEvent(
			'onselect', function () { if (!are['busy']) are_menu_open(); }
		);
	}
}
// create-list while children
function are_create_list(to, key, value) {
	var d = document;
	// data-type (object, string, object or string)
	if (typeof(to) != 'object'
		|| typeof(key) != 'string' || typeof(value) == 'undefined') return false;

	// list
	to.appendChild( d.createTextNode(key) );

	// value-type : object
	if (typeof(value) == 'object') {
		// parent
		var parent = d.createElement('UL');
		to.appendChild(parent);
		// children
		for (var k in value) {
			var child = d.createElement('LI');
			parent.appendChild(child);
			are_create_list(child, k, value[k]);	// to are_create_list()
		}
		return true;
	}
	// value-type : string
	else {
		to.setAttribute('title', value);
		// alternate-attribute
		if (are['attr']) {
			var attr = d.createAttribute(are['attr']);
			attr.nodeValue = value;
			to.setAttributeNode(attr);
		}
		// transform on mousedown
		to.attachEvent('onmousedown', are_begin_transform);
		// focus on mousedown
		to.attachEvent('onmouseup', are_end_transform);
	}
	return true;
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	menu
function are_menu_begin() {
	var d = document;
	var to = d.getElementById(are['id']);
	if (!to || !to.getElementsByTagName('UL')) return;

	to.style['position'] = 'absolute';
	to.style['visibility'] = 'hidden';

	var maxy = 0;	// maxy <= 0
	var menu = to.getElementsByTagName('UL').item(0);
	are_menu_store(menu, maxy);

	to.style['display'] = 'none';

	to.attachEvent('onmouseout', are_menu_close);
	to.attachEvent('onmouseover', are_menu_keep);
	to.attachEvent('onmouseup', are_menu_close_1);
//	addEvents(are_menu_close, 'mouseout', to);
//	addEvents(are_menu_keep, 'mouseover', to);
}


// open-menu-list at mouseover
function are_menu_open(e) {
	var to = document.getElementById(are['id']);
	var xy = are_position(e);
	var x_ = are['x'];
	var y_ = are['y'];

	// menus
	var ula = to.getElementsByTagName('UL');
	if (ula && ula.length) {
		// box_height
		var li_num = 0;
		var lia = ula[0].childNodes;
		for (var i=0; i<lia.length; i++) if (lia[i].nodeType == 1) li_num++;
		var box_height = li_num * (are['height'] + are['border']) || 0;
		var box_width  = 0;

		// window_margin
		var box_margin = new Object();
		box_margin = {
			'top'    : xy[5] - xy[3],
			'bottom' : xy[3] + xy[1] - xy[5],
			'left'   : xy[4] - xy[2],
			'right'  : xy[2] + xy[0] - xy[4]
		};

		// revise position
//		if (!are['x'] && box_margin['right']  - box_width  < 0) xy[4] -= box_width;
//		if ( are['x'] && box_margin['left']   - box_width  < 0) xy[4] += box_width;
		if (!are['y'] && box_margin['bottom'] - box_height < 0) xy[5] -= box_height;
		if ( are['y'] && box_margin['top']    - box_height < 0) xy[5] += box_height;
	}

	// style
	to.style['visibility'] = 'visible';
	to.style['display'] = 'block';
	to.style['left']   = (!are['x']) ? (xy[4]) + 'px' : 'auto';
	to.style['right']  = ( are['x']) ? (xy[2] + xy[0] - xy[4]) + 'px' : 'auto';
	to.style['top']    = (!are['y']) ? (xy[5]) + 'px' : 'auto';
	to.style['bottom'] = ( are['y']) ? (xy[3] + xy[1] - xy[5]) + 'px' : 'auto';

	// timeout
	if (are['set']) clearTimeout(are['set']);
	are['set'] = setTimeout("are_menu_close_1()", are['wait']);
	return false;
}
// keep-menu at mouseover
function are_menu_keep(){
	if (are['set']) clearTimeout(are['set']);
	are['set'] = null;
}
// close-menu-list at mouseout
function are_menu_close() {
	if (are['set']) clearTimeout(are['set']);
	are['set'] = setTimeout("are_menu_close_1()", are['limit']);
}
function are_menu_close_1(q) {
	if (are['set']) clearTimeout(are['set']);
	are['set'] = null;
	var to = document.getElementById(are['id']);
	to.style['visibility'] = 'hidden';
	to.style['display'] = 'none';
	are_menu_list_close_2(to.lastChild);
}


// store-list-children
function are_menu_store(obj, maxy) {
	var lia = obj.childNodes;
	var ht = are['height'];
	var bd = are['border'];

	var li_num = 0;
	for (var i=0; i<lia.length; i++) if (lia[i].nodeType == 1) li_num++;
	var h = 0;
	for (var i=0; i<lia.length; i++, h++) {
		var li = lia[i];
		if (li.nodeType != 1) continue;	// element ?
		li.attachEvent('onmouseover', are_menu_list_open);
		li.attachEvent('onmouseout',  are_menu_list_close);

		li.style['height'] = ht + 'px';
		li.style['lineHeight'] = ht + 'px';
		var ula = li.childNodes;
		for (var j=0; j<ula.length; j++) {
			var ul = ula[j];
			if (ul.nodeName == 'UL'){
				ul.style['visibility'] = 'hidden';
				ul.style['display'] = 'none';
				ul.style['position'] = 'absolute';
				var y = 0;
				var clia = ul.childNodes;
				var cli_num = 0; 	// this-child-length
				for (var k=0; k<clia.length; k++) if (clia[k].nodeType == 1) cli_num++;
				if (!are['y']) {
					y = h;
					if (li_num - h - cli_num < 0) y = li_num - cli_num;
					if (y < maxy) y = maxy;
					ul.style['top'] = (y * (ht + bd)) + 'px';
				}
				else {
					y = (li_num - h - cli_num) * (ht + bd);
					if (y < maxy) y = maxy;
					ul.style['bottom'] = (y * (ht + bd)) + 'px';
					y *= -1
				}
				are_menu_store(ul, maxy + y);
			}
		}
	}
}


// open-menu-list at mouseover
function are_menu_list_open(e) {
	var obj = are_this_object(this, e);
	if (!obj || obj.nodeName != 'LI') return;

	if (are['set']) clearTimeout(are['set']);
	are_menu_list_close_2(obj.parentNode, obj);
	if (are['clas']) obj.className = are['clas'];

	var ula = obj.childNodes;	// ul
	for (var i=0; i<ula.length; i++) {
		var ul = ula[i];
		if (ul.nodeName == 'UL' && ul.style){
			ul.style['visibility'] = 'visible';
			ul.style['display'] = 'block';
		}
	}
	return false;
}
// close-menu-list at mouseover
function are_menu_list_close(e) {
	var obj = are_this_object(this, e);
	if (!obj || obj.nodeName != 'LI') return;

	if (are['set']) clearTimeout(are['set']);
	if (are['clas']) obj.className = '';
	are['obj'] = obj;
	are['set'] = setTimeout("are_menu_list_close_1(are['obj']);", are['limit']);
	return false;
}
function are_menu_list_close_1(o) {
	if (!o && this && window.event) o = are_this_object(this, window.event);
	if (!o) return;
	are_menu_list_close_2(o.parentNode);	// ul
}
function are_menu_list_close_2(o, no) {
	if (!o) return;
	var lia = o.childNodes;	// li
	for (var i=0; i<lia.length; i++) {
		if (no && no == lia[i]) continue;	// self
		var ula = lia[i].childNodes;	// li
		for (var j=0; j<ula.length; j++) {
			var ul = ula[j];
			if (ul.nodeName == 'UL'){
				ul.style['visibility'] = 'hidden';
				ul.style['display'] = 'none';
				are_menu_list_close_2(ul);
			}
		}
	}
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	tips
// (x, y) coordinates of event-cursor, inner, scroll
function are_position(e) {
	if (!e && window.event) e = window.event;	// ie
	var d = document;

	// !ie || ie-strict || ie-transitional || 0
	// window-size
	var inner_x = window.innerWidth
		 || d.documentElement.clientWidth
		 || (d.body ? d.body.clientWidth : 0);
	var inner_y = window.innerHeight
		 || d.documentElement.clientHeight
		 || (d.body ? d.body.clientHeight : 0);
	// scroll-position
	var scroll_x = window.pageXOffset
		 || d.documentElement.scrollLeft
		 || (d.body ? d.body.scrollLeft : 0);
	var scroll_y = window.pageYOffset
		 || d.documentElement.scrollTop
		 || (d.body ? d.body.scrollTop : 0);

	// absolute-cursor-position
	var absolute_x = (e) ? e.pageX || e.clientX + scroll_x : 0;
	var absolute_y = (e) ? e.pageY || e.clientY + scroll_y : 0;
	// window-cursor-position
	var window_x = (e) ? e.clientX || e.pageX - scroll_x : 0;
	var window_y = (e) ? e.clientY || e.pageY - scroll_y : 0;
	// element-cursor-position
	var element_x = (e) ? e.offsetX || e.layerX : 0;
	var element_y = (e) ? e.offsetY || e.layerY : 0;

	// return
	return new Array(
		inner_x, inner_y, scroll_x, scroll_y,
		absolute_x, absolute_y, window_x, window_y, element_x, element_y
	);
}




// IE - Only
if (document.all && !window.opera && document.getElementsByTagName) {
    var ds = document.selection;
	var htmls = document.getElementsByTagName('HTML');
	if (
		htmls && htmls.length && htmls[0].nodeName &&
		typeof(ds) == 'object' && typeof(ds.createRange) == 'object'
	) {
		// Are-Object
		are['text_focus'] = new Object();	// text_focus
		are['busy'] = false;	// onmousedown -> onmouseup
		are['context_title'] = 'Context-Menu';	// context-menu-title
		are['context_id'] = 'context-menu';	// context-menu-id
		are['context_obj'] = new Object();
		are['set'] = 0;	// menu-set-id
		are['xhtml'] = htmls[ htmls.length - 1 ].getAttribute('xmlns') || null;

		// onload
		window.attachEvent('onload', are_create_menu);
	}
	else are = null;
}	else are = null;
