(function(){
var d = document, e = d.documentElement, s = d.createElement('style');
if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
 document.write("<style type=text/css> body { opacity: 0; } </style>");
 $(window).load(function() {
  $("body").animate({ opacity : "1" },300);
 });
}
})();

/* INDEX */

document.write("<style type=text/css> #flipper { background: #EFF3F7; } </style>");

$(document).ready(function(){
 $("#flipper").html("<img src=layout/images/logo.png alt=\"bakulik kreacje wizualne\" class=logo />");
 $("a.wstepniak-link").hover(function(){
  var strona = "#" + $(this).attr("href").replace("/", "");
  var tresc = $(strona).html();
  $("#flipper").flip({
   direction: "tb",
   speed: 150,
   color: "#EFF3F7",
   content: "<div id=flipped>" + tresc + "</div>"
  });
 },
 function(){});
 $("ul#wstepniak").hover(function(){},
 function(){
  $("#flipper").flip({
   direction: "bt",
   speed: 150,
   color: "#EFF3F7",
   content: "<img src=layout/images/logo.png alt=\"bakulik kreacje wizualne\" class=logo />"
  });
 });
});

/* MENU */

$(document).ready(function(){ $("ul#menu > li > a").css({ "color" : "#000" }); });

$(document).ready(function(){
 $("a.menu-link:not(.current)").hover(function(){
  $(this).animate({ marginTop: "-5px", height: "33px" },300)
 }, function(){
  $(this).animate({ marginTop: "0px", height: "28px" },100).animate({ marginTop: "-2px", height: "30px" },100).animate({ marginTop: "0px", height: "28px" },100)
 });
});

/* SCROLL */

$(document).ready(function(){
 $("#winda").click(function(e){
  e.preventDefault();
  $.scrollTo("#start", 750, function(){
   $("#start > a").focus();
  });
 });
});

/* KOMUNIKAT */

$(document).ready(function(){
 $("#komunikat").dialog({
  resizable: false,
  width: 515,
  minHeight: 100,
  closeText: 'zamknij',
  show: 'scale',
  hide: 'scale',
  position: ['center','center'],
  close: function() {
   $("div#komunikat").remove();
  }
 });
});

/* FORMULARZ */

$(document).ready(function() {
 $("input.line").bind("keypress", function(e) {
  if (e.keyCode == 13) {
   $("input[type=submit]").focus();
  }
 });
}); 

$(document).ready(function(){
 $("#kontaktowy").validate({
  focusInvalid: true,
  messages: {
   adres: {
    email: "Podany adres e-mail jest niepoprawny!"
   }
  },
  errorElement: "span",
  errorPlacement: function(error, element) {
   element.parent("label").find("span.opis").hide();
   error.insertAfter( element.parent("label").find("span.opis") ).before(" ");
  },
  success: function(element) {
   element.parent("label").find("span.opis").show();
  },
  invalidHandler: function() {
   $("div#komunikat").remove();
   $("div").last().after("<div id=komunikat title=Komunikat></div>");
   $("div#komunikat").html("<p>Proszę poprawić podświetlone błędy w formularzu!</p>");
   $("div#komunikat").dialog({
    resizable: false,
    width: 515,
    minHeight: 100,
    closeText: 'zamknij',
    show: 'scale',
    hide: 'scale',
    position: ['center','center'],
    close: function() {
     $("div#komunikat").remove();
    }
   });
  },
  submitHandler: function(form) {
   var validpass = $("input#szyfr").val();
   var inputpass = $("input#word").val();
   var encrtpass = hex_md5(inputpass);
   if (validpass != encrtpass) {
    $("div#komunikat").remove();
    $("input#word").addClass("error");
    $("div").last().after("<div id=komunikat title=Komunikat></div>");
    $("div#komunikat").html("<p>Błędne hasło!</p>");
    $("div#komunikat").dialog({
     resizable: false,
     width: 515,
     minHeight: 100,
     closeText: 'zamknij',
     show: 'scale',
     hide: 'scale',
     position: ['center','center'],
     close: function() {
      $("div#komunikat").remove();
     }
    });
    return false;
   }
   else {
    form.submit();
   }
  }
 });
});

/* $(document).ready(function() {
 $("img.passletter").css({"cursor":"pointer"});
 $("input[type=reset]").before("<input id=reader type=button class=go value=\"Czytaj hasło\" /> ");
 $("input#reader").click(function() {
  $("div#komunikat").remove();
  var czytanka = $("input#czytanka").attr("value");
  $("div").last().after("<div id=komunikat title=Czytaj></div>");
  $("div#komunikat").html("<p>" + czytanka + "</p>");
  $("div#komunikat").dialog({
   resizable: false,
   width: 250,
   minHeight: 100,
   closeText: 'zamknij',
   show: 'scale',
   hide: 'scale',
   position: ['center','center'],
   close: function() {
    $("div#komunikat").remove();
   }
  });
 });
 $("img.passletter").click(function() {
  $("div#komunikat").remove();
  var czytanka = $(this).attr("title");
  $("div").last().after("<div id=komunikat title=Czytaj></div>");
  $("div#komunikat").html("<p>" + czytanka + "</p>");
  $("div#komunikat").dialog({
   resizable: false,
   width: 150,
   minHeight: 100,
   closeText: 'zamknij',
   show: 'scale',
   hide: 'scale',
   position: ['center','center'],
   close: function() {
    $("div#komunikat").remove();
   }
  });
 });
}); */

