var moreThumbnailsInterior = false;
var moreThumbnailsExterior = false;

function showMoreThumbnails(photoType, moreLabel, fewerLabel) {
	var newClass = " thumbnailImageRowHiden";
	var moreThumbnails = "";
	var moreRows = true;
	var i = 1;
	var row = "";
	var moreRowsButtom = "";
	var idImg = "";

	if ("thumbnailImageRowExterior" == photoType) {
		moreThumbnails = moreThumbnailsExterior;
		moreRowsButtom = document.getElementById("moreRowsExterior");
		idImg = "Exterior";
	} else {
		moreThumbnails = moreThumbnailsInterior;
		moreRowsButtom = document.getElementById("moreRowsInterior");
		idImg = "Interior";
	}

	if (true == moreThumbnails) {
		moreThumbnails = false;
		moreRowsButtom.className = "moreRows";
		document.getElementById("moreResultsLinks" + idImg).innerHTML = moreLabel;
		$(moreRowsButtom).removeClass('fewerLabel');
	} else {
		newClass = "";
		moreRowsButtom.className += " spacingTop";
		moreThumbnails = true;
		document.getElementById("moreResultsLinks" + idImg).innerHTML = fewerLabel;
		$(moreRowsButtom).addClass('fewerLabel');
	}

	while (true == moreRows) {
		row = document.getElementById(photoType + i);
		if (null == row) {
			moreRows = false;
		} else {
			row.className = row.className.replace("thumbnailImageRowHiden", "") + newClass;
			i++;
		}
	}

	if ("thumbnailImageRowExterior" == photoType) {
		moreThumbnailsExterior = moreThumbnails;
	} else {
		moreThumbnailsInterior = moreThumbnails;
	}
}

function showThumbnailContainer(containerType, object) {
	var containerToShow = document.getElementById("thumbnailImageInterior");
	var containerToHide = document.getElementById("thumbnailImageExterior");

	if ("exterior" == containerType) {
		containerToShow = document.getElementById("thumbnailImageExterior");
		containerToHide = document.getElementById("thumbnailImageInterior");
		document.getElementById("galleryTabExterior").className = "optionTabSelected";
		document.getElementById("galleryTabInterior").className = "optionTab";
		document.getElementById("comparisonImageGlobalContainerExterior").className = "comparisonImageGlobalContainerClass";
		document.getElementById("comparisonImageGlobalContainerInterior").className = "comparisonImageGlobalContainerClass thumbnailImageRowHiden";
	} else {
		document.getElementById("galleryTabExterior").className = "optionTab";
		document.getElementById("galleryTabInterior").className = "optionTabSelected";
		document.getElementById("comparisonImageGlobalContainerExterior").className = "comparisonImageGlobalContainerClass thumbnailImageRowHiden";
		document.getElementById("comparisonImageGlobalContainerInterior").className = "comparisonImageGlobalContainerClass";
	}

	containerToShow.className = "thumbnailImagerows";
	containerToHide.className = "thumbnailImageRowHiden";
}

var imagePositionExterior = 0;
var totalExteriorThumbnailContainer = 0;
var imagePositionInterior = 0;
var totalInteriorThumbnailContainer = 0;
var numImages = 0;
function createThumbnailContainer(src, description,division,photo) {
	var globalContainer = "";
	var imagePosition = imagePositionExterior;
	var totalThumbnailContainers = totalExteriorThumbnailContainer;
	var thumbnailImageRow = "";
	var newImg = "";
	var newDiv = "";
	var newAnchor = "";
	var newImgId = "";
	if (description.match("Interior")) {
		imagePosition = imagePositionInterior;
		totalThumbnailContainers = totalInteriorThumbnailContainer;
	}

	if (parseInt(imagePosition) == 0) {
		newDiv = document.createElement('div');
		if (description.match("Exterior")) {
			globalContainer = document.getElementById("thumbnailImageExterior");
			newDiv.id = "thumbnailImageRowExterior" + totalThumbnailContainers;
			newDiv.className = "thumbnailImageRow";
			if (totalThumbnailContainers > 0) {
				newDiv.className = "thumbnailImageRow spacingTop thumbnailImageRowHiden";
			}
		} else if (description.match("Interior")) {
			globalContainer = document.getElementById("thumbnailImageInterior");
			newDiv.id ="thumbnailImageRowInterior" + totalThumbnailContainers;
			newDiv.className = "thumbnailImageRow";
			if (totalThumbnailContainers > 0) {
				newDiv.className = "thumbnailImageRow spacingTop thumbnailImageRowHiden";
			}
		}

		globalContainer.appendChild(newDiv);
		totalThumbnailContainers++;
	}
	
	newAnchor = document.createElement("a");
	if (description.match("Exterior")) {
		thumbnailImageRow = document.getElementById("thumbnailImageRowExterior" + (totalThumbnailContainers - 1));
		newAnchor.href = "javascript:showComparisonImages(" + "'comparisonImageContainerExterior" + numImages + "'" + ", 'Exterior');photoTracking('"+division+"','"+photo+"');";
		newImgId = "comparisonImageContainerExterior" + numImages + "TN";
	} else if (description.match("Interior")) {
		thumbnailImageRow = document.getElementById("thumbnailImageRowInterior" + (totalThumbnailContainers -1));
		newAnchor.href = "javascript:showComparisonImages(" + "'comparisonImageContainerInterior" + numImages + "'" + ", 'Interior');photoTracking('"+division+"','"+photo+"');";
		newImgId = "comparisonImageContainerInterior" + numImages + "TN";
	}

	if (parseInt(imagePosition) <= 7) {
		thumbnailImageRow.appendChild(newAnchor);

		newImg = document.createElement("img");
		newImg.title = description;
		newImg.src = src;
		newImg.id = newImgId;
		//newImg.onclick=photoTracking(division,photo);
		newAnchor.appendChild(newImg);
		thumbnailImageRow.appendChild(newAnchor);
		imagePosition++;
		numImages++;
	}

	if (parseInt(imagePosition) == 8) {
		imagePosition = 0;
	}

	if (description.match("Exterior")) {
		imagePositionExterior = imagePosition;
		totalExteriorThumbnailContainer = totalThumbnailContainers;
	} else if (description.match("Interior")) {
		imagePositionInterior = imagePosition;
		totalInteriorThumbnailContainer = totalThumbnailContainers;
	}
}

