$(document).ready(function(){
	$("img[title], .album[title]").tooltip({
		layout: '<div class="tooltip"><div class="sprites tooltip_lft"></div><div class="sprites tooltip_bg"></div><div class="sprites tooltip_rgt"></div><div class="sprites tooltip_ar"></div></div>',
		tip: '.tooltip',
		tipContent: '.tooltip .tooltip_bg'
	});

	var i = 1;
	$("#dodaj-wiecej").click(function(){
		i += 1;
		var st = $('.dodajtoggle a').html();
		var p = (st == 'z adresu URL') ? 0 : 1; // 0 z dysku
		if(i <= 5){
			$.ajax({
				type: "POST",
				url: "home/ajax_more_inputs",
				data: "i="+i+"&p="+p+"&null=1",
				success: function(content) {
					$.getScript('js/home.js');
					$("#tagi_info").before(content);
				}
			});
		}
		return false;
	});

	$(".star-rating li a").click(function () {
		$(".current-rating").css({'width':($(this).attr('title')*10)+'%'});
		$.ajax({
			type: "POST",
			url: "zdjecia/ajax_vote",
			data: "ocena="+$(this).attr('title')+"&name="+$(".star-rating").attr('id')+"&null=1",
			success: function(content) {				
				new_overlay();
				$(".msg").prepend(content);
			}
		});
		return false;
	});

	$("#addcom .wyslij").click(function () {
		var rules_are_here = $(this).attr('rel');
		if(rules_are_here.length == 0) rules_are_here = 'zdjecia';
		$("#there_is_an_error").val(0);
		$.ajax({
			type: "GET",
			async: false,
			url: rules_are_here+"/validate",
			data: "field=text&value="+escape($("#addcom_text").val())+"",
			dataType: 'json',
			success: function(data) {
				if(data.msg.length > 0) {
					new_overlay();
					$(".msg").prepend(data.msg);
					$("#there_is_an_error").val(1);
				}
			}
		});
		if($("#there_is_an_error").val() == 1) return false;
		return true;
	});

	$("button.tak").click(function(){
		$("#adultmsg").hide();
		$("#cont").show();
	});

	$(".zgloszenie, .delcom").click(function(){
		$.ajax({
			type: "POST",
			url: $(this).attr('href'),
			data: "null=1",
			success: function(cont) {
				new_overlay('reloadMe');
				$(".msg").prepend(cont);
			}
		});
		return false;
	});

	$('.usun_album').click(function() {
		new_overlay2($(this).attr('href'));
		$(".msg").prepend("<b>Czy na pewno usunąć album?</b> <br /><br /> <span style=\"font-size: 10px;\">Usunięcie albumu nie jest jednoznaczne z usunięciem zdjęć, są one dalej dostępne pod \"zobacz wszystkie zdjęcia użytkownika\"</span>");
		return false;
	});


	$('.ustawienia h2').each(function () {
		$(this).next('div').hide();
		$(this).css('cursor', 'pointer');
	});
	$('.ustawienia h2').toggle(
		function () {
			$(this).next('div').slideDown();
		},
		function () {
			$(this).next('div').slideUp();
		}
	);
	
	$('.dodajtoggle a').toggle(
		function () {
			$(this).html('z dysku');
			$(".zdysku").hide();
			$(".zurl").show();
			$("#uploadtyp").val(1);
		},
		function () {
			$(this).html('z adresu URL');
			$(".zdysku").show();
			$(".zurl").hide();
			$("#uploadtyp").val(0);
		}
	);

});


function reloadThePage() {
	location.reload();
}


function new_overlay(reload){
	if($('#overlay')) $('#overlay').remove();
	var over = '<div id="overlay" class="overlay-window logowanie-window"><div class="overlay logo close"></div><div class="overlay shadow-top"></div>'+
				'<div class="content"><div class="wrapper"><div class="msg"><button class="buttons ok close"></button></div></div></div>'+
				'<div class="overlay shadow-bottom"></div></div>';
	$("body").prepend(over)
	if(reload == 'reloadMe') {
		$("#overlay").overlay({
			closeOnClick: true,
			closeOnEsc: true,
			mask: {color: '#000', opacity: 0.5, loadSpeed: 200},
			load: true,
			onClose: function () {
				reloadThePage();
			}
		});
	} else {
		$("#overlay").overlay({
			closeOnClick: true,
			closeOnEsc: true,
			mask: {color: '#000', opacity: 0.5, loadSpeed: 200},
			load: true,
			onClose: null
		});
	}
}

function new_overlay2(href_if_yes){
	if($('#overlay')) $('#overlay').remove();
	var over = '<div id="overlay" class="overlay-window logowanie-window"><div class="overlay logo close"></div><div class="overlay shadow-top"></div>'+
				'<div class="content"><div class="wrapper"><div class="msg"><div class="clear"></div><button class="overlay_yes buttons tak close left" style="margin-left: 65px;"></button><button class="overlay_no buttons nie close"></button></div></div></div>'+
				'<div class="overlay shadow-bottom"></div></div>';
	$("body").prepend(over)
	$("#overlay").overlay({
		closeOnClick: true,
		closeOnEsc: true,
		mask: {color: '#000', opacity: 0.5, loadSpeed: 200},
		load: true
	});

	$('.overlay_yes, .overlay_no').click(function () {
		var OVERLAY_RET = ($(this).hasClass('tak'));
		if(OVERLAY_RET) location.href = href_if_yes;
	});
}
