// loaded with site

$(document).ready(function()
{
	$('#galeria1 > a').lightBox();
	$('#galeria1 > a > img').css("border", "0");
	
	// inicjalizacja komentarzy
	
	function go_init()
	{
		$('#go').click(function()
		{
			if($('#nick').val() && $('#comment').val())
			{
				$.ajax(
				{
					url: 'php/comment.php',
					type: 'POST',
					data: 
					{
						nick: $('#nick').val(),
						tresc: $('#comment').val(),
						strona: $('#addComment').attr('numer_strony')
					},
					success: function(comments)
					{
						$('#comments').html(comments);
					},
					beforeSend: function()
					{
						$('#comments').hide();
						$('#commentsLoader').show();
					},
					complete: function()
					{
						$('#comments').show();
						$('#commentsLoader').hide();
						
						refresh();
					}
				})
				
				$('#nick').val('')
				$('#comment').val('') 
			}
			else
			{
				alert('Podaj nicka i treść komentarza!')
			}
		});
	}
	
	function refresh()
	{
		$.ajax(
		{
			url: 'php/comment.php',
			type: 'POST',
			data: 
			{
				'strona': $('#addComment').attr('numer_strony')
			},
			success: function(comments)
			{
				$('#comments').html(comments);
			},
			beforeSend: function()
			{
				$('#comments').hide();
				$('#commentsLoader').show();
			},
			complete: function()
			{
				$('#comments').show();
				$('#commentsLoader').hide();
			}
		})
	}
	
	// wczytanie newsow
	
	function news()
	{
		$.ajax(
		{
			url: 'php/news_archiwum.php',
			success: function(news)
			{
				$('#news').html(news);
			},
			beforeSend: function()
			{
				$('#news').hide();
				$('#newsLoader').show();
			},
			complete: function()
			{
				$('#news').show();
				$('#newsLoader').hide();
				
				readmore_init()
			}
		})
	}
	
	function readmore_init()
	{
		$('.readmore').click(function()
		{
			var id = $(this).attr('news');
			
			$.ajax(
			{
				url: 'php/news.php',
				type: 'POST',
				data:
				{
					id: id
				},
				success: function(n)
				{
					$('#news').html(n);
				},
				beforeSend: function()
				{
					$('#news').hide();
					$('#newsLoader').show();
				},
				complete: function()
				{
					$('#news').show();
					$('#newsLoader').hide();
					
					go_init();
					
					$('#refresh').click(function() 
					{
						refresh();
					});
					
					refresh();
					
					$('.readless').click(function()
					{
						news();
						
						return false;
					});
				}
			})
			
			return false;
		})
	}
	
	// po zaladowaniu strony
	
	news();
});
