﻿var offsetfrommouse=[15, -15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var currentimagewidth = 600;	// maximum image width.
var currentimageheight = 400;	// maximum image height.
var prvObj;

if (document.getElementById || document.all)
{
	document.write('<div id="trailimageid" style="width:100; height:100;">');
	document.write('</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 truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(imagename, thumb, title, description, showpreview, width, height, ck)
{
    if (ck == "yes")
    {
        if (showpreview <= 0 || get_cookie("user_preview") != "yes") return;
    }
    else
    {
        if (showpreview <= 0) return;
    }

	if (width == 0 && height == 0)
	{
		var pv = new Image();

		pv.src = imagename;

		currentimagewidth = pv.width;
		currentimageheight = pv.height;
	}
	else
	{
		if (width > 0) { currentimagewidth = width; }
		if (height > 0) { currentimageheight = height; }
	}

	document.onmousemove=followmouse;

	newHTML = '<div style="padding: 0px; background-color: #FFF;">';
	newHTML = newHTML + '<table cellspacing="0" cellpadding="0" border="0" style="border: 1px solid #a0a0a0;"><tr><td bgcolor="#f0f0f0" style="padding: 5px;">';
	newHTML = newHTML + '<b><font color=#333333>' + title + '</b></font>';
    hewHTML = newHTML + '</td></tr></table>';

	newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;">';
	newHTML = newHTML + '<img name="realpreview" id="realpreview" src="' + imagename + '" border="0" width="'+width+'" height="'+height+'" style="display:none;">';
	newHTML = newHTML + '<img name="loadingpreview" id="loadingpreview" src="' + thumb + '" border="0" width="'+width+'" height="'+height+'" style="display:;">';
	newHTML = newHTML + '<div name="loader" id="loader" style="border-width:0px;margin:0px;padding:0px;z-index:100;position:absolute;width:43px;left:'+((width/2)-15)+'px;top:'+((height/2)-10)+'px;"><img src="/kr/imgs/loader.gif"></div>';
	newHTML = newHTML + '</div>';

	newHTML = newHTML + '</div>';

	prvObj = setTimeout("checkPreviewLoading()",100);

	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().visibility="visible";
}

function checkPreviewLoading()
{
	var real;
	var loading, loader;
	
	if (document.getElementById) 
	{
		real = document.getElementById('realpreview');
		loading = document.getElementById('loadingpreview');
		loader = document.getElementById('loader');
	}
	else if (document.all) 
	{
		imgreal = document.all.realpreview;
		loading = document.all.loadingpreview;
		loader = document.all.loader;
	}
	
	if (real.complete)
	{
		loading.style.display = 'none';
		loader.style.display = 'none';
		real.style.display = '';
		
		clearTimeout(prvObj);
	}
	else prvObj = setTimeout("checkPreviewLoading()",100);
}

function hidetrail()
{
	if (prvObj && prvObj > 0) clearTimeout(prvObj);
	
	gettrailobj().visibility="hidden";
	document.onmousemove="";
	gettrailobj().left="-1000px";
}

function followmouse(e)
{
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined")
	{
		if (docwidth - e.pageX < (currentimagewidth + 40))
			xcoord = e.pageX - xcoord - (currentimagewidth + 20); // Move to the left side of the cursor
		else 
			xcoord += e.pageX;

		if (docheight - e.pageY < (currentimageheight + 50))
			ycoord += e.pageY - Math.max(0,(50 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		else 
			ycoord += e.pageY;

	} 
	else if (typeof window.event != "undefined")
	{
		if (docwidth - event.clientX < (currentimagewidth + 40))
			xcoord = event.clientX + truebody().scrollLeft - xcoord - (currentimagewidth + 20); // Move to the left side of the cursor
		else 
			xcoord += truebody().scrollLeft+event.clientX
		
		if (docheight - event.clientY < (currentimageheight + 50))
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(50 + currentimageheight + event.clientY - docheight));
		else 
			ycoord += truebody().scrollTop + event.clientY;
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }
	
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}

function getPosition(o, positionName)
{
    var position = 0;

    while (o.tagName != "BODY") {
        position += eval("o."+positionName);
        o = o.offsetParent;
    }

    return position;
}


