/*
xtab 标签类
Author:Andyfoo
Email:andyfoo@163.com
Company:huacai.cn

*/
//标签
function xtab(name){
	var self = this;
	this.name = name;  //实例名称
	this.IE = (document.getElementById && document.all);
	this.NS = (document.getElementById && !document.all); 
	
	this.tabDefName = this.name;
	this.tabNumber = 0;
	this.tabSelected = '';
	this.tabID = new Array();
	this.tabPaneID = new Array();
	this.tabName = new Array();
	this.tabSelectName = "__xtab_" + this.name + "_select__";
	this.onClick = new Function();

	this.eventType = 'click';
	
	this.useCookie = false;
}
//初始化 
xtab.prototype.init = function(){ 
	var args = this.init.arguments;
	//var args = this.init.length;
	this.tabID = new Array();
	this.tabName = new Array();
	n = args[0];
	if(typeof(n) == 'number'){
		args = new Array();
		for(var i=0; i < n; i++){
			args[i] = this.tabDefName + i;
		}
	}

	for(var i=0; i < args.length; i++){
		this.tabName[i] = args[i];
		try{
			this.tabID[i] = this.getID(this.tabName[i]);
			

			this.tabPaneID[this.tabName[i]] = this.getID("pn_" + this.tabName[i]);
			this.tabPaneID[this.tabName[i]].style.display = "none";

			var _this = this;

			switch(this.eventType){
				case 'over':
					this.addEvent (this.tabID[i],'onclick', function(e){_this.click(e);_this.clickUrl(e); }) ;
					this.addEvent (this.tabID[i],'onmouseover', function(e){ _this.click(e);_this.over(e, true); }) ;
					this.addEvent (this.tabID[i],'onmouseout', function(e){ _this.out(e, true); }) ;
					break;
				default:
					this.addEvent (this.tabID[i],'onclick', function(e){ _this.click(e); }) ;
					this.addEvent (this.tabID[i],'onmouseover', function(e){ _this.over(e); }) ;
					this.addEvent (this.tabID[i],'onmouseout', function(e){ _this.out(e); }) ;
					break;
			}
		}catch(e){
			alert("xtab error:" + this.tabName[i] + ' is null');
			this.tabID = new Array();
			return;
		}
	}
	
	
	this.tabNumber = args.length;
	var CookieTabId = null;
	if(this.useCookie)CookieTabId = this.readCookie(this.tabSelectName);
	if(CookieTabId != null &&  this.inArray(CookieTabId, this.tabName)){
		var div = this.getID(CookieTabId);
		var defaultID = parseInt(CookieTabId.substr(this.tabDefName.length), 10);
	}else{
		var div = this.tabID[0];
		var defaultID = 0;
	}
	this.select(div);
	try{
		this.onClick(defaultID, this.tabID[defaultID], this.tabPaneID[this.tabName[defaultID]]);
	}catch(e){
		alert(e);
	}
}
xtab.prototype.setTabName = function(name){ 
	this.tabDefName = name;
}
// 
xtab.prototype.clickUrl = function(e){ 
	evt = this.fixE(this.NS ? e : event); 
	var sel = 0;
	for(i = 0; i < this.tabID.length; i++)
	{ 
		var obj = this.tabID[i];
		var c = new this.getClass(obj);
		
		if(evt.srcElement.id != obj.id){
			obj.className = c.out;
			this.tabPaneID[this.tabName[i]].style.display = "none";
		}
		if(evt.srcElement.id == obj.id){
			sel = i;
			window.open(c.url);
		}
	}
}
// 
xtab.prototype.click = function(e){ 
	evt = this.fixE(this.NS ? e : event); 
	obj = evt.srcElement;
	if(typeof(obj.id) == 'undefined' || obj.id == '' || obj.id == null || (obj.tagName && obj.tagName.toUpperCase() == "IMG")){
		obj = obj.parentNode;
	}
	var sel = 0;
	var selTab = null;
	var selPane = null;
	for(i = 0; i < this.tabID.length; i++)
	{ 
		var obj2 = this.tabID[i];
		var c = new this.getClass(obj2);
		
		if(obj.id != obj2.id){
			obj2.className = c.out;
			this.tabPaneID[this.tabName[i]].style.display = "none";
		}
		if(obj.id == obj2.id){
			sel = i;
			selTab = obj2;
			selPane = this.tabPaneID[this.tabName[i]];
		}
		
	}
	this.select(obj);
	try{
		this.onClick(sel, selTab, selPane);
	}catch(e){
		alert(e);
	}
}
// 
xtab.prototype.out = function(e, force){
	evt = this.fixE(this.NS ? e : event); 
	obj = evt.srcElement;
	if(typeof(obj.id) == 'undefined' || obj.id == '' || obj.id == null){
		obj = obj.parentNode;
	}
	if(!force && this.tabSelected == obj.id)return;
	var c = new this.getClass(obj);
	if(force && this.tabSelected == obj.id){
		obj.className = c.select;
	}else{
		obj.className = c.out;
	}

}
// 
xtab.prototype.over = function(e, force){
	evt = this.fixE(this.NS ? e : event); 
	obj = evt.srcElement;
	if(typeof(obj.id) == 'undefined' || obj.id == '' || obj.id == null){
		obj = obj.parentNode;
	}
	if(!force && this.tabSelected == obj.id)return;
	var c = new this.getClass(obj);
	
	obj.className = c.over;
}
//
xtab.prototype.select = function(div){

	var c = new this.getClass(div);
	div.className = c.select;
	this.tabPaneID[div.id].style.display = "block";
	if(this.useCookie)this.writeCookie(this.tabSelectName, div.id, 24);
	this.tabSelected = div.id;

}
xtab.prototype.select_item = function(n, noclick){
	for(i = 0; i < this.tabID.length; i++)
	{ 
		var obj = this.tabID[i];
		var c = new this.getClass(obj);
		
		if(i != n){
			obj.className = c.out;
			this.tabPaneID[this.tabName[i]].style.display = "none";
		}

		
	}
	try{
		this.select(this.tabID[n]);
	}catch(e){}
	if(noclick)return;
	
	try{
		this.onClick(n, this.tabID[n], this.tabPaneID[n]);
	}catch(e){
		alert(e);
	}
}
xtab.prototype.getClass = function(obj){
	 
	var t_out = obj.getAttribute('xtab_out');
	var t_over = obj.getAttribute('xtab_over');
	var t_select = obj.getAttribute('xtab_select');
	var t_url = obj.getAttribute('xtab_url');
	this.out = t_out == null || t_out == '' ? 'xtab_out' : t_out;
	this.over = t_over == null || t_over == '' ? 'xtab_over' : t_over;
	this.select = t_select == null || t_select == '' ? 'xtab_select' : t_select;
	this.url = t_url == null || t_url == '' ? '' : t_url;
}

