VehiclePhotoHandler = {
	pageLoadTO : null,
	clickMainImage : function(baseUrl) {
		var imgIndex = (window.SlideshowUtility) ? SlideshowUtility.getCurrentImageIndex() : 0;
		var imgUrl = VehiclePhotoUtility.getImagePath(imgIndex);
		if (!imgUrl) return;
		var height = (window.SlideshowUtility) ? 492 + SlideshowUtility.getSlideshowHeight() : 492;
		fullSizeWin = window.open(baseUrl + "&imgIndex=" + imgIndex, "", "width=640, height=" + height + ", left=200, top=200, screenX=200, screenY=200, scrollbars=no, resizable=no");
		fullSizeWin.name = "cblt_fullSizeWindow";
	},
	completePageLoad : function(startImageIndex) {
		if (VehiclePhotoUtility.mainImagesLoaded()) {
			if (VehiclePhotoHandler.pageLoadTO) window.clearTimeout(VehiclePhotoHandler.pageLoadTO);
		} else {
			VehiclePhotoHandler.pageLoadTO = window.setTimeout("VehiclePhotoHandler.completePageLoad(" + startImageIndex + ");", 200);
			return;
		}
		VehiclePhotoUtility.resizeMainImages();
	},
	pageLoad : function(imagePaths, startImageIndex) {
		VehiclePhotoUtility.init(imagePaths);
		VehiclePhotoHandler.completePageLoad(startImageIndex);
	},
	setMainImage : function(imgIndex) {
		VehiclePhotoUtility.displayImage(imgIndex);
	}
};

VehiclePhotoUtility = {
	IMAGE_CONTAINER_ID : "vehPhoto",
	MAIN_IMG_ID_PREFIX : "vehiclePhoto_",
	MAIN_WRAPPER_ID_PREFIX : "vehiclePhotoWrapper_",
	PHOTO_HEIGHT : 188,
	PHOTO_WIDTH : 250,
	currentIndex : -1,
	imagePaths : [],
	tempImgs : [],
	tempImgLoadTOs : [],
	init : function(imagePaths) {
		VehiclePhotoUtility.imagePaths = imagePaths;
	},
	mainImagesLoaded : function() {
		var mainImg = null;
		var i = -1;
		while ((mainImg = document.getElementById(VehiclePhotoUtility.MAIN_IMG_ID_PREFIX + ++i))) {
			if (!mainImg.complete) return false;
		}
		return true;
	},
	displayImage : function(imgIndex) {
		var oldDiv, newDiv;
		if ((oldDiv = document.getElementById(VehiclePhotoUtility.MAIN_WRAPPER_ID_PREFIX + VehiclePhotoUtility.currentIndex))) {
			oldDiv.style.display = "none";
		}
		VehiclePhotoUtility.currentIndex = imgIndex;
		if ((newDiv = document.getElementById(VehiclePhotoUtility.MAIN_WRAPPER_ID_PREFIX + imgIndex))) {
			newDiv.style.display = "block";
		}
	},
	getImagePath : function(imgIndex) { return VehiclePhotoUtility.imagePaths[imgIndex]; },
	resizeImage : function(index) {
		var tempImg = VehiclePhotoUtility.tempImgs[index];
		if (!tempImg.complete) {
			VehiclePhotoUtility.tempImgLoadTOs[index] = window.setTimeout("VehiclePhotoUtility.resizeImage(" + index + ");", 500);
			return;
		}
		if (VehiclePhotoUtility.tempImgLoadTOs[index]) window.clearTimeout(VehiclePhotoUtility.tempImgLoadTOs[index]);
		if ((tempImg.width <= VehiclePhotoUtility.PHOTO_WIDTH) && (tempImg.height <= VehiclePhotoUtility.PHOTO_HEIGHT)) return;
		// resize based on aspect ratio, not portrait vs landscape. "normal" aspectRatio is .75
		var aspectRatio = tempImg.height / tempImg.width;
		var scaleByWidth = (aspectRatio < .75) ? true : false;
		// don't resize if the image is already equal to or smaller than the desired size
		if (scaleByWidth) {
			// image is too big, and should be scaled to 250 wide
			tempImg.width = VehiclePhotoUtility.PHOTO_WIDTH;
			tempImg.height = tempImg.width * aspectRatio;
		} else {
			// image is too big, and should be scaled to 188 tall
			tempImg.height = VehiclePhotoUtility.PHOTO_HEIGHT;
			tempImg.width = tempImg.height / aspectRatio;
		}
		var img = document.getElementById(VehiclePhotoUtility.MAIN_IMG_ID_PREFIX + index);
		img.height = tempImg.height;
		img.width = tempImg.width;
	},
	resizeMainImages : function() {
		var i = -1;
		var currImg;
		while ((currImg = document.getElementById(VehiclePhotoUtility.MAIN_IMG_ID_PREFIX + ++i))) {
			VehiclePhotoUtility.tempImgs[i] = new Image();
			VehiclePhotoUtility.tempImgs[i].src = currImg.src;
			VehiclePhotoUtility.resizeImage(i);
		}
	}
}
