var dc={};
dc = {
	indexFile: 'vb.good', 
	$: function(id, doc){
		var doc = doc || document;
		return document.getElementById(id);
	},
	cookies: {
		set : function(name, value, expires) {
			if (expires) {
				var date = new Date();
				date.setTime(date.getTime() + expires);
				expires = "; expires=" + date.toGMTString();
			}else{
				expires = "";
			}
			document.cookie = name + "=" + escape(value) + expires + "; path=/";
		},
		get : function(name) {
			if(document.cookie.length>0){
				cStart = document.cookie.indexOf(name + "=");
				if (cStart != -1){
					cStart = cStart + name.length + 1;
					cEnd = document.cookie.indexOf(";", cStart);
					if (cEnd == -1) cEnd = document.cookie.length;
					return unescape(document.cookie.substring(cStart, cEnd));
				}
			}
			return "";
		},
		remove : function(name) {
			this.parent.set(name, '', -1);
		}
	},
	getElementPos : function(el) {
		var x = 0;
		var y = 0;
		if (el.getBoundingClientRect) {
			var box = el.getBoundingClientRect();
			x = box.left + el.scrollLeft - el.clientLeft;
			y = box.top + el.scrollTop - el.clientTop;
		} else {
			x = el.offsetLeft;
			y = el.offsetTop;
			var parent = el.offsetParent;
			while (parent) {
				x += parent.offsetLeft;
				y += parent.offsetTop;
				parent = parent.offsetParent;
			}
		}
		return {'x' : x, 'y' : y};
	},
	tabs: {
		id: {
			t: '', //tab
			p: '', //body
			i: 0, //current
			ts: '', // tab array
			ps: '' // body array
		},
		init: function(args) {
			if (typeof(args.id) == "undefined"){
				return;
			}
			if (typeof(args.sid) == "undefined") {
				args.sid = dc.cookies.get("tabs-pane-"+args.id);
				if (args.sid == "") {
					args.sid = 0;
				}
			}else {
				dc.cookies.set("tabs-pane-"+args.id, args.id, 0);//60 minutes
			}
			var ts = new Array, ps = new Array; // title array
			var wrap = dc.$(args.id);
			var lis = wrap.getElementsByTagName("UL")[0].childNodes;
			var divs = wrap.getElementsByTagName("DIV"); // page array
			var j = 0;
			for (var i=0; i<divs.length; i++) {
				if (divs[i].className == "tabText" || divs[i].className == "tabText hide") {
					ps[j] = divs[i];
					j++;
				}
			}
			j = 0;
			for(var i=0; i<lis.length; i++) {
				if (lis[i].nodeName == "LI") {
					lis[i].id = args.id + "T" + j;
					lis[i].tabsid = j;
					if (j == args.sid) {
						lis[i].state = true;
						lis[i].className = "selected";

						dc.tabs.id.t = lis[i];
						dc.tabs.id.p = ps[j];
						dc.tabs.id.i = j;
						if(ps[j].className == "tabText hide"){
							ps[j].className = ps[j].className.replace(" hide", "");
						}
					}else {
						if(ps[j].className == "tabText"){
							ps[j].className += " hide";
						}
						lis[i].state = false;
					}
					ts[j] = lis[i];

					j++;
					lis[i].onclick = function(){
						if (args.disabled == true) {
							return;
						}
						for (var i=0; i<ts.length; i++) {
							if (ts[i].id == this.id) {
								dc.cookies.set("tabs-pane-"+this.id.replace("T" + i, ''), i, 0);
								dc.tabs.setState.on(this, ps[i]);

								dc.tabs.id.t = ts[i];
								dc.tabs.id.p = ps[i];
								dc.tabs.id.i = i;
							}else {
								dc.tabs.setState.off(ts[i], ps[i]);
							}
						}
					}
				}
			}
			dc.tabs.id.ts = ts;
			dc.tabs.id.ps = ps;
			if(dc.tabs.id.ps[args.sid].getAttribute('click') != null){
				eval(dc.tabs.id.ps[args.sid].getAttribute('click'));
			}
		},
		setState: {
			on : function(t, p){
				if (!t.state){
					t.className = 'selected';
					p.className = p.className.replace(" hide", "");
					t.state = true;
					if(p.getAttribute('click') != null){
						eval(p.getAttribute('click'));
					}
				}
			},
			off : function(t, p){
				if (t.state){
					t.className = "";
					p.className += " hide";
					t.state = false;
				}
			}
		},
		next: function(){
			if (dc.tabs.id.t.id == dc.tabs.id.ts[dc.tabs.id.i + 1].id) {
				return ;
			}else {
				dc.tabs.setState.on(dc.tabs.id.ts[dc.tabs.id.i + 1], dc.tabs.id.ps[dc.tabs.id.i + 1]);
				dc.tabs.setState.off(dc.tabs.id.t, dc.tabs.id.p);

				dc.tabs.id.t = dc.tabs.id.ts[dc.tabs.id.i + 1];
				dc.tabs.id.p = dc.tabs.id.ps[dc.tabs.id.i + 1];
				dc.tabs.id.i++;
			}
		},
		prev: function(){
			if (dc.tabs.id.t == dc.tabs.id.ts[0]) {
				return ;
			}else {
				dc.tabs.setState.on(dc.tabs.id.ts[dc.tabs.id.i - 1], dc.tabs.id.ps[dc.tabs.id.i - 1]);
				dc.tabs.setState.off(dc.tabs.id.t, dc.tabs.id.p);

				dc.tabs.id.t = dc.tabs.id.ts[dc.tabs.id.i - 1];
				dc.tabs.id.p = dc.tabs.id.ps[dc.tabs.id.i - 1];
				dc.tabs.id.i--;
			}
		},
		goTo: function(i){
			if (dc.tabs.id.i == i || i >= dc.tabs.id.ts.length) {
				return ;
			}else {
				dc.tabs.setState.on(dc.tabs.id.ts[i], dc.tabs.id.ps[i]);
				dc.tabs.setState.off(dc.tabs.id.t, dc.tabs.id.p);

				dc.tabs.id.t = dc.tabs.id.ts[i];
				dc.tabs.id.p = dc.tabs.id.ps[i];
				dc.tabs.id.i = i;
			}
		}
	} // end tabs
}

