//底部栏

var chatUsers = new Array();

function hoverButton(node) {
	node.className = 'presence_bar_button hover';
}

function outButton(node) {
	node.className = 'presence_bar_button';
}

function triggerButton(node) {
	if(node.parentNode.className == 'presence_bar_section focus') {
		node.parentNode.className = 'presence_bar_section';
	} else if(node.parentNode.className == 'presence_bar_section') {
		node.parentNode.className = 'presence_bar_section focus';
	}
	
	if(node.parentNode.className == 'presence_bar_section chat_tab focus') {
		node.parentNode.className = 'presence_bar_section chat_tab';
	} else if(node.parentNode.className == 'presence_bar_section chat_tab') {
		node.parentNode.className = 'presence_bar_section chat_tab focus';
	}

	if(node.parentNode.className == 'presence_bar_section chat_tab active') {
		node.parentNode.className = 'presence_bar_section chat_tab focus';
	}
}

function minimizeTab(node) {
	if(node.parentNode.parentNode.className == 'presence_bar_section focus') {
		node.parentNode.parentNode.className = 'presence_bar_section';
	} else if(node.parentNode.parentNode.className == 'presence_bar_section chat_tab focus') {
		node.parentNode.parentNode.className = 'presence_bar_section chat_tab';
	}
}
function closeTab(node) {
	if(node.parentNode.parentNode.parentNode.className == 'presence_bar_section chat_tab focus') {
		document.getElementById('chatuserlist').removeChild(node.parentNode.parentNode.parentNode);
	}
}

function openChatWin(uid, uname) {
	var uid = uid;
	var uname = uname;
	var theChatWin = document.createElement('div');
	theChatWin.className = 'presence_bar_section chat_tab focus';
	theChatWin.id = 'chat_tab_' + uid;
	theChatWin.innerHTML = '<div class="presence_bar_button" onmouseover="hoverButton(this);" onmouseout="outButton(this);" onclick="triggerButton(this);">              <p class="user_name">(' + uid + ')'+ uname +' <span class="close_tab" onclick="closeTab(this,'+ uid +');">X</span></p>            </div>            <div class="presence_menu_opts_wrapper">              <div class="presence_chat_head">                <p class="presence_chat_btn"><span class="presence_chat_btn_minimize" onclick="minimizeTab(this.parentNode.parentNode);">_</span> <span class="presence_chat_btn_close" onclick="closeTab(this.parentNode);">X</span></p>                <p class="presence_chat_user"><a href="#"><img src="member/images/member2.jpg" alt="'+uname+'" width="48" height="48" /></a> <a href="#">'+uname+'</a></p>              </div>              <div class="presence_chat_body fixHeight">                <div>                  <p><a href="#">Johnson('+uname+')</a> <small>16:30</small></p>                  <p>干嘛去了？</p>                </div>                <div class="self">                  <p><a href="#">Mike(郭差那)</a> <small>16:30</small></p>                  <p>他們非要請我吃飯</p>                </div>                <div>                  <p><a href="#">Johnson('+uname+')</a> <small>16:30</small></p>                  <p>吃的啥？</p>                </div>                <div class="self">                  <p><a href="#">Mike(郭差那)</a> <small>16:30</small></p>                  <p>光顧聊天了，誰知吃的啥。</p>                </div>                <div>                  <p><a href="#">Johnson('+uname+')</a> <small>16:30</small></p>                  <p>聊得啥？</p>                </div>                <div class="self">                  <p><a href="#">Mike(郭差那)</a> <small>16:30</small></p>                  <p>光顧聊天了，誰知聊的啥。</p>                </div>              </div>              <div class="presence_chat_foot">                <textarea wrap="virtual"></textarea>              </div>            </div>';
	
	if(document.getElementById('chat_tab_' + uid)) {
		for(i=0;i<chatUsers.length;i++) {
			document.getElementById('chatuserlist').childNodes[i].className = 'presence_bar_section chat_tab';
		}
		document.getElementById('chat_tab_' + uid).className = 'presence_bar_section chat_tab focus';
		return;
	}
	
	if(document.getElementById('chatuserlist').childNodes.length > 0) {
		for(i=0;i<document.getElementById('chatuserlist').childNodes.length;i++) {
			document.getElementById('chatuserlist').childNodes[i].className = 'presence_bar_section chat_tab';
		}
	}

	document.getElementById('chatuserlist').appendChild(theChatWin);
	resizeChatList();
	
	for(i=0;i<chatUsers.length;i++) {
		if(chatUsers[i] == uid) {
			return;
		}
	}

	chatUsers.push(uid);
}

