$(function(){
  // Lazy loading images
  $('.few img').lazyload({
    placeholder: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC',
    effect: 'fadeIn'
  });
  
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
  
  // Fixed-floating sidebar
  if (!msie6) {
    var top = $('aside').offset().top - parseFloat($('aside').css('margin-top').replace(/auto/, 0));
    $(window).scroll(function (event) {
      var y = $(this).scrollTop();
      if (y >= top) {
        $('aside').addClass('fixed');
      } else {
        $('aside').removeClass('fixed');
      }
    });
  }
  
  slider = function() {
    $('div.slider').nivoSlider({
      pauseTime: 4000
    });
  };
  
  // Order form display
  orderShow = function() {
    var salute = $('p.order a').html();
    $('p.order a').toggle(function() {
      $('.one').animate({ left:'-370px' }, 1000);
      $('.one figure').animate({ marginRight:'30px' }, 700);
      $(this).html('&laquo; zdjęcia');
      $('p.order').css({'background-color':'#4A4A4A'});
    },function() {
      $('.one').animate({ left:'0px' }, 1000);
      $('.one figure').animate({ marginRight:'10px' }, 700);
      $(this).html(salute);
      $('p.order').css({ 'background-color':'#7D0000' });
    });
  };
  
  // Order form display instantly
  orderShowNow = function() {
    $('p.order a').trigger("click");
  };
  
  // Popups
  $('a.gallery').attr('rel', 'gallery')
  .attr('href', function() {
    return this.href + 'popup/'
  })
  .fancybox({
    'autoDimensions': false,
    'autoScale': false,
    'height': 330,
    'opacity': true,
    'width': 680,
    onComplete: function() {
      slider();
    }
  });
  
  // Sliding labels
  $('.labby label').each(function() {
    var restingPosition = '7px';
    
    $(this).css({
      'position': 'absolute',
      'top': '5px',
      'left': restingPosition,
      'display': 'inline',
      'z-index': '99'
    });
    
    var inputval = $(this).next('.slabby').val();
    
    var labelwidth = $(this).width();
    var labelmove = labelwidth + 5;
    
    if(inputval !== ''){
      $(this).stop().animate({ 'left':'-'+labelmove }, 1);
    }
    
    $('.slabby').focus(function(){
      var label = $(this).prev('label');;
      var width = $(label).width();
      var adjust = width + 5;
      var value = $(this).val();
      
      if(value == ''){
        label.stop().animate({ 'left':'-'+adjust }, 'fast');
      } else {
        label.css({ 'left':'-'+adjust });
      }
    }).blur(function(){
      var label = $(this).prev('label');
      var value = $(this).val();
      
      if(value == ''){
        label.stop().animate({ 'left':restingPosition }, 'fast');
      }
    })
  });
  
  // Forms validation
  $("form").validate({
    rules: {
      imie: {
        required: true,
        minlength: 2
      },
      email: {
        required: true,
        email: true
      },
      telefon: {
        required: true,
        minlength: 9
      },
      uwagi: {
        minlength: 3
      },
      wiadomosc: {
        minlength: 3
      }
    },
    messages: {
      imie: {
        required: "Podaj imię i nazwisko.",
        minlength: "Wpisz co najmniej 2 znaki."
      },
      email: "Wpisz poprawny adres e-mail.",
      telefon: {
        required: "Podaj numer telefonu.",
        number: true,
        minlength: "Wpisz co najmniej 9 cyfr."
      },
      uwagi: {
        minlength: "Wpisz co najmniej 5 znaków."
      },
      wiadomosc: {
        minlength: "Wpisz co najmniej 5 znaków."
      }
    }
  });
  
  // Gallery pagination
  $('#gallery #main').sweetPages({perPage:20});
  
  $('.swShowPage').click(function() {
    $('html,body').animate({scrollTop: $('#main').offset().top},'slow');
  });
  
  var controls = $('.swControls').detach();
  controls.appendTo('#gallery #main');
});
