var isDOM = document.getElementById ? true : false;
var isIE4 = document.all ? true : false;
var isNS4 = document.layers ? true : false;

function getRef(name)
{
  if (isDOM) return document.getElementById(name);
  if (isIE4) return document.all[name];
  if (isNS4) return document.layers[name];
}
function getStyRef(ref) { return ((isNS4 || ref == null) ? ref : ref.style); }
function getSty(id) { return getStyRef(getRef(id)); };

pics = new Array();
var pocet = 0;
function preload(prvni) {
  pics[pocet] = new Array(1);
  pics[pocet][0] = new Image();
  pics[pocet][0].src = prvni;
  pocet++;
}
var preload_img = new Array(
'logo.png'
);

function hyperlink(url) { window.location=url; }

function targetBlank(url)
{
  var width = 800;
  var height = 600;
  var left = Math.floor( (window.screen.availWidth - 10 - width) / 2);
  var top = Math.floor( (window.screen.availHeight - 20 - height) / 2);
  var href = (typeof(url) == 'string') ? url : url.href;

	if (href != "")
	{
    var winParms = "screenX=" + left + ",screenY=" + top + ",top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
		return !window.open(href,'_blank', 'menubar=yes,toolbar=yes,location=yes,directories=no,fullscreen=no,titlebar=yes,hotkeys=yes,alwayslowered=no,status=yes,scrollbars=yes,resizable=yes,dependent=no,channelmode=NO,alwaysraised=yes,zlock=yes,'+winParms);
	}
}

var nahledImg = null;
var nahledDivStyle = null;
var nahledDiv = null;
var nahledDivPopis = null;
var showNahled = false;

var lang = null;

window.onload =  function() {
  langExpr = new RegExp("/(eu|sk|cz)/");
  lang = langExpr.exec(window.location.href);

  nahledDiv = document.createElement('div');
  nahledDiv.id = 'nahled';

  nahledImg = document.createElement('img');
  nahledImg.alt = 'náhled';
  
  nahledDivPopis = document.createElement('div');
  nahledDivPopis.className = 'p';
  
  nahledDivDiv = document.createElement('div');
  nahledDivDiv.className = 'o';

  nahledDiv.insertBefore(nahledDivPopis, nahledDiv.firstChild);
  nahledDiv.insertBefore(nahledDivDiv, nahledDiv.firstChild);
  nahledDivDiv.insertBefore(nahledImg, nahledDivDiv.firstChild);
  document.body.insertBefore(nahledDiv, document.body.firstChild);

  nahledDivStyle = getStyRef(nahledDiv);

  addMouseEvent(document);

  addEvent(getRef('slovo'), 'click', function() { if (getRef('slovo').value == '-- hledat --') getRef('slovo').value=''; });

  zajimat = getRef('mohlo-by-zajimat');

  setPosition();

  addEvent(window, 'scroll', setPosition);
  addEvent(window, 'resize', setPosition);

  if (zajimat.scrollHeight <= zajimat.clientHeight)
    animateUp();
  else
    animateDown();

  for (soubor in preload_img)
  {
    if ( 'indexOf' == soubor ) { continue; }
    preload(((lang != null) ? '/'+lang[1]: '')+'/' + preload_img[soubor]);
  }
}

function setPosition() {
  screenH = getDocHeight();
  getSty('zapati_null').top = (screenH - 40)+'px'
}

var vyska = 400;
var increment = 7;
var timeout = 20;
var screenH = 0;
var zajimat;
var obs;

function animate(inc)
{
//   ssss = 'scrollHeight'+zajimat.scrollHeight+"\n"+'|clientHeight'+zajimat.clientHeight+
//      '|vyska '+vyska+"\n|inc "+inc+'|screenH '+screenH +'<'+(zajimat.clientHeight + 440);
//   getRef('zapati').innerHTML = getRef('navigace').innerHTML = ssss;

  zajimatSty = getStyRef(zajimat);
  if (
      (inc < 0 && zajimat.scrollHeight > zajimat.clientHeight) ||
      (inc > 0 && screenH < zajimat.clientHeight + 440) || // 440 experimentalne zjisteno - velikost ankety nadpisu aj.
      (inc > 0 && zajimat.scrollHeight <= zajimat.clientHeight)
      ) {
        if (
            (inc < 0 && zajimat.scrollHeight > zajimat.clientHeight) ||
            (inc > 0 && screenH < zajimat.clientHeight + 440)
           ) {
          zajimatSty.height = (vyska-inc+1)+'px'; // konec
        }
      return;
  } else if (inc > 0){
    setTimeout(animateDown,timeout);
  } else {
    setTimeout(animateUp,timeout);
  }

  vyska += inc;
  zajimatSty.height = vyska+'px';
}

function animateUp() {
  animate(-increment);
}

function animateDown() {
  animate(increment);
}

