﻿$(function(){
  $('#search input,#search2 input').focus(function(){
    if(this.value==this.title){
      $(this).removeClass('hint');
      this.value='';
    }else{
      this.select()
    }
  }).blur(function(){
    if(this.value=='' || this.value==this.title){
      $(this).addClass('hint');
      this.value=this.title;
    }
  }).trigger('blur');


//menu
  var menuTimer;
  $('.menu').mouseover(function(){
    if(! $(this).is('.menu-open')){
      $(this).parents('.komentar').css('z-index','100');
      $(this).addClass('menu-open').find('ul').stop().css('height','auto').slideDown(200).css('display','block');
    }
  });

  
  $('.menu,.menu ul').mouseout(function(){
    clearTimeout(menuTimer)
    var el=$(this);
    var close=function(){menuClose(el)};
    menuTimer=setTimeout(close,400);
  }).mouseover(function(){
    clearTimeout(menuTimer)
  });

  function menuClose(el){
    el.find('ul').slideUp(100,function(){el.removeClass('menu-open')})
    el.parents('.komentar').css('z-index','1');
  }
//menu-end

//komentar
  
  $('.komentar-novy textarea,.poznamka-nova textarea').focus(function(){
    if(this.value==this.title){
      this.value='';
      $(this).animate({height:'10em'},200)
      $(this).parents('form').find('.hidden').slideDown(200);
    }
  }).filter(':empty').each(function(){
    this.value=this.title;
  });

  $('.komentar-cancel,.poznamka-cancel').click(function(){
    var textarea=$(this).parents('form').find('textarea');

    if(textarea.val()=='' || (textarea.val()!='' && confirm('Napsaný text bude ztracen. Chcete pokračovat?'))){
      textarea.animate({height:'1.5em'},200)
      $(this).parents('form').find('.hidden').slideUp(200);
         
      textarea.get(0).value=textarea.get(0).title;
    }
    return false;
  });

  if($('input[name=Rate]').length>0){
    $('.hodnoceni-nove').each(function(i,el){

      var el=$(el);
      var input = el.find('input[name=Rate]');
        
      el.find('.hodnoceni-vybrane').html(el.find('ul li').eq(input.val()).html());

      el.find('ul li').each(function(n,li){
        $(li).click(function(){
          el.find('.hodnoceni-vybrane').html($(this).html());
          input.val(n);
          menuClose($(this).parents('.menu'));
          })
      });

    });


  }
  
  if($.scrollTo && location.hash=="#novy-komentar"){
    $.scrollTo( '.komentar-novy',400,{offset:-50,onAfter:function(){
      $('.komentar-novy textarea').eq(0).focus();
    }});
  }

// komentar-end

//fancybox

$('a[rel=fancybox]').removeAttr('rel').fancybox({
  padding:0,
  speedIn:	300,
  transitionIn:'elastic',
  showNavArrows: false,
  hideOnOverlayClick:false,
  ajax : {cache: false}
});

//fancybox - end


// kontakty
$('#contacts-add').click(contactAdd);
$('.contact .remove').click(contactRemove);
//kontakty - end



});


// kontakty - globalni fce
function contactRemove(){
  if(confirm('Opravdu smazat kontakt?')){
    if ($('.contact').length==1){
      contactAdd(true);
    }
    $(this).parents('.contact').remove();      

    $.fancybox.resize();
    }
  return false;
}


function contactAdd(hidden){
   if($('.contact:hidden').length==1){
      $('.contact:hidden').show();
      $.fancybox.resize();
      return false;
   }

   var newline=$('.contact').eq(0).clone();
   newline.find('input').val('');
   newline.find('.remove').click(contactRemove);

   $('.contact:last').after(newline);
   if(hidden==true)newline.hide();

   $.fancybox.resize();
   return false;
}