function showLinkMoreResults(photoType, moreLabel, fewerLabel) {
	var container = "";
	var newDiv = document.createElement("div");
	var newAnchor = document.createElement("a");
	var linkContainer = "";
	var totalThumbnailContainers = totalExteriorThumbnailContainer;
	var linkId = "";

	if ("Interior" == photoType) {
		totalThumbnailContainers = totalInteriorThumbnailContainer;
	}

	newDiv.id = "moreRows" + photoType;
	newDiv.className = "moreRows";

	if (totalThumbnailContainers > 1) {
		container = document.getElementById("thumbnailImage" + photoType);
		container.appendChild(newDiv);

		linkContainer = document.getElementById("moreRows" + photoType);
		newAnchor.id = "moreResultsLinks" + photoType;
		newAnchor.href = "#";
		linkId = "thumbnailImageRow" + photoType;
		newAnchor.onclick = new Function("showMoreThumbnails(" + "'" + linkId + "'," + "'" + moreLabel + "'," + "'" + fewerLabel + "'" + ")");
		linkContainer.appendChild(newAnchor);

		document.getElementById("moreResultsLinks" + photoType).innerHTML = moreLabel;
	}
}

var totalComparisonImageContainer = 0;
var showedComparisonImageContainerExterior = 0;
var showedComparisonImageContainerInterior = 0;
var isFirstInterior = false;
var isFirstExterior = false;
function createComparisonImageContainer(src, numImage, description, modelName, izmomatch) {
	var photoType = "Exterior";
	var globalContainer = document.getElementById("comparisonImageGlobalContainerExterior");
	var newImageContainer = "";
	var imageContainer = "";
	var imageDiv = "";
	var newImg = "";
	
	if(izmomatch == "N" || izmomatch == "Y"){
		modelName = "Pictured: " + modelName;
	}

	if (description.match("Interior")) {
		photoType = "Interior";
		globalContainer = document.getElementById("comparisonImageGlobalContainerInterior");
	}

	if (1 == numImage) {
		newImageContainer = document.createElement('div');
		newImageContainer.id = "comparisonImageContainer" + photoType + totalComparisonImageContainer;
		newImageContainer.className = "comparisonImageBox thumbnailImageRowHiden";
		document.getElementById(newImageContainer.id+"TN").className = "hiddenThumbnail";
		if (((false == isFirstInterior) && (description.match("Interior"))) || ((false == isFirstExterior) && (description.match("Exterior")))) {
			newImageContainer.className = "comparisonImageBox";
			document.getElementById(newImageContainer.id+"TN").className = "showedThumbnail";

			if (description.match("Interior")) {
				isFirstInterior = true;
				showedComparisonImageContainerInterior = "comparisonImageContainerInterior" + totalComparisonImageContainer;
			} else {
				isFirstExterior = true;
				showedComparisonImageContainerExterior = "comparisonImageContainerExterior" + totalComparisonImageContainer;
			}
		}
		globalContainer.appendChild(newImageContainer);
		totalComparisonImageContainer++;
	}

	imageContainer = document.getElementById("comparisonImageContainer" + photoType + (totalComparisonImageContainer - 1));

	imageDiv = document.createElement('div');
	imageDiv.className = "comparisonImage img"+numImage;

	newImg = document.createElement('img');
	newImg.src = src;
	newImg.title = description;
	imageDiv.appendChild(newImg);

	imageContainer.appendChild(imageDiv);

	var newP = document.createElement('p');
	newP.innerHTML = modelName;
	imageDiv.appendChild(newP);
}

function showComparisonImages(imageToShow, photoType) {
	if ("Exterior" == photoType) {
		if (imageToShow != showedComparisonImageContainerExterior) {
			document.getElementById(imageToShow).className = "comparisonImageBox";
			document.getElementById(showedComparisonImageContainerExterior).className = "comparisonImageBox thumbnailImageRowHiden";
			document.getElementById(imageToShow+"TN").className = "showedThumbnail";
			document.getElementById(showedComparisonImageContainerExterior+"TN").className = "hiddenThumbnail";
			showedComparisonImageContainerExterior = imageToShow;
		}
	} else {
		if (imageToShow != showedComparisonImageContainerInterior) {
			document.getElementById(imageToShow).className = "comparisonImageBox";
			document.getElementById(showedComparisonImageContainerInterior).className = "comparisonImageBox thumbnailImageRowHiden";
			document.getElementById(imageToShow+"TN").className = "showedThumbnail";
			document.getElementById(showedComparisonImageContainerInterior+"TN").className = "hiddenThumbnail";
			showedComparisonImageContainerInterior = imageToShow;
		}
	}
}

function photoTracking (division,tabValue){
	var uTabValue	= tabValue.toUpperCase();
	var divisionObj	= division.toUpperCase();
	tabValue = tabValue.toLowerCase()=='photo'?'photo compare: '+mrm.$('#galleryTabs .optionTabSelected a').html():tabValue;
	var obj = {
		prop18: divisionObj,
		prop23: 'en',
		prop27: 'images',
		prop32: divisionObj+': us: compare-photos: '+vehicleModel+': '+tabValue
	};
	clickTrack(obj);
}