dc.ajax = {
	handle: null,
	f: null,
	init:function(){
		try{ // Firefox, Opera 8.0+, Safari
			this.handle = new XMLHttpRequest();
		}catch(e){
			try{ // Internet Explorer
				this.handle = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					this.handle = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					alert("您的浏览器不支持AJAX！部分功能无法使用，我们支持下列浏览器：IE7，IE8，Firefox，Chrome，Opera，Safari。");
					this.handle = false;
				}
			}
		}
	},
	orsc: function(){ // on ready state change
		switch(dc.ajax.handle.readyState){
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				if(dc.ajax.handle.status == 200){
					dc.ajax.f();
				}else{
					alert("页面请求发生异常，如持续出现，请联系本站管理员。");
				}
				break;
		}
	},
	get : function(url, f){
		if(this.handle == null){
			this.init();
		}
		this.f = f;
		this.handle.onreadystatechange = this.orsc;
		this.handle.open('GET', '/'+dc.indexFile+'?' + url, true);
		this.handle.send(null);
	},
	post: function(url, f, p){
		if(this.handle == null){
			this.init();
		}
		this.f = f;
		this.handle.onreadystatechange = this.orsc;
		this.handle.open('POST', '/'+dc.indexFile+'?' + url, true);
		this.handle.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=GB2312");
		this.handle.send(p);
	}
}

dc.win = {
	handle: new Array(),
	bg: null,
	clientWidth: 0,
	clientHeight: 0,
	scrollTop: 0,
	getSize: function(){
		this.clientWidth = document.body.clientWidth;
		this.clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		this.scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	},
	creat: function(i,w,h){
		if(this.handle[i] == null){
			this.bg = document.createElement("div");
			this.bg.style.background = "black";
			this.bg.style.position = 'absolute';
			this.bg.style.filter = "alpha(opacity=40)";
			this.bg.style.opacity = "0.20";
			this.bg.style.top = 0;
			this.bg.style.left = 0;
			this.bg.style.zIndex = '998';
			this.bg.style.display = 'none';

			this.handle[i] = document.createElement("div");
			this.handle[i].className = 'floatwin';
//			this.handle[i].id = id;
			this.handle[i].style.position = 'absolute';
			this.handle[i].style.zIndex = '999';
			this.handle[i].style.display = 'none';
			this.resize(i,w,h);
			// close
			var sd = document.createElement("div");
			sd.className = "right hand";
			sd.onclick = this.close;
			sd.title = i;
			var sdi = document.createElement("img");
			sdi.src = "/public/images/close.gif";
			sdi.title = '关闭';
			sd.appendChild(sdi);
			this.handle[i].appendChild(sd);
			document.body.appendChild(this.bg);
			document.body.appendChild(this.handle[i]);
		}
		return true;
	},
	resize:function(i,w,h){
		this.getSize();
		this.bg.style.width = this.clientWidth + 'px';
		this.bg.style.height = this.clientHeight + 'px';
		this.handle[i].style.width = w + 'px';
		this.handle[i].style.height = h + 'px';
		this.handle[i].style.left = ((this.clientWidth - w) / 2) + 'px';
		this.handle[i].style.top = ((this.clientHeight - h) / 2 + this.scrollTop) + 'px';
	},
	close: function(){
		if(dc.win.handle[this.title] != null){
			dc.win.handle[this.title].style.display = "none";
			dc.win.bg.style.display = "none";
		}
	},
	show: function(i){
		if(this.handle[i] != null){
			this.bg.style.display = "";
			this.handle[i].style.display = "";
		}
	}
}

