//2007-12-12
//俱乐部形象页js
//add by chenzhenhai

//common function
function $(obj) {
	return document.getElementById(obj);
}

//photo preview
function preview() {
	$("grouplive").src = $("livepic").value;
}

//图片不能为空
function checkLive() {
	if(!$("livepic").value) {
		alert("头图不能为空！");
		return false;
	}
}

//图片高度
function checkLength(min, max) {
	var height = $("liveheight").value;
	var reg = /^[0-9]\d*$/;
	if(!reg.test(height)) {
		alert("只能输入数字！");
		return false;
	}

	height = parseInt(height);
	if(height < min || height > max) {
		alert("数字只能在"+min+"-"+max+"之间！");
		return false;
	}
}

//style preview
function showExample($act) {
	switch($act) {
		case "size":
		$("grouplive").style.borderWidth = $("bordersize").value;
		break;
		case "line":
		$("grouplive").style.borderStyle = $("line").value;
		break;
		case "color":
		$("grouplive").style.borderColor = $("bordercolor").value;
		break;
		default:
		break;
	}
}

//init pic style with border information
function initBorder() {
	$("grouplive").style.borderWidth = $("bordersize").value;
	$("grouplive").style.borderStyle = $("line").value;
	$("grouplive").style.borderColor = $("bordercolor").value;
}

//color panel
var ColorHex=new Array('00','33','66','99','CC','FF');
var SpColorHex=new Array('FF0000','00FF00','0000FF','FFFF00','00FFFF','FF00FF');
var current=null;

function initColor(num) {
	var colorTable='';
	for (i=0;i<2;i++) {
		for (j=0;j<6;j++) {
			colorTable=colorTable+'<tr height=12>';
			colorTable=colorTable+'<td width=11 style="background-color:#000000">';

			if (i==0){
				colorTable=colorTable+'<td width=11 style="background-color:#'+ColorHex[j]+ColorHex[j]+ColorHex[j]+'">';
			}else {
				colorTable=colorTable+'<td width=11 style="background-color:#'+SpColorHex[j]+'">';
			}
			colorTable=colorTable+'<td width=11 style="background-color:#000000">';

			for (k=0;k<3;k++) {
				for (l=0;l<6;l++) {
					colorTable=colorTable+'<td width=11 style="background-color:#'+ColorHex[k+i*3]+ColorHex[l]+ColorHex[j]+'">';
				}
			}
		}
	}

	colorTable='<table border="1" cellspacing="0" cellpadding="0" onclick="doClick(event)" onmouseover="doOver(event, '+num+')" style="border-collapse: collapse" bordercolor="000000" style="cursor:hand;">'
	+colorTable+'</table>';

	followEvent($("coverdiv"),{'x':'t.x','y':'t.y+t.h'});
	$("coverdiv").innerHTML=colorTable;
}

function getEvent(evt) {
	evt = evt ? evt : (window.event ? window.event : null);
	events = evt.target||window.event.srcElement;
	return events;
}

function doOver(evt, num) {
	var events = getEvent(evt);
	var inputObj = "";
	if(num == 1) {
		inputObj = "bordercolor";
	}else if(num == 2) {
		inputObj = "fontcolor";
	}else if(num == 3) {
		inputObj = "bgcolor";
	}else{
		alert("装载调色板错误！");
		return ;
	}

	if((events.tagName == "TD") && (current != events)) {
		if(current != null) {
			current.style.backgroundColor = current._background;
		}
		events._background = events.style.backgroundColor;

		if(navigator.userAgent.indexOf('Firefox') > 0) {
			$(inputObj).value = rgbToHex(events.style.backgroundColor);
		}else {
			$(inputObj).value = events.style.backgroundColor;
		}

		if(num == 1) {
			showExample("color");
		}
		events.style.backgroundColor = "white";
		current = events;
	}
}

function doClick(evt) {
	var events = getEvent(evt);
	if(events.tagName == "TD") {
		clearColor();
	}
}

function rgbToHex(aa) {
	aa=aa.replace("rgb(","");
	aa=aa.replace(")","");
	aa=aa.split(",");

	r=parseInt(aa[0]);
	g=parseInt(aa[1]);
	b=parseInt(aa[2]);

	r = r.toString(16);
	if (r.length == 1) {
		r = '0' + r;
	}
	g = g.toString(16);
	if (g.length == 1) {
		g = '0' + g;
	}
	b = b.toString(16);
	if (b.length == 1) {
		b = '0' + b;
	}

	return ("#" + r + g + b).toUpperCase();
}

function clearColor() {
	$("coverdiv").innerHTML = "";
}

//字体选择部分
function switchPic(val) {
	if(val == 1) {
		$("bgpic").style.display = "none";
		$("bgpure").style.display = "block";
	}else if(val == 2) {
		$("bgpic").style.display = "block";
		$("bgpure").style.display = "none";
	}
}

//背景图选择部分
function bgOver(evt) {
	var events = getEvent(evt);

	if(events.tagName == "IMG") {
		events.style.borderStyle = "solid";
		events.style.borderColor = "#000";
	}
}

function bgLeave(evt) {
	var events = getEvent(evt);
	if(events.tagName == "IMG") {
		if(events.id != $("bgcode").value) {
			events.style.borderColor = "#FFF";
		}
	}
}

function bgClick(evt) {
	var events = getEvent(evt);
	if(events.tagName == "IMG") {
		events.style.borderStyle = "solid";
		events.style.borderColor = "#000";

		if($("bgcode").value && ($("bgcode").value != events.id)) {
			$($("bgcode").value).style.borderColor = "#FFF";
		}

		$("bgcode").value = events.id;
	}
}

function initBg(bg) {
	if(bg == "color") {
		switchPic(1);
	}else if(bg == "pic") {
		switchPic(2);
	}

	if($("bgcode").value) {
		$($("bgcode").value).style.borderStyle = "solid";
		$($("bgcode").value).style.borderColor = "#000";
		$($("bgcode").value).style.borderWidth = "1px;";
	}
}

function initTitle() {
	if($("titleno").checked) {
		switchTitle(1);
	}else {
		switchTitle(2);
	}
}

//switch group title
function switchTitle(code) {
	if(code == 1) {
		$("titlefont").style.display = "none";
	}else if(code == 2) {
		$("titlefont").style.display = "block";
	}
}


//main.live
function closeDiv(obj) {
	$(obj).style.display = "none";
}

function showDiv(obj) {
	$(obj).style.display = "";
}

function submitLogin(evt, obj) {
	evt = evt ? evt : (window.event ? window.event : null);
	if(evt.keyCode == 13) {
		$(obj).submit();
	}	
}
