
// ovladani menu -----------------

// close



function closeMenu(){
  new Effect.Morph('mm-menu', {
    style: 'width: 13px; height: 13px; color: #fff; left: 15px; top: 15px;', // CSS Properties
    duration: 0.5 // Core Effect properties
  });
  
  new Effect.Fade('mm-menu', {duration: 1});
  new Effect.Appear('mm-btn', {duration: 1});
}

// open

function openMenu(){
  new Effect.Morph('mm-menu', {
    style: 'width: 120px; height: 120px; color: #fff; left: 70px; top: 70px;', // CSS Properties
    duration: 0.5 // Core Effect properties
  });
  
  new Effect.Appear('mm-menu', {duration: 1});
  new Effect.Fade('mm-btn', {duration: 1});
}

function openMenuHome(){
  new Effect.Morph('mm-menu', {
    style: 'width: 170px; height: 90px; color: #fff; left: 70px; top: 70px;', // CSS Properties
    duration: 0.5 // Core Effect properties
  });

  new Effect.Appear('mm-menu', {duration: 1});
  new Effect.Fade('mm-btn', {duration: 1});
  window.setTimeout(function() {
  closeMenu(); }, 5000);
}


// --------------------------

function wrong_mail_address(adresa){
  var re = /^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$/;
  return re.test(adresa) == 0;
}

function getObj(id){
   if(Boolean(document.getElementById))
      return document.getElementById(id);
   else if(Boolean(document.all))
      return eval('document.all.'+id);
   else
      return eval('document.'+id);
}

//-----------------------------------------------------------------------------
// kontrola formulare (inputy oznacene rel="required")


var styleNormal='btn';
var styleDisabled='btn-dis';

function validateForm(form){
  var submit;
  var dis=false;
  var inputs = form.getElementsByTagName('input');
  for(i=0; i < inputs.length; i++) {
    if(inputs[i].type=='submit') submit=inputs[i];
    if(inputs[i].getAttribute('rel')=='required'){
      if(inputs[i].value==''){
        dis=true;
      }
    } 
    else if(inputs[i].getAttribute('rel')=='req-email'){
      if(wrong_mail_address(inputs[i].value)){
        dis=true;
      }
    }
  }
  
  inputs = form.getElementsByTagName('textarea');
  for(i=0; i < inputs.length; i++) {
    if(inputs[i].getAttribute('rel')=='required'){
      if(inputs[i].value==''){
        dis=true;
      }
    }
  }
  
  submit.disabled=dis;
  submit.className=dis?styleDisabled:styleNormal;
}


function clearField(obj, str){
  if(obj.value == str) obj.value='';
}

// -----------------------------------------------------------------------------
// XML http requst 

function send_xmlhttprequest(obsluha, method, url, content, headers) {
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}


//------------------------------------------------------------------------------
// rozkryti clanku kategorie

function showArticle(){
  var anot = $('anotation');
  var full = $('full-article');
  anot.style.display = 'none';
  full.style.display = 'block';
}

//------------------------------------------------------------------------------
// funkce odesle pozadavek na udalosti roku a zobrazi loader

var gYear = 0;

function loadYear(year){
  gYear = year;
  displayYear('');
  //$('year-content').innerHTML = '<div class="loader">&nbsp;</div>';
//  Effect.Fade('year-content', {duration: 0.2});
//  Effect.Fade('submenu', {duration: 0.2});
//  send_xmlhttprequest(yearReceiveData, 'GET', '/inc/get_year_data.php?year='+year);
//  return true;
}

//------------------------------------------------------------------------------
// funkce zobrazi prijata data roku

function yearReceiveData(xmlhttp){
  if(xmlhttp.readyState == 4) {
    data = xmlhttp.responseText;
    setTimeout(function(){displayYear(data);},200);
  }
}

function displayYear(data){
  $('yearHref').innerHTML = gYear;
  $('yearHref').href='/casova-osa.html?year='+gYear;
//  $('year-content').innerHTML = data;
//  Effect.Appear('year-content', {duration: 0.2});
//  Effect.Appear('submenu', {duration: 0.2});
}


// ovladani casove osy ---------------------------------------------------------

var maxStep;
var fieldWidth = 168;
var busy = false;
var loaded = false;
var timer = null;

function timelineUpdate(to, years){
   if(!to)return;
   var i=0;fromField=0;
   while(i < years.length){
     if(years[i] == to) break;
     i++;
   }
   targetField = i;

   targetField-=2;
   if(targetField<0) targetField=0;
   if(targetField>years.length-5) targetField=years.length-5;
   
   fin = targetField*fieldWidth;maxStep=fin/10;
   scroll(0, fin, 0);
//   divObj.contentScroll(fin,0,false);
}