function resizeChatList() {
	var availableWidth = document.documentElement.clientWidth - 324;
	var tabsWidth = document.getElementById('chatuserlist').childNodes.length * 129;
	//alert(availableWidth + '+' + tabsWidth);
	if(tabsWidth < availableWidth) {
		document.getElementById('chatuserlist').style.width = tabsWidth + 'px';
		document.getElementById('chat_modules').style.width = tabsWidth + 'px';
	} else {
		document.getElementById('chat_modules').style.width = tabsWidth + 48 +'px';
		document.getElementById('chat_modules').childNodes[0].style.display = 'block';
		document.getElementById('chat_modules').childNodes[1].style.display = 'block';
		var showTabsNum = availableWidth / 129;
		document.getElementById('chatuserlist').style.width = tabsWidth + 'px';
	}
}

function switchChat(node) {
	var theTabs = node.parentNode.getElementsByTagName('li');
	var body1 = document.getElementById('user_chat');
	var body2 = document.getElementById('group_chat');
	var theBodies = new Array(body1, body2);
	var tabIndex = 0;
	
	for(i=0;i<2;i++) {
		if(node == theTabs[i]) {
			tabIndex = i;
		}
		theTabs[i].className = '';
		theBodies[i].style.display = 'none';
	}
	
	theTabs[tabIndex].className = 'current';
	theBodies[tabIndex].style.display = 'block';
}

//回复迷博
function expandReply(btnNode) {
	var replyButton = btnNode;
	var replyNodes = btnNode.parentNode.parentNode.getElementsByTagName('ol')[0];
	if(replyNodes.style.display == 'none') {
		replyNodes.style.display = 'block';
		replyButton.firstChild.data = '收起回复';
	} else {
		replyNodes.style.display = 'none';
		replyButton.firstChild.data = '回复(5)';
	}
}

function replyIt(btnNode) {
	var clickNode = btnNode.parentNode.parentNode;
	var replyFormNode = document.getElementById('reply');
	var replyFormParent = replyFormNode.parentNode;
	var replyNode2 = replyFormNode.cloneNode(true);
	var replyMainNode = document.createElement('li');
	replyMainNode.innerHTML = '<p><a href="#" onclick="replyIt(this);">回复主题</a></p>';
	
	replyFormParent.removeChild(replyFormNode);
	
	if(clickNode.innerHTML == replyMainNode.innerHTML) {
		clickNode.id = 'reply';
		clickNode.innerHTML = replyNode2.innerHTML;
	} else {
		
		var replyRoot;
		if(clickNode.parentNode.parentNode.parentNode.nodeName == "UL") { //get the first class OL node
			replyRoot = clickNode.parentNode;
			if(clickNode.getElementsByTagName('ol')[0] != null) {
				var replyChild = clickNode.getElementsByTagName('ol')[0];
				replyChild.appendChild(replyNode2);
			} else {
				var newReplyChid = document.createElement('ol');
				clickNode.appendChild(newReplyChid);
				newReplyChid.appendChild(replyNode2);
			}
		} else if (clickNode.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "UL") {
			replyRoot = clickNode.parentNode.parentNode.parentNode;
			clickNode.parentNode.appendChild(replyNode2);
		}
		
		var replyMain = replyRoot.childNodes[replyRoot.childNodes.length - 1];
		if(replyMain.innerHTML == replyMainNode.innerHTML) {
			return;
		} else {
			replyRoot.appendChild(replyMainNode);
		}
	
	}
}

//用户面板伸缩

