var productImages = [];	
function dwDisplayProductImages() {
	var rel = (productImages.gallery.length > 1) ? 'prettyPhoto[products]' : 'prettyPhoto'; 
	var displayHolder = $("#gallery-main");
	var galleryHolder = $("#gallery-other");

	if(productImages.active.display !== ''){
		writeImage(displayHolder, productImages.active, productImages.active.display, rel, 315, 315);
	}

	for (i = 1; i < productImages.gallery.length; i++) {
		if(productImages.gallery[i].thumbnail !== ''){
			writeImage(galleryHolder, productImages.gallery[i], productImages.gallery[i].thumbnail, rel, 75, 75);
		}
	}
	$("a[rel^='prettyPhoto']").prettyPhoto({overlay_gallery: false});
}

function writeImage(galleryHolder, image, displayImage, rel, resizeW, resizeH) {
	var html = '';
	html = $(document.createElement('a')).attr({
		'href': image.detailed,
		'rel': rel,
		'title': image.title
	}).append($(document.createElement('img')).load(function(){
		var img = this
		if(navigator.appVersion.indexOf('MSIE 7.0') > -1){
			img = new Image()
			img.src = this.src
		}
		var newSize = scaleImageSize(resizeW, resizeH, img.width, img.height);
		$(this).css({
			'width': Math.round(newSize[0]) + 'px',
			'height': Math.round(newSize[1]) + 'px'
		})
	}).attr({
		'src': displayImage,
		'alt': image.alt,
		'title': 'Click to enlarge'
	}).css({
		'margin': '3px'
	}))
	galleryHolder.append(html);
}

function scaleImageSize(maxW, maxH, currW, currH){
	var ratio = currH / currW;
	if(currW >= maxW && ratio <= 2){
		currW = maxW;
		currH = currW * ratio;
	} else if(currH >= maxH){
		currH = maxH;
		currW = currH / ratio;
	}
	return [currW, currH];
}

