﻿/**
 * 全局通用ui控制
 * by Auntion, QQ:82874972
 */

/* 侧边栏分类显示控制器 */
var classRoom = function(id) {
	this.container = Dom.byId("js.classRoom");
	if(!this.container) alert("分类显示的控制器ID错误");
	this.group = new Array();
	this.focus = 0;	//当前焦点
	this.setGroup();
}
classRoom.prototype.setGroup = function() {
	var li,team;
	li = this.container.getElementsByTagName("li");
	for(var i=0; i<li.length; i++) {
		if(li[i].getElementsByTagName("h1")[0].className == "show") this.focus = i;
		this.group.push({
			"tag": li[i].getElementsByTagName("h1")[0],
			"cox": li[i].getElementsByTagName("div")[0]
		});
		this.bindEvent(i);
	}
}
classRoom.prototype.bindEvent = function(index) {
	this.group[index].tag.onclick = function() {
		this.setDisplay(index);
	}.bind(this);
}
classRoom.prototype.setDisplay = function(index) {
	var span = this.group[index].tag.getElementsByTagName("span")[0];
	if(span.className == "show") {
		span.className = "hide";
		this.group[index].cox.className = "hide";
	}else if(span.className == "hide") {
		/* 需隐藏上次打开的 */
		 this.group[this.focus].tag.getElementsByTagName("span")[0].className = "hide";
		this.group[this.focus].cox.className = "hide";
		/* end */
		span.className = "show";
		this.group[index].cox.className = "show";
		this.focus = index;
	}
}

/* 通用下拉菜单 */
var ui_select = function(id) {
	this.contai = Dom.byId(id) || Dom.byId("js.slist");
	if(this.contai == null) return false;
	this.title = Dom.byClass("selected",this.contai)[0];
	this.list  = this.getList();
	this.data  = this.contai.getElementsByTagName("input")[0];
	this.time;
	this.bindEvent();
}
ui_select.prototype.getList = function() {
	var g = new Array();
	var a = this.contai.getElementsByTagName("a");
	for(var i=0; i<a.length; i++) {
		if(a[i].className != "selected") g.push(a[i]);
	}
	return g;
}
ui_select.prototype.bindEvent = function() {
	this.contai.onmouseover = this.contai.onmouseout = function() {
		this.clearTime();
		this.time = window.setTimeout(function(){
			this.display("block");
		}.bind(this), 300)
	}.bind(this);
	this.contai.onmouseout = this.contai.onmouseout = function() {
		this.clearTime();
		this.time = window.setTimeout(function(){
			this.display("none");
		}.bind(this), 300)
	}.bind(this);
	for(var i=0; i<this.list.length; i++) {
		//绑定选中事件
		this.list[i].index = i;
		this.list[i].sends = function(index){ this.selectRow(index) }.bind(this);
		this.list[i].onclick = function() { this.sends(this.index) };
	}
}
ui_select.prototype.selectRow = function(index) {
	this.title.innerHTML = this.list[index].innerHTML;
	this.data.value = this.list[index].getAttribute("value");
	this.display("none");
}
ui_select.prototype.clearTime = function() {
	window.clearTimeout(this.time);
}
ui_select.prototype.display = function(mode) {
	for (var i = 0; i < this.list.length; i++) this.list[i].style.display = mode;
}


