$(function() {
  Cufon.now();
  
  var lang = {
    up: 'nahoru',
    down: 'dolů',
    previous: 'předchozí',
    next: 'další',
    close: 'zavřít',
    older: 'starší',
    newer: 'novější',
    pause: 'pause',
    play: 'play'
  };

  // external links
  $('a[rel=external]').attr('target', '_blank');

  // colorbox
  $('.colorbox').colorbox({
    transition: 'fade',
    opacity: '0.85',
		current: '{current}/{total}',
		previous: lang.previous,
		next: lang.next,
		close: lang.close,
		loop: false
  });

  // news slider
  var $newsInner = $('#news .container ul'),
      newsInnerHeight = $newsInner.height(),
      newsOuterHeight = $('#news .container').css('overflow', 'hidden').height(),
      top = 0;

  var $newsTriggerNext = $('<a class="newsNext" href="#next">' + lang.older + '</a>').hide()
    .appendTo('#news')
    .click(function() {
      slideNews('down');
      return false;
    });

  var $newsTriggerPrev = $('<a class="newsPrev" href="#prev">' + lang.newer + '</a>').hide()
    .appendTo('#news')
    .click(function() {
      slideNews('up');
      return false;
    });
          
  function slideNews(dir) {
    if (dir != false) {
      top = dir == 'up' ? (top + newsOuterHeight) : (top - newsOuterHeight);
      $newsInner.animate({top: top + 'px'}, 'slow');
    }
      
    // show older news button
    if (Math.abs(top) + newsOuterHeight < newsInnerHeight) {
      $newsTriggerNext.show();
    }
    else {
      $newsTriggerNext.hide();
    }

    // show newer news button
    if (top < 0) {
      $newsTriggerPrev.show();
    }
    else {
      $newsTriggerPrev.hide();
    }

    return false;
  }
  slideNews(false);
          
  // slideshow
  var $slideshow = $('#slideshow');
  
  if ($slideshow.length) {
    $('ul', $slideshow).cycle({
      fx:       'fade', 
      speed:    800, 
      timeout:  8000, 
      next:     '#slideshow .next', 
      prev:     '#slideshow .prev',
      before:   function() {
                  $('#slideshow .current').html($(this).parent().children().index(this) + 1);
                  Cufon.refresh('#slideshow .count span');
                }
    });
    
    $('.pause', $slideshow).toggle(function () {
      $('ul', $slideshow).cycle('pause');
      $(this).html(lang.play);
    }, function() {
      $('ul', $slideshow).cycle('resume');
      $(this).html(lang.pause);
    });
  }
});

// hover on image icons
$('.hover').hover(function() {
    var id = $(this).attr('src').replace('.', '_hover.');
    $(this).attr('src', id);
}, function() {
    var id = $(this).attr('src').replace('_hover.', '.');
    $(this).attr('src', id);
});

// basic form validation
$('#i_send').click(function(e) {
  var proceed = true;
  var required = $('.required', $(this).closest('form'));
  $(required).each(function() {
    var val = $(this).val();
    if (val == '' || val == $(this).attr('title') || val == 'povinné pole') {
      $(this).css({
        color: 'rgb(255,0,42)',
        borderColor: 'rgb(255,0,42)'
      });
      //$(this).val('povinné pole');
      proceed = false;
    }
  });
  if (!proceed) {alert('Vyplňte prosím všechna povinná pole');}
  return proceed;
});

$('form input').focus(function() {
  if ($(this).attr('type') != 'submit') {
    clearInput($(this));
  }
});

$('form textarea').focus(function() {
  clearInput($(this));
});

function clearInput(o) {
  $(o).css({
    color: 'rgb(76,76,76)',
    borderColor: 'rgb(78,78,78)'
  });
  if ($(o).val() == $(o).attr('title') || $(o).val() == 'povinné pole') {$(o).val('');}
} 

$('form .required').blur(function() {
  var val = $(this).val();
  var id = $(this).attr('name');
  if (val == '' || val == $(this).attr('title') || val == 'povinné pole') {
    //if ($('h4[rev="'+id+'"]').length == 0) {
    //  $(this).after('<h4 class="err" rev="'+id+'">^ Vyplňte prosím pole <em>'+id+'</em></h4>');
    //}
    //else {$('h4[rev="'+id+'"]').fadeIn();}
    $(this).css({
      color: 'rgb(255,0,42)',
      borderColor: 'rgb(255,0,42)'
    });
    //$(this).val('povinné pole');
  }
  else {
    $(this).css({
      color: 'rgb(76,76,76)',
      borderColor: 'rgb(78,78,78)'
    });
  }
});

