$(function()
{
  /**
   * Centrowanie logo "dp".
   */
  $(window).resize(function()
  {
    var top  = Math.round(($(window).height() - 127) / 2);
    var left = Math.round(($(window).width() - 107) / 2);
    $('figure#dp').css({'top': top + 'px', 'left': left + 'px'});
  });
  
  $(window).resize();
  
  /**
   * Ustalenie minimalnej wysokości diva z treścią sekcji.
   */
  (function()
  {
    var height = $(window).height() - 147;
    $('div#content').css({'min-height': height + 'px'});
  })();

  /**
   * Funkcja odpowiadająca za odsłonięcie menu.
   */
  var showTopMenu = function()
  {
    $('section').slideDown('slow', function()
    {
      $('figure#dp').fadeOut();
      $('div#hint').show();
    });
    $('footer').fadeIn();
    
    var delay = 0;
    
    $('menu#top > li > a').each(function()
    {
      delay = delay + 100;
      $(this).delay(delay).animate({'top': 0});
    });
  };
  
  /**
   * Ręczne odsłonięcie menu (fizyczne kliknięcie).
   */
  $('figure#dp').one('click', function()
  {
    showTopMenu();
    showSlide($('menu#top > li > a[href="#/me"]'));
  });
  
  /**
   * Funkcja odpowiadająca za wyświetlenie sekcji.
   */
  var showSlide = function(slide)
  {
    $('div#content').fadeOut();
    var newColor = $(slide).css('color');
    $('section').animate({borderTopColor: newColor, borderBottomColor: newColor}, 1000);
    
    window.setTimeout(function()
    {
      /**
       * Sekunda oczekiwania na zakończenie fadeOut().
       */
      $.get($(slide).attr('href').substr(2) + '.html?cache=' + Math.random(), function(data)
      {
        data = data.replace(/\{\$color\}/g, newColor);
        data = data.replace(/ (w|i|o|a|z|&mdash;) /gi, ' $1&nbsp;');
        $('div#content').html(data);
        $('section > div').fadeIn();
      });
    }, 1000);
  };
  
  /**
   * Efekt "uniesienia" elementu menu o jeden piksel.
   */
  $('menu#top > li > a').hover(function()
  {
    $(this).css('top', '-1px');
  }, function()
  {
    $(this).css('top', 0);
  });
  
  /**
   * Ręczny wybór sekcji z menu (fizyczne kliknięcie).
   */
  $('menu#top > li > a').click(function()
  {
    $(this).blur(); // Zdejmuje focus z elementu menu (Firefox).
    
    if ($('div#content:visible').length)
    {
      /**
       * Jeśli sekcja nie jest właśnie zmieniana...
       */
      showSlide(this);
    }
  });

  /**
   * Automatyczny wybór sekcji (wejście przez link z wybraną sekcją).
   */
  (function()
  {
    if (document.location.hash.length > 1)
    {
      var slide = null;
      
      switch (document.location.hash.substr(2))
      {
        case 'me'       : slide = 'me';  break;
        case 'portfolio': slide = 'portfolio'; break;
        case 'cv'       : slide = 'cv';        break;
        case 'social'   : slide = 'social';    break;
        case 'contact'  : slide = 'contact';
      }
      
      if (slide != null)
      {
        showTopMenu();
        showSlide($('menu#top > li > a[href="#/' + slide + '"]'));
      }
      else
      {
        /**
         * Usunięcie z paska adresu nazwy nieistniejącej sekcji.
         */
        document.location.hash = '';
      }
    }
  })();
  
  /**
   * Przewijanie sekcji przez najechanie myszą na boczny obszar.
   */
  (function()
  {
    $('section').append('<div id="scroll-up" class="scroll"></div>')
                .append('<div id="scroll-down" class="scroll"></div>');
    var height = Math.round(($(window).height() - 132) * 0.4);
    $('div#scroll-up, div#scroll-down').css('height', height + 'px');
    
    var interval = null;
    
    /**
     * W górę.
     */
    $('div#scroll-up').hover(function()
    {
      interval = window.setTimeout(function()
      {
        interval = window.setInterval(function()
        {
          $('section').scrollTop($('section').scrollTop() - 5);
        }, 50)
      }, 500);
    }, function()
    {
      clearInterval(interval);
    });
    
    /**
     * W dół.
     */
    $('div#scroll-down').hover(function()
    {
      interval = window.setTimeout(function()
      {
        interval = window.setInterval(function()
        {
          $('section').scrollTop($('section').scrollTop() + 5);
        }, 50)
      }, 500);
    }, function()
    {
      clearInterval(interval);
    });
  })();
  
  /**
   * Przewijanie sekcji przez obrót kółkiem myszy.
   */
  $('div#content').mousewheel(function(e, delta)
  {
    if (delta > 0)
    {
      /**
       * W górę.
       */
      $('section').scrollTop($('section').scrollTop() - 50);
    }
    else
    {
      /**
       * W dół.
       */
      $('section').scrollTop($('section').scrollTop() + 50);
    }
  });
  
  $('section').append('<div id="shadow-top" class="shadow"></div>')
              .append('<div id="shadow-bottom" class="shadow"></div>');
  
  /**
   * Podpowiedzi przewijania.
   */
  // Przewijanie strony kółkiem myszy.
  
  (function()
  {
    var scrollHintTimeout;
    
    $('div#content').hover(function()
    {
      if ($(this).height() > $('section').height())
      {
        scrollHintTimeout = window.setTimeout(function()
        {
          $('div#hint').text('Możesz przewinąć tę stronę kółkiem myszy')
                       .animate({bottom: 5}, 250);
        }, 500);
      }
    }, function()
    {
      window.clearTimeout(scrollHintTimeout);
      $('div#hint').animate({bottom: 51}, 250);
    });
  })();
  
  // Przewijanie strony przez najechanie na boczny obszar.
  
  (function()
  {
    var sideHintTimeout;
    
    $('section > div.scroll').hover(function()
    {
      if ($('div#content').height() > $('section').height())
      {
        var dir = $(this).attr('id') == 'scroll-up' ? 'górę' : 'dół';
        sideHintTimeout = window.setTimeout(function()
        {
          $('div#hint').text('Zatrzymaj mysz, aby przewinąć stronę w ' + dir)
                       .animate({bottom: 5}, 250);
        }, 500);
        $(this).css('cursor', 'n-resize');
      }
    }, function()
    {
      window.clearTimeout(sideHintTimeout);
      $('div#hint').animate({bottom: 51}, 250);
      $(this).css('cursor', 'auto');
    });
  })();
  
  /**
   * CSS fix dla Opery/Linux
   */
  if (/Opera.*Linux/.test(navigator.userAgent))
  {
    $('div#content').css('width', '550px');
  }
  
  /**
   * Fancybox CSS fixes
   */
  $('a#fancybox-close').removeAttr('style'); // Close button fix
});

