
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 minFontSize = 10;
var maxFontSize = 20;

function setFontSize(incr){
  var newContentFontSize = contentFontSize + parseInt(incr);
  if(newContentFontSize < maxFontSize && newContentFontSize > minFontSize)
  contentFontSize = newContentFontSize;
  getObj('content').style.fontSize = contentFontSize + 'px';
  
  var d=new Date();
  d.setDate(d.getDate() + 365);
  setCookie('fontSize', contentFontSize, d);
  return false;
}

function resetFontSize(){
  contentFontSize = 12;
  getObj('content').style.fontSize = contentFontSize + 'px';
  
  var d=new Date();
  d.setDate(d.getDate() + 365);
  setCookie('fontSize', contentFontSize, d);
  return false;
}

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 fit_window(){
  pictureWidth = getObj('pic').width;
  pictureHeight = getObj('pic').height;
  window.resizeTo(pictureWidth+60, pictureHeight+96);  
}

function check_payment(){
  f=getObj('order_form');
  if(f.payment.value=="dobírkou"){
    if(f.country.value==12 && f.country.value==48) return true;
    else{
      alert("Platba dobírkou je možná pouze pro české a rakouské zákazníky.");
      return false;
    }
  }
  else return true;  
}

function validateComment(obj){
  if(obj.captcha.value == ''
     || obj.captcha.value == 'opište číslo z obrázku' 
     || obj.cont.value == '' 
     || obj.cont.value == 'text příspěvku' 
     || (obj.email.value != '' && wrong_mail_address(obj.email.value))
     ){
    obj.submit_comment.disabled=true;
  }
  else{
    obj.submit_comment.disabled=false;
  }
}



//-----------------------------------------------------------------------------
// prepinani listu formulare

var sheets = new Array('step-1','step-2','step-3');
var sheetId;

function sheet(id){
  sheetId = 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){}
    }
  }
  validateQuestion($('question_form'))
}

function validateQuestion(obj){

  if(obj.other_institution.checked){
      $('other_inst').style.display = 'block';
  }
  else{
      $('other_inst').style.display = 'none';
  }

  if(obj.urgent.checked){
      $('deadline').style.display = 'block';
  }
  else{
      $('deadline').style.display = 'none';
  }

  // 1. krok
  if(sheetId == 'step-1'){
      if(obj.nickname.value ==  '' ||
         obj.email.values ==  '' ||
         wrong_mail_address(obj.email.value))
      {
          $('next').disabled = true;
          $('next').className = 'btn_dis';
          $('tab-step-2').onclick = function(){return false};
          $('tab-step-2').style.cursor = 'default';
      }
      else{
          $('next').disabled = false;
          $('next').className = 'btn';
          $('tab-step-2').onclick = function(){sheet('step-2'); return false};
          $('tab-step-2').style.cursor = 'pointer';
      }
  }

  if(sheetId == 'step-2'){
      if(obj.inquiry.value == '' ||
         obj.inquiry.value ==  'Text Vašeho dotazu...' ||
         obj.title.value == '' ||
         !obj.agreement.checked)
      {
          obj.save_inquiry.disabled = true;
          obj.save_inquiry.className = 'btn_dis';
      }
      else{
          obj.save_inquiry.disabled = false;
          obj.save_inquiry.className = 'btn';
      }
  }


}
  
function insertSmile(code){
  clearField(getObj('textarea'), 'text příspěvku');
  getObj('textarea').value += " " + code + " ";
}

function clearField(obj, str){
  if(obj.value == str){
      obj.value='';
      obj.className="";
  }
}

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);
}

function switchImg(url){
  getObj('main-pic').src=url;
}

function openImage(file){
  url = '/atelierzidlicky/photo.php?file='+escape(file);
  win = window.open(url, 'image', 'resizable=1,top=20,left=20,menubar=0,scrollbars=1,width=' + 300 + ',height=' + 220 );
  win.focus();
}


