bt_icons = {};

// create element
function bce(name,styles) {
	var el = document.createElement(name);
	if(styles) {
		for(s in styles) {
			if(s.charCodeAt(0) == 95) {
				el[s.substring(1)] = styles[s];
			} else {
				el.style[s] = styles[s];
			}
		}
	}
	if(arguments.length > 2) {
		for(var i=2; i<arguments.length; i++) {
			if(arguments[i] && typeof(arguments[i]) == 'object') {
				el.appendChild(arguments[i]);
			} else {
				el.appendChild(document.createTextNode(arguments[i]));
			}
		}
	}
	return el;
}

// append child
function bac(el) {
	if(arguments.length > 1) {
		for(var i=1; i<arguments.length; i++) {
			if(typeof(arguments[i]) == 'object') {
				el.appendChild(arguments[i]);
			} else {
				el.appendChild(document.createTextNode(arguments[i]));
			}
		}
	}
}

// clear childs
function bcl(el) {
	while(el.firstChild) {
		el.removeChild(el.firstChild);
	}
}

// replace child
function brc(el,ch) {
	bcl(el);
	if(arguments.length > 1) {
		for(var i=1; i<arguments.length; i++) {
			if(typeof(arguments[i]) == 'object') {
				el.appendChild(arguments[i]);
			} else {
				el.appendChild(document.createTextNode(arguments[i]));
			}
		}
	}
}

// import object
function bim(a,b) {
	for(var i in b){
		a[i]=b[i];
	}
}

// get element by id
function bge(id) {
	return document.getElementById(id);
}

// no data
function bnd() {
	return bce('span');
}

// create text
function bct(txt) {
	if(txt =='') return bnd();
	return document.createTextNode(txt);
}

// addevent
function bae(el,ev,f) {
	if(el.attachEvent){
		el.attachEvent("on"+ev,f);
	}else{
		el.addEventListener(ev,f,false);
	}
}

Function.prototype.bind=function(){
	var func=this;
	var args = [];
	for(var i=0,l=arguments.length; i<l; i++) {
		args.push(arguments[i]);
	}
	var obj = args.shift();
	return function(){
		return func.apply(obj,args);
	};
};

// compare string
function bcmps(a,b){
	if(a==b){
		return 0;
	}
	if(a===null){
		return -1;
	}
	if(b===null){
		return 1;
	}
	return a<b?-1:1;
}

// compare number
function bcmpn(a,b,f){
	return a[f]-b[f];
}


function BtabsType_Number(opts) {

	bim(this, opts);

	this.sortable = 1;
	this.sortFunc = bcmpn;

	this.makeCell = function(rowdata, tr) {
		if(typeof(rowdata[this.field]) == 'undefined' || rowdata[this.field] === null) {
			bac(tr,bce('td',null,bnd()));
		} else {
			var f = parseFloat(rowdata[this.field]);
			if(this.precision) {
				f = f.toFixed(this.precision);
			}
			bac(tr,bce('td',null,f));
		}
	};
}

function BtabsType_Lookup(opts) {

	bim(this, opts);

	this.sortable = 1;
	this.sortFunc = bcmpn;

	this.makeCell = function(rowdata, tr) {
		if(this.lookup === null || rowdata[this.field] === null || this.lookup[rowdata[this.field]] === null) {
			bac(tr,bce('td',null,bnd()));
		} else {
			bac(tr,bce('td',null,this.lookup[rowdata[this.field]]));
		}
	};
}

function BtabsType_LookupBitMask(opts) {

	bim(this, opts);

	this.sortable = 1;
	this.sortFunc = bcmpn;

	this.makeCell = function(rowdata, tr) {
		if(this.lookup === null || rowdata[this.field] === null) {
			bac(tr,bce('td',null,bnd()));
		} else {
			var s = '';
			for(var i in this.lookup) {
				if(rowdata[this.field] & i) {
					if(s != '') s += ', ';
					s += this.lookup[i]
				}
			}
			bac(tr,bce('td',null,s));
		}
	};
}


function BtabsType_NumberString(opts) {

	bim(this, opts);

	this.sortable = 1;
	this.sortFunc = bcmpn;

	this.makeCell = function(rowdata, tr) {
		var td = bce('td');
		if(rowdata[this.field] === null) {
			if(rowdata[this.field2] === null) {
				bac(td,bnd());
			} else {
				bac(td,bce('small',null,bct(rowdata[this.field2])))
			}
		} else {
			bac(td,bct(rowdata[this.field]));
			if(rowdata[this.field2]) {
				bac(td,bce('br'));
				bac(td,bce('small',null,bct(rowdata[this.field2])))
			}
		}
		bac(tr,td);
	};
}

function BtabsType_Reagents(opts) {
	this.iconsize = 32;
	bim(this, opts);

	this.makeCell =function(rowdata,tr) {
		var td = bce('td',{_align: 'left'});
		if(rowdata[this.field] && rowdata[this.field].length>0) {
			for(var i=0, l=rowdata[this.field].length; i<l; i++) {
				var re = rowdata[this.field][i];
				bac(td,bt_gi(re.id, this.iconsize, re.c));
			}
		} else {
			bac(td,bnd());
		}
		bac(tr,td);
	};
}

function BtabsType_String(opts) {
	bim(this, opts);

	this.makeCell =function(rowdata,tr) {
		var content;
		if(rowdata[this.field]) {
			content = bct(rowdata[this.field]);
		} else {
			content = bnd();
		}
		if(this.linkfield && rowdata[this.linkfield]) {
			if(this.linkformat) {
				link = this.linkformat.replace(/%[sd]/,rowdata[this.linkfield]);
			} else {
				link = rowdata[this.linkfield];
			}
			content = bce('a',{_href:link},content)
		}
		var properties = null
		if(this.align) {
			properties = {_align:this.align,paddingLeft:'5px'}
		}else{
			properties = {paddingLeft:'5px'}
		}
		bac(tr,bce('td',properties,content));
	};
	this.sortable = 1;
}

