// JavaScript Document

var JRI_TMP = new Array();

function viewConfig(id, model) {

	var tr, td;
  
	var table = document.getElementById('tab' + id);
	var icon = document.getElementById('img' + id);
	var options = document.getElementById('td' + id);

	function expand() {
		for(i = 0; i < JRI[model].length; i++)
			if(JRI[model][i]) {
				tr = table.insertRow(table.rows.length);

				td = tr.insertCell(0);
				td.setAttribute('align', 'left');
				// td.style.setAttribute('borderTop', '1px solid black'); // this doesn't work in Mozilla based browsers
				td.style.borderTop = '1px solid black';
				td.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + JRI[model][i].model;

				td = tr.insertCell(1);
				// td.style.setAttribute('borderTop', '1px solid black'); // this doesn't work in Mozilla based browsers
				td.style.borderTop = '1px solid black';
				td.setAttribute('align', 'left');
				td.innerHTML = JRI[model][i].power_src;

				td = tr.insertCell(2);
				td.setAttribute('colSpan', '3');
				td.setAttribute('align', 'left');
				// td.style.setAttribute('borderTop', '1px solid black'); // this doesn't work in Mozilla based browsers
				td.style.borderTop = '1px solid black';
				td.innerHTML = JRI[model][i].material;

				if(JRI[model][i].options != '') {
				    newRow = table.rows.length;
					tr = table.insertRow(newRow);
					
					td = tr.insertCell(0);
					td.setAttribute('colSpan', '5');
					td.setAttribute('align', 'left');
					td.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
					+ '<a href="#' + id + '_' + i + '" name="' + id + '_' + i + '" onclick="viewCategories(\'' + model + '\', ' + i + ', \'' + id + '\');" style="text-decoration: none" class="links">'
					+ '<img id="img' + id + '_' + i + '" src="images/plus.jpg" height="10px" align="absmiddle" border="1"> Options / Upgrades</a>';
					
					JRI_TMP[model].configurations[i] = new Object();

					JRI_TMP[model].configurations[i].optionsCategoriesExpanded = false;
					
					JRI_TMP[model].configurations[i].optionsCategoriesNumber = 0;

					JRI_TMP[model].configurations[i].optionsCategories =  new Array(); // JRI[model][i].options.length;
					
					for(j = 0; j < JRI[model][i].options.length; j++) // options/upgrades of this config (i) for this model
						for(k = 1; k < JRI_OPTS_CATS.length; k++) // categories of options/upgrades
							for(l = 0; l < JRI_OPTS_CATS[k].options.length; l++) // options/upgrades of the category
								if(JRI[model][i].options[j] == JRI_OPTS_CATS[k].options[l]) {
									if(JRI_TMP[model].configurations[i].optionsCategories[k]) 
										JRI_TMP[model].configurations[i].optionsCategories[k].options += ',' + JRI_OPTS_CATS[k].options[l];
									else {
										JRI_TMP[model].configurations[i].optionsCategories[k] = new Object();
										JRI_TMP[model].configurations[i].optionsCategoriesNumber++;
										JRI_TMP[model].configurations[i].optionsCategories[k].category = k;
										JRI_TMP[model].configurations[i].optionsCategories[k].optionsExpanded = false;
										JRI_TMP[model].configurations[i].optionsCategories[k].options = '' + JRI_OPTS_CATS[k].options[l];
									}
									k = JRI_OPTS_CATS.length; // categoryFound = true; // the category to where the option (j) belongs is found, so stop looping through categories 
									break; // also stop looping through options/upgrades in the category
								}
				}
			} 

		JRI_TMP[model].expanded = true;	
		icon.src = 'images/minus.jpg';
		options.innerHTML = '&nbsp;';
	}

	function collaps() {
		for(i = table.rows.length - 1; i > 0; i--)
			table.deleteRow(i);

		JRI_TMP[model].expanded = false;
		icon.src = 'images/plus.jpg';
		options.innerHTML = 'Options';
	}
	
	if(JRI_TMP[model]) {
		if(JRI_TMP[model].expanded)	collaps();
		else expand();
	}
	else {
		JRI_TMP[model] = new Object();
		JRI_TMP[model].configurations = new Array();
		expand();
	}
}

