var oknoLayerZIndex = 255;
var currentoknoLayer = false;
var okno=false;

function pause(millis)
{
	date = new Date();
	var curDate = null;
	do { var curDate = new Date(); }
	while(curDate-date < millis);
}

function popupImg(event, src, title,sz,wy)
{
	okno=false;
	sz=(sz>0)?sz:180;
	wy=(wy>0)?wy:180;
	okno = new oknoLayer( title, src,sz,wy );
	return false;
}

function oknoLayer( windowTitle, src,sz,wy )
{
	var windowId = generateId(new Date);
	var isMoving = false;
	var zIndex = 200;
	var resetPosition = true;
	createWindow(windowTitle, windowId );
	setImage(src,sz,wy);

	function generateId(d)
	{
		var id = "window" + d.getSeconds() + d.getMilliseconds();
		while(document.getElementById(id)) { id += "1"; }
		return id;
	}

	function setImage(image,sz,wy)
	{
		var imageDiv = document.getElementById(windowId).getElementsByTagName("div").item(1);
		imageDiv.style.backgroundColor = "";
		imageDiv.style.backgroundPosition = "center 55px";
		imageDiv.style.backgroundRepeat = "no-repeat";
		imageDiv.style.backgroundImage = "url('"+ image +"')"; //

		sz=(sz<200)?200:sz;
		wy=(wy<200)?200:wy;
		imageDiv.style.width=sz+20+'px';
		imageDiv.style.height=wy+70+'px';

		moveWindowToCenter();
	}

	function createWindow(windowTitle, windowId)
	{
		var container = document.createElement("div");
		container.onmousedown = getWindowUp;
		container.onmouseup = dropWindowDown;
		container.onselectstart = new Function("return false");
		container.setAttribute("id",windowId);
		container.className='layer_kontener';
		container.innerHTML=oknolayerhtml;

		tmp2 = document.createElement("div");

		if(windowTitle!='')
		{
			var tmp = document.createElement("div");
			tmp.innerHTML = windowTitle;
			tmp.className='layer_tytul';
			tmp2.appendChild(tmp);
		}

		var tmp = document.createElement("div");
		tmp.innerHTML = windowTitle;
		tmp.className='layer_logo';
		tmp.innerHTML=logolayerhtml;
		tmp2.appendChild(tmp);

		tmp = document.createElement("div");
		tmp.innerHTML = "&nbsp;";
		tmp.title = "Zamknij";
		tmp.className='layer_zamknij';

		tmp.onmousemove = dontMoveWindow;
		tmp.onclick = hideWindow;

		tmp2.appendChild(tmp);
		tmp = document.createElement("div");
		tmp.appendChild(tmp2);
		container.appendChild(tmp);

		if(!document.getElementById("oknoLayerparent")) return;
		document.getElementById("oknoLayerparent").appendChild(container);
		document.getElementsByTagName("body").item(0).onmousemove = followWindowByCursor;
		return container;
	}

	function dontMoveWindow(e)
	{
		e = e||event;
		e.cancelBubble = true;
		currentoknoLayer = false;
		return false;
	}

	function hideWindow()
	{
		document.getElementById(windowId).style.display = "none";
	}

	function moveWindowToCursor(e, offsetX, offsetY)
	{
		containerStyle = document.getElementById(windowId).style;
		containerStyle.top = ( e.clientY + offsetY + getScrollY() ) + "px";
		containerStyle.left = ( e.clientX + offsetX ) + "px";
	}

	function moveWindowToCenter()
	{
		var container = document.getElementById(windowId);
		container.style.top = ( ( getScrollY() + (getScreenSize("height") / 2) ) - 175 ) + "px";
		container.style.left = ( (getScreenSize("width") / 2) - 175 ) + "px";
	}

	function getScreenSize(mode)
	{
		if (self.innerHeight)
		{
			if(mode == 'width') return self.innerWidth;
			else return self.innerHeight;
		}
		else if (document.body)
		{
			if(mode == 'width') return ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth );
			else return  document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		}
		return 0;
	}

	function getScrollY()
	{
		return parseInt( window.pageYOffset ? window.pageYOffset :
		( document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) - 20 );
	}

	function clearElement(el)
	{
		while( el.childNodes.length != 0 )
		{
			el.removeChild ( el.childNodes.item(0) );
		}
	}

	function getWindowUp(e)
	{
		currentoknoLayer = windowId;
		e = e||event;
		var container = document.getElementById(windowId);
		container.style.position = "absolute";
		container.cursorX = e.pageX ? e.pageX - container.offsetLeft : e.offsetX;
		container.cursorY = e.pageY ? e.pageY - container.offsetTop  : e.offsetY;
		container.style.zIndex = oknoLayerZIndex++;
	}

	function dropWindowDown()
	{
		currentoknoLayer = false;
	}

	function addEvent(obj, evType, fn)
	{
		if(obj.addEventListener)
		{
			obj.addEventListener(evType, fn, false);
			return true;
		}
		else if (obj.attachEvent)
		{
			var r = obj.attachEvent('on'+evType, fn);
			return r;
		}
		else
		{
			return false;
		}
	}
}

function followWindowByCursor(e)
{
	e = e||event;
	if(currentoknoLayer != false)
	{
		var container  = document.getElementById(currentoknoLayer);
		var leftOffset = (e.pageX ? e.pageX : e.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft)) - container.cursorX;
		var topOffset  = (e.pageY ? e.pageY : e.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)) - container.cursorY;
		if(leftOffset >= 0) { container.style.left = leftOffset + "px"; }
		if(topOffset >= 0) { container.style.top  = topOffset + "px"; }
	}
}
