/* DivUp JavaScript tpl v1.0  by Wirtualna Polska S.A. - Lukasz Scibiorek, 30.11.2005 */

var DivUpPathI   = "http://i.wp.pl/a/i/divup/";  //sciezka do obrazków 
var DivUpUrlJS  = "http://szukaj.wp.pl/get.js";  //sciezka pliku generowanego get.js
var DivUpLoadLimit = 10; // czas, po którym skrypt przestanie oczekiwać na dane [sekundy]
var DivUpCache   = false;  //Czy cache'owac wyniki [true|false]

/****/
var DivUpTS ='';
var DivUpBlocked = false;
var mxwin;
var mywin;

var DivUpTo = 0;
var dposx, sposy;

function returnFalse () { return false; }

function ustawZdarzenie ( 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 doMouseDown (e) {
  dposx = e.clientX - document.getElementById('DivUp').offsetLeft;
  dposy = e.clientY - document.getElementById('DivUp').offsetTop;
  ustawZdarzenie( true, 'onmousemove', 'DivUp', doMouseMove );
  ustawZdarzenie( true, 'onmousemove', document, doMouseMove );
  ustawZdarzenie( true, 'onselectstart', document, returnFalse );
}

function doMouseUp () {
  ustawZdarzenie( false, 'onmousemove', 'DivUp', doMouseMove ); 
  ustawZdarzenie( false, 'onmousemove', document, doMouseMove ); 
  ustawZdarzenie( false, 'onselectstart', document, returnFalse );
}

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

function DivUpPos() {
  // Jeżeli wygenerowany DivUp nie mieści się w widoku to przenies go wyżej
  var t = parseInt(document.getElementById('DivUp').style.top.substr(0,document.getElementById('DivUp').style.top.length-2));
  var b = document.body.clientHeight;
  var d = document.getElementById('DivUp').clientHeight;
  var s = document.body.scrollTop;
  if ( t + d  > b + s  )  document.getElementById('DivUp').style.top = s + b - d - 15;
  // lub niżej
  if ( t < s ) document.getElementById('DivUp').style.top = s + 15;
}

function divup(obj, tytul, count) {
  /* ramka div dla wyników */
  if (undefined==tytul) tytul = obj.title;
  if (undefined==count) count = obj.count;
  if (document.getElementById('DivUp') != null ) { DivUpClose(); }
  DivUpTO = setTimeout('DivUpTimeout()', DivUpLoadLimit * 1000 );
  DivUpCreate(obj, tytul, count); 
  return false;
}

function DivUpCreate(obj, tytul, count) {
  if (!DivUpCache) { DivUpTS='&timestamp=' + String(Math.random()).substring(2); } 
  var vDivUp = document.createElement('div');
  vDivUp.id = 'DivUp';
  document.getElementsByTagName('body').item(0).appendChild(vDivUp);
  var posx, posy;
  if (document.all) { //MSIE, Opera
    posx = document.body.clientWidth /2;
    posy = event.clientY - 30; 
  } else { //Gecko
    posx = window.innerWidth /2;
    posy = window.innerHeight /2; 
  }
  document.getElementById('DivUp').style.left =  posx + document.body.scrollLeft - 10 + "px";
  document.getElementById('DivUp').style.top  =  posy + document.body.scrollTop - 10 + "px";
  /*** zawartość ramki ***/
  /* tytul */
  var vTyt = document.createElement('div');
  vTyt.id = 'DivUpTytul';
  vTyt.innerHTML = '<div style="float:right;"><a href="javascript: DivUpClose();"><img src="'+DivUpPathI+'divupclose.gif" border="0" align="middle" alt="" style="margin-top: 1px;"></a></div>Wyniki wyszukiwania'; 
  vDivUp.appendChild(vTyt);
  ustawZdarzenie( true, 'onmousedown', 'DivUp', doMouseDown );
  ustawZdarzenie( true, 'onmouseup',   'DivUp', doMouseUp );

  /* klepsydra */
  var vCzekaj = document.createElement('div');
  vCzekaj.id = 'DivUpCzekaj';
  vCzekaj.innerHTML = '<img src="' + DivUpPathI + 'divupload.gif" alt="Czekaj...">'; 
  vDivUp.appendChild(vCzekaj);

  /* wyniki */
  if (count>0) countParam = '&s=' + count;
  else countParam = '';
  var vJS = document.createElement('script');
  vJS.id = 'DivUpJS';
  vJS.setAttribute("language", "JavaScript");
  vJS.setAttribute("src", DivUpUrlJS + "?q=" + tytul + countParam + DivUpTS);
  document.getElementsByTagName("head").item(0).appendChild(vJS);
  return true;
}

function DivUpTimeout() {
  DivUpBlocked = true;
  document.getElementById('DivUpCzekaj').parentNode.removeChild(document.getElementById('DivUpCzekaj'));
  var vClose = document.createElement('div');
  vClose.id = 'DivUpTO';
  vClose.innerHTML = 'Przekroczony został czas oczekiwania na dane.<br /><br /><a href="javascript: DivUpClose();">zamknij okienko</a>'; 
  document.getElementById('DivUp').appendChild(vClose);
}

function DivUpClose() {
  //document.getElementById('DivUp').style.display='none';
  document.getElementsByTagName('body').item(0).removeChild(document.getElementById('DivUp'));
  document.getElementsByTagName("head").item(0).removeChild(document.getElementById('DivUpJS'));
}


