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 = "layout/styles/images/loading2.gif";

var steph = 75;
var stepw = 75;
var stepb = 15;
var buffimage = new Image();

var imgpath = "data/nemovitosti/";
var aimage = new Array();
var atext = new Array();
var curimg = 0;
var curmenu = 0;

function mcast(i){
	var display = (i == 3100)?'block':'none';
	document.getElementById('ppraha').style.display = display;
}

function zobraz_dispozice(){
	val = document.getElementById('nemovitost_typ');
	it = document.getElementById('disp');

	if(val.value == 4)
	{
		if (navigator.appName == 'Microsoft Internet Explorer')
			it.style.display = 'block';
		else
			it.style.display = 'table-row';
	}
	else
	{
		it.style.display = 'none';
	}
}

function foto(gal){
  window.open("galerie.php?id_gal="+gal,"x","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=auto,resizable=no,width=955,height=685,left=50,top=50"); 
}
Array.prototype.inArray = function ( search_phrase )
{
  for( var i = 0; i < this.length; i++ )
  {
    if( search_phrase == this[i] )
    {
      return i;
    }
  }
  return false;
}


function submenu(i, sele)
{
	var j = i.split(':');
  var n = 1;
  var delka = j.length;
  if(curmenu > delka)
  {
  	curmenu = delka;
  	return 0;
  }
  curmenu = delka;
  while(document.getElementById('sub'+n) != null) {
    var submenu = document.getElementById('sub'+n);
    var btn = document.getElementById('btn'+n);
    var ifr = document.getElementById('ifr'+n);
		var sel = document.getElementById('sel');
    if(j.inArray(n) === false)
		{
      submenu.style.display = 'none';
      if(ifr) ifr.style.display = 'none';
      if(btn.className == 'btn up hold')
      	btn.className = 'btn up hold';
      else if(btn.className == 'btn up hold last')
      	btn.className = 'btn up hold last';
      else if(btn.className == 'btn last' || btn.className == 'btn sel last')
      	btn.className = 'btn last';
      else
	      btn.className = 'btn';
	  }
    else
		{
      submenu.style.display = 'block';
      if(ifr) ifr.style.display = 'block';
      if(btn.className == 'btn up hold')
      	btn.className = 'btn up hold';
      else if(btn.className == 'btn up hold last')
      	btn.className = 'btn up hold last';
      else if(btn.className == 'btn last' || btn.className == 'btn sel last')
      	btn.className = 'btn sel last';
      else
      	btn.className = 'btn sel';
		}
    n++;
  }
  clearTimeout(timer);
}

function submenux(i, sele)
{
	var j = i.split(':');
  var n = 1;
  var delka = j.length;
  if(curmenu > delka)
  {
  	curmenu = 0;
  	return 0;
  }
  curmenu = delka;
  while(document.getElementById('sub'+n) != null) {
    var submenu = document.getElementById('sub'+n);
    var btn = document.getElementById('btn'+n);
		var sel = document.getElementById('sel');
    if(!inArray(j, n))
		{
      submenu.style.display = 'none';
			if(j.join("").indexOf(n) > 0)
		 	{
		 		btn.style.fontWeight = 'normal';
		 		btn.style.background = '#ffffff';
		 	}
		 	else
			{
				btn.style.color = '#000066';
				btn.style.backgroundPosition = '0px -38px';
		 	}
	  }
    else
		{
      submenu.style.display = 'block';
			if(sele == 0)
			{
				if(j.join("").indexOf(n) > 0)
			 	{
			 		btn.style.fontWeight = 'bold';
			 		btn.style.background = '#FFCC00';
			 	}
			 	else
				{
			 		btn.style.color = '#ffffff';
					btn.style.backgroundPosition = '0px 0px';
			 	}
			}
		}
    n++;
  }
  clearTimeout(timer);
}


function submenu2(i, j)
{
  var n = 1;
  while(document.getElementById('sub'+j+'sub'+n) != null) {
    var submenu = document.getElementById('sub'+j+'sub'+n);
    if(i != n)
		{
      submenu.style.display = 'none';
	  }
    else
		{
      submenu.style.display = 'block';
		}
    n++;
  }
  clearTimeout(timer2);
}
function submenu2s(j)
{
  timer2 = setTimeout('submenu2(0,'+j+')',300);
}