function BtabsType_Name(opts) {
	this.field = 'n';
	this.field2 = 'd';
	this.linkfield = 'id';
	bim(this, opts);

	this.makeCell =function(rowdata,tr) {
		var content = bct(rowdata[this.field])
		if(this.linkfield && rowdata[this.linkfield]) {
			if(this.linkformat) {
				href = this.linkformat.replace(/%[sd]/,rowdata[this.linkfield]);
			} else {
				href = rowdata[this.linkfield];
			}
			content = bce('a',{_href:href, _className:'q'},content);
		}
		
		var tds = bce('td',{_align:'left', paddingLeft:'3px'},content);

		if(rowdata[this.field2]) {
			bac(tds,bce('br'));
			bac(tds,bce('small',null,rowdata[this.field2]));
		}

		if(rowdata.on) {
			bac(tds,bce('br'));
			bac(tds,bce('span',{_className:'origname'},bct(rowdata.on)));
		}		
	
		if(rowdata.viewable){
			var hrefpreview = 'javascript:buffedViewer('+rowdata.viewable+');'
			var preview = bce('small',{_align:'left'},bce('a',{_href:hrefpreview}, BTLANG.Viewerpreview));
			bac(tds,bce('br'));
			bac(tds,preview)
		}
		//bac(tr,tdi);
		bac(tr,tds);

/*		
		var properties = null
		if(this.align) {
			properties = {_align:this.align,paddingLeft:'5px'}
		}else{
			properties = {paddingLeft:'5px'}
		}
		bac(tr,bce('td',properties,content));*/
	};
	this.sortable = 1;
}


function BtabsType_DString(opts) {
	bim(this, opts);

	this.makeCell =function(rowdata,tr) {
		var d = rowdata[this.field];
		if(!d) d=''
		var td = bce('td',null,d);
		if(rowdata[this.field2]) {
			bac(td,bce('br'));
			bac(td,bce('small',null,rowdata[this.field2]));
		}
		bac(tr,td);
	};
	this.sortable = 1;
	this.sortFunc = function(a,b,f) {
		var r = bcmps(a[f],b[f]);
		if(r!==0) return r;
		return bcmps(a[this.field2],b[this.field2]);
	}
}


function BtabsType_Rewards(opts) {
	bim(this, opts);
	this.title = BTLANG.Rewards;

	this.makeCell =function(rowdata,tr) {
		var r = rowdata.rew
		var td = bce('td',null,rowdata[this.field]);
		if(rowdata[this.field2]) {
			bac(td,bce('br'));
			bac(td,bce('small',null,rowdata[this.field2]));
		}
		bac(tr,td);
	};
	this.sortable = 1;
	this.sortFunc = function(a,b,f) {
		var r = bcmps(a[f],b[f]);
		if(r!==0) return r;
		return bcmps(a[this.field2],b[this.field2]);
	}
}

function BtabsType_Spell(opts) {
	bim(this, opts);

	this.field = 'n';
	this.title = BTLANG.Name;
	this.align = 'left';
	this.colspan = 2;
	this.sortable = 1;

	this.makeCell =function(rowdata, tr) {
		var href = '/?s='+rowdata.id;
		var tdi = bce('td',{borderRight:"none",width:"1%"});
		var cls = 'q';
		if(!rowdata.n) {
			rowdata.n = '';
		}

		if(rowdata.p && rowdata.p.id) {
			bac(tdi,bt_gi(rowdata.p.id,40,rowdata.p.c,rowdata.p.cm));
			cls = 'q'+ rowdata.p.q;
		} else {
			bac(tdi,bt_gsi(rowdata.id,rowdata.icon,40));
		}
		var tds = bce('td',{_align:'left'},bce('a',{_href:href, _className:cls},rowdata.n.substring(1)));

		if(rowdata.rank) {
			bac(tds,bce('br'));
			bac(tds,bce('small',null,rowdata.rank));
		}
		
		if(rowdata.on) {
			bac(tds,bce('br'));
			bac(tds,bce('span',{_className:'origname'},bct(rowdata.on)));
		}

		bac(tr,tdi);
		bac(tr,tds);
	};
}

function BtabsType_Quest(opts) {
	bim(this, opts);

	this.field = 'n';
	this.title = BTLANG.Name;
	this.align = 'left';
	this.colspan = 1;
	this.sortable = 1;

	this.makeCell =function(rowdata, tr) {
		var href = '/?q='+rowdata.id;
		var cls = 'q';
		if(!rowdata.n) {
			rowdata.n = '';
		}

		var tds = bce('td',{_align:'left',paddingLeft:'4px'},bce('a',{_href:href, _className:cls},rowdata.n));
		if(rowdata.on) {
			bac(tds,bce('br'));
			bac(tds,bce('span',{_className:'origname'},bct(rowdata.on)));
		}		

		bac(tr,tds);
	};
}


function BtabsType_WOWChar(opts) {
	bim(this, opts);
	this.title = BTLANG.Name;
	this.align = 'left';
	this.colspan = 2;
	this.sortable = 1;

	this.makeCell =function(rowdata,tr) {
		var href = 'char/view/'+rowdata.id;		
		var tdi = bce('td',{borderRight:"none",width:"1%"});

		bac(tdi,bt_gri(rowdata.id,32));

		var tds = bce('td',{_align:'left'},bce('a',{_href:href},rowdata.name), bce('br'));
		bac(tr,tdi);		
		bac(tr,tds);
	};
}

function BtabsType_LanguageImage(opts) {
	bim(this, opts);
	this.sortable = 1;
	
	this.makeCell =function(rowdata,tr) {
		var tdi = bce('td');
		var myimage = bce('img',{_src:"/images/lang-"+rowdata[this.field]+".gif", _width:"18", _height:"12"});
		bac(tdi,myimage);
		bac(tr,tdi);
	};
}

function BtabsType_RealmImage(opts) {
	bim(this, opts);
	this.sortable = 1;
	
	this.makeCell =function(rowdata,tr) {
		var tdi = bce('td');
		var myimage = bce('img',{_src:"/img/style/war/list-realm-"+rowdata[this.field]+".png", _width:"20", _height:"24"});
		bac(tdi,myimage);
		bac(tr,tdi);
	};
}

function BtabsType_FactionImage(opts) {
	bim(this, opts);
	this.sortable = 1;
	
	this.makeCell =function(rowdata,tr) {
		var tdi = bce('td');
		if(rowdata[this.field] == "A"){
			var icon = "alliance-icon-large";
		}else{
			var icon = "horde-icon-large";
		}
		
		var myimage = bce('img',{_src:"/images/wow/"+icon+".gif", _width:"32", _height:"32"});
		bac(tdi,myimage);
		bac(tr,tdi);
	};
}