function togglePanel() {
	var panel = document.getElementById('panelMore');
	var panelControl = document.getElementById('morePanel');
	var panelBtn = panelControl.getElementsByTagName('a')[0];
	panelBtn.blur();
	
	if (panel.style.display == '' || panel.style.display == 'none') {
		panel.style.display = 'block';
		panelBtn.style.backgroundPosition = '5px bottom';
		panelBtn.firstChild.data = '更少';
	} else {
		panel.style.display = 'none';
		panelBtn.style.backgroundPosition = '5px top';
		panelBtn.firstChild.data = '更多';
	}
}
function initPanel() {
	if(document.getElementById('morePanel')) {
		document.getElementById('morePanel').getElementsByTagName('a')[0].onclick = togglePanel;
	}
}
window.onload = initPanel;

function dropMenu(btn, menu) {
	btn.className = 'active';
}
function hideMenu(btn, menu) {
	btn.className = '';
}

function toggleSearch() {
	var btn = document.getElementById('more_search_btn');
	var dmenu = document.getElementById('more_search_list');
	if(btn.className == 'expand') {
		btn.className = 'collapse';
		dmenu.className = 'show';
	} else {
		btn.className = 'expand';
		dmenu.className = 'hide';
	}
}

//打招呼窗口
function showPokeWin() {
	//initialize the UI
	if (document.getElementById('dialog')) {
		return;
	}
	var dialogWrap = document.createElement('div');
	dialogWrap.id = 'dialogWrap';
	var dialogWin = document.createElement('div');
	dialogWin.id = 'dialog'
	var dialogHead = document.createElement('div');
	dialogHead.id = 'dialogHead'
	var dialogBody = document.createElement('div');
	dialogBody.id = 'dialogBody'
	var dialogFoot = document.createElement('div');
	dialogFoot.id = 'dialogFoot';
	
	var dialogTitle = document.createElement('h3');
	var dialogTitleText = document.createTextNode('向 '+who+' 打招呼？');
	
	dialogBody.innerHTML = pokePreview;
	dialogFoot.innerHTML = '<p><input name="btnSshare" type="button" class="button" id="btnPoke" value="确定" onclick="sendPoke();" /> <input name="btnClose" type="button" class="btn" id="btnClose" value="取消" onclick="removeDialog()" /></p>';
	  
	  dialogTitle.appendChild(dialogTitleText);
	  dialogHead.appendChild(dialogTitle);
	  
	  dialogWin.appendChild(dialogHead);
	  dialogWin.appendChild(dialogBody);
	  dialogWin.appendChild(dialogFoot);
	  dialogWrap.appendChild(dialogWin);
	  
	  //adjust position
	  var wrapY = document.documentElement.scrollTop + 120;
	  dialogWrap.style.top = wrapY + 'px';
	  
	  //show dialog
	  document.body.appendChild(dialogWrap);
}
function sendPoke() {	//打招呼
	alert('你刚刚向 '+who+' 打了招呼');
	removeDialog();
}

//移除分享窗口

function removeDialog() {	
	if (document.getElementById('dialogWrap')) {
		document.body.removeChild(document.getElementById('dialogWrap'));
	}
}

function shareDialog() {
	
	//initialize the UI
	if (document.getElementById('dialog')) {
		return;
	}
	var dialogWrap = document.createElement('div');
	dialogWrap.id = 'dialogWrap';
	var dialogWin = document.createElement('div');
	dialogWin.id = 'dialog'
	var dialogHead = document.createElement('div');
	dialogHead.id = 'dialogHead'
	var dialogBody = document.createElement('div');
	dialogBody.id = 'dialogBody'
	//var dialogFoot = document.createElement('div');
	//dialogFoot.id = 'dialogFoot';
	
	var dialogTitle = document.createElement('h3');
	var dialogTitleText = document.createTextNode('分享');
	
	dialogBody.innerHTML = '<dl class="clearfix"><dt><p><strong class="txt_light">描述</strong></p></dt><dd><p><textarea name="shareDesc" class="input"></textarea></p></dd><dt>&nbsp;</dt><dd><p><input name="btnSshare" type="button" class="button" id="btnSshare" value="确定" onclick="sendShare();" /> <input name="btnClose" type="button" class="btn" id="btnClose" value="取消" onclick="removeDialog()" /></p></dd><dt>&nbsp;</dt><dd><div class="shareArea" id="sharePreview">' + sharePreview + '</div></dd></dl>';
	//dialogBody.innerHTML ='<div id="share"><h4>转帖内容</h4><div class="dialog_content">' + sharePreview + '</div><h4>转帖内容</h4><p></p></div>';
	  
	  dialogTitle.appendChild(dialogTitleText);
	  dialogHead.appendChild(dialogTitle);
	  
	  dialogWin.appendChild(dialogHead);
	  dialogWin.appendChild(dialogBody);
	  dialogWrap.appendChild(dialogWin);
	  
	  //adjust position
	  var wrapY = document.documentElement.scrollTop + 120;
	  dialogWrap.style.top = wrapY + 'px';
	  
	  //show dialog
	  document.body.appendChild(dialogWrap);
}
function removeDialog() {	//移除分享窗口
	if (document.getElementById('dialogWrap')) {
		document.body.removeChild(document.getElementById('dialogWrap'));
	}
}
function sendShare() {	//发送分享内容
	alert('发送成功');
	removeDialog();
}

