// JavaScript Document

var chdGeneral = {
	
	addListeners: function() {
		if (!document.getElementsByTagName || !document.getElementById)
			return;		

		// Set Original Image Size
		if (document.getElementById('prjMain')) {
			var objMainImage = document.getElementById('prjMain');
			chdGeneral.intImageOriginal_Width = objMainImage.offsetWidth;
			chdGeneral.arrImage_Widths[0] = objMainImage.offsetWidth;
			chdGeneral.intImageOriginal_Height = objMainImage.offsetHeight;
			chdGeneral.arrImage_Heights[0] = objMainImage.offsetHeight;
		}
		
		// Resize Page Contents
		chdGeneral.winResize();
		
		// Add Listeners
		rwScript.addEvent(window, 'resize', chdGeneral.winResize, false);

		if (document.getElementById("prjThumbnails")) {
			var objThumbnails = document.getElementById("prjThumbnails");
			var arrLinks = objThumbnails.getElementsByTagName('a');
			chdGeneral.arrImage_Heights.length = arrLinks.length;
			chdGeneral.arrImage_Widths.length = arrLinks.length;	
			chdGeneral.arrPreloads.length = arrLinks.length;	
			for (var i=0; i<arrLinks.length; i++) {
				var objA = arrLinks[i];
				rwScript.addEvent(objA, 'click', chdGeneral.getTNClick(objA), false);
				objA.onclick = chdGeneral.cancelClick;

				if (i == 0 && arrLinks.length > 1) {
					// Preload Images
					var objImagePreload = document.getElementById("prjPreloadContainer");
					var imgNew = new Image();
					objImagePreload.appendChild(imgNew);			
					imgNew.src = objA.href.slice(0, -5) + "2.jpg";
					rwScript.addEvent(imgNew, 'load', chdGeneral.imageLoad(imgNew), false);
				}
			}
		}
	},
	
	imageLoad: function(objImage) {
		var intImageIndex = parseInt(objImage.src.slice(-5, -4));
		var intArrayIndex = intImageIndex - 1;
		var strNewImageSRC = objImage.src.slice(0, -5) + String(intImageIndex + 1) + ".jpg";
		
		chdGeneral.arrImage_Heights[intArrayIndex] = objImage.offsetHeight;
		chdGeneral.arrImage_Widths[intArrayIndex] = objImage.offsetWidth;
		
		if ((intArrayIndex+1) < chdGeneral.arrImage_Heights.length) {
			var objImagePreload = document.getElementById("prjPreloadContainer");
			var imgNew = new Image();
			objImagePreload.appendChild(imgNew);
			imgNew.src = strNewImageSRC;
			rwScript.addEvent(imgNew, 'load', chdGeneral.imageLoad(imgNew), false);
		}
	},
	
	updateMainImage: function(e, targetElement) {
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;
		
		// Set new Image in place
		var objMainImage = document.getElementById('prjMain');
		objMainImage.style.width = 'auto';
		objMainImage.style.height = 'auto';
		objMainImage.src = el.href;
		
		// Update class of thumbnail links
		var objThumbnails = document.getElementById("prjThumbnails");
		var arrLinks = objThumbnails.getElementsByTagName('a');
		for (i=0; i<arrLinks.length; i++) {
			arrLinks[i].className = "";
		}
		el.className = "on";
		
		// Reset size of Main Image
		var strImageIndex = el.href.slice(-5, -4);
		var intImageIndex = parseInt(strImageIndex) - 1;
		var intImageHeight = chdGeneral.arrImage_Heights[intImageIndex];
//		var intImageWidth = chdGeneral.arrImage_Widths[intImageIndex];
//		var sngImageRatio = intImageHeight / intImageWidth;
		objMainImage.style.height = parseInt(chdGeneral.intCurrentImage_Height) + 'px';
//		objMainImage.style.width = parseInt(chdGeneral.intCurrentImage_Height / sngImageRatio) + 'px';

		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}

		if (e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
		return false;
	},
	getTNClick: function (node) { return function(e) { chdGeneral.updateMainImage(e, node); }; },
	cancelClick: function() { return false; },
	
	winResize: function() {

		// Get Window Dimensions
		if (window.innerWidth) {
			var intWinHeight = window.innerHeight;
			var intWinWidth = window.innerWidth;
		} else {
			var intWinHeight = document.documentElement.clientHeight;
			var intWinWidth = document.body.clientWidth;
		}

		// Get Image Object and Image Ratio
		var objMainImage = document.getElementById('prjMain');
		var sngImageRatio = objMainImage.offsetHeight / objMainImage.offsetWidth;
//		var sngImageRatio = chdGeneral.intImageOriginal_Height / chdGeneral.intImageOriginal_Width;
		
		// Determine New Image Height
		var intNewImageHeight = intWinHeight - chdGeneral.intPageStructure_Height;
		if (intNewImageHeight < chdGeneral.intImageMin_Height) {
			intNewImageHeight = chdGeneral.intImageMin_Height;
		}
		
		// Set Image Width, check against page width
		intNewImageWidth = intNewImageHeight / sngImageRatio;
		if (intNewImageWidth > (intWinWidth - chdGeneral.intPageStructure_Width)) {
			intNewImageWidth = intWinWidth - chdGeneral.intPageStructure_Width;
			if (intNewImageWidth < chdGeneral.intImageMin_Width) {
				intNewImageWidth = chdGeneral.intImageMin_Width;
			}
			intNewImageHeight = intNewImageWidth * sngImageRatio;
		}
		
		// Enforce Maximum Height
		if (intNewImageHeight > chdGeneral.intImageMax_Height) {
			intNewImageHeight = chdGeneral.intImageMax_Height;
			intNewImageWidth = intNewImageHeight / sngImageRatio;
		}

		// Enforce Maximum Width
		if (intNewImageWidth > chdGeneral.intImageMax_Width) {
			intNewImageWidth = chdGeneral.intImageMax_Width;
			intNewImageHeight = intNewImageWidth * sngImageRatio;
		}
		
		
		strNewImageHeight = parseInt(intNewImageHeight) + 'px';
		strNewImageWidth = parseInt(intNewImageWidth) + 'px';
		
		objMainImage.style.height = strNewImageHeight;
//		objMainImage.style.width = strNewImageWidth;
		
		chdGeneral.intCurrentImage_Height = intNewImageHeight;
		
		
		// Check position of project menu
		if (document.getElementById("list")) {
			var objProjectList = document.getElementById("list");
			var intNewListWidth = 0;
			if (intWinWidth > (chdGeneral.intMenuWidth + chdGeneral.intMenuSpacing)) {
				intNewListWidth = (chdGeneral.intMenuWidth_Project + (intWinWidth - (chdGeneral.intMenuWidth + chdGeneral.intMenuSpacing)));
				objProjectList.style.width = intNewListWidth + 'px';
			} else {
				objProjectList.style.width = chdGeneral.intMenuWidth_Project + 'px';
			}
		}
	},
	
	intImageMin_Width: 400,
	intImageMin_Height: 350,
	intImageMax_Width: 908,
	intImageMax_Height: 560,
	intPageStructure_Width: 400,
	intPageStructure_Height: 345,
	intImageOriginal_Width: 0,
	intImageOriginal_Height: 0,
	intCurrentImage_Height: 0,
	intMenuWidth: 520,
	intMenuSpacing: 950,
	intMenuWidth_Project: 220,
	arrImage_Heights: [],
	arrImage_Widths: [],
	arrPreloads: []
};

rwScript.addEvent(window, 'load', chdGeneral.addListeners, false);
rwScript.addEvent(window, 'unload', EventCache.flush, false);
