var Global={
	css:{hover:'sel'},
	fix:[],
	order:['ccid2','ccid1'],
	def:{orderby:{val:'createdate'},ordermode:{val:1}}
},R=top.main.window,GT=top;

function showTable(id,ch,ma){
	var f,i,k,t,p=Global.param,leimu=$('#leimu')[0];
	if(GT.id&&GT.id!=id){
		$('#tit'+GT.id)[0].className='nor';
		$('#con'+GT.id+'_'+GT.ch)[0].className='hid';
	}
	t=$('#tit'+id)[0];f=t.className=='act';
	t.className=f?'nor':'act';
	$('#con'+id+'_'+ch)[0].className=f?'hid':'show';
	if(GT.id!=id){
		if(GT.id)f=$('#prokey'+GT.id)[0];
		for(k in p){
			if(f)f[k].value='';
			$('#'+p[k].dom).removeClass();
		}
		GT.id=id;
		GT.ch=ch;
		Global.param=Global.def;
		f=$('#prokey'+GT.id)[0];
		f.orderby.value='createdate';
		f.ordermode.value=1;
		if(ma&&(f=/\[(.+)\]/g.exec(ma))){
			f=f[1].split('][');p=0;
			for(i=0;i<f.length;i++){
				k=f[i].split(',');
				if(k[1]&&k[1].charAt(0)!='{'){
					setParam(k[0],k[1],k[0]+id+'_'+k[1],1);
				}
			}
			if(p)makeUrl();else open(t.href,R.name);
		}else{
			open(t.href,R.name);
		}
		for(k=0; k<leimu.options.length; k++){
			if(leimu.options[k].value == id+','+ch){
				leimu.selectedIndex = k;
				break;
			}
		}
	}
}
function showDefault(id,ch,ma){
	if(!ch||ch=='0'){
		var i,l,p,t;if(t=/(\d+)/.exec(location.hash)){
			id=t[1]+'_';t=$('.hid');
			for(i=0,l=t.length;i<l;i++)
				if(t[i].id.indexOf(id)>=0){
					t=/(\d+)_(\d+)/.exec(t[i].id);
					break;
				}
			if(i==l)return;
			ma=location.hash;
		}else{
			t=$('.hid')[0];if(!t||!(t=/(\d+)_(\d+)/.exec(t.id)))return;
		}
		id=parseInt(t[1]);
		ch=parseInt(t[2]);
	}
	GT.id=0;
	showTable(id,ch,ma);
}
function setParam(key,val,id,no){
	var g=Global,p=g.param,f=$('#prokey'+GT.id)[0];
	if(p[key]&&p[key].val==val)return clearParam(key);
	if(!f[key]){
		f[key]=document.createElement('input');
		f[key].type='hidden';
		f[key].name=key;
		f.appendChild(f[key]);
	}
	f[key].value=val;
	if(p[key])$('#'+p[key].dom).removeClass();
	$('#'+id).addClass(g.css.hover);//Ìí¼ÓCSS
	p[key]={dom:id,val:val};
	if(!no)makeUrl();
}
function setOrder(key,mode){
	var g=Global,p=g.param,f=$('#prokey'+GT.id)[0];
	p.orderby={val:f.orderby.value=key};
	p.ordermode={val:f.ordermode.value=mode};
	makeUrl();
}
function clearParam(key){
	var p=Global.param,f=$('#prokey'+GT.id)[0];
	f[key].value='';
	$('#'+p[key].dom).removeClass();//É¾³ýCSS
	delete p[key];
	makeUrl();
}
function clearAllParam(){
	var key,p=Global.param,f=$('#prokey'+GT.id)[0];
	for(key in p){
		f[key].value='';
		$('#'+p[key].dom).removeClass();
		delete p[key];
	}
	Global.param=Global.def;
	f.orderby.value='createdate';
	f.ordermode.value=1;
	makeUrl();
}
function makeUrl(){
	var k,l,h,w,i=[],x=0,d=R.document,g=Global,p=g.param,f=g.fix,o=g.order,form=$('#prokey'+GT.id)[0];
//	if(l=f.length){x=0;for(k in p){for(i=0;i<l;i++)if(k==f[i]){i=0;break}if(i){x=1;break}}}else x=1;
	for(k=0,l=o.length;k<l;k++){if(p[o[k]])i.push(o[k]+'='+ipack(p[o[k]].val))}
	for(k in p)if(k.substr(0,4)!='ccid'&&k!='ordermode'&&(k!='orderby'||p[k].val!='createdate')){x=1;break}
//	i.sort(function(a,b){return a.substr(0,4)=='ccid'&&b.substr(0,4)=='ccid'?(parseInt(b.substr(4))-parseInt(a.substr(4))):0});
	if(x){//º¬ÓÐ²éÑ¯×Ö¶Î
		form.caid.value=GT.id;
		form.chid.value=GT.ch;
		setTimeout(function(){form.submit()},50);
//		x='search.php?caid='+GT.id+'&'+i.join('&');
//		setTimeout(function(){open(x,R.name)},50);
	}else{
		w=d.documentElement.clientWidth||d.body.clientWidth;
		h=d.documentElement.scrollTop||d.body.scrollTop;
		k=d.createElement('div');
		with(k.style){
			position='absolute';
			border='1px solid #ccc';
			backgroundColor='white';
			lineHeight='150%';
			padding='5px';
			width='100px';
			top=h+20+'px';
			left=w-150+'px';
			zIndex='999';
		}
		k.innerHTML='loading page...';
		d.body.appendChild(k);
		$.ajax({url:base+'tools/ajax.php?action=fetchcnodeurl&urltype=indexurl1&caid='+GT.id+'&'+i.join('&'),complete:function(xml){
			open(xml.responseXML.documentElement.firstChild.nodeValue,R.name);
		}});
	}
}
function decToHex(dec){
	var tmp='0123456789ABCDEF',low=dec%16,high=(dec-low)/16;
	return tmp.charAt(high)+tmp.charAt(low);
}
//URLÌØÊâ×Ö·û
function ipack(s){
	return s.replace(/[?&=+ %]/g,function(a){return '%'+decToHex(a.charCodeAt(0))});
}
