var hideTimer;

function getPreview(){
  if (document.getElementById)
    return document.getElementById("preview_div")
}

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

function getScreenCoord(obj) {
  x = obj.offsetLeft;
  y = obj.offsetTop;
  if (obj.offsetParent) {
    pos = getScreenCoord(obj.offsetParent);
    x += pos[0];
    y += pos[1];
  }  
  return [x,y];
}

function getClientToScreen(obj, x, y) {
  pos = getScreenCoord(obj);
  return [x+pos[0], y+pos[1]];
}

function getScreenToClient(obj, x, y) {
  pos = getScreenCoord(obj);
  return [x-pos[0], y-pos[1]];
}

function getMousePos(event) {

	if (!event) var event = window.event;
	if (typeof event == undefined) 
	  return [-1,-1];
	  
	var posx = 0;
  var posy = 0;
	  
	if (event.pageX && event.pageY) {
		posx = event.pageX;
		posy = event.pageY;
	} else if (event.clientX && event.clientY) 	{
		posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return [posx, posy];
}

function hidetrail(event) {	
  clearTimeout(hideTimer);
	hideTimer = setTimeout("getPreview().style.display = \"none\"", 500);
	//document.onmousemove = "";
	//clearTimeout(timer);
}

function showtrail(event, imagename, title) {
 	
 	clearTimeout(hideTimer);
	//document.onmousemove = followmouse;
	
	var pos = getMousePos(event);
	var newHTML = '<h4 id="p2342title" class="product_title" style="display:block;">'+title+'</h4>';
  newHTML += '<img id="p2342img" src="'+imagename+'" style="display:block; margin:0; padding:0; width:100%; height:100%" onload="imageload(event, \''+pos[0]+'\',\''+pos[1]+'\')"></img>';
		
  var p = getPreview();
	p.innerHTML = newHTML;
	p.style.position = "absolute";
	p.style.display = "block";
	p.style.overflow = "hidden";
}

/*
 * maxw: reference width
 * maxh: reference height
 * w: rectangle width
 * h: rectangle height
 *
 * returns [scale_factor, width, height]
 */
function fitRectangle(maxw, maxh, w, h) {

  if (w<maxw && h<maxh)
    return [9999, w, h];
  
  var sw = maxw / w;
  var sh = maxh / h;
  if (sw < sh)
    return [sw, maxw, Math.floor(sw*h)];
  else
    return [sh, Math.floor(sh*w), maxh];
}

/*
 * wa: width area a
 * ha: height area a
 * wb: width area b
 * hb: height area b
 * w: image width
 * h: image height
 *  
 * returns []
 */
function fit2Rectangle(wa, ha, wb, hb, w, h) {

  var a = fitRectangle(wa, ha, w, h);
  var b = fitRectangle(wb, hb, w, h);

  if (a[0] > b[0])
    return [0, a[1], a[2]];
  else
    return [1, b[1], b[2]];
}

function imageload(event, x, y) {

  var margin = 20;
  
  var titleHeight = document.getElementById("p2342title").offsetHeight;
  if (titleHeight==undefined)
    titleHeight = 0;
  titleHeight += 10;
  //alert(titleHeight);

  var img = document.getElementById("p2342img");
  var imgWidth = img.naturalWidth;
  var imgHeight = img.naturalHeight + titleHeight;

	var halfWidth = Math.floor(window.innerWidth/2);
	var halfHeight = Math.floor(window.innerHeight/2);	  
	
	var fullWidth = window.innerWidth - 2*margin;
	var fullHeight = window.innerHeight - 2*margin;

  /* determine maximal width and height and the position of image window */	
	var posx = window.pageXOffset /*+ margin*/;
	var posy = window.pageYOffset /*+ margin*/;
  var ab;	
  
  x -= window.pageXOffset;
  y -= window.pageYOffset;
	
	if (x < halfWidth) {
	  if (y < halfHeight) {	    
	    ab = fit2Rectangle(
	      fullWidth - x,
	      fullHeight,
	      fullWidth,
	      fullHeight - y,
	      imgWidth,
	      imgHeight
	    );
	    if (ab[0]==0)
	      posx += window.innerWidth - ab[1];
	    else
	      posy += window.innerHeight - ab[2];
	  }
	  else {
	    ab = fit2Rectangle(
	      fullWidth - x,
	      fullHeight,
	      fullWidth,
	      y,
	      imgWidth,
	      imgHeight
	    );
	    if (ab[0]==0)
	      posx += window.innerWidth - ab[1];
	  }
	}
	else {
	  if (y < halfHeight) {
	    ab = fit2Rectangle(
	      x,
	      fullHeight,
	      fullWidth,
	      fullHeight - y,
	      imgWidth,
	      imgHeight
	    );
	    if (ab[0]==1)
	      posy += window.innerHeight - ab[2];
	  }
	  else {
	    ab = fit2Rectangle(
	      x,
	      fullHeight,
	      fullWidth,
	      y,
	      imgWidth,
	      imgHeight
	    );
	  }
	}	
	
	var maxWidth = ab[1];
	var maxHeight = ab[2];
      
	/* set values */
	var p = getPreview();
	p.style.maxWidth = maxWidth + "px";
	p.style.maxHeight = maxHeight + "px";
	p.style.left = posx + "px";
	p.style.top = posy + "px";
	
}

