dojo.require("dojo.html");
dojo.require("dojo.fx.html");

wipeShow2 = function(nodeId, callback) {
	var node = dojo.byId(nodeId);

	// Calculate the expanded height
	var overflow = dojo.html.getStyle(node, "overflow");
	node.style.overflow = "hidden";
	node.style.height = "0px";
	dojo.html.show(node);
	var height = node.scrollHeight;

	sinWipe(nodeId, 0, height, getDx(height), 0, function() { 
													node.style.overflow = overflow;
													node.style.height = "auto";
													if(callback) { callback(node); }
												});
}

wipeHide2 = function(nodeId, callback) {
	var node = dojo.byId(nodeId);
	var height = node.offsetHeight;
	var overflow = dojo.html.getStyle(node, "overflow");
	node.style.overflow = "hidden";
	
	sinWipe(nodeId, height, 0, getDx(height), 0, function() {if(callback) { callback(node); }});
}

getDx = function(height) {
	var steps = Math.round(height / 45); // duration / 5.0;
//	steps = (steps > 15 ? 15 : (steps < 7 ? 7 : steps));
	steps = 4;
	var dx = Math.PI / steps;
	return dx;
}

pi = Math.PI;
sinWipe = function(nodeId, startHeight, endHeight, dx, xVal, cb)
{
	var node = dojo.byId(nodeId);
	
	xVal += dx;
	if(xVal > pi) xVal = pi;
	var dh =  (endHeight-startHeight)*(1.0-Math.pow((1.0+Math.cos(xVal))/2.0, 2));
	var newH = startHeight + dh;

	newH = Math.round(newH > endHeight && endHeight > startHeight ? endHeight : (newH < endHeight && endHeight < startHeight ? startHeight : newH));
	newH = (newH > 0 ? newH : 0);
	node.style.height = newH + 'px';
	if(endHeight == 0 && newH == 0)
	{
		node.style.display = 'none';
	}
	
	if(xVal == pi) {
		if(cb) window.setTimeout(cb, 75);
		return;
	}

	window.setTimeout(function() { sinWipe(nodeId, startHeight, endHeight, dx, xVal, cb);}, 75);
}

function hideInlineSpecs(elem, url, setEvent) {
	// Fix up the open link to do nothing while we close
	var linkElem = dojo.byId(elem + 'link');
	//linkElem.onclick = function() { return false;};

	/*var openArrow = dojo.byId(elem + 'openarrow');
	openArrow.style.display = 'none';
	var closedArrow = dojo.byId(elem + 'closedarrow');
	closedArrow.style.display = 'inline';*/
	
	wipeHide2(elem,function() { if (setEvent) { linkElem.onclick = function() {showInlineSpecs(elem, url); return false;}};});
}

function showInlineSpecs(elem, url, setEvent) {

	// Fix up the open link to do nothing while we open
	var linkElem = dojo.byId(elem + 'link');
	//linkElem.onclick = function() { return false;};
	
	/*var openArrow = dojo.byId(elem + 'openarrow');
	openArrow.style.display = 'inline';
	var closedArrow = dojo.byId(elem + 'closedarrow');
	closedArrow.style.display = 'none';*/
	
	wipeShow2(elem, function() { if (setEvent) { linkElem.onclick = function() { hideInlineSpecs(elem, url); return false;}};});

}

function fetchAndShowInlineSpecs(url, elem, setEvent) {
	//var loadingElem = dojo.byId(elem + 'loading');
	//var loadingTimer = window.setTimeout(function() { loadingElem.style.display = 'inline'; }, 850); 
	dojo.io.bind(
		{
			url:	url,
			load:	function(type, data) { 
											//window.clearTimeout(loadingTimer);
											//loadingElem.style.display='none';
											if(data == "") window.history.go(0);
											data = data.replace(/\+/g," ");
											data = unescape(data);							
											/*var closeIconElem = dojo.byId('specsCloseIcon');
											var closeElem = dojo.byId('specsCloseText');
											var closelink = '<A href="#" onclick="hideInlineSpecs(\'' + elem + '\', \'' + url + '\'); return false;">' + 
														closeIconElem.innerHTML + ' ' + closeElem.innerHTML + '</A>';
											*/
											var specElem = dojo.byId(elem);											
											specElem.innerHTML = data;
											showInlineSpecs(elem, url, setEvent); },
			error:	function(type, error) { 							
											//loadingElem.style.display='none';
											window.history.go(0); },
			mimetype: 'text/plain'
		}
	);
}
