var openedArray = new Array();
var toggledArray = new Array();
var openedTrimsArray = new Array();
var toggledTrimsArray = new Array();


function openClosePackage(parentDiv, openHdrClassName, closeHdrClassName) {
	var contentArea = null;
	var leftContent = null;
	var buttonDiv = getItem(parentDiv.id+'Button');
	
	if (navigator.appName.search("Microsoft") != -1) {
		drawerHeader = parentDiv.childNodes[0];
		dataContainer = parentDiv.childNodes[1];
	} else {
		//Mozilla 5.0 reads tabs as a text object.
		drawerHeader = dataContainer = null;
		for (i = 0; i < parentDiv.childNodes.length; i++) {
			if ( parentDiv.childNodes[i].nodeName == "DIV" ) {
				if (drawerHeader == null) {
					drawerHeader = parentDiv.childNodes[i];
				} else {
					dataContainer = parentDiv.childNodes[i];
				}
			}
		}
	}
	
	heightValue = drawerHeader.offsetHeight + dataContainer.offsetHeight + "px";
	
	if (parentDiv.style.height != heightValue) {
		//Open drawer
		if (openHdrClassName != "") {
			drawerHeader.className = openHdrClassName;
		}
		parentDiv.style.height = heightValue;
		//parentDiv.style.borderBottom = "1px solid #666666";
		dataContainer.style.visibility = "visible";
		if (navigator.appName.search("Microsoft") != -1) {
				dataEntry = dataContainer.childNodes[0];
		} else {
			dataEntry = dataContainer.childNodes[1];
		}
		buttonDiv.innerHTML = upArrow;
		if (openedArray.indexOf([parentDiv, openHdrClassName, closeHdrClassName]) == -1) openedArray.push([parentDiv, openHdrClassName, closeHdrClassName]);
	} else {
		//Close Drawer
		if (closeHdrClassName != "") {
			drawerHeader.className = closeHdrClassName;
		}
		parentDiv.style.height = drawerHeader.offsetHeight + "px";
		dataContainer.style.visibility = "hidden";
		buttonDiv.innerHTML = downArrow;
		
		openedArray.remove([parentDiv, openHdrClassName, closeHdrClassName]);
		
	}
	
	//Reset bg size to fix FF bug	
	var drawerDivId;
	if (getItem('allTabContent') != null) 
		drawerDivId = 'allTabContent'; //on the specs.do for all veh
	else
		drawerDivId = parentDiv.parentNode.parentNode.id; //on the trims.do packages tab for all veh. janky reliance on div structure w/ the parentNode lookup
	
	if ((getItem(drawerDivId).offsetHeight + 30) > 495){
		getItem('individual_vehicle_content').style.height = (getItem(drawerDivId).offsetHeight + 30);
	}
	else{
		getItem('individual_vehicle_content').style.height = 495 + "px";
	}
	
	grandParentDiv = parentDiv.parentNode.parentNode.parentNode;
	grandParentDiv.style.height = parentDiv.parentNode.parentNode.offsetHeight + "px";
}


function toggleOpenedPackages(whichArray) {
	toggledArray = new Array();
	for (var i = 0; i < whichArray.length; i++) {
		openArgs = whichArray[i];
		toggledArray.push(openArgs);
		openClosePackage(openArgs[0],openArgs[1],openArgs[2]);
	}
}


function drawLargeImage(callingDiv, imgTag, accText) {
	if (getItem("bigAccImg")) {
		getItem("accContent").removeChild(getItem("bigAccImg"))
	}
	newLayer = document.createElement('DIV');
	newLayer.id = "bigAccImg";
	newLayer.className = "vehBigAccImg";
	callingDiv.parentNode.insertBefore(newLayer, callingDiv);
	tabDiv = getItem("accessoriesTabOn");
	getItem("bigAccImg").innerHTML = imgTag + "<br/><br/>" + accText;
	if ((newLayer.offsetTop + newLayer.offsetHeight) > (callingDiv.parentNode.offsetHeight)) {
		newLayer.style.top = ((callingDiv.parentNode.offsetHeight) - newLayer.offsetHeight + tabDiv.offsetHeight - getItem('accBottom').offsetHeight) + "px";
	}
	
	//If some of the accessory display content drops below the fold bump the layer up so it all displays
	//but keep it within the display area.
	try {
		newLayerPos = getposOffset(newLayer, "top") + newLayer.offsetHeight;
		windowPos = document.body.clientHeight + document.body.scrollTop;
		newTop = (newLayer.offsetTop - (newLayerPos - windowPos));
		var childItr = 0;
		for (i = 0; i < callingDiv.parentNode.childNodes.length; i++) {
			if (callingDiv.parentNode.childNodes[i].className == "vehTabIntroContent") childItr = i;
		}
		minTop =  callingDiv.parentNode.childNodes[childItr].offsetHeight + 20;

		if ( newLayerPos > windowPos ) {
			newLayer.style.top = (newTop > minTop)?newTop:minTop + "px";
		}
	} catch (e) {}
	
}


