function changSpanDisplay(id){
	id = objCheck(id);
	var spans = id.getElementsByTagName('span');
	for (var i=0;i<spans.length;i++) {
		if (spans[i].style.display == 'none') {
			spans[i].style.display = '';
		} else {
			spans[i].style.display = 'none';
		}
	}
}
function showEidt(ftid){
	var li = getObj('ft_'+ftid);
	li.onmouseover = '';
	li.onmouseout = '';
	var spans = li.getElementsByTagName('span');
	for (var i=0;i<spans.length;i++) {
		spans[i].style.display = 'none';
	}
	var a = li.getElementsByTagName('a');
	for (var i=0;i<a.length;i++) {
		a[i].style.display = 'none';
	}
	
	var text= elementBind('input','','txa ftwidth');
	text.setAttribute('type','text');
	text.value	= a[i-1].innerHTML;
	li.appendChild(text);
	text.focus();
	
	var ok	= elementBind('input','','bt3');
	ok.setAttribute('type','button');
	ok.value= '確定';
	
	var cancel	= elementBind('input','','bt','padding:.2em 0;margin:0 0 0 .3em');
	cancel.setAttribute('type','button');
	cancel.value= '取消';

	ok.onclick	= function () {
		var name = char_cv(text.value);
		if (name.length<1) {
			showDialog('error','類型名稱不能為空');
			return false;
		}
		ajax.send('mode.php?m=o&q=ajax&action=eidtfriendtype','u='+winduid+'&name='+name+'&ftid='+ftid,function(){
			var rText = ajax.request.responseText;
			if (rText == 'success') {
				cancelEdit(li,name);
			} else {
				ajax.guide();
			}
		});
	}
	
	cancel.onclick	= function () {
		cancelEdit(li);
	}
	li.appendChild(ok);
	li.appendChild(cancel);
}
function cancelEdit(li,name){
	li = objCheck(li);
	li.onmouseover = function () {
		changSpanDisplay(li);
	}
	li.onmouseout = function () {
		changSpanDisplay(li);
	}
	var input = li.getElementsByTagName('input');
	while (input[0]) {
		delElement(input[0]);
	}
	var a = li.getElementsByTagName('a');
	for (var i=0;i<a.length;i++) {
		if (name) {
			a[i].innerHTML	= name;
		}
		a[i].style.display	= '';
	}
}
function addType(id){
	if (getObj('ft_add')) {
		changeCreatInputDisplay('ft_add');
		return true;
	}
	creatFriendTypeInput(id);
}
function creatFriendTypeInput(id){
	var parent	= getObj(id);
	var li	= addChild(parent,'li','ft_add');
	
	var text= elementBind('input','','txa ftwidth');
	text.setAttribute('type','text');
	li.appendChild(text);
	text.focus();

	var ok	= elementBind('input','','bt3');
	ok.setAttribute('type','button');
	ok.value= '確定';
	ok.onclick	= function () {
		var name = char_cv(text.value);
		if (name.length<1) {
			showDialog('error','類型名稱不能為空');
			return false;
		}
		ajax.send('mode.php?m=o&q=ajax&action=addfriendtype','u='+winduid+'&name='+name,function(){
			var rText = ajax.request.responseText.split('\t');
			if (rText[0] == 'success') {
				var ftid = rText[1] - 0;
				if (isNaN(ftid)==false) {
					creatFriendType('ft_null',ftid,name);
					delElement('ft_add');
				} else {
					alert('error');
				}
			} else {
				ajax.guide();
			}
		});
	}
	li.appendChild(ok);
	var cancel	= elementBind('input','','bt','padding:.2em 0;margin:0 0 0 .3em');
	cancel.setAttribute('type','button');
	cancel.value= '取消';
	cancel.onclick	= function () {
		text.value = '';
		li.style.display = 'none';
	}
	li.appendChild(cancel);
}

