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;

// 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));
}

/* 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 changeheader(trida) {
  obj = get_element('header');
  obj.className = trida;

  obj = get_element('lart');
  obj.className = trida;

}

function changedefault() {
  obj = get_element('lart');
  if (obj != null) {
    obj.className = null;
  }

}

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 == 'product') {
    
    // spocitani produktu
    i = 1;
    do {
      id = 'prod' + i;
      img = get_element(id);
      if (img != null) {
        if (img.className == 'selected') {
          actualproduct = i;
        }
        i++;
      }
    }
    while (img != null);
    count = i-1;
    if ((count > 4) && (actualproduct > 4)) {  // nastaveni offsetu tabulky produktu na aktualni ctverici..
      
      i = actualproduct;
      off = 0;
      do {
        off = off - 352;
        i = i-4;
      }
      while (i > 4);  // v i mam pak pozici, na ktery to je v posledni ctverici
      rozdil = count - actualproduct;
      if ((i < 4) && ((rozdil + i) < 4)) { // zkorigovat offset pokud je to na jiny pozici a nemam takovej pocet
        off = (off + 88 * (4 - (rozdil + i)));
      }

      // nastaveni pozice posunem
      // portfolio_gallery_step = 0;
      // portfolio_gallery_distance = 0;
      // portfolio_gallery_position = 0;
      // timer_portfolio_gallery = setInterval("slide('portfolio_gallery',0,"+off+")",portfolio_gallery_timer);

      // nastaveni pozice primo hned skokem
      tabulka = get_element('portfolio_gallery');
      if (tabulka != null) {
        tabulka.style.left = off + 'px';
      }
    }
  
    // 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);
    }
  }

  // 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);
    }
  }
    
  // inicializace overview stranky
  if (page == 'overview') {

  }
}

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;
}

// ---------------------------------
// posuny tabulky portfolia
// ---------------------------------

function move_left(obj,pocet) {
  if (timer_portfolio_gallery == null) {
    tabulka = get_element(obj);
    if (tabulka != null) {
      newpos = tabulka.offsetLeft + (88)*pocet;
      if (newpos > 0) newpos = 0;
      if (tabulka.offsetLeft != newpos) {
        from = tabulka.offsetLeft;
        portfolio_gallery_step = 0;
        portfolio_gallery_distance = 0;
        portfolio_gallery_position = 0;
        timer_portfolio_gallery = setInterval("slide('"+obj+"',"+from+","+newpos+")",portfolio_gallery_timer);
      }
    }
  }
}

function move_right(obj,pocet) {
  if (timer_portfolio_gallery == null) {
    tabulka = get_element(obj);
    if (tabulka != null) {
      newpos = tabulka.offsetLeft - (88)*pocet;
      maximum = - (tabulka.offsetWidth - 1 - pocet * (88));
      if (newpos <= maximum) newpos = maximum;
      if (tabulka.offsetLeft != newpos) {
        from = tabulka.offsetLeft;
        portfolio_gallery_step = 0;
        portfolio_gallery_distance = 0;
        portfolio_gallery_position = 0;
        timer_portfolio_gallery = setInterval("slide('"+obj+"',"+from+","+newpos+")",portfolio_gallery_timer);
      }
    }
  }
}

function overview_left(obj,pocet) {
  if (timer_portfolio_gallery == null) {
    tabulka = get_element(obj);
    if (tabulka != null) {
      newpos = tabulka.offsetLeft + (162)*pocet;
      if (newpos > 0) newpos = 0;
      if (tabulka.offsetLeft != newpos) {
        from = tabulka.offsetLeft;
        portfolio_gallery_step = 0;
        portfolio_gallery_distance = 0;
        portfolio_gallery_position = 0;
        timer_portfolio_gallery = setInterval("slide('"+obj+"',"+from+","+newpos+")",portfolio_gallery_timer);
      }
    }
  }
}

function overview_right(obj,pocet) {
  if (timer_portfolio_gallery == null) {
    tabulka = get_element(obj);
    if (tabulka != null) {
      newpos = tabulka.offsetLeft - (162)*pocet;
      maximum = - (tabulka.offsetWidth - 1 - pocet * (162));
      if (newpos <= maximum) newpos = maximum;
      if (tabulka.offsetLeft != newpos) {
        from = tabulka.offsetLeft;
        portfolio_gallery_step = 0;
        portfolio_gallery_distance = 0;
        portfolio_gallery_position = 0;
        timer_portfolio_gallery = setInterval("slide('"+obj+"',"+from+","+newpos+")",portfolio_gallery_timer);
      }
    }
  }
}


var pozice = Array(1,2,4,4,5,5,5,6,6,6,6,6,6,6,6,5,5,5,4,4,2,1);
// var pozice = Array(1,2,4,7,12,12,12,12,12,12,7,4,2,1);
// var pozice = Array(1,2,3,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4,3,2,1);

function slide(obj,from,to) {
  tabulka = get_element(obj);
  if (tabulka != null) {
    if (portfolio_gallery_step < portfolio_gallery_steps) {
      portfolio_gallery_distance = (portfolio_gallery_distance == 0) ? to-from : portfolio_gallery_distance;
      portfolio_gallery_position = (portfolio_gallery_position == 0) ? tabulka.offsetLeft : portfolio_gallery_position;
      portfolio_gallery_position = (portfolio_gallery_position + ((pozice[portfolio_gallery_step]/100) * portfolio_gallery_distance));
      newpos = Math.round(portfolio_gallery_position);
      tabulka.style.left = newpos + 'px';
    }
    else {
      tabulka.style.left = to + 'px';
      clearInterval(timer_portfolio_gallery);
      timer_portfolio_gallery = null;
    }
    portfolio_gallery_step++;
  }
  else {
    clearInterval(timer_portfolio_gallery);
    timer_portfolio_gallery = null;
  }
}

// ---------------------------------
// ajax hlasovani
// ---------------------------------

var myAjax = null;

function hlasuj(address,page) {

  if (bIe) {
    try  {
      myAjax = new XDomainRequest();
    }
    catch (e) {
      try {
        myAjax = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {   
        try {       
          myAjax = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {       
          alert("Chyba IE!");
        }
      }
    }
  }
  else {
    try { // Firefox, Opera 8.0+, Safari
      myAjax = new XMLHttpRequest();
    }
    catch (e) { // Internet Explorer   
      alert("Chyba firefoxu!");
    }
  }
 
  // metoda post ..
  myAjax.open("POST", address+'/?event=vote_for_this_page', true);
  myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  myAjax.send("page="+page);

  myAjax.onreadystatechange=function() {
    if (myAjax.readyState == 4) { // stav 4, kdy je poslana odpoved
      if (myAjax.status == 200) {  // http response stav
        
        response = myAjax.responseText;

        if (response == true) {
          // vse je ok.. nastavit jinak tlacitka
          obj = get_element('bookme');
          if (obj != null) {
            obj.style.display = 'block';
          }
          else {
            alert('Díky!');
          }
          obj = get_element('button');
          if (obj != null) {
            obj.style.display = 'none';
          }
          obj = get_element('thankyou');
          if (obj != null) {
            obj.style.display = 'block';
          }
          
        }
        else {
          alert(response);
        }
      }
    }
    else { // ostatni stavy.. postupne to sem pada..
      // alert(myAjax.readyState);
    }
  }
  
  // setTimeout("myAjax.abort();",20000);
}

// ---------------------------------
// bookmarkovani projektu
// ---------------------------------

function bookmark_this_project(type,id,address) {
  thisurl = address + '/?event=bookmark_page';
  window.open(thisurl + '&server='+type+'&page='+id, '_blank');
}
