/// First check for IE4+ on Mac
sshw_blnIsIEOnMac = incJavaScriptBrowser.ie4 && incJavaScriptBrowser.mac;

/// If IE4+ on Mac, then build hidden image for reading original image dimensions
if (sshw_blnIsIEOnMac) {
	document.write('<div style="left: -5000px; top: 0px; position: absolute;">');
	document.write('<img name="originalImageForIEOnMac">');
	document.write('</div>');
}

function sshw_changeSlide(aintShowId, aintDirection, aintSlideId) {
	if (document.images) {

		var intSlideId = sshw_intSlideId[aintShowId] = sshw_recur(aintShowId, (aintDirection == 0 ? aintSlideId : sshw_intSlideId[aintShowId] + aintDirection));

		var objImage = sshw_arrImages[aintShowId][intSlideId] = sshw_cacheImage(sshw_arrImages[aintShowId][intSlideId]);

// ### alert(aintShowId + ": " + aintSlideId);

		sshw_resizeAndDisplayImage(aintShowId);

		/// Cache previous and next image
		sshw_arrImages[aintShowId][sshw_recur(aintShowId, sshw_intSlideId[aintShowId] - 1)] = sshw_cacheImage(sshw_arrImages[aintShowId][sshw_recur(aintShowId, sshw_intSlideId[aintShowId] - 1)]);
		sshw_arrImages[aintShowId][sshw_recur(aintShowId, sshw_intSlideId[aintShowId] + 1)] = sshw_cacheImage(sshw_arrImages[aintShowId][sshw_recur(aintShowId, sshw_intSlideId[aintShowId] + 1)]);

	}
}

function sshw_recur(aintShowId, aintSlideId) {
	if (aintSlideId >= sshw_intImagesCount[aintShowId]) {
		return 0;
	}
	if (aintSlideId < 0) {
		return (sshw_intImagesCount[aintShowId] - 1);
	}
	return aintSlideId;
}

function sshw_cacheImage(aImageSourceOrObject) {

	if (typeof aImageSourceOrObject == "string") { /// Only cache each image once
		var objImage = new Image();
		objImage.src = aImageSourceOrObject;
		/// If Mac and IE
		if (sshw_blnIsIEOnMac) {
			document.originalImageForIEOnMac.src = objImage.src
		}
		return objImage;
	} else { /// Parameter is allready image object
		if (sshw_blnIsIEOnMac) {
			document.originalImageForIEOnMac.src = aImageSourceOrObject.src
		}
		return aImageSourceOrObject;
	}
}

function sshw_resizeAndDisplayImage(aintShowId) {

	sshw_blnChangeImageIsComplete[aintShowId] = false;
	
	/// If the image is not yet downloaded, then wait 200 ms and try again
	if (!sshw_arrImages[aintShowId][sshw_intSlideId[aintShowId]].complete) {
		setTimeout('sshw_resizeAndDisplayImage(' + aintShowId + ')', 200);
		return;
	}

	if (document.layers) {
		var imgLayer = lobj_getLayerObject("slideShowLayer" + aintShowId);
	} else {
		/// If IE 4+ or NS6
		var image = document.images("slideShow" + aintShowId);
	}

	var intWidth;
	var intHeight;

	/// If width > 0 then set width of image
	if (sshw_intWidth[aintShowId] > 0) {intWidth = sshw_intWidth[aintShowId];}
	/// If height > 0 then set height of image
	if (sshw_intHeight[aintShowId] > 0) {intHeight = sshw_intHeight[aintShowId];}

	/// If width = 0 then retrieve original width of image
	if (sshw_intWidth[aintShowId] == 0) {intWidth = Math.floor((sshw_blnIsIEOnMac ? document.originalImageForIEOnMac.width : sshw_arrImages[aintShowId][sshw_intSlideId[aintShowId]].width) * sshw_intResizeFactor[aintShowId] / 100);}
	/// If height = 0 then retrieve original height of image
	if (sshw_intHeight[aintShowId] == 0) {intHeight = Math.floor((sshw_blnIsIEOnMac ? document.originalImageForIEOnMac.height : sshw_arrImages[aintShowId][sshw_intSlideId[aintShowId]].height) * sshw_intResizeFactor[aintShowId] / 100);}

	/// If width = -1 then width = width of canvas (same as 100%)
	if (sshw_intWidth[aintShowId] == -1) {intWidth = sshw_intInnerCanvasWidth[aintShowId];}
	/// If height = -1 then height = height of canvas (same as 100%)
	if (sshw_intHeight[aintShowId] == -1) {intHeight = sshw_intInnerCanvasHeight[aintShowId];}

	/// Find the ratio to be used for adjusting width and height to canvas
	var intWidthRatio = sshw_intInnerCanvasWidth[aintShowId] / intWidth;
	var intHeightRatio = sshw_intInnerCanvasHeight[aintShowId] / intHeight;
	var intRatio = intWidthRatio <= intHeightRatio ? intWidthRatio : intHeightRatio;
	
	/// Adjust/fit to canvas, if image is larger than canvas or
	/// images smaller than canvas are to be fit to canvas
	if (intRatio < 1 || sshw_blnFitToCanvas[aintShowId]) {
		if (sshw_blnPreserveRatio[aintShowId]) {
			intWidth = Math.floor(intWidth * intRatio);
			intHeight = Math.floor(intHeight * intRatio);
		} else {
			/// If ratio is not to be preserved
			/// then change to canvas size
			intWidth = sshw_intInnerCanvasWidth[aintShowId];
			intHeight = sshw_intInnerCanvasHeight[aintShowId];
		}
	}

	/// If NS 4+
	if (document.layers) {

		imgLayer.clip.width = sshw_intInnerCanvasWidth[aintShowId]; 
		imgLayer.clip.height = sshw_intInnerCanvasHeight[aintShowId];

		var html = '';
		html += '<table width="' + sshw_intInnerCanvasWidth[aintShowId] + '" height="' + sshw_intInnerCanvasHeight[aintShowId] + '" border="0" cellpadding="0" cellspacing="0" bgcolor="' + sshw_strCanvasBgColor[aintShowId] + '" background="' + sshw_strCanvasBgImage[aintShowId] + '"><tr><td align="center" valign="middle">'
		html += '<img src="' + sshw_arrImages[aintShowId][sshw_intSlideId[aintShowId]].src + '"';
		html += ' width="' + intWidth + '" height="' + intHeight + '">';
		html += '</td></tr></table>';
		imgLayer.document.open();
		imgLayer.document.write(html);
		imgLayer.document.close();
		imgLayer.visibility = 'show';
	} else {
		/// If IE 4+ or NS6
		image.width = intWidth;
		image.height = intHeight;
		image.src = sshw_arrImages[aintShowId][sshw_intSlideId[aintShowId]].src;
	}

	/// If slideshow has select field, then change selection
	if (sshw_blnHasSelect[aintShowId]) {
		if (document.layers) {
			var formLayer = lobj_getLayerObject("slideShowFormLayer" + aintShowId);
		}
		eval((document.layers ? 'formLayer.' : '') + "document.slideShowForm" + aintShowId + ".slideShowSelect" + aintShowId + ".options[" + sshw_intSlideId[aintShowId] + "].selected = true");
	}

	sshw_blnChangeImageIsComplete[aintShowId] = true;

}

