﻿var zinImgScale_dimensions = [];

function zinImgScale_handleWindowChange() {
	var id;
	
	if(typeof zinImgScale_target == 'undefined') {
		id = '#container';
	} else {
		id = zinImgScale_target;
	}
	var images = $(id).find('img');
	zinImgScale_debug("image length: " + images.length);
	// DEBUG:
	var xRes, yRes;
	
	if(window.innerWidth) {
		xRes = window.innerWidth;
		yRes = window.innerHeight;
	} else {
		xRes = document.body.clientWidth;
		yres = document.body.clientHeight;
	}
	for(var i = 0; i < images.length; i++) {
		zinImgScale_debug('changing size of image #' + i);
		if (!zinImgScale_changeSize(i, images[i], xRes, yRes)) return;
	}
}

function zinImgScale_debug(msg) {
	if(document.getElementById('debug')) {
		document.getElementById('debug').innerHTML = msg + "\r\n" + document.getElementById('debug').innerHTML;
	}
}

function zinImgScale_changeSize(arrayIndex, target, maxW, maxH) {
	zinImgScale_debug("max width: " + maxW + ", max height: " + maxH);
	
	if(!zinImgScale_dimensions[arrayIndex]) {
		zinImgScale_debug("setting new image dimensions: " + target.offsetWidth + ", " + target.offsetHeight);
		if(target.offsetWidth > 0) {
			zinImgScale_dimensions[arrayIndex] = new Object({
				imgW:	target.offsetWidth,
				imgH:	target.offsetHeight
			});
		} else {
			if(!target.clientWidth || target.clientHeight) {
				zinImgScale_debug('returning, clientWidth or clientHeight not numeric');
				return false;
			}
			zinImgScale_dimensions[arrayIndex] = new Object({
				imgW:	target.clientWidth,
				imgH:	target.clientHeight
			});
			zinImgScale_debug('using clientWidth, clientHeight');
		}
	}
	
	$(target).imageView({maxW, maxH});
	
	/*
	var imgW = zinImgScale_dimensions[arrayIndex].imgW;
	var imgH = zinImgScale_dimensions[arrayIndex].imgH;
	var windowAspectRatio = maxW / maxH;
	
	if(maxW < imgW || maxH < imgH) {
		if(imgW * windowAspectRatio > maxH) {
			var targetWidth = maxW;
			var targetHeight = maxW / windowAspectRatio;
			zinImgScale_debug("setting 100% of screen width");
		} else {
			var targetWidth = maxW / windowAspectRatio;
			var targetHeight = maxH;
			zinImgScale_debug("setting 100% of screen height");
		}
		zinImgScale_debug("target width: " + targetWidth + ", target height: " + targetHeight + " (aspect ratio: " + windowAspectRatio+")");
		target.setAttribute("width", targetWidth);
		target.setAttribute("height", targetHeight);
	} else {
		zinImgScale_debug("not too large - inner window dimensions: " + maxW + ", " + maxH);
		target.setAttribute("width", maxW);
		target.setAttribute("height", maxH);
	}
	*/
	return true;
}

function zinImgScale_init() {
	// onload function
	var bodyTags = document.getElementsByTagName('body');
	if(bodyTags[0]) {
		if(typeof zinImgScale_target == 'undefined') return;
		if(typeof $ == 'undefined') return;
		var tmpObj = $(zinImgScale_target);
		if(!tmpObj) return;
		
		var onload = bodyTags[0].getAttribute('onload');
		if (onload) {
			bodyTags[0].setAttribute('onload', 'zinImgScale_handleWindowChange(); ' + onUnload);
		} else {
			bodyTags[0].setAttribute('onload', 'zinImgScale_handleWindowChange()');
		}
	}
	// resize handler
	window.onresize=zinImgScale_handleWindowChange;
}

zinImgScale_init();

