/*
 * Display images. Show border and display large image
 */
var next_image;
function waitForNextImage(next_image, ad_id) {
	var ad_id = ad_id ? ad_id : "";
	var image = document.getElementById("display_image" + ad_id).firstChild;

	if (next_image.width > 0) {
		image.width = next_image.width;
		image.height = next_image.height;
		next_image = null;
	} else {
		setTimeout("waitForNextImage(next_image, ad_id)", 100);
	}
}

function resizeImage(image, path, next_image, admin) {
	if (!next_image) {
		next_image = new Image;
		next_image.src = path;
	}

	if (next_image.width == 0) {
		next_image.onload = setTimeout(function () { resizeImage(image, path, next_image, admin); }, 0);
		return;
	}

	image.src = next_image.src;

	if (admin && next_image.width > 400) {
		var factor = (next_image.width - 400) / next_image.width;
		image.height = next_image.height * (1 - factor);
		image.width = 400;
	} else {
		image.width = next_image.width;
		image.height = next_image.height;
	}
}

function showLargeImage(strDisplayPath, ad_id, admin) {
	var ad_id = ad_id ? ad_id : "";
	var admin = admin ? admin : false;
	var image = document.getElementById("display_image" + ad_id).firstChild;

	if (admin) {
		resizeImage(image, strDisplayPath, null, admin);
	} else {
		if (navigator.userAgent.toLowerCase().indexOf('safari') > 0) {
			next_image = new Image;
			next_image.src = strDisplayPath;

			image.src = next_image.src;
			waitForNextImage(next_image, ad_id);
		} else {
			image.src = strDisplayPath;
		}
	}
}

var styles = [];
function thumbnailBorder(thumb, image_num, ad_id) {
	var ad_id = ad_id ? ad_id : "";

	if (typeof(thumb.style) == 'undefined') {
		return false;
	}

	for (i = 0; i < image_num; i++) {
		var thumb_obj = document.getElementById('thumb' + i + ad_id);
		if (!styles[i]) {
			styles[i] = thumb_obj.className;
			if (styles[i].indexOf('ad_border_solid_black') > 0) {
				styles[i] = styles[i].substring(0, styles[i].indexOf('ad_border_solid_black')) + 'ad_border_solid_grey';
			}
		}
		thumb_obj.className = styles[i];

		if (thumb.id == thumb_obj.id) {
			thumb_obj.className = "ad_thumb ad_border_solid_black";
			document.getElementById('display_image' + ad_id).className = 'ad_pict' + ((styles[i].indexOf('ad_border_dotted') > 0)?' ad_border_dotted':'');
		}
	}
}


