if($.cookie("animationsDisabled")==1){
	var animations = false;
}
else{
	var animations = true;
}
if($.cookie("widescreenLimited")==1){
	var widescreenLimited = true;
}
else{
	var widescreenLimited = false;
}
function flash_disableVote(finalMark){
	$(document).ready(function(){
		$("#vote").empty().append("<img src=\"/img/stars"+finalMark+".png\" alt=\"Ocena:"+finalMark+"/5\" title=\"Ocena:"+finalMark+"/5\" /> <p class=\"thanks\">Dzięki za oddanie głosu!</p>");
	});
}
$(document).ready(function(){
	if(animations){
		$("#animations").text("Wyłącz animacje");
	}
	else{
		$("#animations").text("Włącz animacje");
	}
	if(!widescreenLimited){
		$("body").attr("class", "");
		$("#wideselector").attr("title", "Ogranicz szerokość strony").children("img").attr("src", "/img/unwide.png").attr("alt", "Ogranicz szerokość");
	}
	else{
		$("body").attr("class", "unwide");
		$("#wideselector").attr("title", "Przywróć pełną szerokość").children("img").attr("src", "/img/wide.png").attr("alt", "Przywróć szerokość");
	}
	var currenttab = -1;
	function toggletab(tabnumber, retract){
		if(tabnumber != -1){
			if(retract){
				$("#menu li").eq(tabnumber).attr("class", "");
			}
			else{
				$("#menu li").eq(tabnumber).attr("class", "over");
			}
		}
		else{
			$("#menu li").attr("class", "");
		}
	}
	function slideUpOrHide(id, speed){
		$(id).data("displayed", false);
		if(animations){
			$(id).slideUp(speed);
		}
		else{
			$(id).hide();
		}
	}
	function slideDownOrShow(id, speed){
		$(id).data("displayed", true);
		if(animations){
			$(id).slideDown(speed);
		}
		else{
			$(id).show();
		}
	}
	function extractPathname(element){
		path=$(element).get(0).pathname;
		if(path.substr(0,1)=="/"){
			return path;
		}
		else{
			return "/"+path;
		}
	}
	$("#animations").click(function(){
		if(animations){
			if(typeof(pageTracker) != "undefined") pageTracker._trackPageview("/disabeleAnimations");
			animations = false;
			$.cookie("animationsDisabled", 1, { expires: 365, path: "/" });
			$(this).text("Włącz animacje");
		}
		else{
			if(typeof(pageTracker) != "undefined") pageTracker._trackPageview("/enableAnimations" );
			animations = true;
			$.cookie("animationsDisabled", 0, { expires: 365, path: "/" });
			$(this).text("Wyłącz animacje");
		}
		return false;
	});
	$("#news .news .newscontent").hide().prev().addClass("hidden");
	if($("#movlist").hasClass("startup")){
		$("#spotlight, #loading, #news, #related, #movdescr, #pass input, #pass textarea").hide();
		$("#shownews, #showrelated, #showdescr").removeClass("selected");
	}
	else{
		$("#spotlight, #loading, #news, #related, #movlist, #pass input, #pass textarea").hide();
	}
	if(screen.width<=1024){
		$("#wideselector").parent().hide();
	}
	$("#vote form").rating();
	$("#vote-result").hover(function(){
		$("#vote-stars").hide();
		$("#vote").show();
	},
	function(){
		$("#vote").hide();
		$("#vote-stars").show();
		
	});
	if($("#player").children().eq(0).attr("class") == 'live'){
		if(!DetectFlashVer(9, 0, 115)){
			$(".live").empty().html('<p id="flasherror">Wymagany jest nowy Adobe Flash Player. <a href="http://get.adobe.com/flashplayer/">Pobierz go ze strony Adobe</a>.</p><p><a href="http://get.adobe.com/flashplayer/"><img src="/img/get_adobe_flash_player.png" alt="Pobierz odtwarzacz Adobe Flash" /></p>');
		}
	}
	$("#loginform").css("top", 0).hide();
	$("#loading").bind("ajaxSend", function(){
		if(animations){
			$(this).show().css({"right":"-180px"}).animate({
				"right":"-15px"
			}, "slow", "easeOutElastic");
		}
		else{
			$(this).show().css({"right":"-15px"});
		}
	}).bind("ajaxComplete", function(){
		if(animations){
			$(this).hide().show().animate({
				"right":"-180px"
			}, "normal");
		}
		else{
			$(this).hide().css({"right":"-180px"});
		}
	});
	$("#menu li a").each(function(i, e){
		$(e).click(function(){
			if(i==2){	//Kliknieto na pozycje "Kategorie"
				if($("#ext-menu").data("displayed") == true){
					slideUpOrHide("#ext-menu", "fast");
				}else{
					slideDownOrShow("#ext-menu", "fast");
				}
			}
			else{
				if((currenttab!=-1) && (currenttab!=i)){ 
					if(animations) $("#movlist").slideUp("fast");
					toggletab(currenttab, true);
					slideUpOrHide("#ext-menu", "fast");

				}
				if(currenttab!=i){	
					currenttab=i;
					var url = '';
					if($(this).text() == $("#movlist h2").text()){  // wybrano juz zaladowana zakladke
						slideUpOrHide("#related:visible, #movdescr:visible, #news:visible", "fast");
						$("#shownews, #showrelated, #showdescr").removeClass("selected");
						$(this).addClass("selected");
						slideDownOrShow("#movlist", "fast");
						$("#descr").scrollTo(0, 500);
					}
					else{	//zakladka jeszcze nie zaladowana - ladowanie
						url=extractPathname(this);
						$("#movlist").load(url+"cat", function(){
							slideUpOrHide("#related:visible, #movdescr:visible, #news:visible", "fast")
							$("#shownews, #showrelated, #showdescr").removeClass("selected");
							$(this).addClass("selected");
							slideDownOrShow("#movlist", "fast");
							$("#movlist .overlay").each(function(i, e){
								$(e).mouseover(function(){
									$(e).css('backgroundImage','url(/img/antyrama-o.png)');
								});
					        		$(e).mouseout(function(){
						        	        $(e).css('backgroundImage','url(/img/antyrama.png)');
								});
				        	        });
							$("#descr").scrollTo(0, 500);
						});
					}
					if(typeof(pageTracker) != "undefined") pageTracker._trackPageview(extractPathname(this));
				}
			}
			return false;
		});
		$(e).mouseover(function(){
			toggletab(i, false);
		});
		$(e).mouseout(function(){
			toggletab(-1);
		});
	});
	$("#wideselector").click(function(){
		if(widescreenLimited){
			$("body").attr("class", "");
			$(this).attr("title", "Ogranicz szerokość strony").children("img").attr("src", "/img/unwide.png").attr("alt", "Ogranicz szerokość");
			widescreenLimited = false;
			$.cookie("widescreenLimited", 0, { expires: 365, path:"/" });
		}
		else{
			$("body").attr("class", "unwide");
			$(this).attr("title", "Przywróć pełną szerokość").children("img").attr("src", "/img/wide.png").attr("alt", "Przywróć szerokość");
			widescreenLimited = true;
			$.cookie("widescreenLimited", 1, { expires: 365, path:"/" });
		}
		return false;
	})
	$("#ext-menu li a").each(function(i, e){
		$(e).click(function(){
			if((currenttab!=-1) && (currenttab!=(i+2))){
				if(animations) $("#movlist").slideUp("fast");
					toggletab(currenttab, true);
				}
				slideUpOrHide("#ext-menu", "fast");
				if(currenttab!=(i+2)){
					currenttab=i+2;
					var url = '';
					if($(this).text() == $("#movlist h2").text()){
						slideUpOrHide("#related:visible, #movdescr:visible, #news:visible", "fast");
						$("#shownews, #showrelated, #showdescr").removeClass("selected");
						$(this).addClass("selected");
						slideDownOrShow("#movlist", "fast");
						slideUpOrHide("#ext-menu", "fast");
						$("#descr").scrollTo(0, 500);
					}
					else{
						url=extractPathname(this);
						$("#movlist").load(url+"cat", function(){
							slideUpOrHide("#related:visible, #movdescr:visible, #news:visible, #ext-menu", "fast")
							$("#shownews, #showrelated, #showdescr").removeClass("selected");
							$(this).addClass("selected");
							slideDownOrShow("#movlist", "fast");
							$("#movlist .overlay").each(function(i, e){
								$(e).mouseover(function(){
									$(e).css('backgroundImage','url(/img/antyrama-o.png)');
								});
					        		$(e).mouseout(function(){
						        	        $(e).css('backgroundImage','url(/img/antyrama.png)');
								});
				        	        });
							$("#descr").scrollTo(0, 500);
						});
					}
					if(typeof(pageTracker) != "undefined") pageTracker._trackPageview(extractPathname(this));
				}
				return false;
			});
	});
	$("#submenu li a").each(function(i, e){
		$(e).click(function(){
			$("#submenu li").removeClass("selected");
			url = extractPathname(this);
			$("#submovies").load(url+"cat", function(){
				$(e).parent().attr("style", "").attr("class", "selected");
				if($.browser.msie){
					$("#submovies .frame>a").each(function(i, e){
						$(e).mouseover(function(){
							$(e).siblings(".overlay").css('border', '1px solid #0187c5');
						});
						$(e).mouseout(function(){
							$(e).siblings(".overlay").css('border', '1px solid #999');
						});
					});
				}
				else{
					$("#submovies .overlay, #tempmovies .overlay, #movlist .overlay").each(function(i, e){
						$(e).mouseover(function(){
							$(e).css('backgroundImage','url(/img/antyrama-o.png)'); 
						});
						$(e).mouseout(function(){
							$(e).css('backgroundImage','url(/img/antyrama.png)'); 
						});
					});
				}
			});
			if(typeof(pageTracker) != "undefined") pageTracker._trackPageview(extractPathname(this));
			return false;
		});
	});
	if($.browser.msie){
		$("#submovies .frame>a").each(function(i, e){
			$(e).mouseover(function(){
				$(e).siblings(".overlay").css('border', '1px solid #0187c5');
			});
			$(e).mouseout(function(){
				$(e).siblings(".overlay").css('border', '1px solid #999');
			});
		});
	}
	else{
		$("#submovies .overlay, #tempmovies .overlay, #movlist .overlay").each(function(i, e){
			$(e).mouseover(function(){
				$(e).css('backgroundImage','url(/img/antyrama-o.png)');
			});
			$(e).mouseout(function(){
				$(e).css('backgroundImage','url(/img/antyrama.png)');
			});
		});
	}
	$("#pass input, #pass textarea").click(function(){
		$(this).select();
	});
	$("#sendlink").click(function(){
		$(this).hide();
		$("#passseparator").hide();
		if(animations){
			$("#pass input").slideDown("fast", function(){
				$(this).click();
			});
		}
		else{
			$("#pass input").show().click();
		}
		return false;
	});
	$("#embed").click(function(){
		$(this).hide();
		$("#passseparator").hide();
		if(animations){
			$("#pass textarea").slideDown("fast", function(){
				$(this).click();
			});
		}
		else{
			$("#pass textarea").show().click();
		}
		return false;
	});
	$("#extendcommentfield").click(function(){
		$("#addcomment textarea").animate({
			"height":$("#addcomment textarea").height()*150/100
		}, "fast");
		return false;
	})
	$("#addcomment form input[type=text]").focus(function(){
		if(this.value=='Podpis') this.value='';
	});
	$("#addcomment form textarea").focus(function(){
		if(this.value=='Komentarz') this.value='';
	});
	if(!$("#commentSubmitResult").hasClass("startup")){
		$("#csrslider").hide();
	}
	$("#addcomment form").submit(function(){
		$("#csrslider").hide();
		if($("input", this).get(0).value=='Podpis'){	// Podpis się nie zmienił
			$("#commentSubmitResult").attr("class", "").addClass("warning").text("No bez jaj ;) Podpisz się.");
			slideDownOrShow("#csrslider", "slow");
		}
		else if($("input", this).get(0).value==''){	// Podpis jest pusty
			$("#commentSubmitResult").attr("class", "").addClass("warning").text("No bez jaj ;) Podpisz się.");
			slideDownOrShow("#csrslider", "slow");
		}
		else if($("textarea", this).get(0).value=='Komentarz'){	// treść nie zmieniła się
			$("#commentSubmitResult").attr("class", "").addClass("warning").text("No bez jaj ;) Popraw komentarz.");
			slideDownOrShow("#csrslider", "slow");
		}
		else if($("textarea", this).get(0).value==''){	// treść jest pusta
			$("#commentSubmitResult").attr("class", "").addClass("warning").text("No bez jaj ;) Popraw komentarz.");
			slideDownOrShow("#csrslider", "slow");
		}
		else{	//wszystko wygląda okej
			slideUpOrHide(this, "slow");
			$.ajax({
				type:'POST',
				url:$(this).attr('action'),
				data:$(this).serialize(),
				dataType:"json",
				success: function(data, textStatus){
					if(data.error){
						$("#commentSubmitResult").attr("class", "").addClass("error").html(data.errorText).parent().hide();
						slideDownOrShow("#csrslider", "slow");
						$("#readdComment").click(function(){
							slideUpOrHide("#csrslider", "slow");
							slideDownOrShow("#addcomment form", "slow");
							return false;
						});
					}
					else{
						$("#commentSubmitResult").attr("class", "").addClass("info").html("Twój komentarz został dodany. Dzięki! <a href=\"#\" id=\"addNextComment\">Teraz Możesz dodać kolejny »</a>").parent().hide();
						slideDownOrShow("#csrslider", "fast");
						$("#commentsList").prepend(data.comment);
						slideDownOrShow("#commentsList .selfcomment", "slow");
						$("#commentsList .selfcomment").removeClass("selfcomment");
						$("#addNextComment").click(function(){
							$("#addcomment form textarea").get(0).value='Komentarz';
							slideUpOrHide("#csrslider", "slow");
							slideDownOrShow("#addcomment form", "slow");
							return false;
						});
					}
				},
				error: function(){
					$("#commentSubmitResult").attr("class", "").addClass("error").html("Coś poszło nie tak jak powinno :( Spróbuj <a href=\"#\" id=\"readdComment\">dodać swój komentarz jeszcze raz</a>. Jeśli to nie pomoże, sprawdź ustawienia adblocka i skontaktuj się z administratorem TVPW.").parent().hide();
					slideDownOrShow("#csrslider", "slow")
					$("#readdComment").click(function(){
						slideUpOrHide("#csrslider", "slow");
						slideDownOrShow("#addcomment form", "slow");
						return false;
					});	
				}
			});
		}
		return false;
	});
	$("#ajaxreport").hide();
	$(".showreport").css({"opacity":".3"}).hover(function(){
		$(this).css({"opacity":"1"});		
	},
	function(){
		$(this).css({"opacity":".3"});
	});
	$(".showreport").click(function(){
		if($("#addcomment form input").get(0).value != 'Podpis') $("#ajaxreport #report_nick").get(0).value = $("#addcomment form input").get(0).value;
		$(".showreport").show();
		$("#rsrslider").hide();
		$("#ajaxreport").appendTo($(this).parents(".comment"));
		if(animations){
			$("#ajaxreport").slideDown("fast", function(){
				$("#descr").scrollTo(this, 500);
			});
		}
		else{
			$("#ajaxreport").show();
			$("#descr").scrollTo(this, 0);
		}
		report=this;
		if(animations){
			$(report).fadeOut("fast");
		}
		else{
			$(report).hide();
		}
		$("#ajaxreport form").attr("action", $(report).get(0).href);
		$("#ajaxreport").bind("reset", function(){
			slideUpOrHide(this, "fast");
			if(animations){
				$(report).fadeIn("fast");
			}
			else{
				$(report).show();
			}
		});
		return false;
	});
	$("#ajaxreport form").submit(function(){
		if($("#report_reason").get(0).value==''){
			$("#reportSubmitResult").attr("class", "").addClass("warning").text("No bez jaj ;) Podaj powód.");
			slideDownOrShow("#rsrslider", "slow");
		}
		else if($("#report_nick").get(0).value==''){
			$("#reportSubmitResult").attr("class", "").addClass("warning").text("No bez jaj ;) Podaj swój nick.");
			slideDownOrShow("#rsrslider", "slow");
		}
		else{
			slideUpOrHide($(this).parents(".comment"), "slow");
			thispos = $(this).parents(".comment")
			$.ajax({
				type:'POST',
				url:$(this).attr('action'),
				data:$(this).serialize(),
				dataType:"json",
				complete:function(){
					$(thispos).before("<p class=\"submitinfo\">Zgłosiłeś ten komentarz do usunięcia.</p>");
				}
			});
		}
		return false;
	});
	$("#shownews").click(function(){
		currenttab = -1;
		toggletab(-1, true);
		slideUpOrHide("#ext-menu", "fast");
		if(animations){
			$("#related, #movdescr, #movlist").slideUp("fast");
			$("#news").slideDown("fast", function(){
				$("#descr").scrollTo(0, "linear");
			});
		}
		else{
			$("#related, #movdescr, #movlist").hide();
			$("#news").show();
		}
		$("#shownews, #showrelated, #showdescr").removeClass("selected");
		$(this).addClass("selected");
		$("#descr").scrollTo(0, 500);
		return false;
	});
	$("#showdescr").click(function(){
		currenttab = -1;
		toggletab(-1, true);
		slideUpOrHide("#ext-menu", "fast");
		if(animations){
			$("#news, #related, #movlist").slideUp("fast");
			$("#movdescr").slideDown("fast", function(){
				$("#descr").scrollTo(0, 10, "linear");
			});
		}
		else{
			$("#news, #related, #movlist").hide();
			$("#movdescr").show();
		}
		$("#shownews, #showrelated, #showdescr").removeClass("selected");
		$(this).addClass("selected");
		$("#descr").scrollTo(0, 500);
		if(typeof(pageTracker) != "undefined") pageTracker._trackPageview("/descr" );
		return false;
	});
	$("#news .news h2 a").click(function(){
		if($(this).parent().hasClass("hidden")){
			if(animations){
				$("#news .news .newscontent").slideUp().prev().addClass("hidden");
				$(this).parent().removeClass("hidden").next().slideDown(function(){
					$("#descr").scrollTo($(this).parent(), 500, {easing:'linear'});
				});
			}
			else{
				$("#news .news .newscontent").hide().prev().addClass("hidden");
				$(this).parent().removeClass("hidden").next().show();
				$("#descr").scrollTo($(this).parent(), 0, {easing:'linear'});
			}
		}
		else{
			if(animations){
				$(this).parent().addClass("hidden").next().slideUp();
			}
			else{
				$(this).parent().addClass("hidden").next().hide();
			}
		}
		return false;
	});
	$("#newsbar a, #konkurs a").click(function(){
		var newsbarnbr = parseInt($("#newsbarnbr").attr("value"));
		currenttab = -1;
		toggletab(-1, true);
		slideUpOrHide("#ext-menu", "fast");
		if(animations){
			$("#descr").scrollTo(0, 500);
			$("#news, #related, #movdescr, #movlist").slideUp("fast");
			$("#news").slideDown("fast");
			$("#news .news .newscontent").hide().prev().addClass("hidden");
			$("#news"+newsbarnbr+" h2 a").parent().removeClass("hidden").next().slideDown();
		}
		else{
			$("#descr").scrollTo(0, 0);
			$("#news, #related, #movdescr, #movlist").hide();
			$("#news").show();
			$("#news .news .newscontent").hide().prev().addClass("hidden");
			$("#news"+newsbarnbr+" h2 a").parent().removeClass("hidden").next().show();
		}
		$("#shownews, #showrelated, #showdescr").removeClass("selected");
		$("#shownews").addClass("selected");
		if(typeof(pageTracker) != "undefined") pageTracker._trackPageview(extractPathname(this)+"/newsbar" );
		return false;
	});
	function subcatLoadUnloadBindIn(){
		if($(this).text()=="Więcej »"){
			$("#submovies").load("/categories/"+$("#submenu .selected").text().replace(" ", "_")+"/subcat_full", function(){
				if($.browser.msie){
					$("#submovies .frame>a").each(function(i, e){
						$(e).mouseover(function(){
							$(e).siblings(".overlay").css('border', '1px solid #0187c5');
						});
						$(e).mouseout(function(){
							$(e).siblings(".overlay").css('border', '1px solid #999');
						});
					});
				}
				else{
					$("#submovies .overlay, #tempmovies .overlay, #movlist .overlay").each(function(i, e){
						$(e).mouseover(function(){
							$(e).css('backgroundImage','url(/img/antyrama-o.png)'); 
						});
						$(e).mouseout(function(){
							$(e).css('backgroundImage','url(/img/antyrama.png)'); 
						});
					});
				}
			});
		}
		else{
			$.get("/categories/"+$("#submenu .selected").text().replace(" ", "_")+"/subcat_min_suppress");
			$("#submovies .submov").slice(6).remove();
			$("#submovies .more a").text("Więcej »");
		}
		return false;
	}
	$("#submovies .more a").live("click", subcatLoadUnloadBindIn);
	function sidecatLoadUnloadBindIn(){
		if($(this).text()=="Pobierz wszystkie »"){
			loadurl="/categories/"+$("#movlist h2").text().replace(/(\s)/g, "_")+"/sidecat_full";
			$("#movlist").load(loadurl, function(){
				if($.browser.msie){
					$("#movlist .frame>a").each(function(i, e){
						$(e).mouseover(function(){
							$(e).siblings(".overlay").css('border', '1px solid #0187c5');
						});
						$(e).mouseout(function(){
							$(e).siblings(".overlay").css('border', '1px solid #999');
						});
					});
				}
				else{
					$("#movlist .overlay").each(function(i, e){
						$(e).mouseover(function(){
							$(e).css('backgroundImage','url(/img/antyrama-o.png)');
						});
						$(e).mouseout(function(){
							$(e).css('backgroundImage','url(/img/antyrama.png)');
						});
					});
				};
			});
		}
		else{
			$.get("/categories/"+$("#movlist h2").text().replace(" ", "_")+"/sidecat_min_suppress");
			$("#movlist .submov").slice(6).remove();
			$("#movlist .more a").text("Pobierz wszystkie »");
			$("#movlist h2").after("<p class=\"more noalign\"><small>Wyświetlane jest tylko kilka najnowszych filmów. <a href=\"?sidecat_no_limit&amp;subcat="+$("#movlist h2").text()+">Pobierz wszystkie »</a></small></p>");
		}
		return false;
	}
	$("#movlist .more a").live("click", sidecatLoadUnloadBindIn);
	$("#othermedia li a").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");
		imgsrc = imgsrc.substring(0, imgsrc.length - 7)+".png";
		$(this).children("img").attr("src", imgsrc);
	});
	$("#othermedia li a").mouseout(function(){
		imgsrc = $(this).children("img").attr("src");
		imgsrc = imgsrc.substring(0, imgsrc.length - 4)+"-gs.png";
		$(this).children("img").attr("src", imgsrc);
	});
});
$(window).load(function(){
	if(animations){
		var newsbarnbr = $("#newsbarnbr").attr("value");
		if(($.cookie("newsbarHighlight") != newsbarnbr) || ($.cookie("dbg") == "1")){
			// delay hack: animating to the same color for 2000ms
			$("#newsbar").animate({color:"#EEEEEE"}, 5000, function(){
				$(this).css("background-color", "#60B8FE").animate(
				{
					backgroundColor:"#344A6E"
				}, 4000);
			});
			$.cookie("newsbarHighlight", newsbarnbr, {expires: 31, path:'/'});
		}
	}
});
