alreadyCleared = [];

function cleartb(s)
{
	d = document.getElementById(s);
	if(!alreadyCleared[s]) {
		alreadyCleared[s] = true;
		d.value='';
	}
	
}

// window.location='<?=siteroot;?>/brand-' + document.getElementById('brandsel').options[document.getElementById('brandsel').selectedIndex].value + '/<?=$c->toCompleteURL();?>'/viewcategory;

function expandMenu(id)
{
	document.getElementById("menuexpand_" + id).style.display = "none";
	document.getElementById("menucollapse_" + id).style.display = "";
	
	outer = document.getElementById("menubodyouter_" + id);
	inner = document.getElementById("menubodyinner_" + id);
	
//	alert("id is " + id + " and current height " + outer.offsetHeight + " and target height is " + inner.offsetHeight);
	doResizeMenu(id, outer.offsetHeight, inner.offsetHeight, 10);
}

var menutimer = -1;

function doResizeMenu(id, originalHeight, targetHeight, amount)
{
	if(menutimer>-1) {
		clearTimeout(menutimer);
		menutimer = -1;
	}
//	alert("targetHeight is " + targetHeight);
	outer = document.getElementById("menubodyouter_" + id);
	
	newheight = outer.offsetHeight;
	heightfinished = false;
	if(newheight>targetHeight-amount && newheight<targetHeight+amount) {
		outer.style.height = targetHeight + "px";
//		alert("finito, targetHeight now " + targetHeight);
		if(newheight>targetHeight) {
		
		}
		return;
	} else if(newheight < targetHeight) {
		newheight += amount;
	} else if(newheight > targetHeight) {
		newheight -= amount;
	}


	newamount = amount + 10;
	origdiff = targetHeight - originalHeight;
	curdiff = targetHeight - newheight;
	if(targetHeight<newheight && curdiff>(origdiff/2)
	|| targetHeight>newheight && curdiff<(origdiff/2)) {
		newamount = amount - 10;
		if(newamount<1) newamount = 10;
	}
	
	
	outer.style.height = newheight + "px";
	
	menutimer = setTimeout("doResizeMenu(" + id + ", " + originalHeight + ", " + targetHeight + ", " + newamount + ")", 30);
}

function collapseMenu(id)
{
	document.getElementById("menuexpand_" + id).style.display = "";
	document.getElementById("menucollapse_" + id).style.display = "none";
	
	outer = document.getElementById("menubodyouter_" + id);
	inner = document.getElementById("menubodyinner_" + id);
	
//	alert("id is " + id + " and current height " + outer.offsetHeight + " and target height is " + inner.offsetHeight);
	doResizeMenu(id, outer.offsetHeight, 0, 10);
}



var timeouts = [];

function subcategoryOver(i)
{
	clearTimeout(timeouts[i]);
	timeouts[i] = setTimeout("doSubcategoryExpand(" + i + ",1)", 100);
}


function doSubcategoryExpand(i, dir)
{
	clearTimeout(timeouts[i]);
	
	d = document.getElementById("subcategorydiv_" + i);
	ul = document.getElementById("subcategoryul_" + i);

	h = d.offsetHeight;
	
	if(d.offsetHeight > (ul.offsetHeight/2)) {
		rate = 3 + Math.max((ul.offsetHeight - d.offsetHeight)/2, 1);
	} else {
		rate = Math.max( 5 + d.offsetHeight, 1);
	}
	
	rate = ul.offsetHeight/10;
	
	h += rate * dir;
	
	if(h>ul.offsetHeight) {
		h = ul.offsetHeight;
	} else if(h<0) {
		h = 0;
	} else {
		timeouts[i] = setTimeout("doSubcategoryExpand(" + i + "," + dir + ")", 100);
	}

	d.style.height = h + "px";
}

function subcategoryOut(i)
{
	clearTimeout(timeouts[i]);
	timeouts[i] = setTimeout("doSubcategoryExpand(" + i + ",-1)", 100);
/*	clearTimeout(timeouts[i]);
	d = document.getElementById("subcategorydiv_" + i);
	d.style.height = "0px";*/
}

