function RollOver(imgPath, objId) {
	this.rolloverid = objId;
	this.wmUrl = "http://www.mirrorway.com/idmark.php?h=?1&w=?2&i=?3&hk=$T_ldiDFJKE2" + i;
	this.imagePath = imgPath;
	this.viewedImage = new Image();
	this.thumbImage = new Image();
	this.loadImages = loadImages;
	this.renderThumb = renderThumb;
	this.imageLoaded = 0;
}

function loadImages(thumbH, thumbW, viewH, viewW) {
	this.thumbImage.rollover = this;
	this.thumbImage.onload = this.renderThumb;
	this.thumbImage.src = this.wmUrl.replace("?1", thumbH).replace("?2", thumbW).replace("?3", this.imagePath);

	this.viewedImage.onload = this.renderThumb;
	this.viewedImage.rollover = this;
	this.viewedImage.src =  this.wmUrl.replace("?1", viewH).replace("?2", viewW).replace("?3", this.imagePath);
}

function renderThumb() {
	if(++this.rollover.imageLoaded == 2) {
		thumbOutput = "<a class='deselectedThumb' href='javascript:void(0)' " + 
			"border='0' onmouseout='deselectThumb(this);' onmouseover=\"imageSwap(this,'productDisplay', '" + 
			this.rollover.viewedImage.src + "','1'" + ");selectThumb(this);\"><img border=0 src='" + this.rollover.thumbImage.src + 
			"' /></a>&nbsp;";
			document.getElementById('imgThumbs').innerHTML += thumbOutput;
	}
}

function restoreDefaultImage(origImg, targetH, targetW) { 
	if (!event.fromElement.contains(event.toElement) && 
		!document.getElementById('imgThumbs').contains(event.toElement)) { 
		imageSwap(this,'productDisplay', origImg, '1');
	} 
}

function loadAdditionalImageThumbnails(baseImgName, additionalImages, thumbH, thumbW, targetH, targetW) {
	try {
			var imgArr = additionalImages.split(",");
			for(i=0;i<=imgArr.length;i++) {
				var rol1 = new RollOver("http://www.mirrorway.com/product_img/" + imgArr[i], 1);
				rol1.loadImages(thumbH, thumbW, targetH, targetW);
			}
		} catch (Except) {
		}
}

function parseImageName(imgName) {
	var lastSlash = imgName.lastIndexOf('/');
	return interimImgName = imgName.substring((lastSlash + 1), imgName.length);
}

function selectThumb(thumbObj) {
	thumbObj.className = "selectedThumb";
}

function deselectThumb(thumbObj) {
	thumbObj.className = "deselectedThumb";
}

function resizeImageToTarget(targetH, targetW, imgH, imgW) {
	targetHInt = parseInt(targetH);
	targetWInt = parseInt(targetW);
	imgHInt = parseInt(imgH);
	imgWInt = parseInt(imgW);

	if(imgH >= imgW) {
		imgRatio = imgW/imgH;
		resultImgH = targetH;
		resultImgW = Math.round(resultImgH * imgRatio);
	} else {
		imgRatio = imgH/imgW;
		resultImgW = targetW;
		resultImgH = Math.round(resultImgW * imgRatio);
	}
	return new Array(resultImgH, resultImgW);
}

function imageSwap(hLinkObj, oldImgId, newImgSrc, speed) {
	var newImg = new Image();
	newImg.src = newImgSrc;

	var oldImg = document.getElementById(oldImgId);
	if(oldImg.src == newImg.src) {
		return;
	}

	try{
		transitionImage(oldImg, newImgSrc, speed);
	} catch (Except) {}
}

var isf = { 'clock' : null, 'fade' : true, 'count' : 1 }

function transitionImage()
{	
	//if the timer is not already going
	if(isf.clock == null)
	{
		//copy the image object 
		isf.obj = arguments[0];
		
		//copy the image src argument 
		isf.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof isf.obj.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
		}
		else if(typeof isf.obj.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
		}
		else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
		}
		else if(typeof isf.obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			isf.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[5] != 'undefined' && arguments[5] != '')
		{
			isf.obj.alt = arguments[5];
		}
		
		//if any kind of opacity is supported
		if(isf.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isf.length = parseInt(arguments[2], 10) * 10;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isf.clock = setInterval('isf.transitionImage()', isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf.obj.src = isf.src;
		}
		
	}
};

isf.transitionImage = function()
{
	//increase or reduce the counter on an exponential scale
	isf.count = (isf.fade) ? isf.count * 0.4 : (isf.count * (1/0.4)); 
	
	//if the counter has reached the bottom
	if(isf.count < (1 / isf.resolution))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//do the image swap
		isf.obj.src = isf.src;
		//isf.obj.height = (isf.newHeight);
		//isf.obj.width = isf.newWidth;

		//reverse the fade direction flag
		isf.fade = false;
		
		//restart the timer
		isf.clock = setInterval('isf.transitionImage()', isf.length/isf.resolution);

	}
	
	//if the counter has reached the top
	if(isf.count > (1 - (1 / isf.resolution)))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//reset the fade direction flag
		isf.fade = true;
		
		//reset the counter
		isf.count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isf.type)
	{
		case 'ie' :
			isf.obj.filters.alpha.opacity = isf.count * 100;
			break;
			
		case 'khtml' :
			isf.obj.style.KhtmlOpacity = isf.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
	}
};