function changeCreatInputDisplay(id){
	var ft_add = getObj(id);
	if (ft_add.style.display =='none') {
		ft_add.style.display = '';
	} else {
		ft_add.style.display = 'none';
	}
}
function creatFriendType(latter,ftid,name){
	latter = objCheck(latter);
	var parent = latter.parentNode;
	var li = elementBind('li','ft_'+ftid);
	li.setAttribute('onmouseover',"changSpanDisplay(this);");
	li.setAttribute('onmouseout',"changSpanDisplay(this);");
	li.innerHTML = '<span class="fr b pdr gray1" style="cursor: pointer;display:none" onclick="showDialog(\'confirm\',\'是否確認刪除\',0,function(){delFriendType(\''+ftid+'\')})">x</span><span class="fr pdD gray1" style="cursor: pointer;display:none" onclick="showEidt(\''+ftid+'\');">編輯</span><a href="mode.php?m=o&q=friend&ftid='+ftid+'">'+name+'</a>';
	parent.insertBefore(li,latter);
}
function delFriendType(ftid){
	ajax.send('mode.php?m=o&q=ajax&action=delfriendtype','u='+winduid+'&ftid='+ftid,function(){
		var rText = ajax.request.responseText;
		if (rText == 'success') {
			delElement('ft_'+ftid);
		} else {
			ajax.guide();
		}
	});
}

function showFriendTypeBox(friendid,name,ftid){
	if (isNaN(friendid)) {
		alert('error');
	}
	ajax.send('mode.php?m=o&q=ajax&action=showftlist','u='+winduid,function(){
		var rText = ajax.request.responseText.split('\t');
		if (rText[0]=='success') {
			if (rText[1]!='') {
				var types = JSON.parse(rText[1]);
				if (typeof(types)=='object') {
					var friendTypeList = creatFriendTypeList(types,ftid,friendid);
					read.setMenu(friendTypeList);
					read.menupz();
				}
			} else {
				showDialog('error','您還沒有設置好友分組');
			}
		}
	});
}

function creatFriendTypeList(obj,ftid,friendid){
	var container	= elementBind('div','','','height:100%');
	var title	= elementBind('div','','menu-h b');
	title.innerHTML = '設定好友所屬分組';
	container.appendChild(title);
	var inner_div	= addChild(container,'div','','menu-text p10');
	var ul	= addChild(inner_div,'ul','','ul-50pc cc');
	for (var i in obj) {
		if (typeof(obj[i])=='object') {
			creadListLi(obj[i],ul,ftid);
		}
	}
	var footer	= addChild(container,'div','','bdtA','width:90%; padding:0 5%');
	var footer_title = addChild(footer,'div','','cc p10');
	footer_title.innerHTML = '好友分組只有你自己能夠看到';
	var tar	= addChild(footer,'div','','tar');
	var ok	= elementBind('input','','bt3');
	ok.type	= 'button';
	ok.value= '確定';
	ok.onclick	= function () {
		var radios	= document.getElementsByName('friendtype_radio');
		var ftid	= 0;
		for (var i=0;i<radios.length;i++) {
			if (radios[i].checked) {
				ftid= radios[i].value;
				break;
			}
		}
		ajax.send('mode.php?m=o&q=ajax&action=setfriendtype','friendid='+friendid+'&ftid='+ftid,function(){
			var rText = ajax.request.responseText;
			if (rText=='success') {
				window.location.reload();
			} else {
				ajax.guide();
			}
		});
	}
	
	var cansel	= elementBind('input','','bt','margin-left:8px');
	cansel.type	= 'button';
	cansel.value= '取消';
	cansel.onclick	= closep;
	tar.appendChild(ok);
	tar.appendChild(cansel);
	
	return container;
}

function creadListLi(obj,ul,ftid){
	var li = addChild(ul,'li');
	var add = ftid == obj['ftid'] ? 'checked="checked"' : '';
	li.innerHTML = '<input name="friendtype_radio" type="radio" value="'+obj['ftid']+'" '+add+'> '+ obj['name'];
	ul.appendChild(li);
}