﻿var WSIsResizing = false;
var WSResizeImgArray = new Array();
var WSThreads = new Array();
var WSImages = new Array();


function WSResize(oElem, iMaxWidth, iMaxHeight, bStretch, topcategory) {
    if(topcategory == 'clearance')
    {
        WSImages[WSImages.length] = new Image();
        WSImages[WSImages.length - 1].src = oElem.src;
        WSResizeImgArray[WSResizeImgArray.length] = oElem;
        tmpIndex = WSResizeImgArray.length;
        sWSCall_WSAddImage = "WSCall_WSAddImage = new Function(\"\",\"WSAddImage(" + new String(WSImages.length - 1) + "," + new String(iMaxWidth) + "," + new String(iMaxHeight) + "," + new String(bStretch) + ");\")";
        eval(sWSCall_WSAddImage);
        WSImages[WSImages.length - 1].onload = new WSCall_WSAddImage();
    }
    else
    {
        oElem.width = iMaxWidth;
        oElem.height = iMaxHeight;
        oElem.style.visibility = "visible";

        var oParent = WSGetParentByTag(oElem, "SPAN", 4);
        if (oParent != null) oParent.style.display = "";
    }
}
function WSAddImage(iIndex, iMaxWidth, iMaxHeight, bStretch) {
    propStr = "WSDoResize(" + new String((iIndex)) + "," + new String(iMaxWidth) + "," + new String(iMaxHeight) + "," + new String(bStretch) + ")";
    WSThreads[tmpIndex - 1] = setInterval(propStr, 200);
}

function WSDoResize(iIndex, iMaxWidth, iMaxHeight, bStretch) {
    var WSResizeDone = WSResizeImg(WSResizeImgArray[iIndex], iIndex, iMaxWidth, iMaxHeight, bStretch);
    if (WSResizeDone != -1) {
        clearInterval(WSThreads[iIndex]);
    }
}
function WSResizeImg(oElem, iIndex, iMaxWidth, iMaxHeight, bStretch) {
    if (WSIsResizing || WSImages[iIndex].width == 0 || WSImages[iIndex].height == 0) return -1;
    WSIsResizing = true;
    var width = WSImages[iIndex].width;
    var height = WSImages[iIndex].height;
    var maxWidth = iMaxWidth;
    var maxHeight = iMaxHeight;
    var maxRatio = maxWidth / maxHeight;
    var imgRatio = width / height;
    var newWidth = width;
    var newHeight = height;

    if (width > maxWidth) {
        newWidth = maxWidth;
        newHeight = Math.round(maxWidth / (imgRatio));
    }
    else if (height > maxHeight) {
        newWidth = Math.round((maxWidth * (imgRatio / maxRatio)));
        newHeight = maxHeight;
    }

    if (newWidth > maxWidth || newHeight > maxHeight) {
        ratioWidth = newWidth / maxWidth;
        ratioHeight = newHeight / maxHeight;
        if (ratioWidth > ratioHeight) {
            newWidth = maxWidth;
            newHeight = Math.round(maxWidth / (imgRatio));
        } else {
            newWidth = Math.round((maxWidth * (imgRatio / maxRatio)));
            newHeight = maxHeight;

        }
    }

    if (bStretch) {
        if ((newWidth < maxWidth) && (newHeight < maxHeight)) {
            var widthRatio = maxWidth / newWidth;
            var heightRatio = maxHeight / newHeight;
            if (widthRatio < heightRatio) {
                newWidth = Math.round(newWidth * widthRatio);
                newHeight = Math.round(newHeight * widthRatio);
            }
            else {
                newWidth = Math.round(newWidth * heightRatio);
                newHeight = Math.round(newHeight * heightRatio);
            }
        }
    }

    oElem.width = newWidth;
    oElem.height = newHeight;

    oParent = WSGetParentByTag(oElem, "SPAN", 4);
    
    oElem.style.visibility = "visible";

    if (oParent != null) oParent.style.display = "";
    WSIsResizing = false;
    return iIndex;
}

function WSGetParentByTag(oElem, sTag, iDepth) {
    oParent = oElem.parentNode;
    if (oParent == null) return null;
    if (oParent.nodeName === sTag) { return oParent; }
    if (iDepth == 0) return null;
    return WSGetParentByTag(oParent, sTag, --iDepth);
}