function killLargeImage() {
	if (getItem("bigAccImg")) {
		getItem("accContent").removeChild(getItem("bigAccImg"))
	}	
}


function showHideDetails(detailDiv, viewBtn) {
	curDetDiv = getItem(detailDiv);
	curBtnDiv = getItem(viewBtn);
	if (curDetDiv.style.visibility != 'visible') {
		//Show Detail
		if (openedTrimsArray.indexOf([detailDiv, viewBtn]) != -1) openedTrimsArray.push([detailDiv, viewBtn]);
		curDetDiv.style.visibility = "visible";
		curDetDiv.style.position = "relative";
		curBtnDiv.style.position = "absolute";
		curBtnDiv.style.visibility = "hidden";		
		parentDiv = curDetDiv.parentNode;
		//IE Completely sucks
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			for (var itr = 0; itr < parentDiv.parentNode.childNodes.length; itr++) {
				try {
					if (parentDiv.parentNode.childNodes[itr].offsetTop > parentDiv.parentNode.childNodes[itr].childNodes[0].offsetTop ) {
						//IE Bug, the style value has to change or it doesn't refresh
						parentDiv.parentNode.childNodes[itr].childNodes[0].style.top = 1 + "px";
						parentDiv.parentNode.childNodes[itr].childNodes[0].style.top = 0 + "px";
					}
				} catch (e) {  }
			}
		} else {
			parentDiv.style.height = (curDetDiv.offsetTop + curDetDiv.offsetHeight - parentDiv.offsetTop) + "px";
		}
	} else {
		openedTrimsArray.remove([detailDiv, viewBtn]);
		curDetDiv.style.visibility = "hidden";
		curDetDiv.style.position = "absolute";
		curBtnDiv.style.position = "relative";
		curBtnDiv.style.visibility = "visible";		
		parentDiv = curDetDiv.parentNode;
		//IE Completely sucks
		if (navigator.userAgent.indexOf("MSIE") != -1 ){
			for (var itr = 1; itr < parentDiv.parentNode.childNodes.length; itr++) {
				try {
					//if (parentDiv.parentNode.childNodes[itr].offsetTop < parentDiv.parentNode.childNodes[itr].childNodes[0].offsetTop ) {
							parentDiv.parentNode.childNodes[itr].childNodes[0].style.top = 1 + "px";
							parentDiv.parentNode.childNodes[itr].childNodes[0].style.top = 0 + "px";
					//}
				} catch (e) {}
			}
		} else {
			parentDiv.style.height = (curDetDiv.offsetTop - parentDiv.offsetTop) + "px";
		}
		
	}
		selectTab("trims", "undefined", true);
}


function toggleOpenedTrims(whichArray) {
	toggledTrimsArray = new Array();
	for (var i = 0; i < whichArray.length; i++) {
		openArgs = whichArray[i];
		toggledTrimsArray.push(openArgs);
		showHideDetails(openArgs[0],openArgs[1]);
	}
}


function resetTrimHeight(detailDiv) {
	try {
		detailDiv = getItem(detailDiv);
		parentDiv = detailDiv.parentNode;
		heightVal = parentDiv.offsetHeight - detailDiv.offsetHeight;
		parentDiv.style.height = heightVal + "px";
	} catch (e) {
		try {
			setTimeout(detailDiv.id, 100);
		} catch (e) { return false; }
	}
}


