AudioPlayer.setup("lib/audio-player-2.0b.swf", {  
	width: 350, initialvolume: 60,
	border: "0d0d0d", bg: "0d0d0d", transparentpagebg: "yes", text: "ffffff",
	track: "0d0d0d", tracker: "333333", loader: "dd18d6",
	leftbg: "0d0d0d", voltrack: "990000", volslider: "ff0000"
});

var usuwam = false;	

function listy() {
	$("#list").ajaxForm( { target: '#komunikaty', url: 'mail.php', type: 'post' } );
	$("#list textarea").click( function() { $(this).text(''); } );
	
	$("#list").submit( function() {
		var tresc = $(this).find('textarea').text();
		if(!tresc) tresc = $(this).find('textarea').val();
		if(tresc.search('@') === -1)
			if(!confirm('Nie znalazłem żadnego adresu e-mail. Czy jesteś pewien, że chcesz wysłać wiadomość bez tego?'))
				return false;
		$("#komunikaty").html('<p class="komunikat">Trwa wysyłanie wiadomości... proszę czekać.</p><hr />');
	});
}

function menu() {
	$("#guest #menu .podkresl").removeClass('podkresl');
}

function korekcja() {
	$('#admin #menu .dziedziczenie > li').not(".zablokowany").addClass('tlumaczenie');
	$('#admin .kolejnosc > li').removeClass('tlumaczenie');
}

function admin_menu() {
	$("#admin #menu li").add("#admin #materialy li").dblclick( function() {
		$("#admin .kolejnosc, #admin .dziedziczenie").sortable('disable');
		$("#admin #menu li").add("#admin #materialy li").editable( function() {
			var aliasy = '';
			$("#admin #materialy > ul").each( function() {
				if($(this).attr('id'))
					aliasy += $(this).attr('id') +'='+ $(this).find("> li").text() +"\n";
				$(this).find("ul li").each( function() {
					aliasy += $(this).attr('id') + '=' + $(this).text() + "\n";
				} );
			} );
			$("#admin #menu > ul").each( function() {
				aliasy += $(this).attr('id') +'='+ $(this).find("> li").text() +"\n";
				$(this).find("ul li").each( function() {
					aliasy += $(this).attr('id') + '=' + $(this).text() + "\n";
				} );
			} );
			$.post('kolejnosc.php', { aliasy: aliasy }, function(data) { $("#admin #menu li").add("#admin #materialy li").noteditable(); $("#admin .kolejnosc, #admin .dziedziczenie").sortable('enable').disableSelection(); } );
		});
	});

	$("#admin .kolejnosc, #admin .dziedziczenie").sortable({
		connectWith: '.kolejnosc, .dziedziczenie',
		//cancel: '.zablokowany',
		placeholder: 'placeholder',
		stop: function(event, ui) {
		
			var kolejnosc = '';
			$("#admin #menu > ul").each( function() {
				if($(this).children().hasClass('kolejnosc')) {
					kolejnosc += $(this).attr('id') +"\n";
					kolejnosc += '{{' + "\n";
					$(this).find(".kolejnosc > li").each( function() {
						kolejnosc += $(this).attr('id') + "\n";
					} );
					kolejnosc += '}}' + "\n";
				}
			} );

			korekcja();
			
			var tlumaczenie = '';
			$("#admin #menu .tlumaczenie").each( function() {
				if($(this).next().is('li') && !$(this).next().hasClass('tlumaczenie'))
					tlumaczenie += $(this).next().attr('id') +'='+ $(this).attr('id') +"\n";
			});
			
			$.post('kolejnosc.php', { tlumaczenie: tlumaczenie }, function(data) { } );
			$.post('kolejnosc.php', { kolejnosc: kolejnosc }, function(data) {  } );
		}
	}).disableSelection();
	
	if(usuwam) {
		$("#przycisk_potwierdz").show();	
		$("#przycisk_usun").hide();
	}
	else {
		$("#przycisk_potwierdz").hide();	
		$("#przycisk_usun").show();
	}	
}

function wczytaj(id, q) {
	if(id == 'menu' || id == 'jezyki') {
		$("#guest #menu").load('guest_menu.php?q='+ q, function() {
			menu();
		});
	}
	
	if(id == 'jezyki') {
		$("#jezyki").hide();
		$("#jezyki").load('jezyki.php?q='+ q);
		$("#admin #admin_menu").load('admin_menu.php?rand='+ Math.random() +'&q='+ q, function() { $("#jezyki").show(); admin_menu(); } );
	}
	
	if(id == 'tresc' || id == 'menu' || id == 'jezyki') {
		$("#tresc").load('tresc.php?q='+ q, function() {

			$.get("tytul.php", { q: q }, function(data) {
				document.title = data;	
			} );
		
			$("#jezyki").show();		
		
			listy();
		});
	}
}