xtab.prototype.inArray = function(v, arr){
	try{
		for(var i = 0; i < arr.length;i++){
			if(v == arr[i])return true;
		}
	}catch(e){}
	return false;
}

xtab.prototype.getID = function (obj){
	var element = null;
	if(document.getElementById){
		element = document.getElementById(obj);
	}
	else if(document.all){
		element = document.all[obj];
	}
	else if(document.layers){
		element = document.layers[obj];
	} 
	return element;

} 
xtab.prototype._removeEvent = function(obj, name, func) {
	name = name.toLowerCase();
	// Add the hookup for the event.
	if(typeof(obj.addEventListener) != "undefined") {
		if(name.length > 2 && name.indexOf("on") == 0) name = name.substring(2, name.length);
			obj.removeEventListener(name, func, true);
	} else if(typeof(obj.attachEvent) != "undefined"){
		obj.detachEvent(name, func);
	} else {
		eval("obj." + name) = null;
	}
}

xtab.prototype.addEvent = function(obj, name, func) {
	name = name.toLowerCase();
	// Add the hookup for the event.
	if(typeof(obj.addEventListener) != "undefined") {
		if(name.length > 2 && name.indexOf("on") == 0) name = name.substring(2, name.length);
			obj.addEventListener(name, func, false);
	} else if(typeof(obj.attachEvent) != "undefined"){
		obj.attachEvent(name, func);
	} else {
		if(eval("obj." + name) != null){
			// Save whatever defined in the event
			var oldOnEvents = eval("obj." + name);
			eval("obj." + name) = function(e) {
				try{
					func(e);
					eval(oldOnEvents);
				} catch(e){}
			};
		} else {
			eval("obj." + name) = func;
		}
	}
}
xtab.prototype.readCookie = function (name){
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0)
	{ 
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
				cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	return cookieValue;
}
xtab.prototype.writeCookie = function (name, value, hours){
	var expire = "";
	if(hours != null)
	{
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}
xtab.prototype.fixE = function (e){
	if (typeof e == 'undefined') e = window.event;
	if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
	if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
	if (typeof e.srcElement == 'undefined') e.srcElement = e.target;
	return e;
}

