var bIe=navigator.userAgent.indexOf("MSIE")!=-1 && document.all && !window.innerWidth;
var bMz=navigator.userAgent.indexOf("Gecko")!=-1 && document.getElementById && !document.all;
var bOp=navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById ? 1:0;
var bKn=navigator.userAgent.indexOf("Konqueror")!=-1 && document.getElementById && document.body.offsetWidth ? 1:0;
var bNs=navigator.userAgent.indexOf("Opera")==-1 && document.getElementById && !document.all;
var bOp6=bOp && document.body.offsetWidth ? 1:0;
var bNs4=document.layers ? 1:0;

var interval = 4; // milisekund
var counter = 0; // uhel
var procento = 0; // procento
var barevnejobjekt = 'textik'; // jaky IDcko objektu se bude obarvovat

var pi = 3.14159265358979323846;

var scr = 219;
var scg = 67;
var scb = 147;

var ecr = 237;
var ecg = 190;
var ecb = 63;

// promenny pro slideshow 
var timer = null;
var timerblinder = null;
var timerwait = null;
var slideshowtimer = 2500;
var blindertimer = 20;
var blindersteps = 25;

// dopocitany promenny
var actualstep = 1;
var switched = false;
var switchingstep = blindersteps / 2;
var step = 3.1415926/blindersteps;

var selected = 1;
var maximages = 0;
var actualproduct = 0;

// novy promenny pro Lart web 2.0
var timer_pozadi = null;
var pozadi_opacity = 75;
var pozadi_timer = 30;
var pozadi_actualstep = 7;
var pozadi_steps = 13;
var pozadi_step = 3.1415926/pozadi_steps;
var pozadi_randomtimer = 12000;

var timer_portfolio_gallery = null;
var portfolio_gallery_timer = 30;

var portfolio_gallery_steps = 22;
// var portfolio_gallery_steps = 24;
// var portfolio_gallery_steps = 14;

var portfolio_gallery_step = 0;
var portfolio_gallery_distance = 0;
var portfolio_gallery_position = 0;

var pauza = false;

function get_element(a) {
    return(bIe ? document.all[a]:bNs4 ? document.layers[a]:document.getElementById(a));
}

function zmenBarvu(idecko) {
  if (get_element(idecko) != null) {
    procento = Math.sin((counter/360)*2*pi);
    acr = Math.round((ecr - scr) * procento + scr);
    acg = Math.round((ecg - scg) * procento + scg);
    acb = Math.round((ecb - scb) * procento + scb);

    barvaTextiku = document.getElementById(idecko).style;
    barvaTextiku.color = 'rgb('+acr+','+acg+','+acb+')';
    counter = counter + 1;
    if (counter >= 180) { counter = 0;}
  }
  else {
    clearInterval(menBarvu);
  }
}

function blick() {
  menBarvu = setInterval("zmenBarvu(barevnejobjekt)",interval);
}

var aktualniUkazka = '';  // ukazka vpravo
var aktualniPolozka = ''; // polozka menu

function ukazka(polozka,idecko) {

  if (get_element(idecko) != null) {

    odkazek = document.getElementById(polozka);
    odkazek.className = 'selected';
    ukazecka = document.getElementById(idecko).style;
    ukazecka.display =  'block';

    if (aktualniPolozka != '') {
      starejodkaz = document.getElementById(aktualniPolozka);
      starejodkaz.className = '';
      staraukazecka = document.getElementById(aktualniUkazka).style;
      staraukazecka.display = 'none';
    }
  
    if (aktualniPolozka != polozka) {
      aktualniUkazka = idecko;
      aktualniPolozka = polozka;
    }
    else {
      aktualniUkazka = '';
      aktualniPolozka = '';
    }
  }
}

function toggle_pozn(poznid) {
   el=document.getElementById('poz'+poznid).style;
   el.display=(el.display == 'inline')?'none':'inline';
}

/* Open window funkce */

function OpenWin(adresa,jmeno,sirka,vejska,scrollbar) { 
  var parstring 
  sirka=sirka+20; 
  vejska=vejska+20; 

  if (sirka > window.screen.width) 
    { sirka = (window.screen.width - 100); scrollbar="1"; } 
  if (vejska > window.screen.height) 
    { vejska = (window.screen.height - 100); scrollbar="1"; } 

  pozx=(window.screen.width-sirka)/2; 
  pozy=(window.screen.height-vejska)/2; 
  parstring="scrollbars="+scrollbar+",top="+pozy+",left="+pozx+",width="+sirka+",height="+vejska+",resizable=yes";

  if ((typeof popupWin == 'undefined') || !(popupWin.open) || popupWin.closed) {

    popupWin = window.open(adresa, jmeno, parstring);
    popupWin.focus();

  }
  else {
    popupWin.location = adresa;
    popupWin.moveTo(pozx,pozy);

    if (parseInt(navigator.appVersion)>3) {
      if (navigator.appName=="Netscape") {
       popupWin.innerWidth=sirka;
       popupWin.innerHeight=vejska;
      }
      else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

        aw = popupWin.document.documentElement.clientWidth;
        ah = popupWin.document.documentElement.clientHeight;

        if (sirka == '520') {
          popupWin.resizeBy(504-aw,320-ah);
        }
        else if (sirka == '640') {
          popupWin.resizeBy(624-aw,469-ah);
        }
      }
    }
    popupWin.focus();
  }
}

function CloseWin(jmeno) { 
  popupWin = window.close(jmeno);
}

function startslideshow(delay) {
  timer = setInterval("shownext(true)",delay);
}

