(function($) {
	$.fn.getAttributes = function() {
		var attributes = {}; 
		if(!this.length)
			return this;
		$.each(this[0].attributes, function(index, attr) {
			attributes[attr.name] = attr.value;
		}); 
		return attributes;
	}
})(jQuery);


$(document).ready(function() {
	$('.link').each(function(){
		var anchor = $(this).html();
		var href = $(this).attr('rel');
		$(this).removeClass('link');
		$(this).removeAttr('rel');
		var attributes = $(this).getAttributes();
		var additionaAttributes = "";
		$.each(attributes, function(indexInArray, valueOfElement) {
			additionaAttributes += indexInArray + '="' + valueOfElement + '" ';
		});
		var that = $(this).replaceWith('<a href="' + href + '" ' + additionaAttributes + '>' + anchor + '</a>');
		
	});
  
  
	$("#loader").hide();
	updateLinks();
	setTimeout(function(){
		var img1 = new Image();
		img1.src = "gallery/zabawa/images/_DSC4961.jpg";
		var img2 = new Image();
		img2.src = "gallery/zabawa/images/_DSC8776_1.jpg";
		var img3 = new Image();
		img3.src = "gallery/zabawa/images/_DSC9019.jpg";
		var img4 = new Image();
		img4.src = "gallery/zabawa/images/_DSC9348.jpg";
		var img5 = new Image();
		img5.src = "gallery/zabawa/images/_DSC9529.jpg";

		
		var img6 = new Image();
		img6.src = "gallery/kosciol/images/_DSC8508.jpg";
		var img7 = new Image();
		img7.src = "gallery/kosciol/images/_DSC5883.jpg";
		var img8 = new Image();
		img8.src = "gallery/kosciol/images/_DSC5888.jpg";
		var img9 = new Image();
		img9.src = "gallery/kosciol/images/_DSC5907.jpg";
		var img10 = new Image();
		img10.src = "gallery/kosciol/images/_DSC8180_1.jpg";

		var img11 = new Image();
		img11.src = "gallery/plener/images/_DSC3999.jpg";
		var img12 = new Image();
		img12.src = "gallery/plener/images/_DSC3943.jpg";
		var img13 = new Image();
		img13.src = "gallery/plener/images/_DSC4055.jpg";
		var img14 = new Image();
		img14.src = "gallery/plener/images/_DSC4082.jpg";

		var img15 = new Image();
		img15.src = "gallery/przygotowania_i_blogoslawienstwo/images/_DSC0008.jpg";
		var img16 = new Image();
		img16.src = "gallery/przygotowania_i_blogoslawienstwo/images/_DSC0018.jpg";
		var img17 = new Image();
		img17.src = "gallery/przygotowania_i_blogoslawienstwo/images/_DSC0020.jpg";
		var img18 = new Image();
		img18.src = "gallery/przygotowania_i_blogoslawienstwo/images/_DSC0037.jpg";
	},100);
});

function updateLinks() {
	$("a.actions").each(function(){
		var addr = $(this).attr("href");
		$(this).attr("href","javascript:void(0)");
		$(this).click(function() {
			var el;
  			$(".menu_selected").each(function() {
				$(this).removeClass('menu_selected');
				el = this;
			});
  			$(this).addClass('menu_selected');
			$("#contentBody").fadeOut(300, function() {
				$("#loader").show();
				$.ajax({ 
				  	type: 'POST',
					contentType: "text/html; charset=UTF-8",
					dataType: "html",
				  	beforeSend:function() {
			  		},
			  		url: addr, 
			  		error: function() {
			  			$("#loader").hide();
			  			$(".menu_selected").each(function() {
							$(this).removeClass('menu_selected');
						});
			  			$(el).addClass('menu_selected');
			  			$("#contentBody").fadeIn(300);
			  		},
			  		success: function(data){
			  			$("#contentBody").html(data);
			  			$("#loader").hide();
						$("#contentBody").fadeIn(300);
						contactFormUpdate();
						galleryUpdate();
						updateLinks();
			  		}
				});
			});
		});
	});	
}
function contactFormUpdate() {
	$("#contactForm").submit(function() { return false;});
		$("#contactForm input[type=submit]").click(function() {
			var addr = $("#contactForm").attr("action");
			$("#contentBody").fadeOut(300, function() {
				$("#loader").show();
				$.ajax({ 
				  	type: 'POST',
					contentType: "text/html; charset=UTF-8",
				  	data: $("#contactForm").serialize(),
				  	beforeSend:function() {
			  		},
			  		url: addr, 
			  		error: function() {
			  			$("#loader").hide();
			  			$("#contentBody").fadeIn(300);
			  		},
			  		success: function(data){
			  			$("#contentBody").html(data);
			  			$("#loader").hide();
						$("#contentBody").fadeIn(300);
						contactFormUpdate();
			  		}
				});
			});
		});
}
function galleryUpdate() {
	$(".galleryByCat a").each(function() {
			var galleryAddr = $(this).attr("href");
			$(this).attr("href","javascript:void(0)");
			$(this).click(function() {
				$("#contentBody").fadeOut(300, function() {
					$("#loader").show();
					$.ajax({ 
					  	type: 'POST',
					  	beforeSend:function() {
				  		},
				  		url: galleryAddr, 
				  		error: function() {
				  			$("#loader").hide();
				  			$("#contentBody").fadeIn(300);
				  		},
				  		success: function(data){
				  			$("#contentBody").html(data);
				  			$("#loader").hide();
							$("#contentBody").fadeIn(300);
							//updateLinks();
				  		}
					 });
				});
			});
		});
}

function getGalleries(addr) {
	$("#contentBody").fadeOut(300, function() {
		$("#loader").show();
		$.ajax({ 
		  	type: 'POST',
		  	beforeSend:function() {
	  		},
	  		url: addr, 
	  		error: function() {
	  			$("#loader").hide();
	  			$("#contentBody").fadeIn(300);
	  		},
	  		success: function(data){
	  			$("#contentBody").html(data);
	  			$("#loader").hide();
				$("#contentBody").fadeIn(300);
				contactFormUpdate();
				galleryUpdate();

	  		}
		});
	});
}