function scroll(from, to, pos){
   busy = true;
   x = (pos-from)/(to-from);
   //alert(x);

   step = -4*((x-1/2)*(x-1/2))+1;
   pos += (from < to ? 1 : -1)*Math.round(step*maxStep+1);
  // alert(pos);
   divObj.contentScroll(pos,0,false);
   
   if((from < to && pos < to) || (from > to && pos > to)){ 
       setTimeout(function(){scroll(from, to, pos);}, 50);
    }
   else{
       busy = false;
       if(!loaded) loadYear(years[to/168+2]);
       loaded = true;
   }
}

function timelineGoForward(){
   if(!busy){
      pos = -1*(parseInt(cDvObj.getPos[0]()));
      fin = (Math.round(pos/fieldWidth)+5)*fieldWidth;
      if(fin > (years.length-5)*fieldWidth)
          fin = (years.length-5)*fieldWidth
      maxStep=(fin-pos)/8;
      loaded = false;
      scroll(pos, fin, pos);
   }
}

function timelineGoBack(){
   if(!busy){   
      pos = -1*(parseInt(cDvObj.getPos[0]()));
      fin = (Math.round(pos/fieldWidth)-5)*fieldWidth;
      if(fin < 0)
          fin = 0;
      maxStep=(pos-fin)/8;
      loaded = false;
      scroll(pos, fin, pos);
      
   }
}

// velikost fontu ---------------------------


function setCookie(name, values, expire){
  var d=new Date();
  d.setDate(d.getDate() - 365);
  document.cookie=name+"="+values+"; expires="+d.toGMTString()+"; path=/;";
  document.cookie=name+"="+values+"; expires="+expire.toGMTString()+"; path=/;";
}

var contentFontSize = 12;

var minFontSize = 10;
var maxFontSize = 20;

function setFontSize(incr){
  var newContentFontSize = contentFontSize + parseInt(incr);
  if(newContentFontSize < maxFontSize && newContentFontSize > minFontSize)
  contentFontSize = newContentFontSize;
  $('content').style.fontSize = contentFontSize + 'px';
  
  var d=new Date();
  d.setDate(d.getDate() + 365);
  setCookie('fontSize', contentFontSize, d);
  return false;
}

function resetFontSize(){
  contentFontSize = 12;
  $('content').style.fontSize = contentFontSize + 'px';
  
  var d=new Date();
  d.setDate(d.getDate() + 365);
  setCookie('fontSize', contentFontSize, d);
  return false;
}

var uid = (
    function(){
        var id=0;
        return function(){
            return id++ ;
        };
    }
    )();

function insertSmile(code){
  clearField(getObj('textarea'), 'text příspěvku');
  getObj('textarea').value += " " + code + " ";
}


function addFileField(){
    //$('file_fields').innerHTML += '<p>&nbsp; <input type="file" name="attachment_' + uid() + '" /></p>';
    var newP = document.createElement('p');
    var newInp = document.createElement('input');
    newInp.setAttribute('type', 'file');
    newInp.setAttribute('name', 'attachment[]');
    newP.innerHTML = '&nbsp;';
    newP.appendChild(newInp);
    $('file_fields').appendChild(newP);
}

function showPasswordChange(){
    $('login_btn').disabled = true;
    Effect.SlideDown($('change_pswd'), {duration: 0.5});
    Effect.Fade($('change_pswd_btn'), {duration: 0});
    Effect.Appear($('only_log_btn'), {duration: 0.5});
}

function hidePasswordChange(){
    Effect.Fade($('only_log_btn'), {duration: 0});
    Effect.SlideUp($('change_pswd'), {duration: 0.5});
    Effect.Appear($('change_pswd_btn'), {duration: 0.5});
    
    $('login_btn').disabled = false;
}

//-----------------------------------------------------------------------------
// prepinani listu formulare

var sheets = new Array('CZ','EN','DE','details');

function sheet(id){
  for(var i=0; i<sheets.length; i++){
    if(sheets[i] == id){
      try{
        getObj(sheets[i]).style.display='block';
        getObj('tab-' + sheets[i]).className='active';
      }catch(e){}
    }
    else{
      try{
        getObj(sheets[i]).style.display='none';
        getObj('tab-' + sheets[i]).className='';
      }catch(e){}
    }
  }
}

//-----------------------------------------------------------------------------
// kontrola formulare (inputy oznacene rel="required")

var styleNormal='btn';
var styleDisabled='btn-dis';

function validateForm(form){
  var submit;
  var dis=false;
  var inputs = form.getElementsByTagName('input');
  for(var i=0; i < inputs.length; i++) {
    if(inputs[i].type=='submit') submit=inputs[i];
    if(inputs[i].getAttribute('rel')=='required'){
      if(inputs[i].value==''){
        dis=true;
      }
    }
  }

  inputs = form.getElementsByTagName('textarea');
  for(var i=0; i < inputs.length; i++) {
    if(inputs[i].getAttribute('rel')=='required'){
      if(inputs[i].value==''){
        dis=true;
      }
    }
  }

  submit.disabled=dis;
  if(dis)
    submit.className=styleDisabled;
  else
    submit.className=styleNormal;
}