function stopslideshow() {
  if (timerblinder == null) {
    clearInterval(timer);
    timer = null;
    if (timerwait != null) {
      clearInterval(timerwait);
      timerwait = null;
    }
  }
  else {
    if (timerwait == null) {
      timerwait = setInterval("stopslideshow()",blindertimer);
    }
  }
}

function defocus() {
  if (bIe) window.focus();
  else {
    obj = get_element('focus');
    if (obj != null) {
      obj.focus();
    }
  }
}

function switchimage(imageid,cont) {

  i = 1;

  do {

    id = 'img' + i;
    img = get_element(id);

    idh = 'high' + i;
    imgh = get_element(idh);
    
    if (img != null) {
      if (imageid != i) { // nastavit nepruhlednost
        img.style.display = 'none';
        if (imgh != null) {
          imgh.style.display = 'none';
        }
      }
      else {
        img.style.display = 'block';
        if (imgh != null) {
          imgh.style.display = 'block';
        }
      }
    }
    i++;
  }
  while (img != null);

}

function showukazka(imageid,cont) {

  // vypnuti slideshow, pokud bezi.. pak ji ale zase zapnu
  if (cont) {
    if (timer != null) stopslideshow();
  }
  else {
  	if ((timer != null) || (timerblinder != null)) stopslideshow();
  }
  
  actualstep = 1;
  switched = false;
  if (timerblinder != null) clearInterval(timerblinder);
  timerblinder = setInterval("blinder("+imageid+","+cont+")",blindertimer);
  
  if (!cont) defocus();

}

function setopacity(idi,opacity) {
	obj = get_element(idi);
  if (obj != null) {

    // pro IE
    if (bIe) {
	    obj.style.filter = 'alpha(opacity=' + opacity + ')';
    }

	  // pro ostatni
	  else {
      value = (opacity >= 100) ? 1 : '.'+opacity;
      value = (opacity < 10) ? '.0'+opacity : value;
      obj.style.opacity = value;
      // obj.style.moz-opacity = value;
	  }
  	
  }
}

function blinder(imageid,cont) {

	if (actualstep <= blindersteps) {

    changed = false;

    // pripadne prehozeni obrazku
		if (!switched) {
		  if (actualstep >= switchingstep) {
        setopacity('blinder',100);
		  	switchimage(imageid,cont);
		  	switched = true;
		  	changed = true;
		  }
		}
		
		// zmena opacity normalnim zpusobem
		if (!changed) {
      faze = Math.sin(actualstep*step);
			opacity = Math.round(faze * 100);
			setopacity('blinder',opacity);
		}
		
	}
	else { // ukoncit akci

    // nastavit opacity na 0
    setopacity('blinder',0);
		
		// pustit slideshow
		if (cont) startslideshow(slideshowtimer);
		
    // zrusit blindovani
		clearInterval(timerblinder);
    timerblinder = null;
		
	}

	actualstep++;

}

function hideukazka() {
  obj = get_element('ukazka');
  obj.style.left = '-9999px';
}

function shownext(cont) {

  if (!pauza || !cont) {

    // next
    if (selected < maximages) {
      selected++;
    }
    else {
      selected = 1;
    }
    showukazka(selected,cont);
  
    if (!cont) defocus();
  }
}

function showprev(cont) {
  // prev
  if (selected > 1) {
    selected--;
  }
  else {
    selected = maximages;
  }
  showukazka(selected,cont);

  if (!cont) defocus();
}

function change_location() {
  ido = 'link' + selected;
  obj = get_element(ido);
  if (obj != null) {
    window.location = obj.innerHTML;
  }
}

function view_big_image() {
  ido = 'link' + selected;
  obj = get_element(ido);
  if (obj != null) {
    OpenWin(obj.innerHTML,'katooimage',660,650,1);
  }
}

// ---------------------------------
// novy funkce pro Lart web 2.0
// ---------------------------------

function init_website(page) {

  // inicializace produktovy stranky
  if (page == 'homepage') {
    // spocitani obrazku na strance
    i = 1;
    do {
      id = 'img' + i;
      img = get_element(id);
      if (img != null) {
        i++;
        maximages++;
      }
    }
    while (img != null);
    
    if (maximages > 1) {
      startslideshow(slideshowtimer);
    }
  }

}

function recalc_background() {
  tabulka = get_element('mainframe');
  if (tabulka != null) {
    obj = get_element('pozadi');
    if (obj != null) {
      obj.style.left = tabulka.offsetLeft + 'px';
      obj.style.top = tabulka.offsetTop + 'px';
      obj.style.height = tabulka.offsetHeight + 'px';
   }
  }
}

function pozadi_blik() {
  if (pozadi_actualstep <= pozadi_steps) {
    faze = 0.25 * Math.sin(1.570796 + pozadi_actualstep * pozadi_step);
    opacity = 100 - Math.abs(Math.round(faze * 100));
    setopacity('pozadi',opacity);
  }
  else { // ukoncit akci
    // nastavit opacity na zakladni hodnotu
    setopacity('pozadi',pozadi_opacity);
    // zrusit blindovani
    pozadi_actualstep = 0;
    clearInterval(timer_pozadi);

    // tohle nastavi dalsi opakovani blikani
    // next_interval = pozadi_randomtimer + Math.random() * 6000;
    // timer_pozadi = setInterval("pozadi_delay()",next_interval);

  }
  pozadi_actualstep++;
}

function pozadi_delay() {
  clearInterval(timer_pozadi);
  timer_pozadi = setInterval("pozadi_blik()",pozadi_timer);
}

function pauzni_slideshow() {
  pauza = true;
}

function odpauzni_slideshow() {
  pauza = false;
}
