var CHART = "chart";
var MITS  = "mits";
var COMP  = "comp";
//var PRIOR = "prior";

/* These values need to be kept in-sync with the current vehicle roster; changeouts affect the entries here */
var urlSignature = new Object();
urlSignature["lancer/10"] = "2010 Lancer";
urlSignature["lancer/11"] = "2011 Lancer";
//urlSignature["lancer/09"] = "2009 Lancer";
urlSignature["sportback/10"] = "2010 Lancer Sportback";
urlSignature["evo/10"] = "2010 Lancer Evolution";
//urlSignature["evo/08"] = "2008 Lancer Evolution";
//urlSignature["eclipse/10"] = "2010 Eclipse";
urlSignature["eclipse/11"] = "2011 Eclipse";
//urlSignature["eclipse/09"] = "2009 Eclipse";
//urlSignature["eclipse/08"] = "2008 Eclipse";
//urlSignature["spyder/10"] = "2010 Eclipse Spyder";
urlSignature["spyder/11"] = "2011 Eclipse Spyder";
//urlSignature["spyder/09"] = "2009 Eclipse Spyder";
urlSignature["galant/10"] = "2010 Galant";
urlSignature["galant/11"] = "2011 Galant";
//urlSignature["galant/09"] = "2009 Galant";
urlSignature["endeavor/10"] = "2010 Endeavor";
urlSignature["endeavor/11"] = "2011 Endeavor";
//urlSignature["endeavor/08"] = "2008 Endeavor";
//urlSignature["outlander/09"] = "2009 Outlander";
urlSignature["outlander/10"] = "2010 Outlander";
//urlSignature["raider/09"] = "2009 Raider";
//urlSignature["raider/08"] = "2008 Raider";  

//urlSignature["galant/08"] = "2008 Galant";
//urlSignature["spyder/08"] = "2008 Eclipse Spyder";
//urlSignature["lancer/06"] = "2006 Lancer";
//urlSignature["outlander/06"] = "2006 Outlander";
//urlSignature["endeavor/06"] = "2006 Endeavor";
//urlSignature["endeavor/07"] = "2007 Endeavor";
//urlSignature["montero/06"] = "2006 Montero";
//urlSignature["raider/06"] = "2006 Raider"; 