/* 大框体搜索控制 */
var displayContorl = function(id) {
	this.container = Dom.byId(id) || Dom.byId("js.searchMore");
	this.button = this.getButton();
	this.layer = this.getLayer();
	this.frame = this.container.getElementsByTagName("iframe")[0];
	this.isFrist = true;
	this.initEvent();
}
displayContorl.prototype.initEvent = function() {
	if(this.button == null && this.layer == null) alert("displayContorl: 找不到HTML元素"); 
	this.button.onclick = function() { this.display() }.bind(this);
}
displayContorl.prototype.getButton = function() {
	return Dom.byClass("morebut", this.container)[0];
}
displayContorl.prototype.getLayer = function() {
	return Dom.byClass("more", this.container)[0];
}
displayContorl.prototype.display = function() {
	if(this.button.className.indexOf("open") >= 0) {
		this.button.className = "morebut close";
		this.button.innerHTML = "关闭更多选项";
		this.layer.style.display = "block";
		this.frame.style.display = "block";
		if(this.isFrist) {
			this.frame.style.width = this.layer.offsetWidth;
			this.frame.style.height = this.layer.offsetHeight;
			this.isFrist = false;
		}
	} else if(this.button.className.indexOf("close") >= 0) {
		this.button.className = "morebut open";
		this.button.innerHTML = "更多搜索选项";
		this.layer.style.display = "none";
		this.frame.style.display = "none";
	}
}

/* 侧边栏搜索控制 */
var moreSelect = function() {
	this.c = Dom.byId("js.moreSelect");
	this.tag = Dom.byClass("sadv", this.c)[0];
	this.rom = Dom.byClass("adv", this.c);
	this.tag.setAttribute("state","close");	//设置状态采集
	this.bindEvent();
}
moreSelect.prototype.bindEvent = function() {
	this.tag.onclick = function() {
		this.ShowHide();
	}.bind(this);
}
moreSelect.prototype.ShowHide = function() {
	if(this.tag.getAttribute("state") == "close") {
		this.tag.style.backgroundPositionY = "-388px";
		this.cRom("block");
		this.tag.setAttribute("state", "open");
	} else if (this.tag.getAttribute("state") == "open") {
		this.tag.style.backgroundPositionY = "-374px";
		this.cRom("none");
		this.tag.setAttribute("state", "close");
	}
}
moreSelect.prototype.cRom = function(method) {
	for(var i=0; i<this.rom.length; i++) {
		this.rom[i].style.display = method;
	}
}

/* 二级频道通用图片浏览组件 */
var imgPlay = function(id) {
	this.container = id? Dom.byId(id): Dom.byId("imgExplorer");
	if(!this.container) return false;
	this.list = {
		dom: Dom.byClass("imgList", this.container)[0],
		animation: null
	}
	this.menu = this.getMenu();
	this.conf = {
		menuHeight:0,
		menuTeamHeight:0,
		listHeight:0,
		listTeamHeight:0,
		oldIndex:0,
		debugIndex:0
	}
	this.timer = null;
	this.auto  = {
		fun: null,
		dly: 5000
	};
	this.init();
}
imgPlay.prototype.getMenu = function() {
	var hash, tagc, li;
	tagc = Dom.byClass("tagList", this.container)[0];
	hash = {
		"menu": tagc.getElementsByTagName("li"),
		"bg": new Animation.top(tagc.getElementsByTagName("h4")[0])
	};
	return hash;
}
imgPlay.prototype.init = function(){
	this.list.animation = new Animation.scrollTop(this.list.dom);
	this.conf.menuTeamHeight = 37;
	this.conf.menuHeight = this.conf.menuTeamHeight * this.menu.menu.length;
	this.conf.listTeamHeight = this.list.dom.getElementsByTagName("li")[0].offsetHeight;
	this.conf.listHeight = this.conf.listTeamHeight * this.menu.menu.length;
	for(var i=0; i<this.menu.menu.length; i++) {
		if(this.menu.menu[i].className == "select") this.conf.oldIndex = i;
		this.bindEvent(i);
	}
}
imgPlay.prototype.bindEvent = function(index) {
	this.autoPlay(false);
	this.menu.menu[index].onmouseover = function() {
		this.setDisplay(index)
	}.bind(this);
}
imgPlay.prototype.setDisplay = function(index, autoMode) {
	clearTimeout(this.timer);
	if (autoMode == true) this.conf.oldIndex = this.conf.debugIndex;
	this.menu.menu[this.conf.oldIndex].className = "";
	var menuTarget = (this.conf.menuTeamHeight * index) - this.conf.menuHeight;
	var listTarget = (this.conf.listTeamHeight * index);
	this.menu.bg.to(menuTarget+1);
	this.list.animation.to(listTarget);
	this.conf.oldIndex = index;	
	this.timer = window.setTimeout(function() {
		this.menu.menu[index].className = "select";
	}.bind(this), 250);
	this.autoPlay(false);
}
/* 自动变换 */
imgPlay.prototype.autoPlay = function(e) {
	if(e != false) this.auto.dly = e;
	window.clearTimeout(this.auto.fun);
	this.auto.fun = null;
	this.auto.fun = window.setTimeout(function(){
		this.conf.debugIndex = this.conf.oldIndex;
		if(this.conf.oldIndex + 1 <= 5) {
			this.setDisplay(this.conf.oldIndex += 1, true);
		} else {
			this.setDisplay(0, true);
		}
		this.autoPlay(false);
	}.bind(this), this.auto.dly);
}

