if (!document.getElementById) {
    alert("Your browser does not have the necessary Javascript support; you may experience limited functionality on this page.");
}


/********************* Group Functions ****************************/
function selectGroup(groupName) {
	selectGroupLink(groupName); 
	// collapse all currently expanded Equipment lists
	hideAllEquipmentLists(); 
	// and expand the equipment list for this group
	showBlockElement('equipList_'+groupName);

	loseFocus();
}

function selectGroupLink(groupName) {
	var groupLinkID = 'groupLink_'+groupName;

	// first, set the current selection (if there is one) back to normal
	if (selectedGroup && selectedGroup != "") {
		selectedGroup.className='groupLink';
	}

	// now set the selected group link
	if (document.getElementById) {
		var groupLink = document.getElementById(groupLinkID);
		if (groupLink) {
			groupLink.className = "groupLinkOn";
			selectedGroup = groupLink;

			// and deselect any equipment that is currently selected
			deselectEquipmentLink();
		}

		// make sure the fleet view description is shown again
		var fleetDescBlock = document.getElementById('fleetDescBlock');
		if (fleetDescBlock && fleetDescBlock.style.display == "none") {
			showBlockElement('fleetDescBlock');
			// ..and make sure that the variable description block is hidden
			hideBlockElement('variableDescBlock');
		}

		// and load the group image
		showGroupImage(groupName);

		// and finally, make sure there are no Vehicle View lists showing
		hideAllVehicleViewLists();
	}
}

function deselectGroupLink() {
	// set the current selection (if there is one) back to normal
	if (selectedGroup && selectedGroup != "") {
		selectedGroup.className='groupLink';
		// and now clear out what is selected
		selectedGroup = null;
	}
}

function showGroupImage(groupName) {
	if (document.getElementById) {
		var theImage = document.getElementById('theImage');
		if (theImage) {
			// hide the image while we tweak it, so we don't see any weirdness
			theImage.style.visibility = "hidden";

			theImage.src = imageViewArray[groupName]['src'];
			theImage.style.width = imageViewArray[groupName]['width']+"px";
			theImage.style.height = imageViewArray[groupName]['height']+"px";
			theImage.alt = imageViewArray[groupName]['title'];
			theImage.title = imageViewArray[groupName]['title'];

			// all done, show the image again
			theImage.style.visibility = "visible";
		}
	}
}

/********************* Equipment Functions ****************************/
function selectEquipment(groupName, equipmentID) {
	selectEquipmentLink('equipmentDetailBlock_'+groupName+'_'+equipmentID);	
	
	// and display the details
	showEquipmentDetails(groupName, equipmentID);

	loseFocus();
}

function showEquipmentDetails(groupName, equipmentID) {
	if (document.getElementById) {
		// make sure the fleet view description block is hidden...
		var fleetDescBlock = document.getElementById('fleetDescBlock');
		if (fleetDescBlock && fleetDescBlock.style.display == "block") {
			hideBlockElement('fleetDescBlock');
			// ..and make sure that the variable description block is shown
			showBlockElement('variableDescBlock');
		}

		// get handles to the elements we care about
		var descriptionBlock = document.getElementById('descriptionBlock');
		var seatingBlock = document.getElementById('seatingBlock');
		var amenitiesBlock = document.getElementById('amenitiesBlock');
		var equipmentDetailArray = totalEquipmentArray[groupName+"_"+equipmentID];

		// update the Description display
		if (descriptionBlock) {			
			descriptionBlock.innerHTML = equipmentDetailArray['description'];
		}

		// update the Seating display
		if (seatingBlock) {			
			seatingBlock.innerHTML = equipmentDetailArray['seating'];
		}
		// update the Amenities display
		if (amenitiesBlock) {			
			amenitiesBlock.innerHTML = equipmentDetailArray['amenities'];
		}

		// and show the correct Vehicle View list
		hideAllVehicleViewLists();
		showVehicleViewList(groupName, equipmentID);
	}
}

function hideAllEquipmentLists() {
	// get all the elements we need
	var groupLists = getElementsByClassName('groupedEquipmentList');
	// now hide each one
	for (var i = 0; i < groupLists.length; i++) {
		// only hide those that are currently shown
		if (groupLists[i].style.display == "block") {
			hideBlockElement(groupLists[i].id);
		}		
	}
}

function selectEquipmentLink(equipmentLinkID) {
	// first, set the current selection (if there is one) back to normal
	if (selectedEquipment && selectedEquipment != "") {
		selectedEquipment.className='equipmentDetailBlock';
	}

	// also, deselect the currently selected group
	deselectGroupLink();

	// now set the selected group link
	if (document.getElementById) {
		var equipmentLinkBlock = document.getElementById(equipmentLinkID);
		if (equipmentLinkBlock) {
			equipmentLinkBlock.className = "equipmentDetailBlockOn";
			selectedEquipment = equipmentLinkBlock;			
		}
	}
}

function deselectEquipmentLink() {
	// first, set the current selection (if there is one) back to normal
	if (selectedEquipment && selectedEquipment != "") {
		selectedEquipment.className='equipmentDetailBlock';
		// and clear the selection
		selectedEquipment = null;
	}
}