//Tap切换
function spotIt(wrap, no) {
	for (i=0; i<5; i++) {
		if(wrap.getElementsByTagName('h3')[i]) {
			wrap.getElementsByTagName('h3')[i].className = "secTitle_" + i;
			wrap.getElementsByTagName('div')[i+1].style.display = "none";
		}
	}
	wrap.getElementsByTagName('h3')[no].className = "secTitle_" + no + " current";
	wrap.getElementsByTagName('div')[no+1].style.display = "block";
}


//更改投票选项

var myOptions = new Array();

function myOption(opName, opValue) {
	this.opName = opName;
	this.opValue = opValue;
}

function mkOption(theOption, type) {
	var opItem = document.createElement("p");
	if(type == "A") {
		opItem.innerHTML = '<label>选项' + (theOption.opName + 1) + ' <input type="text" name="textfield3" id="textfield" /></label> <a href="#" onclick="rmvOp(this.parentNode, \'A\')">[删除]</a>';
	} else if(type == "B") {
		opItem.innerHTML = '<strong>选项1：</strong> <br /> <label>名称 <input name="textfield5" type="text" maxlength="20" /></label> <br /> <label> 图片 <input name="file" type="file" /></label> <br /> <label> 网址 <input name="textfield6" type="text" maxlength="50" /></label> <a href="#" onclick="rmvOp(this.parentNode, \'B\')">[删除]</a>';
	}
	return opItem;
}

function initiateOptions(type) {
	myOptions.length = document.getElementById("options").getElementsByTagName("p").length;
	
	for (i=0; i<myOptions.length; i++){
		if(type == "A") {
			myOptions[i] = new myOption(document.getElementById("options").getElementsByTagName("label")[i].firstChild.data.charAt(2));
		} else if(type == "B") {
			myOptions[i] = new myOption(document.getElementById("options").getElementsByTagName("strong")[i].firstChild.data.charAt(2));
		}
	}
}

function rmvOp(option, type) {
	var myTag = type == "A" ? "label" : "strong";
	var opName = option.getElementsByTagName(myTag)[0].firstChild.data.charAt(2) - 1;
	myOptions.splice(option.getElementsByTagName(myTag).length, 1);
	
	document.getElementById("options").removeChild(option);
	updateOptions(type);
}

function addOption(type) {
	var opName = myOptions.length;
	var theOption = new myOption(opName);
	myOptions.push(theOption);
	
	document.getElementById("options").appendChild(mkOption(theOption, type));
	updateOptions(type);
}

function updateOptions(type) {
	for (i=0; i<myOptions.length; i++){
		myOptions[i].opName = i+1;
		if(type == "A") {
			document.getElementById("options").getElementsByTagName("label")[i].firstChild.data = "选项" + (i + 1) +" ";
		} else if(type == "B") {
			document.getElementById("options").getElementsByTagName("strong")[i].firstChild.data = "选项" + (i + 1);
		}
	}
}

