// POPUP: Paweł Gdula

function PopUp(path,desc)
{
	handle = 0;
	handle_c = 0;
	var basic_width = 350;
	var basic_height = 200;
	popup_pg = window.window.open("","_blank","toolbar=no,scrollbars=no,status=no, resizable=no ,width="+basic_width+", height="+basic_height+"");	
	
	popup_pg.document.open();
	popup_pg.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" /><script src="popup.js" type="text/javascript"></script><title>PopUp</title><style type="text/css">body{font-family:Verdana, Arial, Helvetica, sans-serif;font-size:10px;height:100%;background-color:#F0F0F0}html{height:100%;}</style></head><body onload="Ini()" style="padding:0px;margin:0px;"><div style="width:100%;height:100%" id="canvas" ></div><div style="display:none;position:absolute;left:0px;top:0px;width:30px;height:30px;background-color:#F0F0F0" id="info"></div></body></html>');
	popup_pg.document.close();
	LoaderStart();

	var loading_image = new Image();
	
	loading_image.onload = function()
	{
		LoaderStop();
		var x = parseInt((window.screen.availWidth-loading_image.width)/2);
		popup_pg.window.resizeTo(loading_image.width,loading_image.height);
		
		// korekta
		
		if(popup_pg.window.innerHeight)
		{
			var delta_y = loading_image.height - popup_pg.window.innerHeight;
			var delta_x = loading_image.width - popup_pg.window.innerWidth;
			if(Math.abs(delta_x)<50) //ze wzgledu na dziwne zachowanie FF
				popup_pg.window.resizeBy(delta_x,delta_y);
			else
				popup_pg.window.resizeBy(0,delta_y);
		}
		else
		{
			
			var delta_y = loading_image.height - popup_pg.document.documentElement.clientHeight;
			var delta_x = loading_image.width - popup_pg.document.documentElement.clientWidth;
			popup_pg.window.resizeTo(loading_image.width+delta_x,loading_image.height+delta_y);
			
		}
		
		popup_pg.moveTo(x,50);
		popup_pg.document.getElementById("canvas").innerHTML = '<img onmouseover="InfoShow()" onmousemove="Info(event)" style="cursor:pointer" title="close" onclick="window.close()" src="'+loading_image.src+'" />';
		
	}
	
	loading_image.onerror = function()
	{
		LoaderStop();
		CounterStart();
	}
	
	loading_image.src = path; 

}
// =================================================
function Ini()
{
	mouseX = 0;	
	mouseY = 0;	
	old_X = 0;
	old_Y = 0;
	move = false;
	CheckStart();
}
// -------------------------------------------------
function InfoShow()
{
	if(!move)
	{
		var info_div = document.getElementById("info");
		info_div.style.top = mouseY+"px";
		info_div.style.left = mouseX+"px";
		if(info_div.style.display == "none") info_div.style.display = "inline";
	}
	
}

// -------------------------------------------------

function CheckStart()
{
	var handle2 = window.setInterval(Check,1000);
}

// -------------------------------------------------


function Check()
{
	if(old_X==mouseX) alert(old_X);
}


// -------------------------------------------------

function Info(evt)
{
	
	if(document.all)
	{
		mouseX =  window.event.clientX;
		mouseY =  window.event.clientY;
	}
	else
	{
		mouseX =  evt.pageX ;
		mouseY =  evt.pageY ;
	}
	
	move = true;
}

// =================================================

function LoaderStart()
{
	if(handle == 0) 
	{
		dots = 0;
		handle = popup_pg.window.setInterval(Loader,100);
	}
}

// -------------------------------------------------

function Loader()
{
	var text = "";
	if(dots==10) dots = 0;
	for(var i =0;i<=dots;i++) text += ".";
	popup_pg.document.getElementById("canvas").innerHTML = "loading<br/>"+text;	
	dots++;
}

// -------------------------------------------------

function LoaderStop()
{
	popup_pg.window.clearInterval(handle);
	handle = 0;
}

// =================================================

function CounterStart()
{
	if(handle_c == 0) 
	{
		seconds = 5;
		popup_pg.document.getElementById("canvas").innerHTML = '<div style="padding-left:50px"><b>error loading picture</b><br/>'+seconds+' s to close this popup<br/><br/><br/><a href="#" onclick="window.close()">close now</a></div>';	
		handle_c = popup_pg.window.setInterval(Counter,1000);
	}
}

// -------------------------------------------------

function Counter()
{
	seconds--;
	if(seconds==0)
	{
		popup_pg.window.close();
		CounterStop();
	}
	popup_pg.document.getElementById("canvas").innerHTML = '<div style="padding-left:50px"><b>error loading picture</b><br/>'+seconds+' s to close this popup<br/><br/><br/><a href="#" onclick="window.close()">close now</a></div>';	
}

// -------------------------------------------------

function CounterStop()
{
	popup_pg.window.clearInterval(handle_c);
	handle_c = 0;
}