// ===================== funkce fora ==============================

function setImproper(entryId){    
    send_xmlhttprequest(improperConfirm, 'GET', '/inc/set_improper.php?entry_id='+entryId);
    getObj('imp'+entryId).style.display='none';
    getObj('status'+entryId).className='improper';
    getObj('status'+entryId).innerHTML='provádím...';
}

function improperConfirm(xmlhttp) {
    if (xmlhttp.readyState == 4) {
        var resp = xmlhttp.responseXML.getElementsByTagName('improper');
        for (var i=0; i < resp.length; i++) {
            getObj('status'+resp[i].firstChild.data).className='improper';
            getObj('status'+resp[i].firstChild.data).innerHTML='označno za nevhodné!';
        }
    }
}

//====================== ovladani FAQ ==============================


function showAllArticles(catId, a, action){
    for(var i in faqTree[catId]){
        if(typeof faqTree[catId][i] == 'object'){
            showAllAnswers(catId, i, $('art-span-'+i), action);
        }
    }
    if(action == 1){
        a.innerHTML = 'skrýt vše';
    }
    else{
        a.innerHTML = 'rozbalit vše';
    }

    a.onclick = function(){
        showAllArticles(catId, a, 1-action);
        return false;
    }
}

function showAllAnswers(catId, artId, a, action){
    for(var i in faqTree[catId][artId]){
        if(typeof faqTree[catId][artId][i] == 'string'){
            showAnswer($('faq-a-'+faqTree[catId][artId][i]), 'answer-'+faqTree[catId][artId][i], action);
        }
    }
    
    if(action == 1){
        a.innerHTML = 'skrýt odpovědi';
        if($('art-a-'+artId).className == 'plus') $('art-a-'+artId).onclick();
        a.className = 'arrow-up';
    }
    else{
        a.innerHTML = 'zobrazit odpovědi';
       // if($('art-a-'+artId).className == 'minus') $('art-a-'+artId).onclick();
        a.className = 'arrow-down';
    }

    a.onclick = function(){
        showAllAnswers(catId, artId, a, 1-action);
        return false;
    }

    return false;
}

function showAnswer(link, answerID, action){
  var answer = getObj(answerID);
  if(action == 0){
    answer.style.display = 'none';
    link.innerHTML = 'zobrazit odpověď';
  }
  else{
    answer.style.display = 'block';
    link.innerHTML = 'skrýt odpověď';
  }
  link.onclick = function(){
    showAnswer(link, answerID, 1-action);
    return false;
  }
  return false;
}

function showAnswerSearch(link, answerID){
  var answer = getObj(answerID);
  if(answer.style.display == 'none'){
    answer.style.display = 'block';
    link.innerHTML = 'skrýt odpověď';
  }
  else{
    answer.style.display = 'none';
    link.innerHTML = 'zobrazit odpověď';
  }
  return false;
}

function initSearch(form){
  getObj('search-field').className = 'searching'; 
  return true;
}


function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}



//========================= sitemap =========================
var siteMap;
var ulArray;

function initList(objID){
  siteMap = getObj(objID);
  var openLinks = getElementsByClassName('open','li',siteMap);
  for(var i=0; i<openLinks.length; i++){
    openLinks[i].firstChild.onclick = function(){openUl(this)};
  }
}

function openUl(li){
  id = li.parentNode.getAttribute('id').substr(3,5);
  ul = getObj('ul-'+id);
  ul.style.display = 'block';
  li.onclick = function(){hideUl(this)};
  li.className = 'close';
}


function hideUl(li){
  id = li.parentNode.getAttribute('id').substr(3,5);
  ul = getObj('ul-'+id);
  ul.style.display = 'none';
  li.onclick = function(){openUl(this)};
  li.className = 'open';
}


function openInq(type, id, link){
  Effect.SlideDown(type+'-'+id, {duration: 0.3});
  link.className = 'minus';
  link.title = 'skrýt obsah kategorie ' + link.innerHTML;
  link.onclick = function(){
      hideInq(type, id, this);
      return false;
  }
}

