var img
var interval_lb;
var interval_img;
var interval_menu;
var y_scroll;
var patt_ie=/.*msie.*/i;
var ie=patt_ie.test(navigator.userAgent);
var ie_version;
var area_x;
var area_y;
var page_width=780;
var path_url_img;
if(ie===false)
	{
	area_x=window.innerWidth;
	area_y=window.innerHeight;
	}
else
	{
	area_x=document.documentElement.clientWidth-16;
	area_y=document.documentElement.clientHeight;
	var patt_version_ie=/msie [0-9]+\.[0-9]+/i;
	ie_version=String(navigator.userAgent.match(patt_version_ie));
	ie_version=parseInt(ie_version.substring(5,6));
	}
function Get_a_imgs(a_class)
	{
	var a_imgs=new Array;
	var a=document.getElementsByTagName("a");
	for(var i=0;i<a.length;i++)
		{
		if(a[i].className==a_class)
			{
			a_imgs.push(a[i]);
			}
		}
	return a_imgs;
	}
function Set_a_onclick(a_imgs)
	{
	for(var i=0;i<a_imgs.length;i++)
		{
		var children=a_imgs[i].childNodes;
		var url=Get_img_url(children);
		a_imgs[i].onclick=Load_img_ie;
	  	if(ie===true)
	  		{
	 		a_imgs[i].onclick=Load_img_ie;
	 		}
	 	else
	 		{
	 		a_imgs[i].setAttribute("onclick","Load_img('"+url+"');return false");
	 		}
		}
	}
function Get_img_url(children)
	{
	var patt_gif=/.*blank.gif$/;
	var url;
	for(var i=0;i<children.length;i++)
		{
		if(children[i].nodeType===1&&children[i].tagName.toLowerCase()=="img")
			{
			if(patt_gif.test(children[i].src)===false)
				{
				if(path_url_img===undefined&&ie_version===6)
					{
					path_url_img=String(children[i].src.match(/http:\/\/.*\/thumb/));
					path_url_img=path_url_img.replace("thumb","");
					}
				url=children[i].src.replace("thumb/","");
  				break;
				}
			}
		}
	return url;
	}
function Load_img_ie()
	{
	if(ie_version===6)
		{
		var script_url=String(this);
		var patt_path=/[^\/][0-9]{1,4}\/.*\.php$/;
		var for_url=String(script_url.match(patt_path));
		url=for_url.replace("/","-");
		url=path_url_img+url.replace("php","png");
		}
	else
		{
		var children=this.childNodes;
		var url=Get_img_url(children);
		}
	Load_img(url);
	return false;
	}
function Load_img(url)
	{
	Create_shadow();
	img=new Image;
	if(ie===true)
		{
		img.style.filter="alpha(opacity=0)";
		img.onload=Img_onload;
		}
	else
		{
		img.style.opacity=0;
		img.setAttribute("onload","Img_onload()");
		}
	img.src=url;
	img.setAttribute("id","img_lightbox");
	}
function Img_onload()
	{
	var top_line_h=20;
	var img_border=10;
	var div_margin=10;
	var img_width=img.width;
	var img_height=img.height;
	var out_size=2*(img_border+div_margin);
	var max_w=area_x-out_size;
	var max_h=area_y-out_size-top_line_h;
	if(img_width>max_w||img_height>max_h)
		{
		var main_size=img_width;
		var control_size=max_w;
		if(img_height>max_h)
			{
			main_size=img_height;
			control_size=max_h;
			}
		var scale=control_size/main_size;
		img_width=Math.round(img_width*scale);
		img_height=Math.round(img_height*scale);
		img.width=img_width;
		img.height=img_height;
		}
	img.style.position="absolute";
	img.style.top=img_border+"px";
	img.style.left=img_border+"px";
	var end_width=img_width+2*img_border;
	var end_height=img_height+2*img_border;
	var body=document.getElementsByTagName("body")[0];
 	var img_div=document.createElement("div");
 	img_div.setAttribute("id","img_div");
 	img_div.style.position="absolute";
 	img_div.style.width="0px";
 	img_div.style.height="0px";
 	img_div.style.left=(area_x/2)+"px";
 	img_div.style.top=(area_y/2)+"px";
 	img_div.style.backgroundColor="white";
 	body.appendChild(img_div);
 	img_div.appendChild(img);
 	interval_lb=window.setInterval("Resize_img_div("+end_width+","+end_height+","+top_line_h+")",1);
	}
function Resize_img_div(end_width,end_height,top_line_h)
	{
	var img_div=document.getElementById("img_div");
	var div_w=parseInt(img_div.style.width);
	var div_h=parseInt(img_div.style.height);
	div_w=parseInt(div_w)+30;
	if(div_w>end_width)
		{
		div_w=end_width;
		}
	div_h=parseInt(div_h)+30;
	if(div_h>end_height)
		{
		div_h=end_height;
		}
	img_div.style.width=div_w+"px";
	img_div.style.height=div_h+"px";
	var x_left=(area_x-div_w)/2;
	img_div.style.left=x_left+"px";
	y_top=(area_y-div_h)/2+top_line_h/2;
	if(ie===false)
		{
		var y_top=window.pageYOffset+(window.innerHeight-div_h)/2+top_line_h/2;
		}
	img_div.style.top=y_top+"px";
	if(div_w===end_width&&div_h===end_height)
		{
		clearInterval(interval_lb);
		interval_img=window.setInterval("Display_img("+x_left+","+y_top+","+end_width+","+top_line_h+")",1);
		}
	}