function sshw_runAutomaticShow(aintShowId, aintDirection, ablnDisplayRandom) {
	if (sshw_intImagesCount[aintShowId] > 1) {
		clearTimeout(sshw_automaticShowTimeout[aintShowId]);
		if (sshw_blnChangeImageIsComplete[aintShowId]) {
			sshw_changeSlide(aintShowId, aintDirection, (ablnDisplayRandom ? sshw_randomSlideId(aintShowId) : 0));
			sshw_automaticShowTimeout[aintShowId] = setTimeout("sshw_runAutomaticShow(" + aintShowId + ", " + aintDirection + ", " + ablnDisplayRandom + ")",  sshw_intAutomaticShowTimeInterval[aintShowId]);
		} else {
			sshw_automaticShowTimeout[aintShowId] = setTimeout("sshw_runAutomaticShow(" + aintShowId + ", " + aintDirection + ", " + ablnDisplayRandom + ")",  200);
		}
	}
}

function sshw_stopAutomaticShow(aintShowId) {
	clearTimeout(sshw_automaticShowTimeout[aintShowId]);
}

function sshw_randomSlideId(aintShowId) {
	var intSlideId = Math.floor((Math.random() * sshw_intImagesCount[aintShowId]))
	/// If random slide is the same as active slide, then try again
	if (intSlideId == sshw_intSlideId[aintShowId]) {
		return sshw_randomSlideId(aintShowId);
	}
	return intSlideId;
}

function sshw_displaySelect(aintShowId) {
	selectLayer = lobj_getLayerObject('slideShowFormLayer' + aintShowId);
	var html = '';
	html += '<table width="' + sshw_intCanvasWidth[aintShowId] + '" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="' + sshw_strSelectBgColor[aintShowId] + '"><tr>'
	html += '<form name="slideShowForm' + aintShowId + '"><td align="center"><select name="slideShowSelect' + aintShowId + '" onChange="sshw_changeSlideOnSelect(' + aintShowId + ', 0, this.selectedIndex)">' + sshw_strSelectOptions[aintShowId] + '</select></td></form></tr></table>';
/// Implement the following code to adjust size of select.
/// Beware of problems with NS: width only defines minimum, not maximum size
///  width="' + (sshw_intCanvasWidth[aintShowId] - 10) + '"
	selectLayer.document.open();
	selectLayer.document.write(html);
	selectLayer.document.close();
	selectLayer.visibility = 'show';
}

function sshw_changeSlideOnSelect(aintShowId, aintSelectedSlideId) {
	clearTimeout(sshw_automaticShowTimeout[aintShowId]);
	if (document.layers) {
		var formLayer = lobj_getLayerObject("slideShowFormLayer" + aintShowId);
		eval("var intSelectedSlideId = formLayer.document.slideShowForm" + aintShowId + ".slideShowSelect" + aintShowId + ".selectedIndex");
	} else {
		/// If IE 4+ or NS6
		var intSelectedSlideId = aintSelectedSlideId;
	}
	sshw_changeSlide(aintShowId, 0, intSelectedSlideId);
}
