window.onerror = null;
var wait = true;
var dottags = new Array();
function settags() {
	var tag = $('tag');
//	var dottags = $$('.tag');
	if(!dottags || 1 > dottags.length) return false;
	var tags = tag.value.split(/ +/);
	var alltags = {};
	dottags.each(
		function(dottag,idx) {
			var flg = false;
			var lower = dottag.innerHTML.toLowerCase();
			for(var i = 0; i < tags.length; i++) if(lower == tags[i].toLowerCase()) flg = true;
			if(!flg) alltags[lower] = alltags[lower] ? {'tag': alltags[lower].tag, 'cnt': alltags[lower].cnt + 1} : {tag : dottag.innerHTML, cnt: 1};
			dottag.style.color = '#0000ff';
			dottag.style.background = flg ? '#ffcccc' : '#ccffcc';
			dottag.onclick = flg ? del : add;
		}
	);
	var sorttags = new Array();
	for(lower in alltags) sorttags.push({'tag': alltags[lower].tag, 'cnt': alltags[lower].cnt});
	sorttags = sorttags.sort(function(a,b) {return b.cnt - a.cnt});
	var ptags = $('ptags');
	while (ptags.hasChildNodes()) ptags.removeChild(ptags.firstChild);
	var cnt = 0;
//	var len = 0;
	sorttags.each(
		function(ptag,index) {
//			if(30 > cnt && 210 > len) {
			if(50 > cnt) {
				a = document.createElement('A');
				a.appendChild(document.createTextNode(ptag.tag));
				a.setAttribute('href','javascript:void(0);');
				a.style.color = '#0000ff';
				a.style.background = '#ccffcc';
				a.onclick = add;
				ptags.appendChild(a);
				ptags.appendChild(document.createTextNode(' '));
//				len += ptag.replace(/[^0-9a-zｱ-ﾝﾞﾟｧｨｩｪｫｯｬｭｮｰ､｡｢｣\!\"\#\$\%\&\'\(\)\-\=\^\~\\\|\@\`\[\{\;\+\:\*\]\}\,\<\.\>\/\?\_\ ]/ig,"##").length;
//				dmy = /"/;
			}
 			cnt++;
		}
	);
}
function set() {
	var tag = $('tag');
	var stags = $('stags');
	while (stags.hasChildNodes()) stags.removeChild(stags.firstChild);
	if (!tag || !tag.value || tag.value == '' || tag.value.match(/^ +$/)) return false;
	var tag_arr = tag.value.replace(/^ +\| +/,'').replace(/ *\| */g,' | ').split(/ +/);
	tag_arr.each(
		function(value, idx) {
			a = document.createElement('A');
			a.appendChild(document.createTextNode(value));
			a.setAttribute('href','javascript:void(0);');
			a.style.color = '#0000ff';
			a.style.background = '#F9E7EE';
			a.onclick = del;
			stags.appendChild(a);
			stags.appendChild(document.createTextNode(' '));
		}
	);
	tag.value = tag_arr.join(' ') + ' ';
	settags();
	wait = false;
}
function del() {
	var item = this;
	var tag = $('tag');
	if (tag && tag.value && tag.value != '' && !tag.value.match(/^ +$/)) {
		var tag_arr = tag.value.replace(/\|/g,' | ').split(/ +/);
		tag_arr.each(
			function(value, index) {
				if (item.innerHTML.toLowerCase() == value.toLowerCase()) {
					delete tag_arr[index];
				}
			}
		);
		tag.value = tag_arr.join(' ') + ' ';
	}
	set();
}
function add() {
	if (wait == true) return false;
	wait == true;
	var item = this;
	var tag = $('tag');
	var stags = $('stags');
	var vtag = item.innerHTML;
	var tag_arr = tag && tag.value ? tag.value.replace(/\|/g,' | ').split(/ +/) : new Array();
	var flg = true;
	tag_arr.each(
		function(value,index) {if(value.toLowerCase() == item.innerHTML.toLowerCase()) flg = false;}
	);
	if(!flg) return false;
	var duration = 0.2; // sec.
	var interval = 0.01; // sec.
	var timer = 0.0; // sec.
	var animate = null;
	var from_pos = Position.cumulativeOffset(item);

	var a = document.createElement("A");
	a.appendChild(document.createTextNode(item.innerHTML));
	a.setAttribute("href","javascript:void(0);");
	a.style.color = '#0000ff';
	a.style.background = '#F9E7EE';
	a.onclick = del;
	stags.appendChild(a);
//	stags.appendChild(document.createTextNode(' '));

	var to_pos = Position.cumulativeOffset(a);

	a.style.position = "absolute";
	a.style.left = from_pos[0], a.style.top = from_pos[1];

	(function() {
		animate = animate || new PeriodicalExecuter(arguments.callee, interval);
		pos = Position.cumulativeOffset(a);
		timer += interval;
		if (timer > duration) {
			pos = to_pos;
			animate.stop();
			animate = null;
			a.style.position = "static";
			var tag = $('tag');
			tag.value = tag.value + ' ' + item.innerHTML + ' ';
			wait = false;
			set();
		} else {
			var progress = timer / duration;
			leave = 1.0 - progress;
			pos[0] = to_pos[0] * progress + pos[0] * leave;
			pos[1] = to_pos[1] * progress + pos[1] * leave;
		}
		a.style.left = pos[0], a.style.top = pos[1];
	})();
}
function counter() {
	var dir = window.dir ? '&dir=' + window.dir : '';
	var url = '/out.php?to=' + this.href + '&from=' + document.location + dir;
	new Ajax.Request(url,{method: 'get', parameters: '', onComplete: Prototype.emptyFunction});
}
function recent() {
	count(this.href);
}
function setcounter() {
	as = $A(document.getElementsByTagName('A'));
	reg1 = /^javascript/;
//	reg2 = '/'+location.host+'/';
	as.each(
		function(a,idx) {
			href = a.href;
			if(!href.match(reg1) && !a.onclick) {
				a.onclick = counter;
			}
		}
	);
}
function search(action,name,method) {
	form = document.createElement('form');
	form.setAttribute('method',method); 
	form.setAttribute('target','_blank'); 
	form.action = action;
	input = document.createElement('input');
	input.setAttribute('type','hidden');
	input.setAttribute('name',name);
	value = document.getElementById('tag').value;
	input.setAttribute('value',value);
	form.appendChild(input);
	document.body.appendChild(form);
	form.submit();
}
