var modelId;
var modelName;
var numOfYears;
var yearsAll = new Array();
var factsheets = new Array();
var hightlights = new Array();
var hightlight = new Array();
var thumbimgpath;
var allowColNum; //count
var showingYears = new Array(); //year id, e.g., 1,2,3,4
var preYears = new Array(); //year id: e.g., 1
var nextYears = new Array(); //yearid: e.g. 5,6
var preNextStatus; //static indicator for a model, e.g. less, equal, more
var numOfModels;
var rphotolink;
function initColYears(){
	if(numOfYears<allowColNum){ //no pre/next button
		preNextStatus = "less";
		showingYears = new Array();
		for(var i = 1; i <= yearsAll.length; i++){
			showingYears[showingYears.length]=i;
		}
	}else if(numOfYears==allowColNum){ //no pre/next button
		preNextStatus = "equal";
		showingYears = new Array();
		for(var i = 1; i <= yearsAll.length; i++){
			showingYears[showingYears.length]=i;
		}
	}else { //will have pre/next button
		preNextStatus = "more";
		showingYears = new Array();
		for(var i = 1; i <= allowColNum; i++){
			showingYears[showingYears.length]=i;
		}
		preYears = new Array();
		nextYears = new Array();
		for(var i = allowColNum+1; i <= numOfYears; i++){
			nextYears[nextYears.length]=i;
		}
	}
	//alert("initColYears(): preYears="+preYears+"; showingYears="+showingYears+"; nextYears="+nextYears);
}

function updateAColumn(colid, yearid){ //e.g. 1,2,3,4
	var trs = document.getElementById("datatable").getElementsByTagName("tr");
	//1. update head row
	var yearselectorOptions = document.getElementById("yeardropdown_"+colid).getElementsByTagName("option");
	for (var i = 1; i <= yearselectorOptions.length; i++){
		if(i==yearid){
			yearselectorOptions[i-1].selected=true;
		} else {
			yearselectorOptions[i-1].selected=false;
		}
	}
	//alert("updateAColumn("+colid+", "+yearid+"); yearsAll[yearid-1]="+yearsAll[yearid-1]);
	document.getElementById("photolinkbox_"+colid).innerHTML="<a href=\"#\" onclick=\"javascript:document.getElementById('photoOverlay').style.display='block';trackingCPOModelComparisonPhoto('"+modelName+"','"+yearsAll[yearid-1]+"');showyear('"+yearsAll[yearid-1]+"');return false;\" id=\"photolink_"+colid+"\">"+rphotolink+"</a>";
	//alert("document.getElementById(photolinkbox_colid).innerHTML="+document.getElementById("photolinkbox_"+colid).innerHTML);
	document.getElementById("thumbnail_"+colid).src=thumbimgpath+modelId+"_"+yearsAll[yearid-1]+".png";
	document.getElementById("thumbnail_"+colid).title=yearsAll[yearid-1]+" "+modelName;
	//alert("thumbnail_"+colid+"="+document.getElementById("thumbnail_"+colid).src+document.getElementById("thumbnail_"+colid).title);
	document.getElementById("vlbutton_"+colid).onclick=function(){
		trackingCPOInventoryClick();//tracking only;
		if(getDefaultLocation()==null){
			window.location.href=BASE_CONTEXT+"/tools/cpo/advancedSearch.do?startYear="+yearsAll[yearid-1]+"&endYear="+yearsAll[yearid-1]+"&selectedModelList="+modelId;
		}else{
		var zipCode ='';
		if(Cookies['zipCode']!='undefined'){
			zipCode = Cookies['zipCode'];
		}
			window.location.href=BASE_CONTEXT+"/tools/cpo/locateUsedVehicle.do?selectedModelList="+modelId+"&zipCode="+zipCode+"&price=&mileage=60000&startYear="+yearsAll[yearid-1]+"&endYear="+yearsAll[yearid-1]+"&distanceProximity=50";
		}
	};

	//2. update factsheet row
	document.getElementById("rfactlink_"+colid).href=BASE_CONTEXT+"/assets/en/pdf/tools/cpo/"+factsheets[yearid-1];

	//3. update highlights
	for (var i = 2; i < trs.length-1; i++){
		trs[i].getElementsByTagName("td")[colid].innerHTML = hightlights[i-2][yearid-1];
	}
}