function Display_img(x_left,y_top,end_width,top_line_h)
	{
	var img=document.getElementById("img_lightbox");
	if(ie===false)
		{
		var alpha=parseFloat(img.style.opacity)+0.05;
		img.style.opacity=alpha;
		if(alpha===1)
			{
			clearInterval(interval_img);
			Create_menu(top_line_h);
			}
		}
	else
		{
		var patt_alpha=/[0-9]{1,3}/;
		var alpha=parseInt(patt_alpha.exec(img.style.filter))+5;
		img.style.filter="alpha(opacity="+alpha+")";
		if(alpha===100)
			{
			clearInterval(interval_img);
			Create_menu(top_line_h);
			}
		}
	}
function Create_menu(top_line_h)
	{
	var img_div=document.getElementById("img_div");
	var menu_div=document.createElement("div");
	menu_div.setAttribute("id","menu_lightbox");
	menu_div.style.position="absolute";
	menu_div.style.left="0px";
	menu_div.style.top=(-1)+"px";
	menu_div.style.width=img_div.style.width;
	menu_div.style.height="1px";
	menu_div.style.backgroundColor="silver";
	if(ie===false)
		{
		menu_div.style.opacity=0.2;
		}
	else
		{
		menu_div.style.filter="alpha(opacity=20)";
		}
	img_div.appendChild(menu_div);
	interval_menu=window.setInterval("Display_menu("+top_line_h+")",1);
	}
function Display_menu(top_line_h)
	{
	var top_line=document.getElementById("menu_lightbox");
	var y_top=parseInt(top_line.style.top);
	var h=parseInt(top_line.style.height);
	var new_h=h+2;
	if(new_h>=top_line_h)
		{
		new_h=top_line_h;
		var img_close=document.createElement("div");
		if(ie===false)
			{
			img_close.setAttribute("onclick","Close_lightbox()");
			img_close.setAttribute("onmouseover","Close_over(this)");
			img_close.setAttribute("onmouseout","Close_out(this)");
			}
		else
			{
			img_close.onclick=Close_lightbox;
 			img_close.onmouseover=Close_over;
 			img_close.onmouseout=Close_out;
			}
		img_close.setAttribute("title","Zavřít obrázek");
		img_close.style.position="absolute";
		img_close.style.width="10px";
		img_close.style.height="10px";
		img_close.style.top="-15px";
		img_close.style.right="5px";
		img_close.style.backgroundImage="url('/system/frontend/images/close.png')";
		img_close.style.backgroundRepeat="no-repeat";
		img_close.style.backgroundPosition="-10px 0px";
		img_close.style.cursor="pointer";
		var img_div=document.getElementById("img_div");
		img_div.appendChild(img_close);
		clearInterval(interval_menu);
		}
	var new_y_top=y_top-new_h+h;
	top_line.style.top=new_y_top+"px";
	top_line.style.height=new_h+"px";
	}
function Create_shadow()
	{
	var body=document.getElementsByTagName("body")[0];
 	var top_position=window.pageYOffset;
 	y_scroll=top_position;
 	var page=body.getElementsByTagName("div")[0];
 	var page_w=page_width;
 	var left_x=(area_x-page_w)/2;
 	if(ie===false)
 		{
 		page.style.top=-top_position+"px";
		page.style.left=left_x+"px";
		page.style.position="fixed";
 		}
	var shadow=document.createElement("div");
	shadow.setAttribute("id","shadow");
	shadow.style.position=(ie===false?"fixed":"absolute");
// 	shadow.style.position="fixed";
	shadow.style.left="0px";
	shadow.style.top="0px";
	shadow.style.width=area_x+"px";
	shadow.style.height=area_y+"px";
	shadow.style.overflow="hidden";
	shadow.style.backgroundColor="black";
	if(ie===false)
		{
 		shadow.style.opacity=0.9;
 		}
 	else
 		{
 		shadow.style.filter="alpha(opacity=90)";
 		}
 	body.appendChild(shadow);
	}
function Close_over(self_div)
	{
	if(self_div===undefined)
		{
		self_div=this;
		}
	self_div.style.backgroundPosition="0px 0px";
	}
function Close_out(self_div)
	{
	if(self_div===undefined)
		{
		self_div=this;
		}
	self_div.style.backgroundPosition="-10px 0px";
	}
function Close_lightbox()
	{
	var body=document.getElementsByTagName("body")[0];
 	var page=body.getElementsByTagName("div")[0];
 	page.style.position="relative";
 	page.style.top="auto";
 	page.style.left="auto";
	var img_div=document.getElementById("img_div");
	var shadow=document.getElementById("shadow");
	body.removeChild(img_div);
	body.removeChild(shadow);
	window.scrollBy(0,y_scroll);
	}