function BtabsType_CareerImage(opts) {
	bim(this, opts);
	this.sortable = 1;
	
	this.makeCell =function(rowdata,tr) {
		var tdi = bce('td');
		var myimage = bce('img',{_src:"/img/icons/war/24/"+rowdata[this.field]+".png", _width:"24", _height:"24"});
		bac(tdi,myimage);
		bac(tr,tdi);
	};
}

function BtabsType_ClassImage(opts) {
	bim(this, opts);
	this.sortable = 1;
	
	this.makeCell =function(rowdata,tr) {
		var tdi = bce('td');
		var myimage = bce('img',{_src:"/img/icons/wow/32/"+rowdata[this.field]+".png", _width:"32", _height:"32"});
		bac(tdi,myimage);
		bac(tr,tdi);
	};
}

function BtabsType_Items(opts) {
	bim(this, opts);
	this.sortable = 1;
	
	this.makeCell =function(rowdata,tr) {
		var tdi = bce('td');

		for(i = 0;i<rowdata["numpieces"];i++){
			if(typeof rowdata[i] == "undefined") continue;
			var href = "?i="+rowdata[i]["id"];
			var mylink  = bce('a', {_href:href});
			var myimage = bce('img',{_src:"/img/icons/war/24/"+rowdata[i]["icon"]+".png", _width:"24", _height:"24"});
		 	bac(mylink,myimage)
			bac(tdi,mylink);
		}
		
		bac(tr,tdi);
	};
}

function BtabsFilter_Name(opts) {
	bim(this, opts);
	this.appendControl = function(el) {
		if(!this.input) {
			this.input = bce('input',{});
			this.input.id = '__filteredit';
			bae(this.input,'keyup',this.change.bind(this));
		}
		bac(el,this.input);
	}
	
	this.criteria = '';
		
	this.change=function() {
		var value = this.input.value;
		if(value == this.criteria) {
			return
		}
		this.criteria = value;
		if(this.criteria != "") {
			var re = this.criteria.replace(/^\s+/,'');
			var re = re.replace(/\s+$/,'');
			re = re.replace(/([^\w ])/g,'\\$1');
			if(re != '') {
				this.regexp = new RegExp(re,'i');
			} else {
				this.regexp = null;
			}
		} else {
			this.regexp = null;
		}
		if(this.table.applyFilter()) {
			this.table.page=0;
			this.table.makeTable();
		}

	}
	
	this.match = function(row) {
		if(this.regexp) {
			return this.regexp.test(row.rowdata.n);
		}
		return 1;
	}
}

function BtabsFilter_Lookup(opts) {
	bim(this, opts);
	this.appendControl = function(el) {
		if(!this.input) {
			this.input = bce('select',{});
			var opt = new Option('',0);
			bac(this.input,opt);
			for(var id in this.lookup) {
				opt = new Option(this.lookup[id],id);
				this.input.options[this.input.length] = opt;
				//bac(this.input,opt);
			}
			bae(this.input,'change',this.change.bind(this));
		}
		if(this.title) {
			bac(el,bct(' '+this.title+': '));
		}
		
		bac(el,this.input);
	}
	
	this.change=function() {
		
		var value = this.input.value;
		if(value == this.criteria) {
			return
		}
		if(this.lookup[value]) {
			this.criteria = value;
		} else {
			this.criteria = 0;
		}
		if(this.table.applyFilter()) {
			this.table.page=0;
			this.table.makeTable();
		}

	}	
	
	this.match = function(row) {
		if(this.criteria) {
			return row.rowdata[this.field] == this.criteria;
		}
		return 1;
	}	
}

function BtabsFilter_LookupBitMask(opts) {
	bim(this, opts);
	this.appendControl = function(el) {
		if(!this.input) {
			this.input = bce('select',{});
			var opt = new Option('',0);
			bac(this.input,opt);
			for(var id in this.lookup) {
				opt = new Option(this.lookup[id],id,false,(this.criteria==id));
				this.input.options[this.input.length] = opt;
			}
			bae(this.input,'change',this.change.bind(this));
		}
		if(this.title) {
			bac(el,bct(' '+this.title+': '));
		}
		bac(el,this.input);
	}
	
	this.setValue = function(value) {
		if(this.lookup[value]) {
			this.criteria = value;
		} else {
			this.criteria = 0;
		}
	}
	
	this.change=function() {
		
		var value = this.input.value;
		if(value == this.criteria) {
			return
		}
		if(this.lookup[value]) {
			this.criteria = value;
		} else {
			this.criteria = 0;
		}
		if(this.table.applyFilter()) {
			this.table.page=0;
			this.table.makeTable();
		}

	}	
	
	this.match = function(row) {
		if(this.criteria) {
			return (row.rowdata[this.field] & this.criteria) != 0;
		}
		return 1;
	}	
}

function BtabsFilter_Flag(opts) {
	bim(this, opts);
	this.appendControl = function(el) {
		if(!this.input) {
			this.input = bce('input',{_type: "checkbox",_value:1});
			bae(this.input,'click',this.change.bind(this));
		}
		if(this.title) {
			bac(el,bct(' '+this.title+': '));
		}
		bac(el,this.input);
	}
	
	this.change=function() {
		
		var value = this.input.checked;
		if(this.criteria == value) {
			return;
		}
		this.criteria = value;
		
		if(this.table.applyFilter()) {
			this.table.page=0;
			this.table.makeTable();
		}
	}	
	
	this.match = function(row) {
		if(this.criteria) {
		 	return row.rowdata[this.field];
		} 
		return 1;
	}	
}

function BtabsRow(cols, rowdata) {
	this.rowdata = rowdata;
	this.tr = bce('tr',{height:'40px'});
	this.visible = 1;
	for(var i=0, l=cols.length; i<l; i++) {
		cols[i].makeCell(this.rowdata, this.tr);
	}
}