function hideInq(type, id, link){
  Effect.SlideUp(type+'-'+id, {duration: 0.3});
  link.className = 'plus';
  link.title = 'zobrazit obsah kategorie ' + link.innerHTML;
  link.onclick = function(){
      openInq(type, id, this);
      return false;
  }
}

function showResponse(a, dir){
  inqId = a.getAttribute('id').substr(5, 10);
  if(dir == 1){
      Effect.SlideDown($('inq-'+inqId), {duration: 0.2});
  }
  else{
      Effect.SlideUp($('inq-'+inqId), {duration: 0.2});
  }
  a.onclick = function(){
      showResponse(a, 1-dir);
      return false;
  }
  return false;
}

function openAll(dir){
  var links = $$('.open-link');
  for(var i = 0; i < links.length; i++){
      showResponse(links[i], dir);
  }
  if(dir == 1){
      $('openalllink').innerHTML = 'skrýt znění dotazů';
  }
  else{
      $('openalllink').innerHTML = 'zobrazit znění dotazů';
  }
  $('openalllink').onclick = function(){
      openAll(1-dir);
      return false;
  }
}


function showFaqUrl(a){
    buildModalWindow('URL dotazu', a.href, 400, null, null);
}



function buildModalWindow(head, text, width, height, commands){

    var  modal = Builder.node('div', {id: 'modal'});
    modal.hide();

    var  data = Builder.node('div', {id: 'data'});

    if(width == null)
        width = 350;
    data.style.width = width + 'px';

    if(height != null)
        data.style.height = height + 'px';

    closer = Builder.node('a', {id: 'modalClose'});
    closer.onclick = closeModalWindow;
    data.appendChild(closer);

    header = Builder.node('div', {id: 'modalHeader'});
    header.innerHTML = head;
    data.appendChild(header);


    content = Builder.node('div', {id: 'modalContent'});
    content.innerHTML = text;
    data.appendChild(content);

    modal.appendChild(data);
    document.body.appendChild(modal);
    centerElement(modal, width, height);

    Effect.Appear($(modal), {duration: 0.5, from: 0, to: 1});

    window.onresize = function(){
        centerElement(modal);
    }

    if(commands != null && commands.length) eval(commands);

    return modal;
}

function closeModalWindow(){
    $('modal').remove();
    $('ov').remove();
    try{$('modal-msg').remove();} catch(e){}
}


function getViewportDimensions() {
    var intH = 0, intW = 0;

    if(self.innerHeight) {
       intH = window.innerHeight;
       intW = window.innerWidth;
    }
    else {
        if(document.documentElement && document.documentElement.clientHeight) {
            intH = document.documentElement.clientHeight;
            intW = document.documentElement.clientWidth;
        }
        else {
            if(document.body) {
                intH = document.body.clientHeight;
                intW = document.body.clientWidth;
            }
        }
    }

    return {
        height: parseInt(intH, 10),
        width: parseInt(intW, 10)
    };
}

function centerElement(elem, w, h) {
    if(!w) w = $(elem).getDimensions().width;
    if(!h) h = $(elem).getDimensions().height;

    var viewport = getViewportDimensions();
    var left = (viewport.width == 0) ? 50 : parseInt((viewport.width - w) / 2, 10);
    var top = (viewport.height == 0) ? 50 : parseInt((viewport.height - h) / 2, 10);

    elem.style.left = left + 'px';
    elem.style.top = top + 'px';

    viewport, left, top, elem = null;
}

function goToAnchor(id){
    location.href=location.href + '#faq-' + id;
}


function openBox(btn, section){
    if(btn.checked){
        Effect.SlideDown(section+"_obsah", {duration: 0.3});
    }
    else{
        Effect.SlideUp(section+"_obsah", {duration: 0.3});
    }
}
