function showPreview(obj) {
    document.getElementById('divProductMain').innerHTML = obj.parentNode.innerHTML.replace(/SmallThumb/g, 'BigThumb');
    return false;
}


var intPreviewWidth = 600;
var intPreviewHeight = 600;
var divZoomBox = document.getElementById('divZoomBox');
var divProductZoom = document.getElementById('divProductZoom');
var divProductMain = document.getElementById('divProductMain');
divProductMain.onmouseover = zoomOver;

function zoomOver(e) {
    divProductZoom.parentNode.style.visibility = 'visible';
    divProductZoom.innerHTML = divProductMain.innerHTML.replace("width=200", "width=600");
    divZoomBox.style.visibility = 'visible';
    divZoomBox.style.width = (divProductZoom.offsetWidth / intPreviewWidth) * divProductMain.offsetWidth + 'px';
    divZoomBox.style.height = (divProductZoom.offsetHeight / intPreviewHeight) * divProductMain.offsetHeight + 'px';
    divZoomBox.onmouseout = zoomOut;
    document.onmousemove = zoomMove;
    return false;
}

function zoomMove(e) {
    var posX;
    var posY;
    e = fixE(e);
    posX = e.clientX + f_scrollLeft();
    posY = e.clientY + f_scrollTop();
    
    posX = posX - findPosX(divProductMain) - (divZoomBox.offsetWidth / 2);
    posY = posY - findPosY(divProductMain) - (divZoomBox.offsetHeight / 2);
    if (posX < 0) {
        posX = 0;
    }
    else if ((posX + divZoomBox.offsetWidth) > divProductMain.offsetWidth) {
        posX = divProductMain.offsetWidth - divZoomBox.offsetWidth;
    }
    if (posY < 0) {
        posY = 0;
    }
    else if ((posY + divZoomBox.offsetHeight) > divProductMain.offsetHeight) {
        posY = divProductMain.offsetHeight - divZoomBox.offsetHeight;
    }
    divZoomBox.style.left = posX + 'px';
    divZoomBox.style.top = posY + 'px';
    divProductZoom.style.left = (0 - (posX * (intPreviewWidth / divProductZoom.offsetWidth))) + 'px';
    divProductZoom.style.top = (0 - (posY * (intPreviewHeight / divProductZoom.offsetHeight))) + 'px';
    if ((findPosX(divProductMain) + divProductMain.offsetWidth) < e.clientX) {
        zoomOut();
    }
    else if (e.clientX < findPosX(divProductMain)) {
        zoomOut();
    }

}

function zoomOut() {
    divProductZoom.parentNode.style.visibility = 'hidden';
    divZoomBox.style.visibility = 'hidden';
    document.onmousemove = null;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function f_clientWidth() {
    return f_filterResults(
        window.innerWidth ? window.innerWidth : 0,
        document.documentElement ? document.documentElement.clientWidth : 0,
        document.body ? document.body.clientWidth : 0
    );
}
function f_clientHeight() {
    return f_filterResults(
        window.innerHeight ? window.innerHeight : 0,
        document.documentElement ? document.documentElement.offsetHeight : 0,
        document.body ? document.body.offsetHeight : 0
    );
}
function f_scrollLeft() {
    return f_filterResults(
        window.pageXOffset ? window.pageXOffset : 0,
        document.documentElement ? document.documentElement.scrollLeft : 0,
        document.body ? document.body.scrollLeft : 0
    );
}
function f_scrollTop() {
    return f_filterResults(
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0
    );
}
function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function fixE(e) {
    if (typeof e == 'undefined') e = window.event;
    if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
    if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
    return e;
}
