function setGrid(name) {
	if (!name) {
		var name = '.photos-list ul';
	}
	$(name).children('li').each(function(){
		var a = $(this).children('a');
		a.css({
			'margin-top': ($(this).height() - a.height()) / 2 + 'px',
			'margin-left': ($(this).width() - a.width()) / 2 + 'px'
		});
	});
}

//function banner_blink()
//{	
//	var banner = $("div#header-content").children("img#banner");
//	$("div#header-content").css("background-position","0px -580px");
//	banner.fadeTo(20,0.2,function(){
//		banner.fadeTo(20,1.0);
//		$("div#header-content").css("background-position","0px 0px");
//	});
//}

function timedEvent1() {

	/*$('#banner').animate({
		opacity: 0.0
	}, 100, 'linear', function(){
		$('#banner').animate({
			opacity: 1
		}, 100, 'linear');
	});*/
	
	$("#banner").animate({
		top: "600"
	},100,"linear",function(){
		setTimeout(function(){$("#banner").css("top","0")},100);
	});
	t=setTimeout("timedEvent1()",f);
}

function timedEvent2() {

	$('#banner').animate({
		opacity: 0.2
	}, 100, 'linear', function(){
		$('#banner').animate({
			opacity: 0.8
		}, 500, 'linear', function(){
			$('#banner').animate({
				opacity: 0.4
			}, 100, 'linear', function(){
				$('#banner').animate({
					opacity: 1
				}, 1000, 'linear');
			});
		});
	});
	t=setTimeout("timedEvent2()",f);
}

function timedEvent3() {

	$('#banner').animate({
		opacity: 0.3
	}, 300, 'linear', function(){
		$('#banner').animate({
			opacity: 1
		}, 300, 'linear');
	});
	t=setTimeout("timedEvent3()",f);
}

f = 20000;

$(document).ready(function() {

	//Language switcher
	$('#languages a').bind("click",function(event){
		var url = $(this).attr("href");
		var language = $(this).attr("language");
		$.ajax({
			url: "pages,registerLanguage," + language,
			success: function(response){
				if (response === 'TRUE'){
					document.location = url;
				}
			}
		});
		event.preventDefault();
	});
	
	$('#languages span:last').remove();

	switch(Math.floor(Math.random()*4)) {
			
		case 0:
				src = $("#banner").attr("src");
				$("#header-content").css("background-image", "url("+src+")");
				$('#banner').vibrate({
					spread: 20,
					frequency: f,
					duration: 400,
					speed: 40,
					opacity: 0.3
				});
				break;
		case 1:
				$("#banner").css("position","absolute");
				$("#header-content").css("background", "none");
				t=setTimeout("timedEvent1()",f);
				break;
		case 2:
				$("#header-content").css("background", "none");
				t=setTimeout("timedEvent2()",f);
				break;
		default:
				t=setTimeout("timedEvent3()",f);
				break;
	}

	//setInterval(banner_blink,3000);

	//Sticker animation
	var params = {};
	params.wmode = "transparent";
	swfobject.embedSWF("fx/gwiazda.swf", "sticker-flash", "99", "99", "9.0.0", "fx/expressInstall.swf", null, params);

	//Top menu
	$(".top-submenu .subactive").parent().prev().prev().addClass("active");
	$("#top-menu .active").next().next().show();

	//Left menu
	var floatMenu = "#float-menu";
	var menuStartY = parseInt($(floatMenu).css("top").substring(0,$(floatMenu).css("top").indexOf("px")));
    $(window).scroll(function() {
		var scrollTop  = $(document).scrollTop();
		var menuCurrentY = $(floatMenu).offset().top;
		if (scrollTop > menuStartY) {
			var adjustedOffset = scrollTop + "px";
			$(floatMenu).css("top",adjustedOffset);
		}
		else if(menuCurrentY > menuStartY) {
			$(floatMenu).css("top",menuStartY);
		}
    });

	$("#float-menu .subactive").parent().parent().prev().addClass("active");
	$("#float-menu .active").next().show();

	//Photos
	var goUp = false;
	$("a[rel='colorbox']").colorbox({"current":"", "width": "700px"}); //Zdjecie {current} z {total}
	
	setGrid();

	$(".preview-hover").bind("mouseover",function(){
		$(this).find(".overlay").css("background-position","0px -140px");
		$(this).children(".preview-title").children("a").css("background-image","url(images/yellow-strike-big-bg-2.gif)");
	});
	
	$(".preview-hover-wide").bind("mouseover",function(){
		$(this).find(".overlay").css("background-position","0px -80px");
		$(this).children(".preview-title").children("a").css("background-image","url(images/yellow-strike-big-bg-2.gif)");
	});
	
	$(".preview-hover").bind("mouseout",function(){
		$(this).find(".overlay").css("background-position","0px 0px");
		$(this).children(".preview-title").children("a").css("background","none");
	});
	
	$(".preview-hover-wide").bind("mouseout",function(){
		$(this).find(".overlay").css("background-position","0px 0px");
		$(this).children(".preview-title").children("a").css("background","none");
	});
	
	$(".preview-photo-wide").bind("mouseover",function(){
		$(this).find(".overlay-wide").css("background-position","0px -80px");
	});

	$(".preview-photo-wide").bind("mouseout",function(){
		$(this).find(".overlay-wide").css("background-position","0px 0px");
	});
	
	$(document).bind('cbox_closed', function(){
        if (goUp) {
			document.location = "#newsletter-header";
		}
		goUp = false;
	});
	
	$("#lightbox-trigger").bind("click",function(event){
		event.preventDefault();
		goUp = true;
		$("#play-photos ul li:first a").click();
	});
	
	//Back link hack
	$("a.backLink").bind("click",function(event){
		event.preventDefault();
		history.back();
	});
});
