var curType = '';
var _labelData, _productData, _typeData;
function specSelect(type, item)
{
	if(!_labelData)
		loadData();

	curType = type;
	var root = document.getElementById('Specifications');
	var content = getChildByClass(root, 'spec_content');
	var types = getChildByClass(content, 'types');

	if(item)
	{
		// Clear highlights
		var children = types.childNodes;
		for(i=0; i<children.length; i++)
		{
			if(children[i].className && children[i].className != 'typehead')
			{
				children[i].className = 'type';
			}
		}
		
		// Highlight link
		item.className = 'type selected';	
	}
	var subcontent = document.createElement('div');
	removeAllButTypes(content);
	//content.appendChild(types);
	subcontent.innerHTML = buildSubContent(type);
	content.appendChild(subcontent);

	
/*	// Hide bad products and show correct ones, show correct label
	var defaultLabel;
	var showDefaultLabel = true;
	children = content.childNodes;
	for(i=0; i<children.length; i++)
	{
		if(children[i].className && children[i].className == 'product')
		{
			children[i].style.display = 'none';
		}
		if(children[i].getAttribute && children[i].getAttribute('value') == type)
		{
			children[i].style.display = 'block';
		}
		if(children[i].className && children[i].className == 'labels')
		{
			children[i].style.display = 'none';
			if(children[i].getAttribute && children[i].getAttribute('value') == null)
			{
				defaultLabel = children[i];
			}
			else if(children[i].getAttribute && children[i].getAttribute('value') == type)
			{
				children[i].style.display = 'block';
				showDefaultLabel = false;
			}
		}
	}
	
	if(showDefaultLabel && defaultLabel)
	{
		defaultLabel.style.display = 'block';
	}*/
}

function removeAllButTypes(root)
{
	var i = 0;
	while (root.childNodes[i])
	{
		if(root.childNodes[i].className == 'types')
			i++;
		else
			root.removeChild(root.childNodes[i]);
	}
}

function loadData()
{	
	_productData = new Array();
	var root = document.getElementById('Specifications');
	var content = getChildByClass(root, 'spec_content');
	var children = content.childNodes;
	var prdcnt = 0;
	for(j=0; j<children.length; j++)
	{
		if(children[j].className && children[j].className == 'product')
		{
			_productData[prdcnt] = loadNodeData(children[j]);
			prdcnt++;
		}
		if(children[j].className && children[j].className == 'labels')
		{
			_labelData = loadNodeData(children[j]);
		}
	}
}

function loadNodeData(root)
{
	var arr = new Array();
	arr[0] = root.getAttribute('value');
	var sub = root.getElementsByTagName('div')
	for(k = 0; k<sub.length; k++)
	{
		arr[k+1] = sub[k].innerHTML;
	}
	
	return arr;
}

function buildSubContent(type)
{
	// Select appropriate rows
	var outProduct = new Array();
	var cnt = 0;
	for(var x = 0; x<_productData.length; x++)
	{
		if(_productData[x][0] == type)
		{
			outProduct[cnt] = _productData[x];
			cnt++;
		}
	}
	
	if(outProduct.length == 0)
		return 'Content error.  No available content.';
		
	var output = '<table cellspacing=0>';
	var row, hascontent,tag='head';
	for(x = 1; x<_labelData.length; x++)
	{
		row = '';
		hascontent = false;
		for(y = 0; y<outProduct.length; y++)
		{
			if(trim(outProduct[y][x]) != '')
				hascontent = true;
			row += '<td class="productdetail'+tag+'">' + trim(outProduct[y][x]) + '</td>';
		}
		if(hascontent)
		{
			output += '<tr><td class="label'+tag+'">' + _labelData[x] + '</td>' + row + '</tr>\n';
			tag = updateTag(tag);
		}
	}
	output += '</table>';
	
	return output;
}

function trim(str) { if(str=='***') return ' '; return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }

function updateTag(tag)
{
	if(tag == 'head')
		return 'alt';
	if(tag == '')
		return 'alt';
	return '';
}

function setType(type)
{
	var root = document.getElementById('Specifications');
	var content = getChildByClass(root, 'spec_content');
	var types = getChildByClass(content, 'types');
	// Clear highlights
	var children = types.childNodes;
	for(i=0; i<children.length; i++)
	{
		if(children[i].className && children[i].className != 'typehead')
		{
			children[i].className = 'type';
		}
		if(children[i].getAttribute && children[i].getAttribute('value') == type)
		{
			children[i].className = 'type selected';
		}
	}

	specSelect(type);
}

function getChildByClass(root, c)
{
	var children = root.childNodes;
	for(i=0; i<children.length; i++)
	{
		if(children[i].className == c)
		{
			return children[i];
		}
	}
}

function closeSpecs()
{
	document.getElementById('Specifications').style.display = 'none';
}
	
function openSpecs(type, src)
{
	var specsDiv = document.getElementById('Specifications');
	if(specsDiv.style.display != '' || type != curType)
	{
		setType(type);
		specsDiv.style.display = '';
		xTop(specsDiv,findPosY(document.getElementById('col2')));
		xLeft(specsDiv,findPosX(document.getElementById('col2'))-43);
		if(src)
		{
			//xTop(specsDiv,findPosY(getPreviousSibling(src.parentNode))-xHeight(specsDiv));
			xTop(specsDiv,xScrollTop() + (xClientHeight() - specsDiv.offsetHeight) / 2);
		}
	}
	else
	{
		specsDiv.style.display = 'none';
	}
}

function getPreviousSibling(node)
{
	var prev = node.previousSibling
	while(node.previousSibling && prev.nodeType == 3)
		prev = prev.previousSibling;
	return prev;
}

closeSpecs();

function printSpecs()
{
	var printContent = document.getElementById('Specifications');
	var printWindow = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
	printWindow.document.write('<link rel="stylesheet" href="assets/base/style/printspecs.css" type="text/css"></link>');
	printWindow.document.write('<div id="Specifications">');
	printWindow.document.write(printContent.innerHTML);
	printWindow.document.write('</div>');
	printWindow.document.close();
	printWindow.focus();
	printWindow.print();
	printWindow.close();
}