$( function() {
	
	$(".odtwarzacz div").text('Odtwarzacz - potrzebna wtyczka flash');

	$("#admin li").live('click', function() {
		var id = $(this).attr('id');
		var opis = $(this).text();
		if(id)
			if(usuwam) {
				if(confirm('Czy jesteś pewien, że chcesz usunąć "'+ opis +'"?'))
					$.post('upload.php?rand='+ Math.random(), { usun: id } , function(data) { wczytaj('jezyki', '') } );
			}
			else $("#prawa").load('edytor.php', { id: $(this).attr('id') }, function() {
				$("#prawa form").ajaxForm( { target: '#prawa', url: 'edytor.php', type: 'post', success: function(data) {
					$("#prawa").html(data);
				} } );
			} );
	} );

	$("#jezyki a").live('click', function() {
		wczytaj('jezyki', $(this).attr('href'));
		wczytaj('menu', $(this).attr('href'));
		wczytaj('tresc', $(this).attr('href'));
		return false;
	});
	
	$("#guest #menu a").live('click', function() {
		$(this).parent().parent().parent().children().removeClass("podswietlone");
		$(this).parent().parent().addClass("podswietlone").effect("bounce", { times: 1 , direction: "up" }, 300);
		wczytaj('tresc', $(this).attr('href'));
		return false;
	});

	$("#guest .nawigacja a").live('click', function() {
		wczytaj('tresc', $(this).attr('href'));
		return false;
	});


	menu(); listy(); admin_menu();
	
	$("#identyfikacja").find("form").ajaxForm( { url: 'init.php', type: 'post', success: function() { document.location.href = './' } } );	
	
	$("#identyfikacja input").hide();
	$("#logowanie").click(function() {
		$("#identyfikacja input").show();
		$(this).parent().find('input').attr('value', '');
		$(this).parent().find('input').focus();
	});
	
	$("#wysylanie").hide();
	
	function hierarchia() {
		var hierarchia = new String();
		$('#hierarchia').children().each( function() {
			hierarchia += $(this).text() + "\n";
		} );
		//alert(hierarchia);
	}
	
	function przenumeruj() {
		var n = 1;
		$(".numerowane").each( function() {
			$(this).attr("id", "photo"+ n);
			$(this).find(".plik").each(function() {
				$(this).attr("name", "photo"+ n);
			});
			n++;
		});
		if(n == 1) $("#potwierdz").hide(); else $("#potwierdz").show();
		//	alert($("#piaskownica").html());
	}
	
	$( function() {
		$("#potwierdz").hide();
		$("#dodaj").click(function() {
			$("#piaskownica").append('<div class="numerowane"><input class="usun" type="button" value="-" /><input class="plik" type="file" /></div>' + "\n");
			przenumeruj();
			$(".usun").each(function() {
				$(this).click(function() {
					$(this).parent().remove();
					przenumeruj();
				});
			});
		});
	});
	
	
	$("#przycisk_wyslij").live('click', function() {
		$("#wysylanie").dialog({
			title: "Wysyłanie plików jpeg mp3 png",
			buttons: {
				'Wyślij': function() {
					$('#upload').ajaxSubmit( { target: '#komunikaty', success: function() { $(".usun").trigger('click'); wczytaj("jezyki", '') } } );
				}
			},
			close: function() {
				$(this).dialog('destroy');
			}
		});
		
	});
	
	$("#przycisk_dodaj").live('click', function() {
		$.post('upload.php?rand='+ Math.random(), { utworz: 'tekst' } , function(data) { wczytaj('jezyki', '') } );
	});
	
	$("#przycisk_usun").live('click', function() {
		$("#przycisk_potwierdz").show();
		$("#przycisk_usun").hide();
		usuwam = true;
	});

	$("#przycisk_potwierdz").live('click', function() {
		$("#przycisk_potwierdz").hide();
		$("#przycisk_usun").show();
		usuwam = false;
	});		
	
	//losuj();
	
	$(".galeria .zdjecie").each( function() {
		wysokosc = $(this).height();
		wysokoscZdjecia = $(this).find("img").height();
		$(this).find("img").css('margin-top', (wysokosc - wysokoscZdjecia)/2)
	});
});

