function Init()
{
	if (! document.getElementsByTagName) {
		return;
	}
	
	var all_links = document.getElementsByTagName("a");

	for (i = 0; i < all_links.length; i++) {
		if (all_links[i].getAttribute("href")) {
			if (all_links[i].getAttribute("href").search(/(.*)\.(jpg|jpeg|gif|png|bmp|tif|tiff)/gi) != -1) {
				if (all_links[i].getAttribute("rel") != "no_zoom") {
					all_links[i].rel = i;
					all_links[i].onclick = function (event) { return imageZoomIn(this, event); };
					all_links[i].onmouseover = function () { imagePreload(this); };
//					alert(i);
				}
			}
		}
	}
	var inBody = document.getElementsByTagName("body").item(0);

	var Ol = document.createElement("div");
	Ol.setAttribute('id', 'overlay');
	inBody.insertBefore(Ol, inBody.firstChild);

	
	var Bx = document.createElement("div");
	Bx.setAttribute('id', 'center');
	inBody.insertBefore(Bx, inBody.firstChild);
	
	var Img = document.createElement("img");
	Img.onclick = function (event) { imageZoomOut(); return false; };	
	Img.setAttribute('src','');
	Img.setAttribute('id','photo');
	Img.setAttribute('border', '0');
	Bx.appendChild(Img);	

	var Ds = document.createElement("div");
	Ds.setAttribute('id', 'desc');
	Ds.style.display = 'none';
	Bx.appendChild(Ds);
}


var skala = 0.8; 		// poczatek animacji od 80% wielkosci
var margin = 5; 		// margines po 5 px do okola
var margin_top = 0; 	// odstep od gory ekranu
var preload = null; 
var pr = new Array(); 	// tabela preloadu
var petla = 0;			// ograniczenie czasu wczytania obrazka
var obj;
var tmp;

    function imageLoaded()
	{ 
		obj = tmp;
		name = obj.getAttribute("rel");
		
		if (pr[name])
		{
//			preload = pr[name];
						
//			alert(preload.src);
			endW = preload.width;
			endH = preload.height;
			
			if (!endW && !endH)		
			{
				endW = 850;
				endH = 635;
			}
		
			src = obj.getAttribute("href");
			title = obj.getAttribute("title");


			if (!$("myDiv"))
			{
				eDIV = document.createElement("div");
				eDIV.setAttribute("id","myDiv");
				eDIV.appendChild(document.createTextNode(title));
				$("desc").appendChild(eDIV);		
			}

//			alert("sss");
				
			// ustawianie obrazka na srodku ekraniu w IE6 oraz odpowiednia wysokosc overlay
			if ( navigator.appName == "Microsoft Internet Explorer" && (parseFloat(navigator.appVersion)<7 && navigator.appVersion.search("MSIE 7.0")<0) )
			{
				margin_top = document.documentElement.scrollTop;
				$("overlay").style.height=document.body.offsetHeight + (margin*2) + "px";
			}

		
		
	 // wielkosc okna przegladarki
	 if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }


			// dopasowanie obrazka do wielkosci przegladarki
			if (endW+5*margin > myWidth)
			{
				newW = Math.round(myWidth*0.85);
				endH = Math.round((newW*endH) / endW);
				endW = newW;
			}
			
			if (endH+5*margin > myHeight)
			{
				newH = Math.round(myHeight*0.85);
				endW = Math.round((newH*endW) / endH);
				endH = newH;
			}


			$("photo").src = src;
			$("photo").title = title;
			

			
			$("photo").style.width = endW*skala+"px";
			$("photo").style.height= endH*skala+"px";
		
			$("overlay").style.display="block";
			$("center").style.display="block";
			$("center").style.width = endW*skala + margin + "px";
			$("center").style.height = endH*skala + margin + "px";
	
			imageResize (endW, endH);	

			return;
			
		}
		else if(!pr[name] && petla<10) // Czekaj az sie zaladuje
		{
			window.status = petla;
			petla++;
			setTimeout("imageLoaded()", 100); 
		} 
		else if (petla==100) 
		{ 
			window.status = "Brak obrazka"; 
			return;  
		}
    } 


function imageZoomIn(obj, x)
{
	name = obj.getAttribute("rel");
	if (!pr[name])
	{
		imagePreload(obj);
		window.status = "Laduje...";
		petla = 0;
	}
	tmp = obj;
	imageLoaded();
	return false;
}



function imageResize(endW, endH)
{

	dx = endW - $("center").offsetWidth;
	dy = endH - $("center").offsetHeight;

	x = Math.round($("center").offsetWidth+dx/2);
	y = Math.round($("center").offsetHeight+dy/2);
	

	$("center").style.width = x + margin*2 + "px";
	$("center").style.height = y + margin*2 + "px" ;
	$("center").style.marginLeft = -x/2 + "px";
	$("center").style.marginTop = -y/2 + margin_top + "px";		

	$("photo").style.width = x + "px";
	$("photo").style.height = y + "px";		

	if (x < endW || y < endH)
	{
		window.setTimeout("imageResize(endW, endH)",10);
	}
	else
	{	
		if ($("photo").title && $("photo").title!="null")
		{
			$("desc").style.display = "block";
			$("center").style.marginTop = -(y + $("desc").offsetHeight + margin)/2 + margin_top + "px";
			$("center").style.height = y + $("desc").offsetHeight + margin*2 + "px";
		}
	}
}



function imageZoomOut()
{
	$("desc").style.display = 'none';
	$("desc").innerHTML = '';
	$("photo").src = '';
	$("center").style.display = 'none';
	$("overlay").style.display="none";
	return false;
}

function imagePreload(obj)
{
	name = obj.getAttribute("rel");
//	if (!pr[name])
	{
		pr[name] = false;
		preload = new Image();
		preload.onload = function () { pr[name] = true;  return true; }
		preload.src = obj.getAttribute("href");
		return false;
	}
}


function checkKeycode(e) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;

	if (keycode == 27) 
	{
		imageZoomOut();
	}
}
document.onkeyup = checkKeycode;