function BtabsTable(opts) {
	
	this.addFilter = function(f) {
		f.table = this;
		this.filters.push(f);
	}
	
	this.applyFilter = function() {
		var changed = 0;
		this.visiblecount = 0;
		var l2 = this.filters.length;
		for(var i=0, l=this.rows.length;i<l;i++) {
			var visible = true;
			for(var i2=0;i2<l2;i2++) {
				if(!this.filters[i2].match(this.rows[i])) {
					visible = false;
				}
			}
			if(this.rows[i].visible != visible)	{
				changed = 1;
				this.rows[i].visible = visible;
			}
			if(this.rows[i].visible) this.visiblecount++;
		}
		return changed;
	}

	this.getTemplate = function(tpl) {
		var cols = [];
		switch(tpl) {
			case "blueposts":
				cols.push(new BtabsType_Blueposts({title:BTLANG.Topic, width:'60%', field:'topic', linkfield:'topicid', linkformat:'/blueposts/viewtopic/%d', align:'left'}));
				cols.push(new BtabsType_Blueposts_Author({title:BTLANG.Author, width:'10%', field:'author'}));
				//cols.push(new BtabsType_Blueposts_Category({width:'1%'}));
				cols.push(new BtabsType_String({title:BTLANG.Board, width:'10%', field:'boardname'}));
				cols.push(new BtabsType_String({title:BTLANG.Date, sortreverse: 1, field:'date', width:'10%'}));
				cols.push(new BtabsType_String({title:BTLANG.OriginalBy, width:'10%', field:'org_author', linkfield:'org_author', linkformat:'/?f=%d#chars'}));
				this.defsort=['date'];
				this.addFilter(new BtabsFilter_Lookup({lookup:bt_authors,title:BTLANG.Author,field:'author'}));
				this.addFilter(new BtabsFilter_Lookup({lookup:bt_boards,title:BTLANG.Board,field:'boardname'}));				
			break;
			case 'zonelist':
				cols.push(new BtabsType_ZoneName({width:'50%'}));
				cols.push(new BtabsType_String({title: BTLANG.Level, field: "level", width:'15%'}));
				cols.push(new BtabsType_ZoneTerritoryMask({width:'15%'}));
				cols.push(new BtabsType_String({title: BTLANG.InstanceType, field: "instance", width:'20%'}));
			break;			
			case 'factionlist':
				cols.push(new BtabsType_FactionName({width:'50%'}));
				cols.push(new BtabsType_DString({title: BTLANG.Group, field: "maingroup", field2: "subgroup", width:'30%'}));
				cols.push(new BtabsType_FactionMask({width:'20%'}));
			break;
			case "itemlist":
				cols.push(new BtabsType_Item({width:'40%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'15%'}));
				cols.push(new BtabsType_DString({title: BTLANG.Type, field: "cl", field2:"scl", width:'25%'}));
			break;
			case "itemsetlist":
				cols.push(new BtabsType_ItemSet({width:'30%'}));
				cols.push(new BtabsType_Number({width:'10%',title:BTLANG.Level,field:'l',sortreverse:1}));
				cols.push(new BtabsType_ItemList({width:'40%',field:'si',title: BTLANG.Items}));
				cols.push(new BtabsType_LookupBitMask({width:'20%',field:'cm',title: BTLANG.Class,lookup:bt_class}));
				//cols.push(new BtabsType_String({width:'20%',field:'classes',title: BTLANG.Class}));
				this.defsort=['l','n'];
				//this.addFilter(new BtabsFilter_Name({}));
				this.addFilter(new BtabsFilter_Lookup({lookup:bt_settag,title:BTLANG.SetType,field:'t'}));
				this.addFilter(new BtabsFilter_LookupBitMask({lookup:bt_class,title:BTLANG.Class,field:'cm',id:'class'}));
			break;
			case "questlist":
				cols.push(new BtabsType_Quest({width:'45%'}));
				cols.push(new BtabsType_Faction({field:"f",width:'10%'}));
				cols.push(new BtabsType_ItemList({title: BTLANG.Rewards,width:'35%'}));
				cols.push(new BtabsType_NumberString({title: BTLANG.Level, field: "level", field2: "qi",width:'5%'}));
				cols.push(new BtabsType_NumberString({title: BTLANG.ReqLevel, field: "rlevel", field2: "qi",width:'5%'}));
				this.addFilter(new BtabsFilter_Name({}));
				this.addFilter(new BtabsFilter_Flag({title:BTLANG.Daily,field:'d'}));
			break;
			case "questlistsmall":
				cols.push(new BtabsType_Quest({}));
				cols.push(new BtabsType_NumberString({title: BTLANG.Level, field: "level", field2: "qi"}));
			break;
			case "classspells":
				cols.push(new BtabsType_Spell({width:'50%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 0, width:'20%'}));
				cols.push(new BtabsType_Reagents({title: BTLANG.Reagents, width:'10%', field:'re'}));
				cols.push(new BtabsType_String({title: BTLANG.School, field: "school", width:'20%'}));
				this.defsort = ['level','n'];

			break;
			case "classtalents":
				cols.push(new BtabsType_Spell({width:'50%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'20%'}));
				cols.push(new BtabsType_String({title: "Pos", field: "pos", width:'10%'}));
				cols.push(new BtabsType_String({title: BTLANG.School, field: "school", width:'20%'}));
				this.defsort = ['school','pos'];
			break;
			case "spelllistsmall":
				cols.push(new BtabsType_Spell({width:'80%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'20%'}));
			break;
			case "abilitylist":
				cols.push(new BtabsType_Ability({width:'40%'}));
				cols.push(new BtabsType_String({title: BTLANG.Type, field: "atype", width:'20%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Rank, field: "level", sortreverse: 0, width:'10%'}));
				cols.push(new BtabsType_String({title: BTLANG.Path, field: "path", width:'30%'}));
				this.defsort = ['level','n'];
			break;
			case "profspells":
				cols.push(new BtabsType_Spell({width:'40%'}));
				cols.push(new BtabsType_Reagents({title: BTLANG.Reagents, width:'35%', field:'re'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'10%'}));
				cols.push(new BtabsType_Reagents({title: BTLANG.Recipe_abbr, width:'15%', field:'rec', iconsize:32}));
				this.defsort = ['level','n'];
			break;
			case "creaturelist":
				cols.push(new BtabsType_Name({title:BTLANG.Name, linkformat:"?n=%d"}));
				cols.push(new BtabsType_DString({title:BTLANG.Level, field:'l', field2:'t'}));
				cols.push(new BtabsType_String({title:BTLANG.Zone, field:'z'}));
				break;
			case "objectlist":
				cols.push(new BtabsType_Name({title:BTLANG.Name, linkformat:"?o=%d"}));
				cols.push(new BtabsType_String({title:BTLANG.Zone, field:'z'}));
				break;
			case "war_creaturelist":
				cols.push(new BtabsType_Name({title:BTLANG.Name, linkformat:"?n=%d"}));
				cols.push(new BtabsType_DString({title:BTLANG.Level, width:'10%', field:'lmax', field2:'tier'}));
				cols.push(new BtabsType_String({title:BTLANG.Zone, width:'10%', field:'z'}));
				this.defsort = ['name'];
			break;
			case "war_creaturesearchlist":
				cols.push(new BtabsType_Name({title:BTLANG.Name, linkformat:"?n=%d"}));
				cols.push(new BtabsType_DString({title:BTLANG.Level, width:'10%', field:'lmax', field2:'tier'}));
				this.defsort = ['name'];
			break;
			case "war_zonelist":
				cols.push(new BtabsType_String({title: BTLANG.Zonename, field: "n", width:'90%', linkfield:'id', linkformat:'/zone/view/%d', align:'left'}));
				cols.push(new BtabsType_String({title: BTLANG.Level, field: "level", width:'10%'}));
				this.defsort = ['name'];
			break;
			case "war_serverlist":
				cols.push(new BtabsType_String({title: BTLANG.Servername, field: "name", width:'50%', linkfield:'id', linkformat:'/server/view/%d', align:'left'}));
				cols.push(new BtabsType_LanguageImage({title:  BTLANG.Language, field: "language", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Characters, field: "chars", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Order, field: "order", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Destruction, field: "destro", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Guilds, field: "guilds", width:'10%'}));
				this.defsort = ['language','name'];
			break;
			case "war_guildlist":
				cols.push(new BtabsType_String({title: BTLANG.Guildname, field: "name", width:'50%', linkfield:'id', linkformat:'/guild/view/%d', align:'left'}));
				cols.push(new BtabsType_RealmImage({title:  BTLANG.Realm, field: "realm", width:'5%'}));
				cols.push(new BtabsType_String({title: BTLANG.Created, field: "created", width:'15%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Members, field: "members", width:'15%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Rank, field: "rank", width:'15%'}));
				this.defsort = ['name'];
			break;
			case "war_characterlist":
				cols.push(new BtabsType_String({title: BTLANG.Name, field: "name", width:'50%', linkfield:'id', linkformat:'/char/view/%d', align:'left'}));
				cols.push(new BtabsType_Number({title: BTLANG.Rank, field: "rank", width:'5%'}));
				cols.push(new BtabsType_CareerImage({title: BTLANG.Career, field: "career", width:'5%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Renown, field: "renown", width:'5%'}));
				cols.push(new BtabsType_String({title: "Server", field: "server", width:'15%', linkfield:'serverid', linkformat:'/server/view/%d'}));
				cols.push(new BtabsType_String({title: BTLANG.Guild, field: "guild", width:'20%', linkfield:'guildid', linkformat:'/guild/view/%d'}));
				this.defsort = ['name'];
			break;
			case "war_guildcharacterlist":
				cols.push(new BtabsType_String({title: BTLANG.Name, field: "name", width:'40%', linkfield:'id', linkformat:'/char/view/%d', align:'left'}));
				cols.push(new BtabsType_CareerImage({title: BTLANG.Career, field: "career", width:'10%'}));
				cols.push(new BtabsType_String({title: BTLANG.Title, field: "title", width:'20%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Rank, field: "rank", width:'10%'}));
				cols.push(new BtabsType_String({title: BTLANG.Lastlogin, field: "lastlogin", width:'20%', linkfield:'guildid', linkformat:'/guild/view/%d'}));
				this.defsort = ['name','title'];
			break;
			case "war_itemsetlist":
				cols.push(new BtabsType_String({title: BTLANG.Name, field: "name", width:'40%', linkfield:'id', linkformat:'/itemset/view/%d', align:'left'}));
				cols.push(new BtabsType_CareerImage({title: BTLANG.Career, field: "career", width:'10%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'10%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Pieces, field: "numpieces", sortreverse: 1, width:'10%'}));
				cols.push(new BtabsType_Items({title: BTLANG.Items, field: "items", width:'40%'}));
				this.defsort = ['level','name'];
			break;
			case "war_itemlist":
				cols.push(new BtabsType_Item({width:'58%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'10%'}));
				cols.push(new BtabsType_DString({title: BTLANG.Type, field: "cl", field2:"scl", width:'30%'}));
				
			break;
			case "war_influence_itemlist":
				cols.push(new BtabsType_Item({width:'50%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'10%'}));
				cols.push(new BtabsType_DString({title: BTLANG.Type, field: "cl", field2:"scl", width:'20%'}));
				cols.push(new BtabsType_String({title: BTLANG.Tier, field: "tier",  width:'20%'}));
				this.defsort = ['tier','level'];
			break;
			case "war_abilitylist":
				cols.push(new BtabsType_Ability({width:'40%'}));
				cols.push(new BtabsType_String({title: BTLANG.Type, field: "atype", width:'20%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Rank, field: "level", sortreverse: 0, width:'10%'}));
				cols.push(new BtabsType_String({title: BTLANG.Path, field: "path", width:'30%'}));
				this.defsort = ['level','n'];
			break;
			case "achievementlist":
				cols.push(new BtabsType_Achievement());
				cols.push(new BtabsType_Faction());
				cols.push(new BtabsType_Number({title: BTLANG.Points, field:"p"}));
			break;
			case "wow_serverlist":
				cols.push(new BtabsType_String({title: BTLANG.Servername, field: "name", width:'40%', linkfield:'id', linkformat:'/server/view/%d', align:'left'}));
				cols.push(new BtabsType_LanguageImage({title:  BTLANG.Language, field: "language", width:'10%'}));
				cols.push(new BtabsType_String({title:  BTLANG.Type, field: "type", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Guilds, field: "guilds", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Characters, field: "chars", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.FHorde, field: "horde", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.FAlliance, field: "alliance", width:'10%'}));				
				this.defsort = ['language','name'];
			break;
			case "wow_guildlist":
				cols.push(new BtabsType_String({title: BTLANG.Guildname, field: "name", width:'80%', linkfield:'id', linkformat:'/guild/view/%d', align:'left'}));				
				cols.push(new BtabsType_FactionImage({title:  BTLANG.Faction, field: "faction", width:'10%'}));
				cols.push(new BtabsType_String({title: "Server", field: "server", width:'20%', linkfield:'serverid', linkformat:'/server/view/%d'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Members, field: "members", width:'10%'}));
				this.defsort = ['name'];
			break;
			case "wow_characterlist":
				cols.push(new BtabsType_WOWChar({width:'58%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "rank", width:'5%'}));				
				cols.push(new BtabsType_ClassImage({title: BTLANG.Class, field: "class", width:'5%'}));
				cols.push(new BtabsType_String({title: "Server", field: "server", width:'20%', linkfield:'serverid', linkformat:'/server/view/%d'}));
				cols.push(new BtabsType_String({title: BTLANG.Guild, field: "guild", width:'25%', linkfield:'guildid', linkformat:'/guild/view/%d'}));
				this.defsort = ['name'];
			break;
			case "wow_guildcharacterlist":
				//cols.push(new BtabsType_String({title: BTLANG.Name, field: "name", width:'60%', linkfield:'id', linkformat:'/char/view/%d', align:'left'}));
				cols.push(new BtabsType_WOWChar({width:'58%'}));
				cols.push(new BtabsType_ClassImage({title: BTLANG.Class, field: "class", width:'10%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", width:'10%'}));
				cols.push(new BtabsType_String({title: BTLANG.LastUpdate, field: "last_update", width:'20%', linkfield:'guildid', linkformat:'/guild/view/%d'}));
				this.defsort = ['name','title'];
			break;
			case "wow_guildcrafterlist":
				//cols.push(new BtabsType_String({title: BTLANG.Name, field: "name", width:'60%', linkfield:'id', linkformat:'/char/view/%d', align:'left'}));
				cols.push(new BtabsType_WOWChar({width:'58%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Rank, field: "rank", width:'10%'}));
				cols.push(new BtabsType_ClassImage({title: BTLANG.Profession, field: "skill", width:'10%'}));
				//this.defsort = ['rank'];
			break;
			case "rom_itemlist":
				cols.push(new BtabsType_Item({width:'40%'}));
				cols.push(new BtabsType_Number({title: BTLANG.RomTier, field: "l", width:'25%'}));
				cols.push(new BtabsType_DString({title: BTLANG.Type, field: "cl", field2:"scl", width:'25%'}));
			break;
			case "rom_itemsetlist":
				cols.push(new BtabsType_ItemSet({width:'30%'}));
				cols.push(new BtabsType_Number({width:'10%',title:BTLANG.RomTier,field:'l',sortreverse:1}));
				cols.push(new BtabsType_ItemList({width:'40%',field:'si',title: BTLANG.Items}));
				this.defsort=['l','n'];
				//this.addFilter(new BtabsFilter_Lookup({lookup:bt_settag,title:BTLANG.SetType,field:'t'}));
				//this.addFilter(new BtabsFilter_LookupBitMask({lookup:bt_class,title:BTLANG.Class,field:'cm',id:'class'}));
			break;
			case "rom_questlist":
				cols.push(new BtabsType_Quest({width:'50%'}));				
				cols.push(new BtabsType_ItemList({title: BTLANG.Rewards,width:'35%'}));
				cols.push(new BtabsType_NumberString({title: BTLANG.Level, field: "level", field2: "qi",width:'5%'}));
				this.addFilter(new BtabsFilter_Name({}));
				this.addFilter(new BtabsFilter_Flag({title:BTLANG.Daily,field:'d'}));
			break;
			case "rom_npclist":
				cols.push(new BtabsType_Name({title:BTLANG.Name, linkformat:"?n=%d"}));
				cols.push(new BtabsType_DString({title:BTLANG.Level, field:'l', field2:'t'}));
				cols.push(new BtabsType_String({title:BTLANG.Zone, field:'z'}));
				break;
			case "rom_recipelist":
				cols.push(new BtabsType_Item({width:'40%'}));
				cols.push(new BtabsType_Reagents({title: BTLANG.Reagents, width:'35%', field:'re'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'10%'}));
				cols.push(new BtabsType_Reagents({title: BTLANG.Recipe_abbr, width:'15%', field:'rec', iconsize:32}));
				this.defsort = ['level','n'];
			break;
			case "rom_skilllist":
				cols.push(new BtabsType_Skill({width:'30%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", width:'10%'}));
				cols.push(new BtabsType_String({width:'60%', field:"desc", title:""}));
				this.defsort = ['level','n'];
			break;
			case "rom_minelist":
				cols.push(new BtabsType_Name({title:BTLANG.Name,linkformat:"?o=%d",width:'90%'}));
				cols.push(new BtabsType_Number({title: BTLANG.Level, field: "level", sortreverse: 1, width:'10%'}));				
			break;
			case "rom_serverlist":
				cols.push(new BtabsType_String({title: BTLANG.Servername, field: "name", width:'40%', linkfield:'id', linkformat:'/server/view/%d', align:'left'}));				
				cols.push(new BtabsType_LanguageImage({title:  BTLANG.Language, field: "region", width:'10%'}));				
				cols.push(new BtabsType_String({title:  BTLANG.Type, field: "type", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Guilds, field: "guilds", width:'10%'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Characters, field: "population", width:'10%'}));
				this.defsort = ['region','name'];
			break;
			case "rom_characterlist":				
				cols.push(new BtabsType_RoM_Char({width:'25%'}));
				cols.push(new BtabsType_RoM_Race({width:'15%'}));
				cols.push(new BtabsType_RoM_Level({width:'25%'}));
				cols.push(new BtabsType_String({title: BTLANG.Guild, field: "gn", width:'20%'}));
				cols.push(new BtabsType_String({title: BTLANG.Servername, field: "sn", width:'15%'}));
				//this.defsort = ['name'];
			break;
			case "rom_guildlist":
				cols.push(new BtabsType_String({title: BTLANG.Guildname, field: "name", width:'80%', align:'left'}));				
				cols.push(new BtabsType_String({title: "Server", field: "server", width:'20%', linkfield:'serverid', linkformat:'/server/view/%d'}));
				cols.push(new BtabsType_Number({title:  BTLANG.Members, field: "members", width:'10%'}));
				this.defsort = ['name'];
			break;
			default:			
				cols.push(new BtabsType_String({title: BTLANG.Name, field: "n"}));				
			break;
		}
		return cols;
	};
			
	this.name = opts.n;
	this.subtitle = opts.st;
	this.id = opts.id;
	this.filters = [];


	this.cols = this.getTemplate(opts.tpl);
	if(opts.cols && opts.cols.length) {
		for(var i=0,l=opts.cols.length; i<l; i++) {
			if(opts.cols[i].c) {
				this.cols.push(opts.cols[i].c);
			}
		}
	}

	this.rows = [];

	this.page = 0;

	this.pagelength = 30;

	this.headline = opts.h;
	this.sortcol = -1;
	this.sortdefault = 0;
	this.sortreverse = 0;


	for(var i=0, l=opts.rows.length; i<l; i++) {
		this.rows.push(new BtabsRow(this.cols, opts.rows[i]));
	}
	
	this.visiblecount = this.rows.length;

	if(opts.prefilter) {
		for(var i=0;i<this.filters.length;i++) {
			if(this.filters[i].id == opts.prefilter.id) {
				this.filters[i].setValue(opts.prefilter.val);
			}
		}
		this.applyFilter();
		
	}
	

	this.getPager = function() {

		var td = bce('div',{_align:'right'});
		var s;
		if(this.visiblecount > this.pagelength) {
			s = (this.page * this.pagelength + 1).toString();
			s += ' - ';
			s += Math.min((this.page + 1) * this.pagelength, this.visiblecount).toString();
			s += ' '+BTLANG.Of+' ';
			s += this.visiblecount.toString();
		} else {
			s = this.visiblecount.toString();
			s += ' '+BTLANG.Results;
		}
		s = ' '+s+' ';

		var a_prev = bce('a',{textDecoration:'none'},'<<');
		if(this.page > 0) {
			a_prev.style.cursor = 'pointer';
			a_prev.style.fontWeight = 'bold';
			bae(a_prev,'click',this.pageClick.bind(this,-1));
		} else {
			a_prev.className = 'q0';
		}

		bac(td,a_prev);
		bac(td,document.createTextNode(s));

		var a_next = bce('a',{textDecoration:'none'},'>>');
		if((this.page +1) * this.pagelength < this.visiblecount) {
			a_next.style.cursor = 'pointer';
			a_next.style.fontWeight = 'bold';
			bae(a_next,'click',this.pageClick.bind(this,1));
		} else {
			a_next.className = 'q0';
		}
		bac(td,a_next);
		return td;
	};

	this.pageClick = function(i) {
		if(this.page + i >=0 && (this.page + i) * this.pagelength < this.rows.length ) {
			this.page += i;
			this.makeTable();
		}
	};

	this.getCount = function() {
		return this.rows.length;
	};

	this.getBody = function() {
		var tbody = bce('tbody');
//		for(var i=this.page*this.pagelength; i<Math.min((this.page+1)*this.pagelength,this.rows.length); i++) {
//			bac(tbody,this.rows[i].tr);
//		}
		var i=0;
		var x=0;
		for(var i=0,l=this.rows.length;i<l;i++) {
			if(this.rows[i].visible) {
				if((x>=this.page*this.pagelength) && (x<(this.page+1)*this.pagelength)) {
					bac(tbody,this.rows[i].tr);
				}
				x++;
			}
		}
		return tbody;
	};

	this.setSort = function(i) {
		if(Math.abs(this.sortcol) == i+1) {
			this.sortcol = -this.sortcol;
		} else {
			if(this.cols[i].sortreverse) {
				this.sortcol = -(i+1);
			} else {
				this.sortcol = i+1;
			}
		}
		this.sort();
		this.makeTable();
	};

	this.sortFunc = function(a,b) {
		var r,col = bt_cols[Math.abs(bt_sortcol)-1];
		if(col.sortFunc) {
			r = col.sortFunc(a.rowdata,b.rowdata,col.field);
		} else {
			r = bcmps(a.rowdata[col.field],b.rowdata[col.field]);
		}
		if(r!==0) {
			return r*bt_sortcol;
		}
		return a.idx-b.idx;
	};

	this.fsortFunc = function(a,b) {
		for(var i=0, l=bt_sortcol.length; i<l; i++) {
			var r,col = bt_cols[Math.abs(bt_sortcol[i])-1];
			if(col.sortFunc) {
				r = col.sortFunc(a.rowdata,b.rowdata,col.field);
			} else {
				r = bcmps(a.rowdata[col.field],b.rowdata[col.field]);
			}
			if(r!==0) {
				return r*bt_sortcol[i];
			}
		}
		return 0;
	};

	this.firstSort = function() {
		var f=[];
		for(var j=0,l=this.cols.length; j<l; j++) {
			if(this.cols[j].fsort) {			
				if(this.cols[j].sortreverse) {
					f.push(-(j+1));
				} else {
					f.push(j+1);
				}
			}
		}
		if(this.defsort) {
			for(var i=0; i<this.defsort.length; i++) {
				for(var j=0,l=this.cols.length; j<l; j++) {
					if(this.cols[j].field == this.defsort[i]) {
						if(this.cols[j].sortreverse) {
							f.push(-(j+1));
						} else {
							f.push(j+1);
						}
					}
				}
			}
		}
		if(f.length === 0){
			f = [this.cols[0].sortreverse?-1:1];
		}
		this.sortcol = f[0];
		bt_sortcol = f;
		bt_cols = this.cols;
		this.rows.sort(this.fsortFunc);
	};

	this.makeIndex = function() {
		for(var i=0,l=this.rows.length;i<l;i++) {
			this.rows[i].idx = i;
		}
	};

	this.firstSort();
	this.makeIndex();

	this.sort = function() {
		bt_sortcol = this.sortcol;
		bt_cols = this.cols;
		this.rows.sort(this.sortFunc);
	};

	this.getHeader = function() {
		var thead = bce('thead');
		var tr = bce('tr');
		for(var i=0, l=this.cols.length; i<l; i++) {
			var C = this.cols[i];
			var th = bce('th');

			if(C.width) { th.width=C.width; }
			if(C.colspan > 1) { th.colSpan=C.colspan; }

			if(C.align) { th.align = C.align; }
			bac(th,bce('h3',{'_className':'headline3'},C.title));

			if(C.sortable) {
				th.style.cursor = 'n-resize';
				bae(th,'click',this.setSort.bind(this,i));
				if(Math.abs(this.sortcol) == i+1) {
					if(this.sortcol < 0) {
						bac(th.firstChild,bce('img',{_src:'/img/style/arrow-up.gif'}));
					} else {
						bac(th.firstChild,bce('img',{_src:'/img/style/arrow-down.gif'}));
					}
				}
			}
			bac(tr,th);
		}
		//bac(thead,colgroup);
		bac(thead,tr);


		return thead;
	};

	this.getTable = function() {
		return bce('table',{'_className' : 'db-item-table','_cellSpacing':0},this.getHeader(),this.getBody());
	};

	this.makeTable = function() {
		/*if(!this.layout) {
			var pdiv1 = bce('div',{_align:'right',padding:'0px 0px 8px 0px'},this.getPager());
			var filterdiv = bce('div', {});
			for(var i=0;i<this.filters.length;i++) {
				this.filters[i].appendControl(filterdiv);
			}
			
			var htable = bce('table',{_width:"100%"});
			bac(htable,bce('tr',{},bce('td',{},filterdiv),bce('td',{},pdiv1)));

			var pdiv2 = bce('div',{_align:'right',padding:'8px 0px 4px 0px'},this.getPager());
			var maintable= this.getTable();
			maintable.id='__maintable';
			brc(this.maindiv,pdiv1,filterdiv,maintable,pdiv2);

			this.layout = {};
			this.layout.pagerdiv1=pdiv1;
			this.layout.maintable=maintable;
			this.layout.pagerdiv2=pdiv2;
		
		} else {
			brc(this.layout.pagerdiv1,this.getPager());
			var maintable = bge('__maintable');
			brc(this.layout.maintable,this.getTable(););
			brc(this.layout.pagerdiv2,this.getPager());
		}*/
	
		if(!this.layout) {
			var filterdiv = bge('__filterdiv');
			for(var i=0;i<this.filters.length;i++) {
				this.filters[i].appendControl(filterdiv);
			}
			this.layout = true;
		}
		
		var pdiv1 = bce('div',{_align:'right',padding:'0px 0px 8px 0px'},this.getPager());
		var pdiv2 = bce('div',{_align:'right',padding:'8px 0px 4px 0px'},this.getPager());
		var maintable= this.getTable();
		brc(this.maindiv,pdiv1,maintable,pdiv2);

	};
}


function BtabsTableDiv(opts) {
	this.div = bge(opts.div);
	this.name = opts.n;
	this.id = opts.id;
	this.counter = opts.counter;

	this.div.style.display = 'none';

	this.getCount = function() {
		if(!this.counter) {
			return 0;
		}
		return this.div.getElementsByTagName(this.counter).length;
	};

	this.makeTable = function() {
		var htable = bce('div',{_align:'right',padding:'0px 0px 4px 0px'});
		var ftable = bce('div',{_align:'right',padding:'0px 0px 4px 0px'});
		this.div.style.display = 'block';
		brc(this.maindiv,htable,this.div,ftable);



	};
}


function Btabs(data) {

	this.init = function() {
		var cont = bge('db-content');
		cont.style.position = 'relative';
		var filterdiv = bce('div',{position:'absolute',top:'7px',left:'10px','_className':'filterdiv'});
		filterdiv.id='__filterdiv';
		bac(cont,filterdiv);
		bac(cont,
			bce('div',{_className:'db-table-box-ro'},
				bce('div',{_className:'db-table-box-lo'},
					bce('div',{_className:'db-table-box-headline'},
						bce('div',{_className:'db-table-box-headline-wrapper', paddingTop:'4px'})))));
		bac(cont,
			bce('div', {_className: 'db-table-box-content'},
				bce('div',null,
					bce('div', {_className: 'db-table-box-content-wrapper',_id: 'db-table-div', padding: '0px 9px',paddingLeft: '7px'}))));
		bac(cont,
			bce('div',{_className:'db-table-box-bottom'},
				bce('div',{_className:'db-table-box-ru'},
					bce('div',{_className:'db-table-box-lu'}))));
		this.maindiv = bge('db-table-div');
		var tab = 0;
		var h = window.location.hash;
		if(h.substring(0,1) == '#') h = h.substring(1);
		for(var i=0, l=this.tables.length; i<l; i++) {
			if(h && h == this.tables[i].id) tab = i;
		}
		this.setTab(tab,1);

	};


	this.makeTabs = function() {
		var div=bge('db-tabs-container');
		if(!div) {
			return;
		}
		var ul = bce('ul');
		var hassubtitle = false;
		for(var i=0, l=this.tables.length; i<l; i++) {
			if(this.tables[i].subtitle) {
				hassubtitle = true;
			}
		}
		for(var i=0, l=this.tables.length; i<l; i++) {
			var li = bce('li',null,bce('a',null,this.tables[i].name));
			var count = this.tables[i].getCount();
			if(count) {
				bac(li.firstChild,bce('span', {fontWeight: 'normal'}, ' ('+count+')'));
			}
			
			if(this.tables[i].subtitle) {
				bac(li.firstChild,bce('br'));
				bac(li.firstChild,bce('small',{fontWeight: 'normal'},bct(this.tables[i].subtitle)));
			} else if(hassubtitle) {
				bac(li.firstChild,bce('br'));
				bac(li.firstChild,bce('small',{fontWeight: 'normal'},bct('\u00a0')));
			}
			if(i == this.currenttab) {
				li.className='current';
				li.style.cursor='default';
			} else {
				li.style.cursor='pointer';
				bae(li,'click',this.setTab.bind(this,i));
			}
			bac(ul,li);
		}
		brc(div,ul);
	};
	
	this.trackClick = function() {
		var d = location.hostname.split('.');
		var get_rand = "d=" + (Math.random()*100000);
		var get_referer = "r=" + escape(document.referrer);
		var countIVW = new Image();
		
		var verz_str;
		switch(d[0]) {			
			default:
			case 'wowdata':
				verz_str = 'wow_verz_tab';
			break;
			case 'romdata':
				verz_str = 'rom_verz_tab';
			break;
			case 'wardata':
				verz_str = 'war_verz_tab';
			break;
		}

		if(d[1] == 'getbuffed') {
			verz_str = 'BFD_USA_tab';
		}

		if(d[1] + '.' + d[2] == 'buffed.ru') {
			verz_str = 'BFD_RU_tab';
		}

		countIVW.src = "http://buffed.ivwbox.de/cgi-bin/ivw/CP/" + verz_str + ";?" + get_referer + "&" + get_rand;
		pageTracker._trackPageview();
	};

	this.setTab = function(num,init) {
		this.currenttab = num;
		this.makeTabs();
		var t = this.tables[this.currenttab];
		t.maindiv = this.maindiv;
		t.makeTable();
		if(!init) {
			this.trackClick();
			window.location.hash = t.id;
		}
	};

	this.tables = [];
	for(var i=0; i<data.length; i++) {
		if(data[i].div) {
			this.tables.push(new BtabsTableDiv(data[i]));
		} else {
			this.tables.push(new BtabsTable(data[i]));
		}
	}
}
