var closeButton = 'user/images/popup_close.gif';	
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var xScroll;
	var yScroll;

	if (self.pageXOffset) {
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		xScroll = document.body.scrollLeft;
	}

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//--------------------------------------------------------

function openPopup(content, w)
{
	
	initPopup();
	// prep objects
	var objBodyHtml = document.getElementsByTagName("body").item(0);
	var objLightboxPopup = document.getElementById('lightboxPopup');

	var objLightboxDetailsPopup = document.getElementById('lightboxDetailsPopup');
	var objContentPopup = document.getElementById('contentPopup');

	var arrayPageSize = getPageSize(); // jeszcze raz zczytuje ze wzgledu na ajaxa
	var arrayPageScroll = getPageScroll(); // jeszcze raz zczytuje ze wzgledu na ajaxa
	
	var lightboxTop = document.body.scrollTop+30;
	

	objLightboxPopup.style.top = arrayPageScroll[1]+30;
	objLightboxPopup.style.left = arrayPageScroll[0]+(arrayPageSize[2]/2)-(w/2);
	objLightboxPopup.style.top = arrayPageScroll[1]+30;
	objLightboxPopup.style.width=w+'px'; 
	//alert(urldecode(content));
	
	objContentPopup.innerHTML = (content);
	
	objLightboxPopup.style.display = 'block';
	
	posPopup(w);
}

function posPopup(w)
{
	var objLightboxPopup = document.getElementById('lightboxPopup');
	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();

	objLightboxPopup.style.left = (arrayPageScroll[0]+(arrayPageSize[2]/2)-(w/2))+'px';
	objLightboxPopup.style.top = (arrayPageScroll[1]+30)+'px';

	if (objLightboxPopup.offsetHeight<(arrayPageSize[3]-60)) // zakladam ze formularz nie bedzie szerszy niz szerokosc strony wiec sprawadzam tylko wysokosc
		setTimeout('posPopup('+w+')',200);
	else
		clearTimeout();
}

function closePopup()
{
	objLightboxPopup = document.getElementById('lightboxPopup');
	objContentPopup = document.getElementById('contentPopup');
	
	objLightboxPopup.style.display = 'none';

	objContentPopup.innerHTML = '';
	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}


function initPopup()
{
	var objBodyHtml = document.getElementsByTagName("body").item(0);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// create lightbox div, same note about styles as above
	var objLightboxPopup = document.createElement("div");
	objLightboxPopup.setAttribute('id','lightboxPopup');
	objLightboxPopup.style.display = 'none';
	objLightboxPopup.style.position = 'absolute';
	objLightboxPopup.style.zIndex = '100';
	objBodyHtml.insertBefore(objLightboxPopup, objBodyHtml.firstChild);

	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Kliknij by zamkn±æ okno');
	objLink.onclick = function () {closePopup(); return false;}
	objLightboxPopup.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload=function(){
		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objLink.appendChild(objCloseButton);

		return false;
	}

	imgPreloadCloseButton.src = closeButton;

	// create details div, a container for the caption and keyboard message
	var objLightboxDetailsPopup = document.createElement("div");
	objLightboxDetailsPopup.setAttribute('id','lightboxDetailsPopup');
	objLightboxPopup.appendChild(objLightboxDetailsPopup);
	
	var objContentPopup = document.createElement("span");
	objContentPopup.setAttribute('id','contentPopup');
	objContentPopup.setAttribute('innerHTML','');
	objLightboxDetailsPopup.appendChild(objContentPopup);
	
}




