var timer;
var timer2;
var nhgt = 120;
var nwdt = 90;
var nopa = 0;
var gal_id = 0;
var gal_type = 0;
var tempimg = new Image();
tempimg.src = "app/layout_web/styles/images/loading.gif";

var steph = 75;
var stepw = 75;
var stepb = 15;
var buffimage = new Image();

var aimage = new Array();
var curimg = 0;

function imgwait()
{
  var n_win = document.getElementById("n_win");

	if(isImageOk(buffimage))
	{
		n_win.style.backgroundImage = 'none';
		resizeh();
	}
	else
	{
		setTimeout("imgwait()", 50);
	}
}

function isImageOk(img) {
   if (!img.complete)
		return false;
   if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
    return false;
		
  return true;
}

function winHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
    myHeight = window.innerHeight;
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    myHeight = document.documentElement.clientHeight;
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    myHeight = document.body.clientHeight;
  return myHeight;
}

function winWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
    myWidth = window.innerWidth;
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    myWidth = document.documentElement.clientWidth;
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    myWidth = document.body.clientWidth;
  return myWidth;
}

function nfor()
{
	zobraznahled(curimg + 1, gal_id, gal_type);
}
function nbac()
{
	zobraznahled(curimg - 1, gal_id, gal_type);
}

function zobraznahled(i, j, type)
{
	var n_bgr = document.getElementById("n_bgr");
  var n_win = document.getElementById("n_win");
	var n_img = document.getElementById("n_img");
  var n_ifr = document.getElementById("n_ifr");
	var n_bac = document.getElementById("n_bac");
  var n_for = document.getElementById("n_for");
  var n_txt = document.getElementById("n_txt");
	gal_id = j;
	gal_type = type;
  buffimage = new Image();

	var whgt = winHeight();
	var wwdt = winWidth();
	
	n_win.style.left = ((wwdt - nwdt) / 2) + 'px';
	n_win.style.top = ((whgt - nhgt) / 2) + 'px';
	
	n_win.style.width = nwdt;
	n_win.style.height = nhgt;

	n_bgr.style.display = 'block';
	n_win.style.display = 'block';
	n_ifr.style.display = 'block';
	
	n_bac.style.display = ((i > 0) ? 'block' : 'none');
	n_for.style.display = ((i < (aimage[j].length - 1)) ? 'block' : 'none');
	
	switch(type)
	{
		case 0: buffimage.src = nimage(i, j, "_m"); break;
		case 1: buffimage.src = nimage(i, j, "_3"); break;
		case 2: buffimage.src = aimage[j][i]; break;
	}
	n_txt.innerHTML = '';
	
	curimg = i;
	setTimeout("imgwait()", 300);
}
function zobraznahled_all(i)
{
	var nahled = document.getElementById("nahled"+i);
	var zobraznahled = document.getElementById("zobraznahled"+i);
	if(nahled.style.display == 'block')
	{
		nahled.style.display = 'none';
		zobraznahled.innerHTML = 'zobrazit další foto';
	}
	else
	{
		nahled.style.display = 'block';
		zobraznahled.innerHTML = 'skrýt další foto';
	}
}

function resizeh()
{
  var n_win = document.getElementById("n_win");
	var n_img = document.getElementById("n_img");
	
	var hgt = buffimage.height + 30;
	var whgt = winHeight();
	var wwdt = winWidth();

	if((nhgt + steph) < hgt)
	{
		nhgt += steph;
		n_win.style.height = nhgt + 'px';
		n_win.style.top = ((whgt - nhgt) / 2) + 'px';
		n_win.style.left = ((wwdt - nwdt) / 2) + 'px';
		timer2 = setTimeout("resizeh()",50);
	}
	else
	{
		nhgt = hgt;
		n_win.style.height = nhgt + 'px';
		n_win.style.top = ((whgt - nhgt) / 2) + 'px';
		clearTimeout(timer2);
		resizew();
	}

}
function resizew()
{
  var n_win = document.getElementById("n_win");
	var n_img = document.getElementById("n_img");
	var test = document.getElementById("test");
	
	var wdt = buffimage.width;
	var whgt = winHeight();
	var wwdt = winWidth();
	
	if((nwdt + stepw) < wdt)
	{
		nwdt += stepw;
		n_win.style.width = nwdt + 'px';
		n_win.style.top = ((whgt - nhgt) / 2) + 'px';
		n_win.style.left = ((wwdt - nwdt) / 2) + 'px';
		timer2 = setTimeout("resizew()",50);
	}
	else
	{
		nwdt = wdt;
		clearTimeout(timer2);
		n_win.style.width = nwdt + 'px';
		n_win.style.left = ((wwdt - nwdt) / 2) + 'px';
		n_img.src = buffimage.src;
		n_img.style.display = 'block';
		resizeb();
	}
}
function resizeb()
{
  var n_win = document.getElementById("n_win");
  var n_bot = document.getElementById("n_bot");
	
	var bhgt = buffimage.height + 30;
	
	if((nhgt + stepb) < bhgt)
	{
		nhgt += stepb;
		n_win.style.height = nhgt + 'px';
		timer2 = setTimeout("resizeb()",50);
	}
	else
	{
		nhgt = bhgt;
		clearTimeout(timer2);
		n_win.style.height = nhgt + 'px';
		n_bot.style.display = 'block';
		//nhgt = bhgt - 30;
		opacity();
	}
}
function opacity()
{
	var n_img = document.getElementById("n_img");
	if(nopa < 1.0)
	{
		nopa += 0.2;
		n_img.style.opacity = nopa;
		timer2 = setTimeout("opacity()", 1);
	}
	else
	{
		nopa = 1;
		n_img.style.opacity = nopa;
		clearTimeout(timer2);
	}
}

function skryjnahled()
{
	var n_bgr = document.getElementById("n_bgr");
  var n_win = document.getElementById("n_win");
	var n_img = document.getElementById("n_img");
  var n_ifr = document.getElementById("n_ifr");
  var n_bot = document.getElementById("n_bot");
	var n_bac = document.getElementById("n_bac");
  var n_for = document.getElementById("n_for");
	
	n_bgr.style.display = 'none';
	n_win.style.display = 'none';
	n_img.style.display = 'none';
	n_ifr.style.display = 'none';
	n_bot.style.display = 'none';
	n_bac.style.display = 'none';
	n_for.style.display = 'none';
	n_img.src = "";
	
	nhgt = buffimage.height;
	nwdt = buffimage.width;
	n_win.style.height = nhgt + 'px';
	n_win.style.width = nwdt + 'px';

	n_win.style.backgroundImage = 'url("'+ tempimg.src +'")';
}

