/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/
var offsetfrommouse = [15, 0]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var anchorImageToCursor = false;
var offsetLeft;
var offsetTop;

function resetOffset() {
    offsetLeft = -150;
    offsetTop = 160;
}
resetOffset();

function gettrailobj() {
    if (document.getElementById) {
        return document.getElementById("imagePreviewContainerOuterDiv").style;
    } else if (document.all) {
        return document.all.imagePreviewContainerOuterDiv.style;
    }
}

function gettrailobjnostyle() {
    if (document.getElementById) {
        return document.getElementById("imagePreviewContainerOuterDiv");
    } else if (document.all) {
        return document.all.imagePreviewContainerOuterDiv;
    }
}

function truebody() {
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function hidetrail() {
    gettrailobjnostyle().innerHTML = "";
    gettrailobj().display = "none";
    // reset to call positiontip in tooltip.js
    //document.onmousemove = positiontip;
    gettrailobj().left = "-1500px";
    resetOffset();
}

function showtrail(imagename, maxWidth, maxHeight, minWidth, minHeight, displayIfMin, anchorToCursor, enlargeItemImage) {
    if (enlargeItemImage != null && enlargeItemImage == true) {
        offsetLeft = 320;
        offsetTop = 150;
    }
    anchorImageToCursor = anchorToCursor;
    document.onmousemove = followmouse;
    var html = '<div id="imagePreviewContainerInnerDiv"><div id="imagePreviewDiv"><img id="largeImage"src="/listings/images/stock/page/ajax-loader.gif"alt="loading image"/></div></div>';
    gettrailobjnostyle().innerHTML = html;
    gettrailobj().display = "inline";
    var image = new Image();
    image.onload = function () {
        var width = image.width;
        var height = image.height;
        var solwidth = 500;
        var solheight = 500;
        var scaleRatio;
        if (width > maxWidth || height > maxHeight) {
            if (width > height) {
                scaleRatio = width / maxWidth;
                width = width / scaleRatio;
                height = height / scaleRatio;
            } else {
                scaleRatio = height / maxHeight;
                width = width / scaleRatio;
                height = height / scaleRatio;
            }
        }
        if (!displayIfMin && (width <= minWidth && height <= minHeight)) {
            hidetrail();
        }
        if (document.getElementById('largeImage') != undefined) {
            document.getElementById('largeImage').width = width;
            document.getElementById('largeImage').height = height;
            document.getElementById('largeImage').src = imagename;
        }
    }
    image.src = imagename;
}

function followmouse(e) {
    var xcoord = offsetfrommouse[0];
    var ycoord = offsetfrommouse[1];
    var largeImage = document.getElementById('largeImage');
    var imagePaddingHeight = 45;
    var imagePaddingWidth = 35;
    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15;
    var docheight = document.all ? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight);

    if (typeof e != "undefined" && largeImage != undefined) {

        //Flip image to the top of the pointer
        if (docheight - e.pageY < (largeImage.height + imagePaddingHeight)) {
            ycoord += e.pageY - Math.max(0, (imagePaddingHeight + largeImage.height + e.pageY - docheight - truebody().scrollTop));
        } else {
            ycoord += e.pageY;
        }
    } else if (typeof window.event != "undefined" && largeImage != undefined) {
        if (docheight - event.clientY < (largeImage.height + imagePaddingHeight)) {
            ycoord += event.clientY + truebody().scrollTop - Math.max(0, (imagePaddingHeight + largeImage.height + event.clientY - docheight));
        } else {
            ycoord += truebody().scrollTop + event.clientY;
        }
    }

    gettrailobj().display = "block";
    gettrailobj().left = offsetLeft + "px";
    gettrailobj().top = ycoord - offsetTop + "px";
}
