// PHP-compatible urlencode() for Javascript
function urlencode(s) {
	s = encodeURIComponent(s);
	return s.replace(/~/g,'%7E').replace(/%20/g,'+');
}
anchor={ch:'#',sep:'-'};
anchor.set=function(str){
//	var url=self.location.href.replace(/#.+/,'');
//	self.location.href=url+this.ch+str;
}
anchor.get=function(){
//	var urla=self.location.href.replace(/^[^#]+#?/,'');
//	return (urla)? urla.split('-') : [];
}
	
tree = {
	par:0,
	anc:[],
	active:0,
	request:0,
	order:0
}
//#<categoryId>-<productId>-<>
tree.open = function(id,notload) {
	if(!id) return;
	if(this.active)
		dl.el('lnk-' + this.active).className = '';
	if(id &&!notload) //load products list
		this.list(id);
	if(dl.is.el('lnk-'+id)) {
		this.active = id;
		dl.el('lnk-' + this.active).className = 'active';
		var el = dl.el('cnt-' + id);
	} else
		return;
	if(el.style.display == 'block') {
		el.off()
		if(el.parentNode.className == 'open')
			el.parentNode.className = 'close';
	} else {
		el.on();
		if(el.parentNode.className == 'close')
			el.parentNode.className = 'open';
		//attempt to open all parent nodes
		var cel=el.parentNode;
		while(cel.className!='nav') {
		//while(cel.className!='nav') {
			cel.className='open';
			cel=cel.parentNode;	
		}
	}
	return false;//prevents default action
}
tree.list = function(id, search, page, par, order) {
	if(!id && !search && !page && !par && !order)
		return;
	if(id)
		this.open(id,'notload');
	this.par ='time='+dl.gmt();
	if(id && id>0)
		this.par += '&parentId='+id;
	if(search)
		this.par += '&search='+urlencode(search);
	else
		search='';
	if(page && !order)
		this.par+='&page='+page;
	if(order)
		this.order = order;
	this.par+='&order='+this.order;
	if(this.request && this.request.abort)
		this.request.abort();
	if(par)
		this.par = par;
	this.request = new http('products'+dl.gmt(),'/productsList.php?'+this.par).ondata = function() {
		try {
			dl.el('plot').innerHTML = this.req.responseText;
			setTimeout(function(){tree.breadcrumb()},10);
			} catch(e) { alert(e);}
		tree.request = 0;
	}
}
tree.product = function(id, cat) {
//	this.anc[1]=id;
//	anchor.set(this.anc.join('-'));
	if(cat) 
		this.open(cat,'notload');
	var par = '?Id='+id+'&time='+dl.gmt();
	if(this.request && this.request.abort)
		this.request.abort();
	this.request = new http('products'+dl.gmt(),'/productsDetails.php'+par).ondata = function() {
		dl.el('plot').innerHTML = this.req.responseText;
		setTimeout(function(){tree.breadcrumb()},10);
		tree.request = 0;
	}
}
tree.breadcrumb = function(){
	if(dl.is.el('breadcrumb') && dl.is.el('breadcrumbHidden'))
		dl.is.el('breadcrumb').innerHTML = dl.is.el('breadcrumbHidden').innerHTML;
}
