var scrollElement = null;

window.onload = function(){
  //upozorneni na stary IE
  if(GETBrowser.ie6){
		alert('Používáte prohlížeč Internet Explorer verze 6 nebo starší. ' + 
		      'Pro správné zobrazení tohoto webu budete potřebovat novější verzi prohlížeče.');
	}
	
	//odskrolovani na element
  if(scrollElement){
    wd_skroluj(null, scrollElement);
  }
  
	//antispam
	if(GETElement('protispamu')){
	  GETElement('protispamu').value = 'protispamu';
	  GETElement('antispam').style.display = 'none';
	}
}

var aktualniPozice;
var novaPozice;
var animace;
function posun(smer){
  aktualniPozice = parseInt(GETElement('posuvnikAktualit').style.marginLeft);
  if(!aktualniPozice){
    aktualniPozice = 0;
  }
  
  if(smer == 'vlevo'){
    novaPozice = aktualniPozice - 250;
    animace = setInterval('animujDoleva()', 10);
  }
  else{
    novaPozice = aktualniPozice + 250;
    animace = setInterval('animujDoprava()', 10);
  }
  if(novaPozice <= hranice){
    GETElement('tlacitkoVlevo').onclick = null;
    GETElement('tlacitkoVpravo').onclick = function(){
      posun('vpravo');  
    };
  }
  else if(novaPozice >= 0){
    GETElement('tlacitkoVlevo').onclick = function(){
      posun('vlevo');  
    };
    GETElement('tlacitkoVpravo').onclick = null;
  }
  else{
    GETElement('tlacitkoVlevo').onclick = function(){
      posun('vlevo');  
    };
    GETElement('tlacitkoVpravo').onclick = function(){
      posun('vpravo');  
    };
  }
  
}

function animujDoleva(){
  aktualniPozice -= 25;
  
  if(aktualniPozice <= novaPozice || novaPozice < hranice){
    clearInterval(animace);
    if(novaPozice < hranice){
      novaPozice = hranice;
    }
    GETElement('posuvnikAktualit').style.marginLeft = novaPozice + 'px';
    return;
  }
  GETElement('posuvnikAktualit').style.marginLeft = aktualniPozice + 'px';
}

function animujDoprava(){
  aktualniPozice += 15;
  if(aktualniPozice >= novaPozice || novaPozice > 0){
    clearInterval(animace);
    if(novaPozice > 0){
      novaPozice = 0;
    }
    GETElement('posuvnikAktualit').style.marginLeft = novaPozice + 'px';
    return;
  }
  GETElement('posuvnikAktualit').style.marginLeft = aktualniPozice + 'px';
}

var cisloVozidla = 1;
var cisloPosadky = 1;
function pridejRadek(typ){
  var tabulka = GETElement(typ);
  var radek = document.createElement('tr');
  tabulka.appendChild(radek);
  switch(typ){
    case 'posadka':
      var cisloBunka = document.createElement('td');
      var inputBunka = document.createElement('td');
      var zavritBunka = document.createElement('td');
      var input = document.createElement('input');
      var zavrit = document.createElement('a');
      radek.appendChild(cisloBunka);
      radek.appendChild(inputBunka);
      radek.appendChild(zavritBunka);
      inputBunka.appendChild(input);
      zavritBunka.appendChild(zavrit);

      cisloBunka.innerHTML = ++cisloPosadky;
      input.setAttribute('name', 'posadka' + cisloPosadky);
      input.setAttribute('id', 'posadka' + cisloPosadky);
      zavrit.innerHTML = '×';
      zavrit.style.fontWeight = 'bold';
      zavrit.style.fontSize = '1.5em';
      zavrit.onclick = function (){
        tabulka.removeChild(radek);
      };
      input.focus();
      break;
      
    case 'vozidlo':
      var typBunka = document.createElement('td');
      var rokBunka = document.createElement('td');
      var zemeBunka = document.createElement('td');
      var spzBunka = document.createElement('td');
      var zavritBunka = document.createElement('td');
      var typ = document.createElement('input');
      var rok = document.createElement('input');
      var zeme = document.createElement('input');
      var spz = document.createElement('input');
      var zavrit = document.createElement('a');
      radek.appendChild(typBunka);
      radek.appendChild(rokBunka);
      radek.appendChild(zemeBunka);
      radek.appendChild(spzBunka);
      radek.appendChild(zavritBunka);
      typBunka.appendChild(typ);
      rokBunka.appendChild(rok);
      zemeBunka.appendChild(zeme);
      spzBunka.appendChild(spz);
      zavritBunka.appendChild(zavrit);

      typ.setAttribute('name', 'typ' + (++cisloVozidla));
      typ.setAttribute('id', 'typ' + cisloVozidla);
      rok.setAttribute('name', 'rok' + cisloVozidla);
      rok.setAttribute('id', 'rok' + cisloVozidla);
      zeme.setAttribute('name', 'zeme' + cisloVozidla);
      zeme.setAttribute('id', 'zeme' + cisloVozidla);
      spz.setAttribute('name', 'spz' + cisloVozidla);
      spz.setAttribute('id', 'spz' + cisloVozidla);
      zavrit.innerHTML = '×';
      zavrit.style.fontWeight = 'bold';
      zavrit.style.fontSize = '1.5em';
      zavrit.onclick = function (){
        tabulka.removeChild(radek);
      };
      typ.focus();
      break;
  }
}

function vyberBezVozidla(){
  if(GETElement('bezVozidla').checked){
    GETElement('vyberVozidla').style.display = 'none';
  }
  else{
    GETElement('vyberVozidla').style.display = 'block';
  }
}

// ===========================
//           UTILITY
// ===========================

//externi odkaz
function wd_odkaz(el){
  return !window.open(el.href);
}

//scroll na strance
function wd_skroluj(el, id){
  if(!el){
    var el = GETElement(id);
  }
  var top = GETElementTop(el);
  window.scrollTo(0, top);
  return true;
}

//vraci absolutni pozici elementu na strance
function GETElementTop(el, id){
  if(!el){
    var el = GETElement(id);
  }
  var top = -1;
  if(el.offsetParent){
    top = 0;
    while(el){
      top += el.offsetTop;
      el = el.offsetParent;
    }
  }
  else if(el.y){
    top += el.y;
  }
  return top;
}

// ====================================
//           GOOGLE ANALYTICS
// ====================================
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'TODO']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