function initAllCols(){
	for (var i = 1; i <= showingYears.length; i++){
		updateAColumn(i, i);
	}
	//init pre/next buttons
	if(preNextStatus == "more"){
		updatePreNextButtons();
	}
}
function updatePreNextButtons(){
	//alert("updatePreNextButtons(): preYears="+preYears+"; showingYears="+showingYears+"; nextYears="+nextYears);
	//alert("preYears.length="+preYears.length+"; nextYears.length="+nextYears.length);
	if(preYears.length>0){
		document.getElementById("prelinkdiv").style.display="block";
	} else {
		document.getElementById("prelinkdiv").style.display="none";
	}
	if(nextYears.length>0){
		document.getElementById("nextlinkdiv").style.display="block";
	} else {
		document.getElementById("nextlinkdiv").style.display="none";
	}
}
function updateAllCols(){
//1. loop through allowColNum, if changed, update the data(colid, yearid) and showingYears
	for (var i = 1; i <= showingYears.length; i++){
		var selectedYearId = document.getElementById("yeardropdown_"+i).getElementsByTagName("select")[0].selectedIndex+1;
		if(showingYears[i-1]!=selectedYearId){
			updateAColumn(i, selectedYearId);
			showingYears[i-1]=selectedYearId;
		}
	}
//2. no need to update pre/next button and values;
}

function previousyear(){
	//1. update showingYears; update pre/next button and values;
	var preshow = [preYears[preYears.length-1]];
	var shownext = [showingYears[showingYears.length-1]];
	showingYears=preshow.concat(showingYears);
	showingYears.splice(showingYears.length-1,1);
	nextYears=shownext.concat(nextYears);
	preYears.splice(preYears.length-1,1);
	//alert("previousyear(): preYears="+preYears+"; showingYears="+showingYears+"; nextYears="+nextYears);
	updatePreNextButtons();
	//2. loop through allowColNum, if changed, update the (column, showing)
	for (var i = 1; i <= allowColNum; i++){
		var selectedYearId = document.getElementById("yeardropdown_"+i).getElementsByTagName("select")[0].selectedIndex+1;
		if(showingYears[i-1]!=selectedYearId){
			updateAColumn(i, showingYears[i-1]);
		}
	}
}
function nextyear(){
	//1. update showingYears; update pre/next button and values;
	showingYears[showingYears.length]=nextYears.splice(0,1);
	preYears[preYears.length]=showingYears.splice(0,1);
	//alert("nextyear(): preYears="+preYears+"; showingYears="+showingYears+"; nextYears="+nextYears);
	updatePreNextButtons();
	//2. loop through allowColNum, if changed, update the (column, showing)
	for (var i = 1; i <= allowColNum; i++){
		var selectedYearId = document.getElementById("yeardropdown_"+i).getElementsByTagName("select")[0].selectedIndex+1;
		if(showingYears[i-1]!=selectedYearId){
			updateAColumn(i, showingYears[i-1]);
		}
	}
}
function leftTnImgOver(index){
	for (var i = 0; i < numOfModels; i++){
		if(index==i){
			document.getElementById("leftTnTxt_"+i).style.display="block";
		} else {
			document.getElementById("leftTnTxt_"+i).style.display="none";
		}
	}
}
function leftTnImgOut(index){
	document.getElementById("leftTnTxt_"+index).style.display="none";
}

