var dposx, dposy;

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) 
{
  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; 
  }
  //alert((document.documentElement||document.body).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';
  new Effect.Fade('stronka', { duration: 0.3, from: 1.0, to: 0.2 });
  /* ramka div dla wyników */
  if (undefined==tytul) tytul = obj.title;
  if (document.getElementById('pokobr') != null ) { pokobrClose(); }
  pokobrCreate(obr, tytul); 
  return false;
}

function pokobrCreate(obr, tytul) 
{
  var vpokobr = document.createElement('div');
  vpokobr.id = 'pokobr';
  document.getElementsByTagName('body').item(0).appendChild(vpokobr);
 // alert('asas');
  
  
  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);
    }
    
 
  
  return true;
}  
 

function pokobrCreate2(vpokobr, vobr, tytul) 	
{
	 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;
  
  vTyt.innerHTML = '<div style="float:left;font-family:Tahoma, Arial, Helvetica;font-size:9px;margin-left:2px;width:'+(szer-30)+'px;">'+tytul+'</div><div style="float:right;top:0px;border: 0px solid black;"><a href="javascript: pokobrClose();"><img src="pokobrclose.gif" border="0" align="middle" alt="[x]" style="margin-right: 0px;"></a></div>'; 
  vpokobr.appendChild(vTyt);
  
  document.getElementById('pokobr').xOpacity = 0;
  setOpacity(document.getElementById('pokobr'));
  
  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+12+"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() 
{
  
  new Effect.Appear('stronka', { duration: 0.3, from: 0.2, to: 1.0 });
  document.getElementsByTagName('body').item(0).removeChild(document.getElementById('pokobr'));
}