function submenus()
{
	clearTimeout(timer);
  timer = setTimeout('submenu(\'0\')',300);
}

function isImageOk(img) {
   if (!img.complete)
		return false;
   if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
    return false;
		
  return true;
}

function imgwait()
{
  var n_win = document.getElementById("n_win");

	if(isImageOk(buffimage))
	{
		n_win.style.backgroundImage = 'none';
		resizeh();
	}
	else
	{
		setTimeout("imgwait()", 50);
	}
}

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;

	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;
	}
	n_txt.innerHTML = atext[j][i];
	
	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 +'")';
}

function nemonahled(i, j)
{
  var nemo_nahled = document.getElementById("nemo_nahled");
	nemo_nahled.src = nimage(i, j, "_w");
}
function nimage(i, j, size)
{
	var parts = aimage[j][i].split('.');
	var name = parts[0];
	var ext = parts[1];
	
	return imgpath+name+size+"."+ext;
}

function gval(id) { return document.getElementById(id).value; }
function sval(id, val) { document.getElementById(id).value = val; }
function scol(id, col) { document.getElementById(id).style.background = '#'+col; }
function dott(num)
{
	num = Math.ceil(num).toString();
	var length = num.length;
	var string = "";
	var i;
	
	for(i = (length - 3); i >= 0; i = i - 3)
	{
		if(string != "") string = num.substr(i, 3) + "." + string; else string = num.substr(i, 3) + string;
	}
	string = num.substr(0, (length % 3)) + "." + string;
	if(string.substr(0, 1) == ".") string = string.substr(1, (string.length - 1));
	if(string.substr(0, 2) == "-.") string = '-'+string.substr(2, (string.length - 2));
	if(string.substr(0, 2) == "0.") string = "0";
	
	return string+",-";
}
function calc1()
{
	if(isNaN(gval("tc11")))
	{
		document.getElementById("calc2").style.display = "none";
		document.getElementById("calc3").style.display = "none";
		scol("tc11", "F3D3D3");
		return 0;
	}
	var pcena = parseInt(gval("tc11"), 10);
	
	var provize = pprovize = iprovize = 0;
	sval("tc12", dott(pcena));
	if(pcena <= 600000) provize = 30000;
	else if(pcena > 600000 && pcena <= 5000000) pprovize = 5;
	else if(pcena > 5000000) iprovize = 1;
	
	if(iprovize != 0)
	{
		sval("tc13", "individuálně");
		sval("tc14", "individuálně");
		sval("tc15", "individuálně");
	}
	else if(pprovize != 0)
	{
		sval("tc13", pprovize+"%");
		sval("tc14", dott(pcena + (pcena * pprovize / 100)));
		sval("tc15", dott(pcena + ((pcena * pprovize / 100) * 1.20)));
	}
	else
	{
		sval("tc13", dott(provize));
		sval("tc14", dott(pcena + provize));
		sval("tc15", dott(pcena + (provize * 1.20)));
	}
	sval("tc22", dott(pcena / 2));
	document.getElementById("calc2").style.display = "block";
	scol("tc11", "FFFFFF");
}
function calc2()
{
	var pcena = parseInt(gval("tc11"), 10);
	var zaloha = parseInt(gval("tc21"), 10);
	if(zaloha < 10000 || (zaloha > pcena / 2) || isNaN(gval("tc21")))
	{
		document.getElementById("calc3").style.display = "none";
		scol("tc21", "F3D3D3");
		return 0;
	}
	sval("tc31", dott(zaloha));
	if(zaloha == 100000)
		//mes = new Array(0, 0, 0, 4, 4, 4);
		mes = new Array(3, 3, 3, 3, 3, 3);
	else
		mes = new Array(3, 3, 3, 3, 3, 3);
	var i, urok = 0;
	for(i = 0; i <= 5; i++)
	{
		urok = urok - (mes[i] * zaloha / 100)
		sval('tc41'+i, dott(pcena - zaloha + urok));
		sval('tc42'+i, dott(pcena + urok));
		sval('tc43'+i, dott(urok));
		if(urok == 0) scol('tc43'+i, "E7F8FE"); else scol('tc43'+i, "EBEBE4");
	}
	document.getElementById("calc3").style.display = "block";
	scol("tc21", "FFFFFF");
}