/*
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,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration = 0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 400;	// maximum image height.
var currentimagewidth = 400;	// maximum image width.

function addToLibraryArray(id) {
    libraryItems[libraryItems.length] = id
}

function gettrailobj() {
    return  gettrailobjnostyle().style;
}

function gettrailobjnostyle() {
   if (!$('trailimageid')) {
        var div = document.createElement('div');
        div.setAttribute("id", "trailimageid");
        document.getElementsByTagName("body")[0].appendChild(div);
    }
    return  $('trailimageid');
}

function truebody() {
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat")
            ? document.documentElement : document.body
}

function followmouse(e) {
    var xcoord = e.pointerX() + offsetfrommouse[0];
    var ycoord = e.pointerY() + offsetfrommouse[1];

    var cumHeight = currentimageheight + offsetfrommouse[1];
    var cumWidth = currentimagewidth + offsetfrommouse[0];

    var docWidth = document.viewport.getWidth() + document.viewport.getScrollOffsets().left;
    var docHeight = document.viewport.getHeight() + document.viewport.getScrollOffsets().top;
    if (docHeight - e.pointerY() < cumHeight) {
        ycoord = e.pointerY() - cumHeight;
    }
    if (docWidth - e.pointerX() < cumWidth) {
        xcoord = e.pointerX() - cumWidth;
    }

    gettrailobj().left = xcoord + "px"
    gettrailobj().top = ycoord + "px"

}

function showtrail(imagename, title, showthumb) {
    Event.observe(document, 'mousemove', followmouse);

    newHTML = '<div class="hoverpage">';
    newHTML = newHTML + '<h2>' + title + '</h2>';

    if (showthumb > 0) {
        newHTML = newHTML + '<div class="hoverimg"><img src="' + imagename + '"></div>';
    }

    newHTML = newHTML + '</div>';

    gettrailobjnostyle().innerHTML = newHTML;

    gettrailobj().visibility = "visible";
    currentimagewidth = gettrailobjnostyle().clientWidth;
    currentimageheight = $(gettrailobjnostyle()).childElements().reject(function(el) {
        return el.tagName.include('PANEL') || el.tagName.include('panel');
    })[0].getHeight();
}

var tooltipTimer;
function showActivityPopup(id) {

    gettrailobjnostyle().innerHTML = document.getElementById(id).innerHTML;
    if (tooltipTimer) {
        window.clearTimeout(tooltipTimer);
    }
    tooltipTimer = window.setTimeout(function() {
        gettrailobj().visibility = "visible";
    }, 500);
    currentimagewidth = gettrailobjnostyle().clientWidth;
    /*var elem = $(gettrailobjnostyle()).childElements().reject(function(el) {
        return el.tagName.include('PANEL') || el.tagName.include('panel');
    })[0];*/

    var elem = $(gettrailobjnostyle()).select('.hoverpage')[0];
    currentimageheight = elem.getHeight();
    Event.observe(document, 'mousemove', followmouse);

}

function showBarTooltip(message) {

    newHTML = '<div class="hoverpage">';
    newHTML = newHTML + message;
    newHTML = newHTML + '</div>';

    gettrailobjnostyle().innerHTML = newHTML;
    gettrailobj().visibility = "visible";
    currentimagewidth = gettrailobjnostyle().clientWidth;
    currentimageheight = gettrailobjnostyle().clientHeight;
    Event.observe(document, "mousemove", followmouse);
}

function hidetrail() {
    window.clearTimeout(tooltipTimer);
    Event.stopObserving(document, 'mousemove', followmouse);
    gettrailobj().visibility = "hidden"
}