/* 通用滑动门 */
var tabRoom = function(id) {
	this.container = Dom.byId(id);
	if(!this.container) {
		alert("tabRoom: 传入id的容器未找到");
		return;
	}
	this.oldIndex = 0;
	this.tab = this.getChild("tab");
	this.tem = this.getChild("tem");
	this.timer = null;
	this.initEvent();
}
tabRoom.prototype.getChild = function(name) {
	var container = Dom.byClass(name,this.container)[0];
	return container.children;
}
tabRoom.prototype.initEvent = function() {
	if(this.tab.length != this.tem.length) alert("tabRoom: 标签数不匹配");
	for(var i=0; i<this.tab.length; i++) {
		this.tem[i].style.display = "none";
		if (this.tab[i].className == "select") {
			this.oldIndex = i;
			this.tem[i].setAttribute("load", "ok");
			this.setDisply(i, 0);
		}
		this.tab[i].index = i;
		this.tab[i].sends = function(i) { this.setDisply(i) }.bind(this);
		this.tab[i].onmouseover = function() { this.sends(this.index) };
		this.tab[i].onmouseout = function() {
			clearTimeout(this.timer);
		}.bind(this);
	}
}
tabRoom.prototype.setDisply = function(i, time) {
	if(!time)var time = 500;
	this.timer = setTimeout(function() {
		this.tem[this.oldIndex].style.display = "none";
		this.tab[this.oldIndex].className = "";
		this.tem[i].style.display = "block";
		this.tab[i].className = "select";
		this.oldIndex = i;
	}.bind(this),time);
}

/* 
 * 无间断滚动 
 * @id 
 */
var scrollNoStop = function(id, time) {
	this.container = Dom.byId(id) || Dom.byId("js.scrollNoStop");
	this.scrollMod = this.container.children[0];
	this.copyedMod = null;
	this.iteam = this.scrollMod.children;
	this.infos = {
		iteamHeight: null,
		totalHeight: null,
		size: this.iteam.length
	}
	this.animation = new Animation.scrollTop(this.container);
	this.timer = null;
	this.target = 0;
	this.outTime = time || 1500
	this.init();
}
scrollNoStop.prototype.init = function(){
	/* 复位 */
	this.container.style.cursor = "pointer";
	this.container.scrollTop = 0;
	this.infos.iteamHeight = this.iteam[0].offsetHeight;
	this.infos.totalHeight = this.infos.iteamHeight * this.infos.size;
	/* 复制div */
	this.copyedMod = document.createElement(this.scrollMod.tagName);
	this.copyedMod.className = this.scrollMod.className;
	this.copyedMod.innerHTML = this.scrollMod.innerHTML;
	this.container.appendChild(this.copyedMod);
	this.bindTimer();
	this.bindEvent();
}
scrollNoStop.prototype._scroll = function() {
	if(this.target >= this.infos.totalHeight) this.container.scrollTop = 0;
	this.target = this.container.scrollTop + this.infos.iteamHeight;
	this.animation.to(this.target)
}
scrollNoStop.prototype.bindEvent = function() {
	this.container.onmouseover = function() {
		window.clearInterval(this.timer);
	}.bind(this);
	this.container.onmouseout = function() {
		this.bindTimer();
	}.bind(this);
}
scrollNoStop.prototype.bindTimer = function() {
	this.timer = window.setInterval(function() {
		this._scroll();
	}.bind(this), this.outTime);	//svn update dot
}