function viewCategories(model, configuration, id) {

	var table = document.getElementById('tab' + id);
	var icon = document.getElementById('img' + id + '_' + configuration);

	function getOffset(configuration) {
		l = 0;	
		for(k = 1; k < configuration; k++) 
			if(JRI_TMP[model].configurations[k].optionsCategoriesExpanded) { // see if categories expanded
				l += JRI_TMP[model].configurations[k].optionsCategoriesNumber;
				for(m = 1; m < JRI_TMP[model].configurations[k].optionsCategories.length; m++) // if categories expanded see if options in each category are expanded
					if(JRI_TMP[model].configurations[k].optionsCategories[m] && JRI_TMP[model].configurations[k].optionsCategories[m].optionsExpanded)
						l += JRI_TMP[model].configurations[k].optionsCategories[m].options.split(',').length;
			}
		
		return l;
	}

	function expand() {
	
		j = 0;
		for(i = 0; i < JRI_TMP[model].configurations[configuration].optionsCategories.length; i++)
			if(JRI_TMP[model].configurations[configuration].optionsCategories[i]) {
				offset = getOffset(configuration);
				newRow = configuration * 2 + 1 + j++ + offset;

				tr = table.insertRow(newRow);
			
				td = tr.insertCell(0);
				td.setAttribute('colSpan', '5');
				td.setAttribute('align', 'left');
				td.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
				+ '<a href="#' + id + '_' + configuration + '_' + i + '" name="' + id + '_' + i + '" onclick="viewOptions(\'' + model + '\', ' + configuration + ', \'' + i + '\', \'' + id + '\');" style="text-decoration: none" class="links">'
				+ '<img id="img' + id + '_' + configuration + '_' + i + '" src="images/plus.jpg" height="10px" align="absmiddle" border="1"> ' + JRI_OPTS_CATS[JRI_TMP[model].configurations[configuration].optionsCategories[i].category].name + '</a>';
			}
		
		JRI_TMP[model].configurations[configuration].optionsCategoriesExpanded = true;
		icon.src = 'images/minus.jpg';
	}

	function collaps() {
	
		offset1 = configuration * 2 + getOffset(configuration + 1);
		offset2 = configuration * 2 + getOffset(configuration);
		
		for(i = offset1; i > offset2; i--)
			table.deleteRow(i);
		
		for(i = 1; i < JRI_TMP[model].configurations[configuration].optionsCategories.length; i++) 
			if(JRI_TMP[model].configurations[configuration].optionsCategories[i])
				JRI_TMP[model].configurations[configuration].optionsCategories[i].optionsExpanded = false;

		JRI_TMP[model].configurations[configuration].optionsCategoriesExpanded = false;
		icon.src = 'images/plus.jpg';
	}
	
	if(JRI_TMP[model].configurations[configuration].optionsCategoriesExpanded) collaps();
	else expand();
}

function viewOptions(model, configuration, category, id) {

	var table = document.getElementById('tab' + id);
	var icon = document.getElementById('img' + id + '_' + configuration + '_' + category);
	var options = JRI_TMP[model].configurations[configuration].optionsCategories[category].options.split(',');

	function getOffset() {
		l = 0;	
		for(k = 1; k < configuration; k++) 
			if(JRI_TMP[model].configurations[k].optionsCategoriesExpanded) { // see if categories expanded
				l += JRI_TMP[model].configurations[k].optionsCategoriesNumber;
				for(m = 1; m < JRI_TMP[model].configurations[k].optionsCategories.length; m++) // if categories expanded see if options in each category are expanded
					if(JRI_TMP[model].configurations[k].optionsCategories[m] && JRI_TMP[model].configurations[k].optionsCategories[m].optionsExpanded)
						l += JRI_TMP[model].configurations[k].optionsCategories[m].options.split(',').length;
			}
			
		for(k = 1; k < category; k++)
			if(JRI_TMP[model].configurations[configuration].optionsCategories[k]) {
				l++;
				if(JRI_TMP[model].configurations[configuration].optionsCategories[k].optionsExpanded)
					l += JRI_TMP[model].configurations[configuration].optionsCategories[k].options.split(',').length;
			}
		
		return l;
	}

	function expand() {
		for(i = 0; i < options.length; i++) {
			offset = getOffset();
			newRow = (configuration + 1) * 2 + i + offset;

			tr = table.insertRow(newRow);
			
			td = tr.insertCell(0);
			td.setAttribute('align', 'left');
			td.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
			+ JRI_OPTS[options[i]].model;
			
			td = tr.insertCell(1);
			td.setAttribute('colSpan', '4');
			td.setAttribute('align', 'left');
			td.innerHTML = JRI_OPTS[options[i]].name;
		}
		
		JRI_TMP[model].configurations[configuration].optionsCategories[category].optionsExpanded = true;
		icon.src = 'images/minus.jpg';
	}

	function collaps() {
		for(i = options.length; i > 0; i--) {
			offset = getOffset();
			row = configuration * 2 + 1 + i + offset;
			table.deleteRow(row);
		}

		JRI_TMP[model].configurations[configuration].optionsCategories[category].optionsExpanded = false;
		icon.src = 'images/plus.jpg';
	}
	
	if(JRI_TMP[model].configurations[configuration].optionsCategories[category].optionsExpanded) collaps();
	else expand();
}
