var szerokosc = 0;
var offsetx = 10;
var offsety = 10;

var x = 0;
var y = 0;
var ruszaj = 0;
var dir = 1;
var okienko = null;
var akt_tresc = '';

var nav_ok = false;
var nav_ie = false;
var nav_brak = false;

if (document.all) {
	nav_ie = true;
} else if (document.getElementById || (navigator.userAgent.indexOf('Konqueror') > 0)) {
	nav_ok = true;
} else {
	nav_brak = true;
}

document.onmousemove = mouseMove;

function ukryj(obj) {
	obj.style.visibility = "hidden";
}

function pokaz(obj) {
	obj.style.visibility = "visible";
}

// powyższe funkcje chyba mają konflikt nazw z innymi, ale zachowuję je dla kompatybilności - Daniel

function okienka_ukryj(obj) {
	obj.style.visibility = "hidden";
}

function okienka_pokaz(obj) {
	obj.style.visibility = "visible";
}

function createLayer(id, nestref, left, top, width, height, content, bgColor, visibility, zIndex) {
	var div = document.createElement("div");
	div.id = id;
	div.style.position = "absolute";
	div.style.left = left + "px"; 
	div.style.top = top + "px"
	if (width != null) {
		div.style.width = width + "px";
    }
	if (height != null) {
		div.style.height = height + "px";
    }
	if ((width != null) && (height != null)) {
		div.style.clip = "rect(0px " + width + "px " + height + "px 0px)";
    }
	if (bgColor != null) {
		div.style.backgroundColor = bgColor;
    }
	div.style.padding = "0px 0px 0px 0px";
	if (zIndex != null) {
		div.style.zIndex = zIndex;
    }
	if (visibility != null) {
		div.style.visibility = visibility;
    }
		
	div.innerHTML = content;

	var parentEl = document.body;
	if (nestref) {
		if (nav_ok) {
			parentEl = document.getElementById(nestref);
		} else if (nav_ie) {
			index = nestref.lastIndexOf(".");
			var nestlyr = (index != -1)? nestref.substr(index+1) : nestref;
			parentEl = document.all[nestlyr];
		}
	};
	parentEl.appendChild(div);

	return div;
}

// Clears popups if appropriate
function nd() {
	if (!nav_brak && ruszaj) {
		ruszaj = 0;
		okienka_ukryj(okienko);
	}
}

function ustaw_okienko() {
    switch (dir) {
        case 0:
            posx = x - offsetx - szerokosc; // Left
            break;
        case 1:
            posx = x + offsetx - (szerokosc / 2); // Center
            break;
        default:
            posx = x + offsetx; // Right
    };
	moveTo(okienko, posx, y + offsety);
}

// Common calls
function disp() {
	if (!nav_brak) {
        ustaw_okienko();
		okienka_pokaz(okienko);
	}
}

// Moves the layer
function mouseMove(e) {
    if (!e) {
        e = window.event;
    }
    x = e.clientX + document.body.scrollLeft; 
    y = e.clientY + document.body.scrollTop;

	if (ruszaj && okienko && (okienko.style.visibility == 'visible')) {
        ustaw_okienko();
	}
}

// Move a layer
function moveTo(obj,xL,yL) {
	obj.style.left = xL;
	obj.style.top = yL;
}

function zawartosc(staly, tresc, nagl, sto) {
    if (sto == 1) {
        par = 'width="100%" height="100%"';
    } else {
        par = '';
    }
	zaw = '<table ' + par + ' border="0" cellspacing="0" cellpadding="2" class="panel_okienka_js">';
	
	if (staly || nagl) {
		if (!nagl) {
			nagl = '&nbsp;';
        }
		zaw += '<tr><td><b>' + nagl + '</b></td>';
		if (staly) {
			zaw += '<td align="right"><a href="#" onClick="okienka_ukryj(okienko); return false;"><img src="img/okienko_zamknij.gif" border="0"></a></td>';
        }
		zaw += '</tr>'
	};
	zaw += '<td height="100%" valign="top"' + (staly ? ' colspan=2' : '') + '>' + tresc + '</td></tr></table>';

	return zaw;
}

// polozenie: 0 - lewa, 1 - srodek, 2 - prawa
function wyswietlOkienko(staly, polozenie, id, width, height, content, header, left, top, nestref, bgColor, visibility, zIndex) {

	if ((okienko == null) || (akt_tresc != content)) {
		akt_tresc = content;
		if (typeof left == 'undefined') {
			left = 0;
        }
		if (typeof top == 'undefined') {
			top = 0;
        }
		if (typeof zIndex == 'undefined') {
			zIndex = 1;
        }
		if (typeof visibility == 'undefined') {
			visibility = 'hidden';
        }
		if (width != undefined) {
            szerokosc = width;
        }

		zaw = zawartosc(staly, content, header, (((width > 0) && (height > 0)) ? 1 : 0));

		okienko = createLayer(id, nestref, left, top, width, height, zaw, bgColor, visibility, zIndex);
	};
	
	if (!staly) {
		ruszaj = 1;
	}
    dir = polozenie;
	disp();
}

function schowajOkienko() {
	if (okienko != null) {
		okienka_ukryj(okienko);
    }
}
