lastTabVisibleId = "advantage";
function changeFeatureSubTab(tabId) {

	var listWrapper = document.getElementById('pCategories');
	var liArr = listWrapper.getElementsByTagName('li');

	// update tab menu
	for (var i = 0; i < liArr.length; i++) {
		if(liArr[i].id==('pCategory'+tabId)) {
			document.getElementById(liArr[i].id).className = "active";
		} else {
			document.getElementById(liArr[i].id).className = "inactive";
		}
	}
    //css function for removing and add css class would not work in IE6 if css class has "classname1 classname 2) format
	// update body content
	var contentArr = util.getElementsByClassName('pSection', document.body);
	for (var i = 0; i < contentArr.length; i++) {
		//css.removeClassName(contentArr[i], 'active');
		contentArr[i].className="pSection";
	}

	//css.addClassName(document.getElementById('pSection' + tabId),'active');

	document.getElementById('pSection' + tabId).className="pSection active";

	clickTrackCC(tabId);
}

var comparisonVehicles = "";
function addComparisonVehicles(trimID, count) {
	var vehicle = "" + trimID;
	if (count > 0) {
		vehicle = "~" + trimID;
	}
	comparisonVehicles += vehicle;
}

function doRemoveVehicle(trimID, action, newTrimId) {
	if ("" == newTrimId) {
		if (comparisonVehicles.contains("~" + trimID)) {
			comparisonVehicles = comparisonVehicles.replace("~" + trimID, "");
		} else {
			comparisonVehicles = comparisonVehicles.replace(trimID, "");
		}
	} else {
		comparisonVehicles = comparisonVehicles.replace(trimID, newTrimId);
	}
	if (comparisonVehicles.indexOf("~") < 1) {
		comparisonVehicles = comparisonVehicles.replace("~", "");
	}
	if ("" == comparisonVehicles) {
		comparisonVehicles = "NONE";
	}
	mrm.$("#comparisonVehiclesAddRemove").val(comparisonVehicles);
	document.polkComparisonActionForm.action = window.location.pathname;
	document.polkComparisonActionForm.submit();
}

var titleReplace = "Replace";
var titleAdd = "Add";
var linkAdd = "Add";
var linkReplace = "Replace";
var addReplaceTextTitle = 'with (please make selections below):';
function showAddRemoveVehicle(toShowId, toHideId, type, trimName) {
	var anchorObj = mrm.$("#addvehicle");
	var addReplaceTitle = mrm.$("#addReplaceTitle");

	mrm.$("#" + toHideId).toggleClass("hide");
	mrm.$("#" + toShowId).toggleClass("hide");
	anchorObj.attr('class','');
	if ("replace" != type) {
		carToReplace = "";
		anchorObj.attr("title", titleAdd);
		anchorObj.addClass("add");
		anchorObj.val(linkAdd);
		addReplaceTitle.innerHTML = linkAdd + " " + addReplaceTextTitle;
	} else {
		anchorObj.attr("title", titleReplace);
		anchorObj.addClass("replace");
		anchorObj.val(linkReplace);
		addReplaceTitle.innerHTML = linkReplace + " " + trimName + " " + addReplaceTextTitle;
	}
	
	//Resets polkComparisonActionForm select boxes.
	var selectBox = mrm.$('.addRemoveVehiclePanelBox select').toArray();
	for(var i = 0;i < selectBox.length;i++) {
		mrm.$(selectBox[i][0]).attr('selected','selected');
		if(i >= 1) {
			mrm.$(selectBox[i][0]).nextAll().remove();
			selectBox[i].setAttribute('disabled','');
		}
	}
	
	
}

function clickTrackCC(myParam) {
	var myObj = {
		linkTrackVars: "pageName,prop27,eVar31,prop32,pe,pev2",
		pageName: "ca:no:us:en:index:vehicles:compare vehicles:features",
		prop27: "contentarea",
		eVar31: "usa",
		prop32: "cadillac:us:compare-features:" + Omniture_s.prop2 + ":" + myParam,
		pe: "lnk o",
		pev2: "cadillac:us:compare vehicles:features"
	};
	clickTrack(myObj);
}

var carToReplace = "";
function showReplace(trimId, toShowId, toHideId, trimName) {
	carToReplace = trimId;
	showAddRemoveVehicle(toShowId, toHideId, "replace", trimName);
}

var addBtnClicked = false;
function doAddReplace(trimId, action, message) {
	if (validateDropDowns()) {
		if ("" != carToReplace) {
			doRemoveVehicle(carToReplace, "", trimId);
		} else {
			if (!addBtnClicked) {
				addBtnClicked = true;
				addComparisonVehicles(trimId, 1);
			}
		}
		//document.getElementById("comparisonVehicles").value = comparisonVehicles;
		if (comparisonVehicles.indexOf("~") < 1) {
			comparisonVehicles = comparisonVehicles.replace("~", "");
		}
		document.getElementById("comparisonVehiclesAddRemove").value = comparisonVehicles;
		document.polkComparisonActionForm.action = window.location.pathname;
		document.polkComparisonActionForm.submit();
	} else {
		alert(message);
	}
}

//Used to override the default change year behavior in subnav
function changeYearOverride (year){
	var URL = INTERNAL_URL+ '?';
	for (var key in PARAM){
		switch (key) {
		case  'year':
			URL += key + '=' + year+'&';
			URL += 'preYear' + '=' + PARAM[key]+'&';
			break;
		case 'pvc':
			URL += 'prePVC' + '=' + anchorVehiclePVC+'&';
			break;
		default:
			URL += key + '=' + PARAM[key]+'&';
			break;
		}
	}
	window.location.href = BASE_CONTEXT + URL;
}

function handleTabBorders(clickedSpan$) {
	var cS$ = clickedSpan$;
	var allTs$ = mrm.$('#pCategories>ul>li');
	var allSs$ = mrm.$('#pCategories>ul>li>span');
	var cT$ = cS$.closest('li');
	var pT$ = cT$.prev('li');
	var lT$ = allTs$.last();
	// alert('Tab clicked! id: ' + cT$.attr('id') + '\n  Previous tab id: ' + pT$.attr('id') + '\n  Last tab id: ' + lT$.attr('id'));
	
	// Handle .jPrev class switch.  .jPrev class should apply only to the tab left of clicked tab.
	allSs$.removeClass('jPrev');
	if (pT$.length > 0) {
		// alert('Adding .jPrev class to previous tab (' + pT$.attr('id') + ')...');
		pT$.children('span').addClass('jPrev');
	}
	
}

mrm.$(function() {
	// Handle category tab widths (to hide/show right border dynamically based on active tab).
	mrm.$('#pCategories>ul>li>span').click(function() {
		handleTabBorders(mrm.$(this));
	});
	
	if (mrm.$.browser.msie) {
		mrm.$('tr>th:first-child').css('border-left', '0');
		mrm.$('tr>th:last-child, tr>td:last-child').css('border-right', '0');
		mrm.$('tr.data:last-child>th, tr.data:last-child>td').css('border-bottom', '0');
	}
});

