//console.log();

$(document).ready(function(){
	
	var CheckBoxOn = '<img src="templates/img/checkboxOn.gif" alt="" />';
	var CheckBoxOff = '<img src="templates/img/checkboxOff.gif" alt="" />';
	$(".checkbox").each(function() {
		var Input = $(this).children("input");
		
		if (Input.is(":checked")) {
			Input.after(CheckBoxOn);
		} else {
			Input.after(CheckBoxOff);
		}
	}).click(function() {
		var Input = $(this).children("input");
		var Img = $(this).children("img")
		
		if (Input.is(":checked")) {
			Input.removeAttr("checked");
			Input.after(CheckBoxOff);
			Img.remove();
		} else {
			Input.attr("checked", "checked");
			Input.after(CheckBoxOn);
			Img.remove();
		}
	});
	
	$("#photoList .addPhoto").click(function() {
		var PhotoId = $(this).attr("id");
		
		$.ajax({
			type: "POST",
			url: "zarzadzanie-albumami",
			data: "albumAction=addAlbumPhoto&photo="+PhotoId,
			beforeSend: function(){
				$("#photoList #"+PhotoId).hide();
				$("#photoList .l-"+PhotoId).show();
			},
			complete: function(){
				$(".s-"+PhotoId).show();
				$("#photoList .l-"+PhotoId).hide();
			}
		});
	});
	
	$("#photoList .deletePhoto").click(function() {
		var PhotoId = $(this).attr("id");
		
		$.ajax({
			type: "POST",
			url: "zarzadzanie-albumami",
			data: "albumAction=removePhoto&photo="+PhotoId,
			beforeSend: function(){
				$("#photoList #"+PhotoId).hide();
				$("#photoList .l-"+PhotoId).show();
			},
			complete: function(){
				$("#photoList .d-"+PhotoId).remove();
				if (!$("#photoList").is(":has(li)")) {
					$("#photoList").remove();
					$(".buttons:last").after('<div style="clear: both;"/><div class="msgBox" tal:condition="not: photos"><div class="msg">Album jest pusty.</div></div>');
					
				}
			}
		});
	});
	
	$("#photoDetails .addPhoto").click(function() {
		$.ajax({
			type: "POST",
			url: "zarzadzanie-albumami",
			data: "albumAction=addAlbumPhoto&photo="+$(this).attr("id"),
			beforeSend: function(){
				$("#addMsgFailure").hide();
				$("#photoDetails .addPhoto").hide();
				$("#photoDetails .loading").show();
			},
			error: function(){
				$("#photoDetails .addPhoto").show();
				$("#addMsgFailure").show();
			},
			success: function(response){
				$("#addMsgFailure").hide();
				if (response == 1) {
					$("#photoDetails .addPhoto").hide();
					$("#addMsgSuccess").show();
				} else {
					$("#photoDetails .addPhoto").show();
					$("#addMsgFailure").show();
				}
			},
			complete: function(){
				$("#photoDetails .loading").hide();
			}
		});
	});
	
	$('#slideshow').crossSlide({
		sleep: 2,
		shuffle: true,
		fade: 1
	}, [
		{ src: 'templates/img/cross-slide/01.jpg' },
		{ src: 'templates/img/cross-slide/02.jpg' },
		{ src: 'templates/img/cross-slide/03.jpg' },
		{ src: 'templates/img/cross-slide/04.jpg' },
		{ src: 'templates/img/cross-slide/05.jpg' },
		{ src: 'templates/img/cross-slide/06.jpg' },
		{ src: 'templates/img/cross-slide/07.jpg' },
		{ src: 'templates/img/cross-slide/08.jpg' },
		{ src: 'templates/img/cross-slide/09.jpg' },
		{ src: 'templates/img/cross-slide/10.jpg' },
		{ src: 'templates/img/cross-slide/11.jpg' },
		{ src: 'templates/img/cross-slide/12.jpg' },
		{ src: 'templates/img/cross-slide/13.jpg' },
		{ src: 'templates/img/cross-slide/14.jpg' },
		{ src: 'templates/img/cross-slide/15.jpg' },
		{ src: 'templates/img/cross-slide/16.jpg' },
		{ src: 'templates/img/cross-slide/17.jpg' },
		{ src: 'templates/img/cross-slide/18.jpg' },
		{ src: 'templates/img/cross-slide/19.jpg' },
		{ src: 'templates/img/cross-slide/20.jpg' }
	]);
	
});

function confirmAlbumRemoving(formName) {
	var response = confirm("Czy napewno chcesz usunąć aktualny album?");
	if (response == true) {
		submitForm(formName);
	}
}

function submitForm(formName) {
	var form = document.getElementById(formName);
	form.submit();
}