/*
* 全站通用弹出窗口
* 已提供一个外部事件close. 在关闭时触发
*/
var trace = function(_title, width, height) {
	var body = Dom.byBODY();
	this.layer = document.createElement("div");
	this.layer.className = "pub_trace";
		this.box = document.createElement("div");
		this.box.className = "tbox";
			var title = document.createElement("ul");
				title.className = "tbox_tit clearfix";
				var left = document.createElement("li");
					left.className = "titleft";
				var center = document.createElement("li");
					center.className = "titcont";
						this.font = document.createElement("h1");
						this.font.innerHTML = (_title)? _title: "提示信息";
						this.close = document.createElement("p");
					center.appendChild(this.font);
					center.appendChild(this.close);
				var right = document.createElement("li");
					right.className = "titright";
				title.appendChild(left);
				title.appendChild(center);
				title.appendChild(right);
			this.conte = document.createElement("div");
			this.conte.className = "content";
			this.box.appendChild(title);
			this.box.appendChild(this.conte);
		this.layer.appendChild(this.box);
	/* mask */
	this.mask = document.createElement("div");
	this.mask.className = "pub_trace_mask";
	this.mask.style.width = body.scrollWidth + "px";
	this.mask.style.height= body.scrollHeight + 10 + "px";
	body.appendChild(this.mask);
	/* end */
	body.appendChild(this.layer);
	this.bindEvent();
	if (width && height) 
		this.resize(width, height);
	else 
		this.setCenter();
}
trace.prototype.bindEvent = function() {
	document.onscroll = Dom.byHTML().onscroll = function() {
		this.setCenter();
	}.bind(this);
	this.close.onclick = function() {
		try { this.close() } catch(e) {}
		this.display("none");
	}.bind(this);
}
trace.prototype.setCenter = function() {
	var left,top;
	left = parseInt((Dom.byHTML().clientWidth-this.layer.offsetWidth)/2 - 1);
	top  = parseInt((Dom.byHTML().clientHeight-this.layer.offsetHeight)/2);
	if(Dom.byHTML().scrollTop > 0) {
		top += Dom.byHTML().scrollTop;
	} else {
		top += Dom.byBODY().scrollTop;
	}
	this.layer.style.left= left + "px";
	this.layer.style.top = top + "px";
}
//@html 可为object 或者string
trace.prototype.insertHTML = function(html) {
	if(typeof(html) == "string") {
		this.conte.innerHTML = html;
	} else if (typeof(html) == "object") {
		this.conte.appendChild(html);
	}
}
trace.prototype.clearHTML = function() {
	this.conte.innerHTML = "";
}
trace.prototype.getContainer = function() {
	return this.conte;
}
trace.prototype.remove = function() {
	this.layer.parentNode.removeChild(this.layer);
}
trace.prototype.resize = function(width, height) {
	try {
		this.layer.style.width = width.toString().replace(/[^0-9]/ig, "") + "px";
		this.box.style.height = height.toString().replace(/[^0-9]/ig, "") + "px";
		this.setCenter();
	} catch (e) { /*do nothing*/ }
}
trace.prototype.retitle = function(title) {
	if(title) this.font.innerHTML = title;
} 
//@target 可为block 或none
trace.prototype.display = function(target){
	if (target == "none") {
		this.mask.style.visibility = "hidden";
		this.layer.style.visibility = "hidden";
	} else {
		this.mask.style.visibility = "visible";
		this.layer.style.visibility = "visible";
	}
}

