document.write('<div id="bigImg" style="position:absolute; z-index:1000; display:none;"></div>');

  _bigImg = document.getElementById('bigImg');
  var timeOutID = 0;
  function bildePos(el) {
        x = 0; y = 0; var temp
        if(el.offsetParent) {
              temp = el
              while(temp.offsetParent) {
                    temp = temp.offsetParent;
                    x += temp.offsetLeft
                    y += temp.offsetTop;
              }
        }
        x += el.offsetLeft
        y += el.offsetTop
        return [x, y, el.width, el.height]
  }

  function bilde(act, obj, img, nos) {
        if(act == 'hide') {
              _bigImg.innrHTML = '';
              _bigImg.style.display = 'none';
              clearTimeout(timeOutID);
        }
        if(act == 'show') {
              pos = bildePos(obj);
              
              //bish nostaak
              atstarpe = 5;
              pos[0] -= atstarpe;
              pos[1] -= atstarpe;
              pos[2] += atstarpe * 2;
              pos[3] += atstarpe * 2;
              
              divL = pos[0];
              divT = pos[1] + pos[3];
              _bigImg.style.left = (pos[0] + (pos[2] - 16) / 2) + 'px';
              _bigImg.style.top = (pos[1] + (pos[3] - 16) / 2) + 'px';
              _bigImg.innerHTML = '<img src="/img/loading-anim.gif" border="0" width="16" height="16">'; 
              _bigImg.style.display = 'block';
              imgLarge = new Image();
              imgLarge.onload = function() {
                    imgLarge.onload = null;

                    //CW = navigator.userAgent.indexOf("MSIE") >= 0 ? document.body.clientWidth : window.innerWidth;
                    //CH = navigator.userAgent.indexOf("MSIE") >= 0 ? document.body.clientHeight : window.innerHeight;
                    //scrTop = navigator.userAgent.indexOf("MSIE") >= 0 ? document.body.scrollTop : window.scrollY;
                    if(navigator.userAgent.indexOf("MSIE") >= 0) {
                          if (document.body && document.body.scrollTop) {
                                CW = document.body.clientWidth;
                                CH = document.body.clientHeight;
                                scrTop = document.body.scrollTop;
                          } else {
                                CW = document.documentElement.clientWidth;
                                CH = document.documentElement.clientHeight;
                                scrTop = document.documentElement.scrollTop;
                          }
                    } else {
                          CW = window.innerWidth;
                          CH = window.innerHeight;
                          scrTop = window.scrollY;
                    }
                    CW -= 30;
                    CH -= 30;
                    //alert(CH);
                    LW = imgLarge.width;
                    LH = imgLarge.height;
                    __H = nos ? 20 : 0;
                    
                    //atrod vietu kur vislielaakaa bilde sanaak
                    ll = [];
                    ll[0] = bildeLaukums(CW, (CH - pos[1] + scrTop - pos[3] > 0 ? CH - pos[1] + scrTop - pos[3] : 0), LW, LH + __H);
                    ll[1] = bildeLaukums(CW, (pos[1] - scrTop > 0 ? pos[1] - scrTop : 0), LW, LH + __H);
                    ll[2] = bildeLaukums((CW - pos[0] - pos[2] > 0 ? CW - pos[0] - pos[2] : 0), CH, LW, LH + __H);
                    ll[3] = bildeLaukums(pos[0], CH, LW, LH + __H);
                    l = [ll[0][0] * ll[0][1], ll[1][0] * ll[1][1], ll[2][0] * ll[2][1], ll[3][0] * ll[3][1]];
                    ff = 0; f = 0;
                    for(var i = 0; i <= 3; i++) {
                          if(l[i] > ff) {
                                f = i;
                                ff = l[i];
                          }
                    }
                    LW = ll[f][0];
                    LH = ll[f][1];
                    
                    //ieliekam vietaa
                    if(f == 0) {
                          divT = pos[1] + pos[3];
                          divL = pos[0] + LW >= CW ? CW - LW : pos[0];
                    }
                    if(f == 1) {
                          divT = pos[1] - LH - __H;
                          divL = pos[0] + LW >= CW ? CW - LW : pos[0];
                    }
                    if(f == 2) {
                          divT = pos[1] + LH >= CH - scrTop ? CH + scrTop - LH - __H: pos[1];
                          divL = pos[0] + pos[2];
                    }
                    if(f == 3) {
                          divT = pos[1] + LH >= CH - scrTop ? CH + scrTop - LH - __H: pos[1];
                          divL = pos[0] - LW;
                    }
                    
                    _bigImg.style.left = divL + 'px';
                    _bigImg.style.top = divT + 'px';

                    _bigImg.innerHTML = '<img src="' + img + '" border="0" width="' + LW + '" height="' + LH + '">';
                    if(nos) {
                          _bigImg.innerHTML += '<div style="position:relative; width:' + LW + 'px; height:20px; overflow:hidden; background:#FFFFFF"><div style="position:relative; left:0px" id="mansTexts"><span id="wt"><nobr>' + nos + '</nobr></span></div></div>';
                          _mansTexts = document.getElementById('mansTexts');
                          _wt = document.getElementById('wt');
                          if(_wt.offsetWidth > LW) {
                                _mansTexts.style.left = LW + 'px';
                                tinam(LW);
                          } else {
                                _mansTexts.style.textAlign = 'center';
                          }
                    }
              }
              imgLarge.src = img;
        }
  }
  
  function bildeLaukums(w, h, ww, hh) {
        if(ww > w || hh > h) {
              if(ww / hh >= w / h) {
                    hh = w * hh / ww;
                    ww = w;
              } else {
                    ww = h * ww / hh;
                    hh = h;
              }
        }
        return [parseInt(ww), parseInt(hh)];
  }
  
  function tinam(w) {
        if(_bigImg.style.display == 'block') {
              _mansTexts = document.getElementById('mansTexts');
              _wt = document.getElementById('wt');
              __x = parseInt(_mansTexts.style.left);
              __x -= 10;
              if(__x < -_wt.offsetWidth) __x = w;
              _mansTexts.style.left = __x + 'px';
              timeOutID = setTimeout('tinam(' + w + ')', 120);
        }
  }

