/*if (typeof BrowserDetect == "undefined")
   document.write("<script src='includes/javascript/browser_detect.js'></script>");*/

var debug = 0;
var _gar_timer;
var X;
var Y;

document.write('<div id="trailimageid" style="position: absolute;display: none;left: 0px;top: 0px;z-index: 200;"></div>');
document.write('<div id="trailmenuid" style="position: absolute;display: none;left: 0px;top: 0px;z-index: 200;"></div>');

function gettrailobj()
{
    if (document.getElementById)
        return document.getElementById("trailimageid").style
    else if (document.all)
        return document.all.trailimagid.style
}

function gettrailobjnostyle()
{
    if (document.getElementById)
        return document.getElementById("trailimageid")
    else if (document.all)
        return document.all.trailimagid
}

function getmenuobj()
{
    if (document.getElementById)
        return document.getElementById("trailmenuid").style
    else if (document.all)
        return document.all.trailmenuid.style
}

function getmenuobjnostyle()
{
    if (document.getElementById)
        return document.getElementById("trailmenuid")
    else if (document.all)
        return document.all.trailmenuid
}

function truebody()
{
    return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(imagename)
{
	document.onmousemove=followmouse;

	if (debug != 1)
	{
	    newHTML = '<div style="padding: 0px; background-color: #FFF; border: 1px solid #888;">';
	    newHTML = newHTML + '<div align="center" style="padding: 2px 2px 2px 2px;">';
	    newHTML = newHTML + '<img src="' + imagename + '" border="0"></div>';
	    newHTML = newHTML + '</div>';
	}
	else
	{
	    newHTML =           '<table width=500 height=500 style="background:white;border:1px solid black;">';
	    newHTML = newHTML + '<tr><td>document.body.offset<br>taille de la fenetre client</td><td>y:<br>x:</td><td><span id="pos1_y">0</span> px<br><span id="pos1_x">0</span> px</td></tr>';
	    newHTML = newHTML + '<tr><td>docwidth/docheight<br></td><td>y:<br>x:</td><td><span id="pos2_y">0</span> px<br><span id="pos2_x">0</span> px</td></tr>';
	    newHTML = newHTML + '<tr><td>offset<br></td><td>y:<br>x:</td><td><span id="pos3_y">0</span> px<br><span id="pos3_x">0</span> px</td></tr>';
	    newHTML = newHTML + '<tr><td>mouse<br></td><td>y:<br>x:</td><td><span id="pos4_y">0</span> px<br><span id="pos4_x">0</span> px</td></tr>';
	    newHTML = newHTML + '<tr><td>div<br></td><td>y:<br>x:</td><td><span id="pos5_y">0</span> px<br><span id="pos5_x">0</span> px</td></tr>';
	    newHTML = newHTML + '<tr><td>left :<span id="left">0</span></td><td><span id="right">0</span>:right</td><td><span id="positionLR">right</span></td></tr>';
	    newHTML = newHTML + '<tr><td>top :<span id="top">0</span></td><td><span id="bottom">0</span>:bottom</td><td><span id="positionTB">bottom</span></td></tr>';
	    newHTML = newHTML + '</table>';
	}
	gettrailobjnostyle().innerHTML = newHTML;
}

function hidetrail(){
	gettrailobj().innerHTML = " ";
	gettrailobj().display="none"
	document.onmousemove=""
	gettrailobj().left="-500px";
}
function get_position(element) 
{
    var p = {x: element.offsetLeft || 0, y:element.offsetTop || 0};
    while (element = element.offsetParent) {
        p.x += element.offsetLeft;
        p.y += element.offsetTop;
    }
    if (!p)
        p = {x: element.offsetWidth || 0, y:element.offsetHeight || 0};
    return p;
}

function posmouse(e)
{
    X = (typeof e != "undefined") ? e.pageX : event.clientX;
    Y = (typeof e != "undefined") ? e.pageY : event.clientY;    
}

function followmouse(e)
{
	var xcoord=0
	var ycoord=0
	
	var mouseX = (typeof e != "undefined") ? e.pageX : event.clientX;
    var mouseY = (typeof e != "undefined") ? e.pageY : event.clientY;
    var divWidth = gettrailobjnostyle().clientWidth;
    var divHeight = gettrailobjnostyle().clientHeight;
    var scrollL = truebody().scrollLeft;
    var scrollT = truebody().scrollTop;
    
	var docwidth=document.all? scrollL+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
	
    var positionLR = "right";
    var positionTB = "bottom";    
    
    var top = mouseY; 
    var bottom = docheight - mouseY;
    var left = mouseX;
    var right = docwidth - mouseX;
    if (right > divWidth + 10)
    {
        positionLR = "right";
        xcoord = scrollL+ mouseX + 10
    }
    else if (left > divWidth + 10)
    {
        positionLR = "left";
        xcoord = mouseX + scrollL - divWidth - 10;
    }
    else
    {
        if (left > right)
        {
            positionLR = "left";
            xcoord = mouseX + scrollL - divWidth - 10;
        }
        else
        {
            positionLR = "right";
            xcoord = scrollL+mouseX + 10
        }
    }
        
    if ( bottom > divHeight + 10)
    {
        positionTB = "bottom";
        ycoord += scrollT + mouseY + 10;
    }
    else
    {
        positionTB = "bottom";
        ycoord += scrollT + mouseY + 10;
    }

    if (debug)
    {
        document.getElementById('pos1_x').innerHTML = document.body.offsetHeight;
        document.getElementById('pos1_y').innerHTML = document.body.offsetWidth;
        document.getElementById('pos2_x').innerHTML = docheight;
        document.getElementById('pos2_y').innerHTML = docwidth;
        document.getElementById('pos3_x').innerHTML = xcoord;
        document.getElementById('pos3_y').innerHTML = ycoord;
        document.getElementById('pos4_x').innerHTML = mouseX;
        document.getElementById('pos4_y').innerHTML = mouseY;
        document.getElementById('pos5_x').innerHTML = divHeight;
        document.getElementById('pos5_y').innerHTML = divWidth;
        document.getElementById('top').innerHTML    = top;
        document.getElementById('bottom').innerHTML = bottom;
        document.getElementById('left').innerHTML   = left;
        document.getElementById('right').innerHTML  = right;
        document.getElementById('positionLR').innerHTML  = positionLR;
        document.getElementById('positionTB').innerHTML  = positionTB;
    }	

	if (xcoord + divWidth > docwidth  + scrollL)        xcoord = docwidth + scrollL - divWidth;
	if (ycoord + divHeight > docheight + scrollT)       ycoord = docheight + scrollT - divHeight;
	if (ycoord - scrollT < 0) 	   ycoord = scrollT; 
	if (xcoord - scrollL < 0)      xcoord = scrollL;

	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
	if (gettrailobj().display=="none")
		gettrailobj().display="inline";
}