/********************* Vehicle View Functions ****************************/
function selectView(equipmentKey, viewIndex) {
	// hide all currently shown image lists
	hideAllImageLists();

	selectViewLink(equipmentKey, viewIndex); 

	loseFocus();
}

function selectViewLink(equipmentKey, viewIndex) {
	var viewLinkID = 'viewLink_'+equipmentKey+'_'+viewIndex;

	// first, set the current selection (if there is one) back to normal
	if (selectedView && selectedView != "") {
		selectedView.className='viewLink';
	}

	// now set the selected view link
	if (document.getElementById) {
		var viewLink = document.getElementById(viewLinkID);
		if (viewLink) {
			//viewLink.className = "viewLinkOn";
			selectedView = viewLink;
			// show the list of images for this view
			showBlockElement('imageList_'+equipmentKey+'_'+viewIndex);
			// and select the first image in that view
			selectImage(equipmentKey, viewIndex, "0");
		}
	}
}

function hideVehicleViewList(groupName, equipmentID) {	
    if (document.getElementById) {		
        var theViewListing = document.getElementById("viewList_"+groupName+"_"+equipmentID);
        if (theViewListing) {
			hideBlockElement("viewList_"+groupName+"_"+equipmentID);
        }
    }
}

function showVehicleViewList(groupName, equipmentID) {	
    if (document.getElementById) {		
        var theViewListing = document.getElementById("viewList_"+groupName+"_"+equipmentID);
        if (theViewListing) {
			showBlockElement("viewList_"+groupName+"_"+equipmentID);
			// and select the first view in the list
			selectView(groupName+'_'+equipmentID, '0');
        }
    }
}

function hideAllVehicleViewLists() {
	// get all the elements we need
	var viewLists = getElementsByClassName('viewList');
	// now hide each one
	for (var i = 0; i < viewLists.length; i++) {
		// only hide those that are currently shown
		if (viewLists[i].style.display == "block") {
			hideBlockElement(viewLists[i].id);
		}		
	}
}

function hideAllImageLists() {
	// get all the elements we need
	var viewLists = getElementsByClassName('imageList');
	// now hide each one
	for (var i = 0; i < viewLists.length; i++) {
		// only hide those that are currently shown
		if (viewLists[i].style.display == "block") {
			hideBlockElement(viewLists[i].id);
		}		
	}
}

function selectImage(equipmentKey, viewIndex, imageIndex) {
	selectImageLink(equipmentKey, viewIndex, imageIndex);	
	
	// and display the image
	showImage(equipmentKey, viewIndex, imageIndex);

	loseFocus();
}

function showImage(equipmentKey, viewIndex, imageIndex) {
	if (document.getElementById) {
		var theImage = document.getElementById('theImage');
		if (theImage) {
			// hide the image while we tweak it, so we don't see any weirdness
			theImage.style.visibility = "hidden";

			theImage.src = imageViewArray[equipmentKey][viewIndex][imageIndex]['src'];
			theImage.style.width = imageViewArray[equipmentKey][viewIndex][imageIndex]['width']+"px";
			theImage.style.height = imageViewArray[equipmentKey][viewIndex][imageIndex]['height']+"px";
			theImage.alt = imageViewArray[equipmentKey][viewIndex][imageIndex]['title'];
			theImage.title = imageViewArray[equipmentKey][viewIndex][imageIndex]['title'];

			// all done, show the image again
			theImage.style.visibility = "visible";
		}
	}
}

function selectImageLink(equipmentKey, viewIndex, imageIndex) {
	// first, set the current selection (if there is one) back to normal
	if (selectedImage && selectedImage != "") {
		selectedImage.className='imageDetailBlock';
	}
	// now set the selected image link
	if (document.getElementById) {
		var imageLinkBlock = document.getElementById('imageLinkBlock_'+equipmentKey+'_'+viewIndex+'_'+imageIndex);
		if (imageLinkBlock) {
			imageLinkBlock.className = "imageDetailBlockOn";
			selectedImage = imageLinkBlock;			
		}
	}
}


/********************* Base Functions ****************************/

/****************************************
This is sort of a kludge.  Basically,
our links are getting the dopey little 'link border'
surrounding them after they're clicked, and I don't
like it.  So I'm setting the focus to a fake element,
to remove the link border effect.
****************************************/
function loseFocus() {
	if (document.getElementById) {
		var theElement = document.getElementById("focusHolder");
		if (theElement) {
			theElement.focus();
		}
	}
}

function getElementsByClassName(className) {
	var retnode = [];
	var myclass = new RegExp('\\b'+className+'\\b');
	var elem = document.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}

function showBlockElement(theElementID) {
	if (document.getElementById(theElementID)) {
		var theElement = document.getElementById(theElementID);
		if (theElement) {
			theElement.style.display = "block";
		}		
	}

}

function hideBlockElement(theElementID) {
	if (document.getElementById(theElementID)) {
		var theElement = document.getElementById(theElementID);
		if (theElement) {
			theElement.style.display = "none";
		}		
	}

}