var currentImg = 0;
function initPhotoOverlay(){
	showimg(0);
	currentImg = 0;
}
// utility
function getPosition( element ){
	var left = 0;
	var top  = 0;
	while( element.offsetParent ){
		left += element.offsetLeft;
		top += element.offsetTop;
		element = element.offsetParent;
	}
	left += element.offsetLeft;
	top  += element.offsetTop;
	return {x:left, y:top};
}
function showimg(imgIndex){
	//1. show image
	if(document.getElementById("imgbox")){
	var imgs = document.getElementById("imgbox").getElementsByTagName("img");
	for (var i = 0; i < imgs.length; i++){
		if(i==imgIndex){
			imgs[i].style.display="block";
			document.getElementById("imgtxt"+i).style.display="block";
			mrm.$("item-"+i).addClass('imgActive');
		} else {
			imgs[i].style.display="none";
			document.getElementById("imgtxt"+i).style.display="none";
			mrm.$("item-"+i).removeClass('imgActive');
		}
	}
	currentImg = imgIndex;
	//2. update previous and next link
	var preimg = -1;
	var nextimg = imgs.length;
	if(currentImg>0){
		preimg = currentImg - 1;
		document.getElementById("preimglink").innerHTML = "<a href=\"javascript:showimg("+preimg+")\">&lt;</a>";
	} else {
		document.getElementById("preimglink").innerHTML = "&lt;";
	}
	if(currentImg<imgs.length-1){
		nextimg = eval(currentImg) + 1;
		document.getElementById("nextimglink").innerHTML = "<a href=\"javascript:showimg("+nextimg+")\">&gt;</a>";
	} else {
		document.getElementById("nextimglink").innerHTML = "&gt;";
	}
	//alert("preimg="+preimg+"; currentImg="+currentImg+"; nextimg="+nextimg);
	}
}
function showyear(year){
	var imgs = document.getElementById("imgbox").getElementsByTagName("img");
    var ignorme = false;
    for(var j = 0; j < yearsAll.length; j++){
       if($("yeartab_" + yearsAll[j]).className.indexOf('active', 0) >= 0)
           mrm.$("yeartab_" + yearsAll[j]).removeClass('active');
    }
    mrm.$("yeartab_" + year).addClass('active');
	for (var i = 0; i < imgs.length; i++){
		if(imgs[i].src.indexOf(year)>0 && !ignorme){
            showimg(imgs[i].id);
            ignorme = true;
		}
	}
}
function photolink(year){
	document.getElementById('photoOverlay').style.display='block';
	showyear(year);
}
function searchInventory(url){
	window.location.href=url;
}
function trackingCPOInventoryClick(){
	console.log("here");
	var divisionObj=BRAND.toUpperCase();
	clickTrack({pageName: divisionObj+' | DIVISIONAL | CPO | VEHICLE SHOWROOM', prop1: 'DIVISIONAL', prop2:'CPO', prop3: 'DIVISIONAL | CPO', prop4:'VEHICLE SHOWROOM', prop5:'CPO | VEHICLE SHOWROOM', prop6: 'DIVISIONAL | CPO | VEHICLE SHOWROOM', prop7:'SEARCH INVENTORY', prop8: 'VEHICLE SHOWROOM | SEARCH INVENTORY', prop9:'CPO | VEHICLE SHOWROOM | SEARCH INVENTORY', prop10:'DIVISIONAL | CPO | VEHICLE SHOWROOM | SEARCH INVENTORY',prop25:divisionObj, prop26:(new Date()).getHours(), prop27: weekday[(new Date()).getDay()]});
}
function trackingCPOFactSheetDownload(modelName){
	clickTrack( {
		pageName : BRAND.toUpperCase() + ' | DIVISIONAL | CPO | VEHICLE SHOWROOM',
		prop1 : 'DIVISIONAL',
		prop2 : 'CPO',
		prop3 : 'DIVISIONAL | CPO',
		prop4 : 'VEHICLE SHOWROOM',
		prop5 : 'CPO | VEHICLE SHOWROOM',
		prop6 : 'DIVISIONAL | CPO | VEHICLE SHOWROOM',
		prop7 : 'MODEL COMPARISON',
		prop8 : 'VEHICLE SHOWROOM | MODEL COMPARISON',
		prop9 : 'CPO | VEHICLE SHOWROOM | MODEL COMPARISON',
		prop10 : 'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON',
		prop11 : modelName + ' | DOWNLOAD FACT SHEET',
		prop12 : 'MODEL COMPARISON | ' + modelName + ' | DOWNLOAD FACT SHEET',
		prop13 : 'VEHICLE SHOWROOM | MODEL COMPARISON | ' + modelName + ' | DOWNLOAD FACT SHEET',
		prop14 : 'CPO | VEHICLE SHOWROOM | MODEL COMPARISON | ' + modelName + ' | DOWNLOAD FACT SHEET',
		prop15 : 'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON | ' + modelName + ' | DOWNLOAD FACT SHEET',
		prop24 : 'EN',
		prop25 : BRAND.toUpperCase(),
		prop26 : (new Date()).getHours(),
		prop27 : weekday[(new Date()).getDay()]
	});
}
 function trackingCPOModelComparisonPhoto(model, year) {
 	model = model.toUpperCase();
	clickTrack( {
		pageName : BRAND.toUpperCase() + ' | DIVISIONAL | CPO | VEHICLE SHOWROOM',
		prop1 : 'DIVISIONAL',
		prop2 : 'CPO',
		prop3 : 'DIVISIONAL | CPO',
		prop4 : 'VEHICLE SHOWROOM',
		prop5 : 'CPO | VEHICLE SHOWROOM',
		prop6 : 'DIVISIONAL | CPO | VEHICLE SHOWROOM',
		prop7 : 'MODEL COMPARISON',
		prop8 : 'VEHICLE SHOWROOM | MODEL COMPARISON',
		prop9 : 'CPO | VEHICLE SHOWROOM | MODEL COMPARISON',
		prop10 : 'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON',
		prop11 : 'PHOTOS | '+ model + ' | '+year,
		prop12 : 'MODEL COMPARISON | PHOTOS | '+ model + ' | '+year,
		prop13 : 'VEHICLE SHOWROOM | MODEL COMPARISON | PHOTOS | '+ model + ' | '+year,
		prop14 : 'CPO | VEHICLE SHOWROOM | MODEL COMPARISON | PHOTOS | '+ model + ' | '+year,
		prop15 : 'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON | PHOTOS | '+ model + ' | '+year,
		prop24 : 'EN',
		prop25 : BRAND.toUpperCase(),
		prop26 : (new Date()).getHours(),
		prop27 : weekday[(new Date()).getDay()]
	});
 }