function getDocHeight() {
    var D = document;
    var db = D.body;
    var de = D.documentElement;
    return Math.max(
        db.scrollHeight, de.scrollHeight,
        db.offsetHeight, de.offsetHeight,
        db.clientHeight, de.clientHeight
    );
}
function hledat() {
  if (getRef('slovo').value != '-- hledat --' && getRef('slovo').value != '') {
    window.location = ((lang != null) ? '/'+lang[1]: '')+"/hledat/"+getRef('slovo').value;
  }
  return false;
}

function addMouseEvent(element)
{
  for (var elementIndex = 0; elementIndex < element.childNodes.length; elementIndex++)
  {
    if (element.childNodes[elementIndex].className == 'obrazek')
    {
      addEvent(element.childNodes[elementIndex], 'mousemove', function(event) { mouseMove(event); });
      addEvent(element.childNodes[elementIndex], 'mouseout', function() { showNahled = false;
      if (nahledDivStyle != null) nahledDivStyle.display = 'none'; } );
    }
    addMouseEvent(element.childNodes[elementIndex]);
  }
}

function addEvent(obj, event, funct)
{
  if (obj.attachEvent) { obj.attachEvent('on'  + event, funct); }
  else if (obj.addEventListener) { obj.addEventListener(event, funct, false); }
  else eval('obj.' + event + ' = ' + funct); 
}

function mouseMove(event)
{
  if (showNahled && nahledDivStyle != null) setNahledPosition(event);
}

function setNahledPosition(event)
{
  event= window.event || event;

  var screenWidth = getScreenWidth();
  var screenHeight = getScreenHeight();

  var left = event.clientX + 5;
  var top = event.clientY + 5;

  var sirkaNahled = nahledDiv.clientWidth + 10;
  var vyskaNahled = nahledDiv.clientHeight + 10;

//   stri = '|top'+top+'|vyskaNahled'+vyskaNahled+'|screenHeight'+screenHeight;
//   getRef('navigace').innerHTML = stri;
//   getRef('zapati').innerHTML = stri;
  
  if (left + sirkaNahled > screenWidth) {
    if (top + vyskaNahled > screenHeight) {
      top = screenHeight - vyskaNahled;
      left = left - sirkaNahled - 10;
    } else left = screenWidth - sirkaNahled;
  }
  else if (top + vyskaNahled > screenHeight) top = screenHeight - vyskaNahled;

  nahledDivStyle.left = left + ((window.scrollX ? window.scrollX : document.documentElement.scrollLeft)) + "px";
  nahledDivStyle.top = top + ((window.scrollY ? window.scrollY : document.documentElement.scrollTop)) + "px";
}

function show(event, image, nadpis, popis)
{
  if (nahledDivStyle != null)
  {
    setNahledPosition(event);
    nahledImg.src = ((lang != null) ? '/'+lang[1]: '')+'/nahled/' + image;

    nahledDivPopis.innerHTML = '';
    if (popis != '')
    {
      popisTAG = document.createElement('div');
      popisTAG.className = 'lbl';
      popisTAG.innerHTML = popis;
      nahledDivPopis.insertBefore(popisTAG, nahledDivPopis.firstChild);
    }
    nadpisTAG = document.createElement('h1');
    nadpisTAG.innerHTML = nadpis
    nahledDivPopis.insertBefore(nadpisTAG, nahledDivPopis.firstChild);

//     getStyRef(nahledImg.parentNode).height = nahledImg.height;
//     getStyRef(nahledImg.parentNode).width = nahledImg.width;

    nahledDivStyle.display = 'block';
    showNahled = true;
  } 
}

function sw(id)
{
  menuItem = getSty(id);
  if (menuItem.display == 'none' || menuItem.display == '')
    menuItem.display = 'block';
  else
    menuItem.display = 'none';
  zobr = '';

  setPosition();
  return false;
}

function getHeight()
{
  return Math.max(window.innerHeight || 0,
                  document.documentElement.clientHeight || 0,
                  document.body.clientHeight || 0
                 );
}

function getScreenHeight()
{
  return Math.min(window.innerHeight || 10000,
                  document.documentElement.clientHeight || 10000,
                  document.body.clientHeight || 10000
                 );
}

function getWidth()
{
  return Math.max(window.innerWidth || 0,
                  document.documentElement.clientWidth || 0,
                  document.body.clientWidth || 0
                 );
}

function getScreenWidth()
{
  return Math.min(window.innerWidth || 10000,
                  document.documentElement.clientWidth || 10000,
                  document.body.clientWidth || 10000
                 );
}

function openCalendar(field, type) {
    dateField = getRef(field);
    dateType = type;
    window.open(((lang != null) ? '/'+lang[1]: '')+'/calendar', 'eXnico_Calendar', 'width=450,height=200,status=yes');
}

var den;
var hodina;
var termin;
var poradi;
var pobocka;

function openWebCalendar() {
    var terapie = getRef('terapie');
    den = getRef('den');
    hodina = getRef('hodina');
    termin = getRef('termin');
    poradi = getRef('poradi');
    pobocka = getRef('pobocka');

    window.open('/webcalendar/month.php?terapie='+terapie.value, 'eXnico_Calendar', 'width=1024,height=700,status=yes,scrollbars=yes,resizable=yes');
}

