var dposx, dposy;

var inshow=false;

function returnFalse () { return false; }

function doEvent ( tryb, zdarzenie, obj, funkcja ) {
  if (document.getElementById(obj)) obj = document.getElementById(obj);
  if (obj.addEventListener) { // Gecko
    if (tryb) {obj.addEventListener( zdarzenie.substr(2), funkcja, false )} else {obj.removeEventListener( zdarzenie.substr(2), funkcja, false )}
  } else { // MSIE, Opera
    if (tryb) {obj.attachEvent( zdarzenie, funkcja )} else {obj.detachEvent( zdarzenie, funkcja )}
  }
}

function doMouseDn (e) {
  dposx = e.clientX - document.getElementById('pokobr').offsetLeft;
  dposy = e.clientY - document.getElementById('pokobr').offsetTop;
  doEvent( true, 'onmousemove', 'pokobr', doMouseMv );
  doEvent( true, 'onmousemove', document, doMouseMv );
  doEvent( true, 'onselectstart', document, returnFalse );
}

function doMouseUp () {
  doEvent( false, 'onmousemove', 'pokobr', doMouseMv ); 
  doEvent( false, 'onmousemove', document, doMouseMv ); 
  doEvent( false, 'onselectstart', document, returnFalse );
}

function doMouseMv (e) {
  document.getElementById('pokobr').style.left = e.clientX - dposx;
  document.getElementById('pokobr').style.top  = e.clientY - dposy;
}

function setOpacity(obj) 
{
		if(obj.xOpacity>1) {
			obj.xOpacity = 1;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

function pokobr(obr, tytul, nast, poprz) 
{
  document.getElementById('hourglass').style.zIndex=5;
   if (document.all) 
  { //MSIE, Opera
    posx = document.body.clientWidth;
    posy = document.body.clientHeight; 
  } 
  else 
  {
    posx = window.innerWidth;
    posy = window.innerHeight; 
  }
  //scrollby=window.pageYOffset;
  //if(scrollby)
  //alert(document.documentElement.scrollTop );
  document.getElementById('hourglass').style.top=posy/2-16+(document.documentElement||document.body).scrollTop + "px";
  //alert(document.getElementById('hourglass').style.top);
  document.getElementById('hourglass').style.visibility='visible';
  
  if(!inshow)
  {
   new Effect.Fade('stronka', { duration: 0.3, from: 1.0, to: 0.2 });
   inshow=true;
  } 
  /* ramka div dla wyników */
  if (undefined==tytul) tytul = obj.title;
  if (document.getElementById('pokobr') != null ) { pokobrClose(); }
  pokobrCreate(obr, tytul, nast, poprz); 
  return false;
}

function pokobrCreate(obr, tytul, nast, poprz) 
{
  var vpokobr = document.createElement('div');
  vpokobr.id = 'pokobr';
  document.getElementsByTagName('body').item(0).appendChild(vpokobr);

  
  
  var vobr = document.createElement('img');
  vobr.src=obr+"&random="+Math.random(); //uwaga jesli nie thumb!!!!
  vobr.id='obrobr';

  vobr.onload = function (evt) 
    {   //alert('jest!');
        pokobrCreate2(vpokobr, vobr, tytul, nast, poprz);
    }
    
 
 
  return true;
}  
 

function pokobrCreate2(vpokobr, vobr, tytul, nast, poprz) 	
{
	 document.getElementById('hourglass').style.visibility='hidden';
	  
  
	 
  var posx, posy;
  if (document.all) 
  { //MSIE, Opera
    posx = document.body.clientWidth;
    posy = document.body.clientHeight; 
  } 
  else 
  {
    posx = window.innerWidth;
    posy = window.innerHeight; 
  }
  
  document.getElementById('hourglass').style.top=posy/2-16 + "px";
  
  var vTyt = document.createElement('div');
  vTyt.id = 'DivUpTytul';
  szer=vobr.width;
  inHTML ='';
  inHTML = '<div style="position:absolute;top:0px;left:0px;font-family:Tahoma, Arial, Helvetica;font-size:9px;margin-left:2px;width:'+(szer-90)+'px;">'+tytul+'</div><div style="float:right;top:0px;border: 0px solid black;">';
  if(poprz!="")
   inHTML+='<a href="javascript:nextImg(\''+poprz+'\')"><img src="pokobrprev.png" border="0" align="middle" alt="[&lt;--]"></a>';
  if(nast!="") 
   inHTML+='<a href="javascript:nextImg(\''+nast+'\')"><img src="pokobrnext.png" border="0" align="middle" alt="[--&gt;]"></a>';
  inHTML+='<a href="javascript: pokobrClose();"><img src="pokobrclose.png" border="0" align="middle" alt="[x]" style="margin-right: 0px;"></a></div>'; 
  vTyt.innerHTML=inHTML;
 
  vpokobr.appendChild(vTyt);
  
  document.getElementById('pokobr').xOpacity = 0;
  setOpacity(document.getElementById('pokobr'));
  vobr.style.position = "absolute";
  vobr.style.top="24px";
  vobr.style.left="0px";
  vpokobr.appendChild(vobr);

  szer=document.getElementById('obrobr').width;
  wys=document.getElementById('obrobr').height;
   

  
  document.getElementById('pokobr').style.position = "absolute";
  document.getElementById('pokobr').style.zIndex = 10;
  document.getElementById('pokobr').style.left =  (posx -szer)/2+ (document.documentElement||document.body).scrollLeft + "px";
  document.getElementById('pokobr').style.top  =  (posy-wys)/2 + (document.documentElement||document.body).scrollTop + "px";
  document.getElementById('pokobr').style.width  = szer+"px";
  document.getElementById('pokobr').style.height  = wys+24+"px";
  document.getElementById('pokobr').style.border = "1px solid black";
  document.getElementById('pokobr').style.background = "#aaaaaa";
  

  
  doEvent( true, 'onmousedown', 'pokobr', doMouseDn );
  doEvent( true, 'onmouseup',   'pokobr', doMouseUp );

  setTimeout(obrpokaz,50);
  
  return true;
}

function obrpokaz() 
{ //alert('pokaz'); 
	cOpacity = document.getElementById('pokobr').xOpacity;
	cOpacity+=.1; 
	
	document.getElementById('pokobr').xOpacity = cOpacity;
	setOpacity(document.getElementById('pokobr')); 
	if(cOpacity<1.1) 
	{
		setTimeout(obrpokaz,50);
	}
	
	
}

function pokobrClose() 
{
  inshow=false;
  new Effect.Appear('stronka', { duration: 0.3, from: 0.2, to: 1.0 });
  document.getElementsByTagName('body').item(0).removeChild(document.getElementById('pokobr'));
}

function nextImg(nast)
{
  //new Effect.Appear('stronka', { duration: 0.3, from: 0.2, to: 1.0 });
  document.getElementsByTagName('body').item(0).removeChild(document.getElementById('pokobr'));
  var tmpe=document.getElementById(nast);
  tmpe.onclick();
}


