﻿$(document).ready(function(){
  $('#header_holder .menu a').not('.active a').hover(
    function(){
      $(this).parent().stop().animate({backgroundColor: 'rgb(166, 179, 15)'}, 'fast')
    },
    function(){
      $(this).parent().stop().animate({backgroundColor: 'rgb(98, 77, 45)'}, 'fast')
    }
  );
  
  $(window).resize(function(){
    center_menu_items()
  });
  
  $('.with_shadow').each(function(){
    padding_top = parseInt($(this).css('padding-top'));
    padding_bottom = parseInt($(this).css('padding-bottom'));
    $(this).append(
      $('<div class="bgr_left"></div>'),
      $('<div class="bgr_bottom"></div>').css('height', (padding_bottom - 2) + 'px'),
      $('<div class="bgr_right"></div>'),
      $('<div class="bgr_top"></div>').css('height', (padding_top - 2) + 'px'),
      $('<div class="bgr_cnr_lu"></div>').css('height', (padding_top - 2) + 'px'),
      $('<div class="bgr_cnr_ru"></div>').css('height', (padding_top - 2) + 'px'),
      $('<div class="bgr_cnr_lb"></div>').css('height', (padding_bottom - 2) + 'px'),
      $('<div class="bgr_cnr_rb"></div>').css('height', (padding_bottom - 2) + 'px'),
      $('<div class="bgr_tlu"></div>').css('top', (padding_top - 2) + 'px'),
      $('<div class="bgr_tru"></div>').css('top', (padding_top - 2) + 'px'),
      $('<div class="bgr_trb"></div>').css('bottom', (padding_bottom - 2) + 'px'),
      $('<div class="bgr_tlb"></div>').css('bottom', (padding_bottom - 2) + 'px')
    )
  });
  
  $('.photo_holder').each(function(){
    $(this).append(
      $('<div class="left"></div>'),
      $('<div class="bottom"></div>'),
      $('<div class="right"></div>'),
      $('<div class="top"></div>')
    )
  });
  
  $('#content_right .content_right_bottom a').hover(
    function(){
      $(this).stop().animate({color: '#FCEACE'})
    },
    function(){
      $(this).stop().animate({color: '#FFFFFF'})
    }
  );
  update_clock();
  setInterval(function(){update_clock()}, 1000);
});

function center_menu_items(){

  $('#header_holder .menu a').each(function(){
    $(this).css({left: ($(this).parent().width() - $(this).width())/2 + 'px',top: ($(this).parent().height() - $(this).height())/2 + 'px'});
  });
}

function remove_last_margin(){

   $(".content_right_photos .one_photo:last-child").css('margin-bottom', '0')
}

var count_down_latch;

function init_one_photo(source){

  src = source.replace('prw', 'big');
  src = src.replace('png', 'jpg');
  alt = src.substring(src.lastIndexOf('/') + 1, src.lastIndexOf('.'));
  count_down_latch = 2;
  $('body').append(
    $("<div id='overlay'></div>").css({width: $(window).width() + 'px', height: $(window).height() + 'px'})
                                 .fadeTo('slow',0.7, function(){show_one_photo()}),
    $("<div class='image_holder'>").append(
      $("<img src='" + src + "' alt='" + alt + "' />").load(function(){show_one_photo()}),
      $("<div class='close'></div>")
    )
  );
  $(window).resize(function() {
    $("#overlay").css({width: $(window).width() + 'px', height: $(window).height() + 'px'})
  });
  $('#overlay, .close').click(function(){
    close_one_photo();
  });
}

function show_one_photo(){
  
  if(--count_down_latch != 0){
    return;
  }
  photo = $('.image_holder img');
  photo_width = photo.width();
  photo_height = photo.height();
  photo_ratio = photo_width / photo_height;
  
  if(photo_width < ($(window).width() - 8) && photo_height < ($(window).height() - 8)){
    $('.image_holder').css({left: ($(window).width() - $('.image_holder').width() - 8)/2 + 'px', top: ($(window).height() - $('.image_holder').height() - 8)/2 + 'px'}).animate({opacity: '1'});
  }
  else if($(window).height() * photo_ratio > $(window).width()){
    photo.css({width: ($(window).width() - 8) + 'px', height: (($(window).width() - 8) / photo_ratio) + 'px'});
  }
  else{
    photo.css({width: (($(window).height() - 8) * photo_ratio) + 'px', height: ($(window).height() - 8) + 'px'});
  }
  $('.image_holder').css({left: ($(window).width() - $('.image_holder').width() - 8)/2 + 'px', top: ($(window).height() - $('.image_holder').height() - 8)/2 + 'px'})
                    .animate({opacity: '1'});
  photo.mouseover(function(){
    $('.image_holder .close').stop().animate({opacity: '.7'}, 'fast')
  }).mouseout(function(){
    $('.image_holder .close').stop().animate({opacity: '0'}, 'fast')
  });
  $('.image_holder .close').mouseenter(function(){
    $(this).stop().animate({opacity: '1'}, 'fast')
  }).mouseleave(function(){
    $(this).stop().animate({opacity: '0'}, 'fast')
  })
  $(window).resize(function(){
    $('.image_holder').animate({
      top: ($(window).height() - $('.image_holder').height() - 8)/2,
      left: ($(window).width() - $('.image_holder').width() - 8)/2
    })
  })
}

function close_one_photo(){

  $("#overlay, .image_holder, .image_holder img ").stop().fadeOut('slow', function(){
    $("#overlay, .image_holder").remove();
  });
}

function update_clock(){

  now = new Date();
  date_time = now.getHours() + ':' + get_minutes(now.getMinutes()) + ' - ' + get_day(now.getDay()) + ' ' + now.getDate() + '. ' + get_month(now.getMonth()) + ' ' + now.getFullYear();
  $('.date span').html(date_time);
}

function get_minutes(minutes){
  if (minutes > 9){
    return minutes;
  }
  else{
    return '0' + minutes;
  }
}

function get_day(day){

  switch (day){
    case 1: return 'pondělí';
    case 2: return 'úterý';
    case 3: return 'středa';
    case 4: return 'čtvrtek';
    case 5: return 'pátek';
    case 6: return 'sobota';
    case 0: return 'neděle';
  }
}

function get_month(month){

  switch (month){
    case 0: return 'ledna';
    case 1: return 'února';
    case 2: return 'března';
    case 3: return 'dubna';
    case 4: return 'května';
    case 5: return 'června';
    case 6: return 'července';
    case 7: return 'srpna';
    case 8: return 'září';
    case 9: return 'října';
    case 10: return 'listopadu';
    case 11: return 'prosince';
  }
}
