var i = 0;

$(function() {
/*	var optionw = {
	  x:      1, 
	  y:      1, 
	  radius: 1.5,
	  color:  "#fff"
	}
	var optionb = {
	  x:      1, 
	  y:      1, 
	  radius: 1.5,
	  color:  "#fff"
	}
	var optione = {
	  x:      1, 
	  y:      1, 
	  radius: 1.5,
	  color:  "#eee"
	}
	var optiond = {
	  x:      1, 
	  y:      1, 
	  radius: 1.5,
	  color:  "#ddd"
	}
	var optionn = {
	  x:      0, 
	  y:      0, 
	  radius: 0,
	  color:  "#ddd"
	}
	var optionn = {
	  x:      1, 
	  y:      1, 
	  radius: 1.5,
	  color:  "#666"
	}
	*/
	var photos = new Array();
	var imageArray = new Array();
	var hidden = $('body').append('<div id="img-cache" style="display: none">').children('#img-cache');
	$.ajax({
		url: "/js/photos.xml",
		dataType: "xml",
		success: function(data){
			$(data).find('photo').each(function() {
			    var src = $(this).find('src').text();
			    var href = $(this).find('href').text();
			    var head = $(this).find('header').text();
			    var desc = $(this).find('desc').text();
				var photo = new Array(src, href, head, desc);
				photos.push(photo);
				imageArray.push(src);
			});
			$.each(imageArray, function (i, val) {
				$('<img>').attr('src', val).appendTo(hidden);
			});
			cyclePhotos(photos);
		}
	});
	
	/*
	var header_banners_img = new Array("banner-amarant.png", "banner-oldtimery.png", "banner-paszke.png");
	var header_banners_url = new Array("http://amarant.3oceans.pl/", "http://oldtimery.3oceans.pl/", "http://romanpaszke.3oceans.pl/");
	var header_banners_i = 1;
	
	function change_header_banner()
	{
		$("#header-banner em").delay(5000).animate({ opacity: 0 }, 1000, function(){
			$("#header-banner").attr("href", header_banners_url[header_banners_i]);
			$("#header-banner em").css("background-image", "url('/img/" + header_banners_img[header_banners_i] + "')")
				.animate({ opacity: 1 }, 1000, function(){
					header_banners_i++;
					header_banners_i %= header_banners_img.length;
					change_header_banner();
				});
		});
	}
	
	change_header_banner();
	*/
	
	$("#gallery a").fancybox();
	/*$("#dane, #footer").textShadow( optionb );
	$("#cbg h3, #cbg h4, #cbg h5").textShadow( optione );
	$("#cbg h2, #nav a, #userm-li, #oceans h1, #oceans p, #newsletter p").textShadow( optionw );
	$("#cbg ul, #cbg p").textShadow( optiond );
	$("#cbg a, #cbg a:hover, #userm-li:hover, #userm-li:hover #usermode, #userm-li:hover a, #nav a.here, #nav a.here:hover").textShadow( optionn );
	$("#nav a:hover, #userm-li:hover").textShadow( option6 );*/
	
	$("#galeria a").fancybox();
	
	if( window.location.hash && $("#photo-"+window.location.hash.substr(1)).length )
	{
		$("#photo-"+window.location.hash.substr(1)).trigger("click");
	}
	else
	{
		$("#gallery table td:first-child a").trigger("click");
	}
	
	$(".ukryj-mnie table, .ukryj-mnie p, .ukryj-mnie legend span").hide();
	$(".ukryj-mnie legend").css("cursor", "pointer").click(function() {
		$(this).children("span").toggle();
		$(this).next().toggle();
		$(this).next().next().toggle();
	});
	
	$(".ukryj-mnie a[href*=przenies]").click(function(event) {
		event.stopPropagation();
	});
	
	$("a[href*=/galeria-admin/?delete=]").click(function() {
		if( confirm("Czy na pewno chcesz usunąć CAŁĄ kategorię #" + $(this).attr("href").substr(51) + "?") )
			return true;
		else
			return false;
	});
	
	$("a[href*=/galeria-admin/?delete_photo=]").click(function() {
		if( confirm("Czy na pewno chcesz usunąć zdjęcie #" + $(this).attr("href").substr(57) + "?") )
			return true;
		else
			return false;
	});
	
	$("input[name^=photo_description]").focus(function() {
		input = $(this);
		input.hide();
		
		$('<textarea cols="20" rows="5">' + input.val() + '</textarea>').insertAfter(input).focus().blur(function() {
			input.val( $(this).val() );
			$(this).remove();
			input.show();
		});
	});
	
	$(".all-reservations table, .all-reservations h6").hide();
	$("h5").css("cursor", "pointer").click(function() {
		$(this).next().next().toggle();
	});
	
	$('<form class="form" style="float: right;">').prependTo(".all-reservations");
	
	$('<a href="#" id="toggle-reservations">Pokaż wszystkie rezerwacje</a>').appendTo(".all-reservations form.form").toggle(
		function() {
			$("#toggle-reservations").html("Ukryj wszystkie rezerwacje");
			$(".all-reservations table, .all-reservations h6").show();
		},
		function() {
			$("#toggle-reservations").html("Pokaż wszystkie rezerwacje");
			$(".all-reservations table, .all-reservations h6").hide();
		}
	);
	
	$('<input type="text" value="Podaj ID..." id="search-id">').appendTo(".all-reservations form.form").focus(function() {
		if( $(this).val() == "Podaj ID..." )
			$(this).val("");
	}).blur(function() {
		if( $(this).val() == "" )
			$(this).val("Podaj ID...");
	});
	
	$('<input class="subm" type="button" Value="Szukaj!">').appendTo(".all-reservations form.form").click(function() {
		found = false;
		$(".highlight").removeClass("highlight");
		
		$(".all-reservations td:first-child").each(function() {
			if( $(this).html() == $("#search-id").val() )
			{
				$(this).parent().addClass("highlight");
				
				table = $(this).parent().parent().parent();
				table.show(0, function() {
					$("html, body").animate({scrollTop: table.prev().prev().offset().top}, 1000);
				});
				
				found = true;
				return;
			}
		});
		
		if( !found )
			alert("Nie znaleziono rezerwacji #" + $("#search-id").val());
		
		$("#search-id").val("");
		
		return false;
	});
	
	$("#select-voyage").change(function() {
		window.location = "http://fryderykchopin.3oceans.pl/rejsy/" + $(this).val() + ".htm";
	});
	
	$("#select-charter").change(function() {
		window.location = "http://fryderykchopin.3oceans.pl/czartery/" + $(this).val() + ".htm";
	});
});

function cyclePhotos(ph) {
	j = (i%4)+1;
	k = i%(ph.length);
	$('#galph'+j).stop().delay(5000).animate({
		opacity: 0
	}, 500, function() {
		$(this).children('a').css('background-image', 'url('+ph[k][0]+')').attr('href', ph[k][1]).attr('title', ph[k][3]);
		$(this).children('div').children('h3').text(ph[k][2]);
		$(this).children('div').children('p').text(ph[k][3]);
		$('#galph'+j).animate({
			opacity: 1
			}, 500, function() {
				i++;
				cyclePhotos(ph);
			}
		);
	});
}
