﻿$(document).ready(function() {

	jQuery.fn.exists = function(){return jQuery(this).length>0;}


// Focus border remove

			$("a").focus(function() {
				$(this).blur();
			});




	// Lightbox

			/*if ($(".lightbox").exists()) {
				$(".lightbox").prettyPhoto({ theme:'dark_square' });
			}

			if ($("a[rel^='prettyPhoto']").exists()) {
				$("a[rel^='prettyPhoto']").prettyPhoto({ theme:'dark_square' });
			}

			if ($("#gallery").exists()) {

				$("#gallery a").prettyPhoto({ theme:'dark_square' });
				$('.row a:last-child').addClass('last');

				// Remove margin in last photo in row

					if ($(".long").exists()) {
						$("#gallery a:eq(6)").css({ marginRight: '0' });
					} else {
						$("#gallery a:eq(4)").css({ marginRight: '0' });
					}
			}*/



	// Fancybox

		if ($(".lightbox, #gallery a").exists()) {
			$(".lightbox, #gallery a").fancybox({
				'titlePosition'	:	'over',
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0,
				'onComplete'	:	function() {
					$("#fancybox-wrap").hover(function() {
						$("#fancybox-title").show();
					}, function() {
						$("#fancybox-title").hide();
					});
				}
			});
		}

		if ($("a[rel^='prettyPhoto']").exists()) {
			("a[rel^='prettyPhoto']").fancybox({
				'titlePosition'	:	'over',
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0,
				'onComplete'	:	function() {
					$("#fancybox-wrap").hover(function() {
						$("#fancybox-title").show();
					}, function() {
						$("#fancybox-title").hide();
					});
				}
			});
		}


		if ($("#gallery").exists()) {

			$("#gallery a").fancybox({
				'titlePosition'	:	'over',
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0,
				'onComplete'	:	function() {
					$("#fancybox-wrap").hover(function() {
						$("#fancybox-title").show();
					}, function() {
						$("#fancybox-title").hide();
					});
				}
			});


			$('.row a:last-child').addClass('last');

			// Remove margin in last photo in row

			if ($(".long").exists()) {
				$("#gallery a:eq(6)").css({ marginRight: '0' });
			} else {
				$("#gallery a:eq(4)").css({ marginRight: '0' });
			}
		}








	// Clear input





			clearInput = function (arg) {

				var $nam = $(arg + " input[type=text]").val();

				$(arg + " input[type=text]").focus(function() {
					if($(this).val() == $nam) {
						$(this).val('');
					}
				});



				$(arg + " input[type=text]").blur(function() {
					if($(this).val() == '') {
						$(this).val($nam);
					}
				});

			}



			clearInput("#newsletter");
			clearInput("#wyszukiwarka");











	// Menu Drop-Down #menu-vert:



			$(".menu_head").click(function() {
				$(this).next(".menu_body").slideToggle(300).siblings(".menu_body").slideUp("slow");
    			$(this).siblings();
			});

			$("#menu-vert .hit").next("#menu-vert .menu_body").slideToggle(300).siblings("#menu-vert .menu_body").slideUp("fast");







	// Menu Drop-Down #events-wide:


			$("#events-wide .menu_head").click(function() {
				$("#events-wide .menu_body").slideUp (300);
				$(this).parent().next(".menu_body").slideToggle(300);
			});

			$("#events-wide .hit").parent().next("#events-wide .menu_body").slideToggle(300).siblings("#events-wide .menu_body").slideUp("fast");








	// Remove margin, padding, border in last element

			$("#social a:last").css({ border: 'none', marginRight: '8px' });
			$("#menu-horiz a:last").css({ border: 'none' });
			$("#foot .links a:last").css({ border: 'none', paddingRight: '0' });
			$("#gallery-index .row a:odd").css({ marginRight: '0' });








	// Change background in odd positions

			$("#events dl:odd").css({ background: '#efefef' });











	// Change Font Size

			$(".small").click(function(){

				var $cookie_name = "textsize";
				var $cookie_line = "lineheight";
				var $cookie_backPos = "backgroundposition";

				var $newBackPos = "0 0";
				var $newFontSize = "100%";
				var $newLineHeight = "16px";

				$("#middle").css({ fontSize: $newFontSize, lineHeight: $newLineHeight });

				$(".text-size").css({ backgroundPosition: $newBackPos });

				$.cookie($cookie_name, $newFontSize);
				$.cookie($cookie_line, $newLineHeight);
				$.cookie($cookie_backPos, $newBackPos);

				return false;

			});



			$(".medium").click(function(){

				var $cookie_name = "textsize";
				var $cookie_line = "lineheight";
				var $cookie_backPos = "backgroundposition";
				var $newBackPos = "0 -15px";
				var $newFontSize = "120%";

				var $newLineHeight = "18px";

				$("#middle").css({ fontSize: $newFontSize, lineHeight: $newLineHeight });
				$(".text-size").css({ backgroundPosition: $newBackPos });

				$.cookie($cookie_name, $newFontSize);
				$.cookie($cookie_line, $newLineHeight);
				$.cookie($cookie_backPos, $newBackPos);

				return false;

			});



			$(".big").click(function(){

				var $cookie_name = "textsize";
				var $cookie_line = "lineheight";
				var $cookie_backPos = "backgroundposition";
				var $newBackPos = "0 -30px";
				var $newFontSize = "140%";
				var $newLineHeight = "20px";

				$("#middle").css({ fontSize: $newFontSize, lineHeight: $newLineHeight });
				$(".text-size").css({ backgroundPosition: $newBackPos });
				$.cookie($cookie_name, $newFontSize);
				$.cookie($cookie_line, $newLineHeight);
				$.cookie($cookie_backPos, $newBackPos);

				return false;

			});





			checkCookies = function() {
				var $cookie_name = "textsize";
				var $cookie_line = "lineheight";
				var $cookie_backPos = "backgroundposition";
				var $originalFontSize = $("#middle").css("font-size");

				if($.cookie($cookie_name)){
					var $getSize = $.cookie($cookie_name);
					var $getLine = $.cookie($cookie_line);
					var $getPos = $.cookie($cookie_backPos);
					$("#middle").css({ fontSize: $getSize, lineHeight: $getLine });
					$(".text-size").css({ backgroundPosition: $getPos });
				}

				var $cookie_event = "event";
				if($.cookie($cookie_event)){
					var $getEvent = $.cookie($cookie_event);
					$("#"+$getEvent).parent().next("#events-wide .menu_body").slideToggle(300).siblings("#events-wide .menu_body").slideUp("fast");
				}

			}



			checkCookies();















	// Scroll infinite


			function scroll (e) {
				var ticker		= $(e);
				var ticker_w	= ticker.parent().width ();
				var li_all		= 0;

				i_width = 0;
				ticker.children().filter("li").each(function () {
					var li = $(this);
					containe = $("<div>");
					li.prependTo (containe);
					containe.appendTo (ticker);
					containe.css ("left", i_width);
					i_width += li.width () + parseInt (li.css("margin-left")) + parseInt (li.css("padding-left"));
				});
				scroll_copy_el ();
				li_all = ticker.children().filter("div").size();

				ticker.css ("overflow", "hidden");

				function animator(currentItem) {
					var distance = currentItem.innerWidth();
					duration = (distance + parseInt(currentItem.css("left"))) / 0.05;
					currentItem.animate({ left: -distance }, duration, "linear", function() {
						el = currentItem.parent().children(":last");
						li = el.children().filter("li");
						last_left = parseInt (el.css("left")) + parseInt (li.outerWidth()) + parseInt (li.css("margin-left")) + parseInt (li.css("padding-left"));
						currentItem.appendTo(currentItem.parent()).css("left", last_left);
						animator (currentItem);
					});
				};

				function scroll_copy_el () {
					if (i_width < ticker_w + (ticker_w * 2)) {
						ticker.children().filter("div").each (function () {
							el = $(this).clone();
							el.appendTo (ticker);
							el.css ("left", i_width);
							li = el.children().filter("li");
							i_width += li.width () + parseInt (li.css("margin-left")) + parseInt (li.css("padding-left"));
						})
						scroll_copy_el ();
					}
				}

				for (i=0; i<li_all; i++) {
					animator(ticker.children(":eq(" + i + ")"));
				}

				ticker.mouseenter(function() {
					ticker.children().stop();
				});

				ticker.mouseleave(function() {
					for (i=0; i<li_all; i++) {
						animator(ticker.children(":eq(" + i + ")"));
					}
				});
			}

			scroll("#scroll-bottom .ticker");
			scroll ("#scroll-top .ticker");
















		// Calendar

			function calendar () {
				var el_cal		= $("#calendar");
				var el_events	= el_cal.find ("FORM").attr("action");


				// zmiana okresu

				el_cal.find ("select[name=month], select[name=year]").unbind ("change").change (function () {
					$.ajax({
						url:		"/event.php?year=" + el_cal.find("select[name=year]").val() + "&month=" + el_cal.find("select[name=month]").val() + "/",
						cache:		false,
						success:	function (data){
					        el_cal.html (data);
						calendar();
				    	}
					});
				})


				/*el_cal.find ("#days li").unbind ("click").css("cursor", "pointer").click (function () {
					location.href = "http://" + el_events;
				})*/


				// hover

				$("#calendar #days li").hover(function () {
					$(this).addClass("days-hover");
					}, function () {
					$(this).removeClass("days-hover");
					}
				);


				// click

				$("#calendar #days li a").click(function(){
					$f = $(this).attr("href");
					$e = $(this).attr("id");

					// set new cookies
					var $cookie_event = "event";
						$.cookie($cookie_event, $e);

					location.href = $f;
					//$(".e"+$id).parent().next("#events-wide .menu_body").slideToggle(300).siblings("#events-wide .menu_body").slideUp("fast");
					//$("#e1").parent().next("#events-wide .menu_body").slideToggle(300).siblings("#events-wide .menu_body").slideUp("fast");
					return false;
				});


				// find current date
				var d = new Date();
				var $curr_date = d.getDate();

				$.expr[":"].econtains = function(obj, index, meta, stack){
					return (obj.textContent || obj.innerText || $(obj).text() || "").toLowerCase() == meta[3].toLowerCase();
				}

				$("#days li:econtains"+"('"+$curr_date+"')").css({ backgroundColor: "red", color: "white" });

			}

			calendar ();











		// Remove margin



			if ($("#kategorie").exists()) {
				$("#kategorie ul:last, #kategorie ul li:last-child").css({ border: 'none' });
			}




		// wymagane pola w ankiecie

		$('#ankieta').submit(function(){
			if($('.required').val() == ""){
				$('.required').addClass('incorrect').after('<span class="incorrect-label"> - to pole jest wymagane!</span>');

				return false;
			} else if($('.required').val() != ""){
				$('.required').removeClass('incorrect');
				$('.incorrect-label').fadeOut();
				return true;
			}
		});


});

