/*
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 = 270; // maximum image size.
var imageaddition = 130; // Celkova velikost okraju image

if (document.getElementById || document.all)
{
  document.write('<div id="trailimageid" class=LabTrail>');
  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,title,description,widthPar,heightPar,resolution)
{
  width = widthPar;
  height = heightPar;
  if (heightPar > 500)
  {
    width = (widthPar * 500) / heightPar;
    height = 500;
  }
  if (height > 0)
  {
    currentimageheight = height;
  }

  document.onmousemove=followmouse;

  newHTML = '<div style="padding: 5px; background-color: #FFF; border: 1px solid #888;" align=center>';
  newHTML += '<h4>' + unescape(title) + '</h4>';
  if (description && description != "")
  {
    newHTML += '<font size="-1">'+unescape(description)+'</font><br/>';
    currentimageheight+=20;
  }
  if (resolution > 0)
  {
    wMM = Math.floor(width*25.4/resolution+0.5);
    hMM = Math.floor(height*25.4/resolution+0.5);
    wIN = Math.floor(width*10/resolution+0.5)/10;
    hIN = Math.floor(height*10/resolution+0.5)/10;
    newHTML+=wMM+' x '+hMM+' mm ('+wIN+'" x '+hIN+'")';
    currentimageheight+=20;
  }

  newHTML += '<div align="center" style="padding: 8px 2px 2px 2px;">';
  newHTML += '<img src="' + imagename + '" border="0" width=' + width + 'height=' + height + '></div>';

  newHTML += '</div>';
  gettrailobjnostyle().innerHTML = newHTML;
  gettrailobj().display="inline";
}

function hidetrail()
{
  gettrailobj().innerHTML = " ";
  gettrailobj().display="none"
  document.onmousemove=""
  gettrailobj().left="-500px"
}

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 (document.all){
  //  gettrailobjnostyle().innerHTML = 'A = ' + truebody().scrollHeight + '<br>B = ' + truebody().clientHeight;
  //} else {
  //  gettrailobjnostyle().innerHTML = 'C = ' + document.body.offsetHeight + '<br>D = ' + window.innerHeight;
  //}

  if (typeof e != "undefined"){
    if (docwidth - e.pageX < 380)
    {
      xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
    }
    else 
    {
      xcoord += e.pageX;
    }
    if (docheight - e.pageY < (currentimageheight + imageaddition)){
      ycoord += e.pageY - Math.max(0,(imageaddition + currentimageheight + e.pageY - docheight - truebody().scrollTop));
    } else 
    {
      ycoord += e.pageY;
    }

  }
  else if (typeof window.event != "undefined")
  {
    if (docwidth - event.clientX < 380)
    {
      xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
    }
    else 
    {
      xcoord += truebody().scrollLeft+event.clientX
    }
    if (docheight - event.clientY < (currentimageheight + imageaddition))
    {
      ycoord += event.clientY + truebody().scrollTop - Math.max(0,(imageaddition + currentimageheight + event.clientY - docheight));
    } 
    else 
    {
      ycoord += truebody().scrollTop + event.clientY;
    }
  }

  if(ycoord < 0) 
  {
    ycoord = ycoord*-1;
  }
  gettrailobj().left=xcoord+"px"
  gettrailobj().top=ycoord+"px"
}

//=====================================================
//Tvorba tabulky
//
var ImgTab=[],
    ElId="ImgId",
    CellsSpc=5,
    nColl=0,
    LastWWidth=0;
function O()
{
  R(window,"load",L)
}

function L(){return false;}
function S(){Vykreslit()}
function OnResize()
{
  var curWWidth = GetWW();
  if(Math.abs(curWWidth-LastWWidth) > 60)
    Vykreslit();
}
// "img/$img","thu/$thu",$thuW,$thuH,$imgW,$imgH,$imgRes,"$lab.name","$lab.txt","$legend"
function D(img,thu,thuW,thuH,imgW,imgH,imgRes,labName,labText,labLegend1,labLegend2)
//function D(pp,bb,cc,aa,         hh,gg,kk)
{
  var f=new Image;
  f.img=img;f.thu=thu;f.thuW=thuW;f.thuH=thuH;f.imgW=imgW;f.imgH=imgH;f.imgRes=imgRes;f.labName=labName;f.labText=labText;f.labLegend1=labLegend1;f.labLegend2=labLegend2;
  ImgTab[nColl]=f;
  nColl++
}
function R(a,c,d){var b="on"+c;if(a.addEventListener){a.addEventListener(c,d,false)}else if(a.attachEvent){a.attachEvent(b,d)}else{var e=a[b];a[b]=function(){var g=e.apply(this,arguments),h=d.apply(this,arguments);return g==undefined?h:(h==undefined?g:h&&g)}}}
function GetWW(){if(document&&document.body&&document.body.clientWidth){return document.body.clientWidth-170}else{return window.innerWidth-170}}
function P(a){return false}
function GetEl(a)
{
  //window.prompt(GetEl,a);
  return document.getElementById(a)
}
function Vykreslit()
{
//window.prompt("Vykreslit","Vykreslit");
  var html="<table cellspacing="+CellsSpc+" cellpadding=0 border=0 align=center><tr>";
  var iRow=1;
  var eImgTab;
  var lRow=0;
  var thuW=0;
  var n;
  var w;
  var curWWidth = GetWW();
  for(n=0;n<nColl;n++)
  {
    eImgTab=ImgTab[n];
    if (eImgTab.labLegend1&&eImgTab.thuW<50){thuW=50}else{thuW=eImgTab.thuW}
    w=thuW+CellsSpc+10;
    if (lRow+w > curWWidth)
    {
      html+="</tr></table><table cellspacing="+CellsSpc+" cellpadding=0 border=0 align=center><tr>";
      iRow++;
      lRow=0
    }
    html+="<td id="+ElId+n+" align=center xnowrap valign=top";
    html+=" width="+thuW;
    html+="></td>";
    lRow+=w;
  }
  html+="</tr></table>";
//window.prompt(ElId,html);
  GetEl(ElId).innerHTML=html;
  // obsahy bunek tabulek
  for(n=0;n<nColl;n++)
  {
    eImgTab=ImgTab[n];
    html='<a href="'+eImgTab.img+'"><img style="border:1px solid;" src="'+eImgTab.thu+'" alt="'+eImgTab.labName+'"';
    html+=' onmouseover="showtrail('+"'"+eImgTab.img+"','"+escape(eImgTab.labName)+"','"+escape(eImgTab.labText?eImgTab.labText:eImgTab.labLegend2)+"',"+eImgTab.imgW+","+eImgTab.imgH+","+eImgTab.imgRes+");"+'"';
    html+=" onmouseout=hidetrail(); width="+eImgTab.thuW+" height="+eImgTab.thuH+"></a>";
    if (eImgTab.labLegend1)
      html+="<div class=LabLegend>"+eImgTab.labLegend1+"</div>";
//window.prompt(ElId+n,html);
    GetEl(ElId+n).innerHTML=html;
  }
  LastWWidth=curWWidth;
}