//初始化預覽
function initPreview () {
	document.getElementById("preview_page").style.borderColor = document.getElementById("group_bg").value;
	document.getElementById("preview_page").getElementsByTagName("th")[0].style.backgroundColor = document.getElementById("group_header").value;
	for(i=1; i<5; i++){
		document.getElementById("preview_page").getElementsByTagName("th")[i].style.color = document.getElementById("group_title").value;
	}
	for(i=1; i<5; i++) {
		document.getElementById("preview_page").getElementsByTagName("th")[i].style.backgroundColor = document.getElementById("group_title_bg").value;
	}
	document.getElementById("preview_page").style.color = document.getElementById("group_txt").value;
	for(i=0; i<25; i++) {
		document.getElementById("preview_page").getElementsByTagName("span")[i].style.color = document.getElementById("group_link").value;
	}
	document.getElementById("preview_page").style.backgroundColor = document.getElementById("group_col_bg").value;
}

//创建调色板
function myCS(innitial, destination, currentRGB, preview) {
	clear();
	
	var currentHexRGB = currentRGB.value;
	if(currentHexRGB == null) {
		currentHexRGB = "#000000";
	}
	
	if(innitial == false) {
		destination.innerHTML = '<div class="cs" id="cs"><p class="bar"><span class="color" id="cs_color">预览颜色</span><label>颜色代码<input name="textfield2" type="text" size="5" maxlength="7" class="code" id="cs_code" /></label><input type="button" name="Submit1" value="确定" class="button" id="btn_set" /><input type="button" name="Submit2" value="取消" class="btn" id="btn_cancel" /></p><p class="hue" id="cs_hue"></p><p class="brightness" id="cs_brightness"></p></div>';
		
		document.getElementById("cs_color").style.backgroundColor = currentHexRGB;
		document.getElementById("cs_color").style.color = currentHexRGB;
		code = document.getElementById("cs_code");
		code.value = currentHexRGB;
		
		switch(preview) {
			case 0:
				document.getElementById('preview_page').style.borderColor = currentHexRGB;
				document.getElementById("btn_set").onclick = set0;
				break;
				
			case 1:
				document.getElementById("preview_page").getElementsByTagName("th")[0].style.backgroundColor = currentHexRGB;
				document.getElementById("btn_set").onclick = set1;
				break;
				
			case 2:
				for(i=1; i<5; i++) {
					document.getElementById("preview_page").getElementsByTagName("th")[i].style.color = currentHexRGB;
				}

				document.getElementById("btn_set").onclick = set2;
				break;
				
			case 3:
				for(i=1; i<5; i++) {
					document.getElementById("preview_page").getElementsByTagName("th")[i].style.backgroundColor = currentHexRGB;
				}
				document.getElementById("btn_set").onclick = set3;
				break;
			
			case 4:
				document.getElementById("preview_page").style.color = currentHexRGB;
				document.getElementById("btn_set").onclick = set4;
				break;
			
			case 5:
				for(i=0; i<25; i++) {
					document.getElementById("preview_page").getElementsByTagName("span")[i].style.color = currentHexRGB;
				}
				document.getElementById("btn_set").onclick = set5;
				break;
			
			case 6:
				document.getElementById("preview_page").style.backgroundColor = currentHexRGB;
				document.getElementById("btn_set").onclick = set6;
				break;

			default:
				break;
		}
		
		
		document.getElementById("cs_hue").appendChild(myHue(30, 16));
		document.getElementById("cs_brightness").appendChild(myBrightness(16));
		
		
		document.getElementById("btn_cancel").onclick = clear;
		
	} else {
		destination.innerHTML = null;
	}
	
}



var hexCh = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');

function toHex(n){
	var h, l;
	n=Math.round(n);
	l=n%16;
	h=Math.floor((n / 16)) % 16;
	
	return (hexCh[h]+hexCh[l]);
}

function toHex1(n){
	var l1;
	n=Math.round(n);
	l1=n % 16;
	
	return (hexCh[l1]+hexCh[l1]);
}