/* PORTFOLIO */

$(document).ready(function() {
/* if ((window.location.href.indexOf("#")>0) && (window.location.href.split("#,2").pop().length > 0)) {
  var podstrona = window.location.href.split("#").pop();
  var takemeto =  "content/" + window.location.href.split("/",4).pop() + "_" + podstrona + ".php";
  $("#kontener").hide().addClass("loading").html("").fadeIn(100);
  $.get(takemeto, function(data){
   $("#kontener").fadeOut(100, function(){
    $("#kontener").removeClass("loading").html(data).slideDown(300);
    $("a.submenu-link[href*=" + podstrona + "]").addClass("current");
   });
  });
 }
 else */ if ($("a.submenu-link:first").attr("href")) {
  var starter = "content/" + $("a.submenu-link:first").attr("href").replace("/", "_").replace("/", ".php");
  $("#kontener").hide().addClass("loading").html("").fadeIn(100);
  $.get(starter, function(data){
   $("#kontener").fadeOut(100, function(){
    $("#kontener").removeClass("loading").html(data).slideDown(300);
    $("a.submenu-link:first").addClass("current");
   });
  });
 }
 $("a.submenu-link").click(function(e){
  $(this).blur();
  var actlink = $(this);
  var toload = "content/" + $(this).attr("href").replace("/", "_").replace("/", ".php");
  if ((window.location.href.indexOf("/oferta")<0) && (window.location.href.indexOf("/portfolio")<0)) {
   e.preventDefault();
  }
  $("#kontener").slideUp(300, function(){
   previous = $("#kontener").html();
   $("#kontener").addClass("loading").html("").fadeIn(100);
   $.get(toload, function(data){
    $("#kontener").fadeOut(100, function(){
     $("#kontener").removeClass("loading").html(data).slideDown(300);
	 $("a.submenu-link.current").removeClass("current");
	 actlink.addClass("current");
/*	 window.location = window.location.href.split("#",1) + "#" + actlink.attr("href").split("/",2).pop(); */
    });
   });
  });
 });
 $(".sampel > img").livequery(function(){
  $(this).css({"cursor":"pointer"});
 });
 $(".sampel > img").livequery(function(){
  $(this).hover(function(){
   $(this).stop().animate({ opacity: "0.75" },300)
  }, function(){
   $(this).stop().animate({ opacity: "1" },300)
  });
 });
 $("#galeria > a > img").livequery(function(){
  $(this).hover(function(){
   $(this).stop().animate({ opacity: "0.75" },300)
  }, function(){
   $(this).stop().animate({ opacity: "1" },100)
  });
 });
 $(".sampel > img").livequery("click", function(){
  var obrazek = $(this).attr("src").replace(".png", ".jpg");
  var tytul = $(this).attr("alt");
  $("div#zblizenie").remove();
  $("div#zawartosc").after("<div id=zblizenie title=\"" + tytul + "\"></div>");
  $("div#zblizenie").html("<img src=" + obrazek + " alt=\"\" class=powiekszenie />");
  $("div#zblizenie").dialog({
   resizable: false,
   width: 550,
   minHeight: 400,
   closeText: 'zamknij',
   show: 'scale',
   hide: 'scale',
   position: ['center','center'],
   close: function() {
    $("div#zblizenie").remove();
   }
  });
 });
 $("a.sublink").livequery("click", function(e){
  if ((window.location.href.indexOf("/starsze")<0) && (window.location.href.indexOf("/portfolio")<0)) {
   e.preventDefault();
  }
  var path = $(this)[0].href;
  var pieces = path.split("/");
  var toload = "content/" + pieces[pieces.length-3] + "_" + pieces[pieces.length-2] + ".php";
  $("#kontener").slideUp(300, function(){
   previous = $("#kontener").html();
   $("#kontener").addClass("loading").html("").fadeIn(100);
   $.get(toload, function(data){
    $("#kontener").fadeOut(100, function(){
     $("#kontener").removeClass("loading").html(data).slideDown(300);
/*	 window.location = window.location.href.split("#",1) + "#" + pieces[pieces.length-2]; */
    });
   });
  });
 });
 $("#kontener").ajaxError(function(event, request, settings){
  $("div").last().after("<div id=komunikat title=Błąd></div>");
  $("div#komunikat").html("<p>Przepraszamy, wystąpił błąd wczytywania strony!<br />Postaramy się usunąć problem w niedługim czasie.</p>");
  $("div#komunikat").dialog({
   resizable: false,
   width: 515,
   minHeight: 100,
   closeText: 'zamknij',
   show: 'scale',
   hide: 'scale',
   position: ['center','center'],
   close: function() {
    $("div#komunikat").remove();
    $("#kontener").fadeOut(100, function(){
     $("#kontener").removeClass("loading").html(previous).fadeIn(300)
    });
   }
  });
 });
});