/* type can be CHART (comparison chart), COMP (competitive), or MITS (mistubishi) */
function comparison(type) {
	//test for null values from AIC
	/*
	4:02:13 PM: ah, i follow ... finding the best place to test for that null ...
	4:03:31 PM: I'd say the first thing you do in comparison() is check for it
	4:04:02 PM: but, you'll have to make sure that it doesn't return to comparison if it is null
	4:04:16 PM: you'll have to initiate an ajax call, and have the callback of that be comparison
	4:04:53 PM: mmm, yeah i suppose the entrypoint, good call ... ok right, otherwise it would still try and chew on no data ... and that way it'll catch null data if it comes back that way again
	4:05:17 PM: yeah, and you can display some kind of error message
	4:05:23 PM: 'cause if you have no Mits models, you've got nothing
	*/
	
	//resize the container, accordingly ... 3 ancestorsBack should be the same size as element with id bottomHousing. last param is extra padding added to the height
	//resizeAncestor("bottomHousing", 3, $("bottomHousing").offsetTop + 250); 
	
	//set the style of tabs for "Compare Mitsubishi Models" and "Compare Competitive Models"	
	getItem("compareChart").className = (type == CHART) ? "vehNewAICTabSelected" : "vehTab";	
	getItem("compareCompetitors").className = (type == COMP) ? "vehNewAICTabSelected" : "vehTab";
	getItem("compareMits").className = (type == MITS) ? "vehNewAICTabSelected" : "vehTab";
	getItem("selectNewModel1").innerHTML = "<a id=\"selectNewLink1\" href=\"javascript:selectNewGateway('1');\">" + TEXT_SELECT_NEW_MODEL + "</a>";

	//some init business:
	StateMonitor.clear();
	hideCompareResults();
	//redrawFlash(); this breaks the menu, so why redraw??
	
	determineDefaultMitsuTrim();
	var defaultModelName = defaultModelReference.name.split(" ");

	var priorYears = new Array();
	getPriorYears();

	switch(type) {
		case CHART:
			$('#modelsContainer').hide();
			displayModelChart(aicModels);
			displayPDFLink(aicModels);
			$('#chartContainer').show();
			StateMonitor.setCompetitiveTabState(selectedTrimsMonitor.OFF);
			setMitsModelOnChangeHandlers();			
			$("#vImg1Border").hide();
	    break;
		case COMP:
			$('#chartContainer').hide();
			$('#modelsContainer').show();
			StateMonitor.setCompetitiveTabState(selectedTrimsMonitor.ON); //passing a constant
			setMitsModelOnChangeHandlers(); // set/restore event handlers
			buildModelDropdown((1), aicModels);		
			if(mitsuModelIndexes == null) { 
				buildMitsuIndexes();
			}
			setDefaultTrimColumn();
			prepareCompetitiveScreen();
			$("#vImg1Border").hide();
		break;
		case MITS:
			$('#chartContainer').hide();
			$('#modelsContainer').show();
			StateMonitor.setCompetitiveTabState(selectedTrimsMonitor.OFF); //passing a constant
			setMitsModelOnChangeHandlers(); // set/restore event handlers
			for(var i=1; i <= 4; i++) {
				resetSelectset(i);
				buildModelDropdown((i), aicModels);
			}
			setDefaultTrimColumn(); // leftmost column needs to match the current mitsu vehicle
			$("#vImg1Border").show();
		break;
		/*
		case PRIOR:
			StateMonitor.setCompetitiveTabState(selectedTrimsMonitor.OFF); //passing a constant
			setMitsModelOnChangeHandlers(); // set/restore event handlers
			for(var i=1; i <= 4; i++) {
				resetSelectset(i);
				buildModelDropdown((i), aicModels);
				if(i==1) setDefaultTrimColumn(); // leftmost column needs to match the current mitsu vehicle
				if (priorYears[i-2] != undefined) {
					this.numberSelected ++; 
					setTrimColumn(i, priorYears[i-2]);
				}
			}
			$("vImg1Border").style.visibility = "hidden";
		break;
		*/
	}
	setAICMessaging(); // instructive text above columns
	
	function getPriorYears() {
		var i = 0;
		for(key in aicModels) {
			var model = aicModels[key].name;
			var modelName = model.split(" ");
			//look for the same model of a different year in the list of mitsubishi models
			if(modelName.length == defaultModelName.length && modelName[1] == defaultModelName[1] && modelName[0] != defaultModelName[0]) {
				if(modelName.length == 2) {
					//alert(modelName[0] +modelName[1]);
					priorYears[i++] = modelName[0] +" "+ modelName[1];
				} else if(modelName[2] == defaultModelName[2]) {
					//alert(modelName[0] +modelName[1] +modelName[2]);
					priorYears[i++] = modelName[0] +" "+ modelName[1] +" "+ modelName[2];
				}
			}
		}
	}
	
}
comparison["name "]= "comparison";

// jjg: scrape names of mitsu vehicles from refSelectOptions object
// and use them to fill mitsuModelIndexes dropdown/select ???
function buildMitsuIndexes() {
	mitsuModelIndexes = new Object;
	var refSelectOptions = document.getElementById("models1").options;
	for(var i=0; i < refSelectOptions.length; i++) {
		var option = refSelectOptions[i];
		mitsuModelIndexes[option.text] = i;
	}
}

function displayModelChart (modelList) {
	var modelChartName = String(window.location.pathname.split('/')[3] + window.location.pathname.split('/')[4] + '_compare.jpg').toLowerCase();
	var imgRoot = $('#comparisonChart').attr('src').split('/');
	imgRoot.pop();
	var imgLink = imgRoot.join('/');
	$('#comparisonChart').attr('src', imgLink + '/' + modelChartName);
}

function displayPDFLink (modelList) {
	var modelPDFName = String(window.location.pathname.split('/')[3] + window.location.pathname.split('/')[4] + '_compare.pdf').toLowerCase();
	var pdfRoot = $('#compPDFLink').attr('href').split('/');
	pdfRoot.pop();
	var pdfLink = pdfRoot.join('/');	
	$('#compPDFLink').attr('href', pdfLink + '/' + modelPDFName);
}

function hasComparisonChart () {
	var hasChart = {'lancer': ['11'], 'endeavor': ['11'], 'galant': ['10', '11'], 'outlander': ['10'], 'eclipse': ['11'], 'spyder': ['11']};
	var chartList = hasChart[window.location.pathname.split('/')[3]];
	if (chartList != undefined) {
		if ($.inArray(window.location.pathname.split('/')[4], chartList) > -1) {
			return true;
		}
	}
	return false;
}

$(document).ready ( function () {
	if ( hasComparisonChart() ) {
		$("#compareChart").show();
		comparison(CHART);
	} else {
		comparison(COMP);
	}
});