function wc(r, g, b, n){
    r=((r*16+r)*3*(15 - n)+0x80*n) / 15;
    g=((g*16+g)*3*(15 - n)+0x80*n) / 15;
    b=((b*16+b)*3*(15 - n)+0x80*n) / 15;
	
	var color = "#" + toHex(r) + toHex(g) + toHex(b);
	
	var dot =  document.createElement("span");
	dot.style.color = dot.style.backgroundColor = color;
	dot.onmouseover = preview;
	dot.onclick = pick;
	
	var txt = document.createTextNode(color);
	dot.appendChild(txt);
	
	return dot;
}

var cnum=new Array(1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0);


//创建色调

function myHue (x, y) {
	var hue = document.createElement("div");
	
	for(i = 0; i < y; i ++) {

		for(j = 0; j < x; j ++) {
			n1=j % 5;
			n2=Math.floor(j / 5)*3;
			n3=n2+3;
			
			hue.appendChild(wc((cnum[n3]*n1+cnum[n2]*(5 - n1)),(cnum[n3+1]*n1+cnum[n2+1]*(5 - n1)),(cnum[n3+2]*n1+cnum[n2+2]*(5 - n1)), i));
			}
		//var br = document.createElement("br");
		//hue.appendChild(br);
	}
	
	return hue;
}


//创建明暗
function myBrightness (z) {
	var brightness = document.createElement("div");
	
	for (i = 255; i >= 0; i -= 8.21) {
		var color = "#" + toHex(i) + toHex(i) + toHex(i);
		var line = document.createElement("span");
		var txt = document.createTextNode(color);
		line.appendChild(txt);
		line.style.color = line.style.backgroundColor = color;
		brightness.appendChild(line);
		line.onmouseover = preview;
		line.onclick = pick;
	}
	return brightness;
}


//为每个颜色块添加事件
function preview(){
	var obj = document.getElementById("cs_color");
	var color = this.firstChild.data;
	
	obj.style.backgroundColor = color;
	obj.style.color = color;
}
function pick(){
	var obj = document.getElementById("cs_color");
	var color = this.firstChild.data;
	
	document.getElementById("cs_code").value = color;
	obj.style.backgroundColor = color;
	obj.style.color = color;
	
	return color;
}

function set0() {
	document.getElementById("group_bg").value = document.getElementById("cs_code").value;
	document.getElementById("preview_page").style.borderColor = document.getElementById("cs_code").value;
	
	clear();
}
function set1() {
	document.getElementById("group_header").value = document.getElementById("cs_code").value;
	document.getElementById("preview_page").getElementsByTagName("th")[0].style.backgroundColor = document.getElementById("cs_code").value;
	
	clear();
}
function set2() {
	document.getElementById("group_title").value = document.getElementById("cs_code").value;
	for(i=1; i<5; i++){
		document.getElementById("preview_page").getElementsByTagName("th")[i].style.color = document.getElementById("cs_code").value;
	}
	
	clear();
}
function set3() {
	document.getElementById("group_title_bg").value = document.getElementById("cs_code").value;
	for(i=1; i<5; i++) {
		document.getElementById("preview_page").getElementsByTagName("th")[i].style.backgroundColor = document.getElementById("cs_code").value;
	}
	
	clear();
}
function set4() {
	document.getElementById("group_txt").value = document.getElementById("cs_code").value;
	document.getElementById("preview_page").style.color = document.getElementById("cs_code").value;
	
	clear();
}
function set5() {
	document.getElementById("group_link").value = document.getElementById("cs_code").value;
	for(i=0; i<25; i++) {
		document.getElementById("preview_page").getElementsByTagName("span")[i].style.color = document.getElementById("cs_code").value;
	}
	clear();
}
function set6() {
	document.getElementById("group_link").value = document.getElementById("cs_code").value;
	document.getElementById("preview_page").style.backgroundColor = document.getElementById("cs_code").value;
	clear();
}

function clear() {
	document.getElementById("cs_0").innerHTML = "";
	document.getElementById("cs_1").innerHTML = "";
	document.getElementById("cs_2").innerHTML = "";
	document.getElementById("cs_3").innerHTML = "";
	document.getElementById("cs_4").innerHTML = "";
	document.getElementById("cs_5").innerHTML = "";
	document.getElementById("cs_6").innerHTML = "";
}