dc.login = {
	handle: null,
	show: function(){
		if(dc.win.handle['login'] == null){
			dc.win.creat('login', 600, 270);
		}else{
			dc.win.resize('login', 600, 270);
		}
		if(dc.login.handle == null){
			dc.login.handle = document.createElement("div");
			dc.login.handle.id = 'login';
			dc.login.handle.innerHTML = 'Loading...';
			dc.ajax.get('/vb.good?action=ajax&do=login', function(){dc.login.handle.innerHTML = dc.ajax.handle.responseText;dc.win.handle['login'].appendChild(dc.login.handle)});
		}
		dc.win.show('login');
		return false;
	},
	showValidate: function(e){
		var img = dc.$(e);
		if(img.src == ''){
			img.src = "/vb.good?action=login&do=validate";
		}
	},
	changeValidate: function(e){
		var img = dc.$(e);
		if(img.src == ''){
			img.src = "/vb.good?action=login&do=validate";
		}else{
			img.src = "/vb.good?action=login&do=validate&refresh=" + Math.random();
		}
	}
}

dc.comment = {
	handle: null,
	t: '',
	i: 0,
	form: function(e,t,i){
		if(dc.comment.handle == null){
			this.t = t;
			this.i = i;
			dc.comment.handle = dc.tabs.id.ps[e];
			dc.ajax.get('action=ajax&do=commentForm&type='+t+'&id='+i, function(){dc.comment.handle.innerHTML = dc.ajax.handle.responseText});
		}
	},
	check: function(){
		var cName = dc.$("commentName");
		if(cName.value.length < 2){
			alert("您需要填写 <你的名字>.");
			cName.focus();
			return false;
		}
		var cValidate = dc.$("commentValidate");
		if(cValidate.value.length==0 || cValidate.value.length != 6){
			alert("发表评论前您需要填写6位的 <验证码>. (字母不区分大小写)");
			cValidate.focus();
			return false;
		}
		var cText = dc.$("commentText");
		if(cText.value.length < 4){
			alert("您需要填写 <评论内容>. (至少4个字)");
			cText.focus();
			return false;
		}
		return true;
	},
	post: function(){
		if(this.check()){
			dc.$("commentSubmit").disabled = true;
			dc.$("commentReset").disabled = true;
			var p = new Array();
			p.push('validate='+dc.$("commentValidate").value, 'name='+escape(dc.$("commentName").value), 'ctext='+escape(dc.$("commentText").value));
			dc.ajax.post('action=ajax&do=commentInsert&module='+this.t+'&id='+this.i, function(){alert(dc.ajax.handle.responseText);dc.$("commentSubmit").disabled=false;dc.$("commentReset").disabled=false;dc.$("commentText").value='';dc.$("commentValidate").value='';dc.login.changeValidate('commentImgValidate');dc.tabs.prev()}, p.join('&'));
		}
		return false;
	}
}

dc.forward = {
	handle: null,
	t: '',
	i: 0,
	form: function(e,t,i){
		if(dc.forward.handle == null){
			this.t = t;
			this.i = i;
			dc.forward.handle = dc.tabs.id.ps[e];
			dc.ajax.get('action=ajax&do=forwardForm&type='+t+'&id='+i, function(){dc.forward.handle.innerHTML = dc.ajax.handle.responseText});
		}
	},
	check: function(){
		var fromName = dc.$('fromName');
		if(fromName.value.length < 2){
			alert("你需要填写 <你的名字>.");
			fromName.focus();
			return false;
		}
		var fromEmail = dc.$('fromEmail');
		if(fromEmail.value.length < 7){
			alert("你需要填写 <你的邮箱>.");
			fromEmail.focus();
			return false;
		}
		var toName = dc.$('toName');
		if(toName.value.length < 2){
			alert("你需要填写 <收件人名>.");
			toName.focus();
			return false;
		}
		var toEmail = dc.$('toEmail');
		if(toEmail.value.length < 7){
			alert("你需要填写 <收件邮箱>.");
			toEmail.focus();
			return false;
		}
		var subject = dc.$('subject');
		if(subject.value.length < 4){
			alert("你需要填写 <主题>. (不能少于2个字)");
			subject.focus();
			return false;
		}
		var message = dc.$('message');
		if(message.value.length < 2){
			alert("你需要填写邮件的 <内容>. (不能少于2个字)");
			message.focus();
			return false;
		}
		return true;
	},
	post: function(){
		if(this.check()){
			dc.$("forwardSubmit").disabled = true;
			dc.$("forwardReset").disabled = true;
			var p = new Array();
			p.push('fromName='+escape(dc.$("fromName").value), 'fromEmail='+escape(dc.$("fromEmail").value), 'toName='+escape(dc.$("toName").value), 'toEmail='+escape(dc.$("toEmail").value), 'subject='+escape(dc.$("subject").value), 'message='+escape(dc.$("message").value));
			dc.ajax.post('action=ajax&do=forwardInsert&module='+this.t+'&id='+this.i, function(){alert(dc.ajax.handle.responseText);dc.$("forwardSubmit").disabled=false;dc.$("forwardReset").disabled=false;dc.$("toName").value='';dc.$("toEmail").value='';}, p.join('&'));
		}
		return false;
	}
}
