/* ========== easing ========================================= */
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
/* ========== end of easing ================================== */

/* ========== head animations ================================ */
(function(jQuery) {

	var defaults = {
		eyesSelector: '.eyes',
		eyelidsTopSelector: '.eyelids-top',
		eyelidsBottomSelector: '.eyelids-bottom',
		normalClass: 'normal',
		sadClass: 'sad',
		angryClass: 'angry',
		deafultDuration: 200
		};
	var $face, $eyes, $eyelidsTop, $eyelidsBottom;
	var eyelidsTopPositionTop, eyelidsBottomPositionTop, eyelidsDistance;
	var s; // settings

	// helper functions
	function eyelidsDefaultPosition() {
		$eyelidsTop.stop().css({top: eyelidsTopPositionTop});
		$eyelidsBottom.stop().css({top: eyelidsBottomPositionTop});
	}
	function eyelidsNormal() {
		eyelidsDefaultPosition();
		$face
			.removeClass(s.sadClass + ' ' + s.angryClass)
			.addClass(s.normalClass);
	}
	function eyelidsSad() {
		eyelidsDefaultPosition();
		$face
			.removeClass(s.normalClass + ' ' + s.angryClass)
			.addClass(s.sadClass);
	}
	function eyelidsAngry() {
		eyelidsDefaultPosition();
		$face
			.removeClass(s.sadClass + ' ' + s.normalClass)
			.addClass(s.angryClass);
	}

	brscFace = jQuery.fn.brscFace = jQuery.brscFace = function(o) {
		$face = $this = this;

		if ($this.selector && !$this.length) {
			return $this;
		}

		if (!$this.length || $this.selector === undefined) { // detects $.brscFace() and $.fn.brscFace()
			$this = $('<a/>');
		}

		s = jQuery.extend(defaults, o || {});

		$eyes = $(s.eyesSelector, $this);
		$eyelidsTop = $(s.eyelidsTopSelector, $this);
		$eyelidsBottom = $(s.eyelidsBottomSelector, $this);
		eyelidsTopPositionTop = $eyelidsTop.position().top;
		eyelidsBottomPositionTop = $eyelidsBottom.position().top;
		eyelidsDistance = eyelidsBottomPositionTop - eyelidsTopPositionTop - $eyelidsTop.height();

		return $this;
	}

	brscFace.blink = function(times, duration) {

		if (duration === undefined) duration = s.deafultDuration/2;
		if (times === undefined) times = 1;

		eyelidsNormal();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2}, duration,
			function() {
				$(this).animate({top: eyelidsTopPositionTop}, duration);
			}
		);
		$eyelidsBottom.animate({top: eyelidsBottomPositionTop - eyelidsDistance/2}, duration,
			function() {
				$(this).animate({top: eyelidsBottomPositionTop}, duration);
			}
		);
		if (times > 1) setTimeout("brscFace.blink(" + duration + ", " + (times - 1) + ")", 3 * duration);
	}

	brscFace.normal = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		$eyelidsTop.animate({top: eyelidsTopPositionTop}, duration);
		$eyelidsBottom.animate({top: eyelidsBottomPositionTop}, duration,
			function(){
				eyelidsNormal();
			}
		);
	}
	brscFace.handsome = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsNormal();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/3 + 1}, duration);
	}
	brscFace.smiling = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsNormal();
		$eyelidsBottom.animate({top: eyelidsBottomPositionTop - eyelidsDistance/3 - 1}, duration);
	}
	brscFace.clever = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsNormal();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/3 + 1}, duration);
		$eyelidsBottom.animate({top: eyelidsBottomPositionTop - eyelidsDistance/3 - 1}, duration);
	}
	brscFace.dizzy = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsSad();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2}, duration);
	}
	brscFace.sleepy = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsSad();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2 + 4}, duration);
	}
	brscFace.sad = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsSad();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2}, duration);
		$eyelidsBottom.animate({top: eyelidsBottomPositionTop - eyelidsDistance/3 - 1}, duration);
	}
	brscFace.bad = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsAngry();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2}, duration);
	}
	brscFace.evil = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsAngry();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2 + 2}, duration);
		$eyelidsBottom.animate({top: eyelidsBottomPositionTop - eyelidsDistance/3 - 1}, duration);
	}
	brscFace.satanic = function(duration) {
		if (duration === undefined) duration = s.deafultDuration;
		eyelidsAngry();
		$eyelidsTop.animate({top: eyelidsTopPositionTop + eyelidsDistance/2 + 6}, duration);
	}
	brscFace.random = function(duration) {
		var functions = ['handsome',
						'smiling',
						'clever',
						'dizzy',
						'sleepy',
						'sad',
						'bad',
						'evil',
						'satanic'];
		var randomnumber = Math.floor(Math.random() * functions.length);
		window["$"]["fn"]["brscFace"][functions[randomnumber]](duration);
	};

}(jQuery));

/* ========== end of head animations ========================= */

/* ========== main =========================================== */
$(document).ready(function() {

	// remember initial menu position
	var menuPosition = $("#menu").position().top;
	var menuHidden = true;

	function hideMenu() {
		menuHidden = true;
		$("#menu").animate({top: menuPosition}, 500,
			function() {
				$("#head").animate({right: -5}, 500);
			}
		).find("ul > li > a > img.image").css({display: 'none'});
	}

	function showMenu() {
		$("#menu").animate({top: 0}, 1000, 'easeOutBounce',
			function() {
				$("ul > li > a > img.image", this).css({display: 'block'});
				menuHidden = false;
			}
		);
		$("#head").stop().animate({right: -80}, 300);
	}

	// hide menu illustrations
	$("#menu > ul > li > a > img.image").css({opacity: 0});

// BIND

	$("body").click(
		function(e) {
			if ((e.target.id != 'menu-trigger') && (!menuHidden)) {
				hideMenu();
			}
		}
	);

	// show menu illustrations on hover
	$("#menu > ul > li > a").hover(
		function() {
			$("img.image", this).stop().animate({opacity: 1}, 1000);
			$.fn.brscFace.random();
		},
		function() {
			$("img.image", this).stop().animate({opacity: 0}, 500);
			$.fn.brscFace.normal();
		}
	);

	// hide menu
	$("#menu-hider").click(
		function() {
			hideMenu();
		}
	).hover(
		function() {$.fn.brscFace.clever()},
		function() {$.fn.brscFace.normal()}
	);

	// show menu
	$("#menu-trigger").click(
		function() {
			showMenu();
		}
	).hover(
		function() {$.fn.brscFace.evil()},
		function() {$.fn.brscFace.normal()}
	);

	$("#head").brscFace({
			eyesSelector: '#eyes',
			eyelidsTopSelector: '#eyelids-top',
			eyelidsBottomSelector: '#eyelids-bottom'
			})
		.hover(
			function() {$.fn.brscFace.bad()},
			function() {$.fn.brscFace.normal()}
		)
		.click(
			function() {$.fn.brscFace.satanic(300)}
		)
		.animate({right: -108}, 1000,
			function() {
				$.fn.brscFace.blink(2);
				$(this).animate({right: -108}, 2000,
					function() {
						$(this).animate({right: -5}, 800);
					}
				);
			}
		);

	$("#header #logo").hover(
		function() {$.fn.brscFace.handsome()},
		function() {$.fn.brscFace.normal()}
	);
	$("#header #title").hover(
		function() {$.fn.brscFace.handsome()},
		function() {$.fn.brscFace.normal()}
	);
	$("#footer #author").hover(
		function() {$.fn.brscFace.smiling()},
		function() {$.fn.brscFace.normal()}
	);

	// gallery
	$("#main .container .gallery .img a").hover(
		function() {
			$(this).parents(".img").find("a img, .comment, .title").addClass('comment-hover img-hover title-hover');
		},
		function() {
			$(this).parents(".img").find("a img, .comment, .title").removeClass('comment-hover img-hover title-hover');
		}
	);

	$("a[rel='gallery']").colorbox({
		transition: "elastic",
		speed: 350,
		opacity: 0.3,
		maxWidth: "90%",
		initialHeight: "240",
		initialWidth: "320",
		slideshow: true,
		slideshowAuto: false,
		slideshowSpeed: 4000,
		slideshowStart: 'uruchom pokaz slajdów',
		slideshowStop: 'zatrzymaj pokaz slajdów',
		current: '{current}/{total}',
		previous: 'poprzedne',
		next: 'następne',
		close: 'zamknij'
	});

	// guestbook form actions
	var $guestbook = $("div.cms-guestbook");
	if ($guestbook.length > 0) {
		if ($("p.error", $guestbook).length == 0) {
			$("div.cms-guestbook-div-insert > form", $guestbook).hide(0).prev().html('Kliknij tutaj aby dodać wpis').css({
				cursor: 'pointer'
			}).click(
				function() {
					if ($(this).html() == 'Kliknij tutaj aby dodać wpis') {
						$(this).html('Wypowiedz się:').next().slideDown();
					}
					else {
						$(this).html('Kliknij tutaj aby dodać wpis').next().slideUp();
					}
				}
			);
		}
		$("img.cms-guestbook-insert-senderavatar", $guestbook).click(
			function() {
				$(this).next("#avatar-chooser").show();
			}
		);
		$("#avatar-chooser img", $guestbook).click(
			function() {
				$("img.cms-guestbook-insert-senderavatar", $guestbook).attr('src', $(this).attr('src'));
				$(this).addClass('active');
				$(this).siblings().removeClass('active');
				$("form #guest-homepage", $guestbook).attr('value', $(this).attr('src'));
				$("#avatar-chooser", $guestbook).hide();
			}
		);
	}

	// comments form actions
	var $comments = $("div.comments");
	if ($comments.length > 0) {
		$("div.comments-div-insert > form", $comments).hide(0).prev().html('Kliknij tutaj aby dodać komentarz').css({
			cursor: 'pointer'
		}).click(
			function() {
				if ($(this).html() == 'Kliknij tutaj aby dodać komentarz') {
					$(this).html('Wypowiedz się:').next().slideDown();
				}
				else {
					$(this).html('Kliknij tutaj aby dodać komentarz').next().slideUp();
				}
			}
		);

		$input = $("form #comments-website-hidden input", $comments);
		$("form #comments-website-hidden").append('<input type="hidden" value="' + $("img.comments-insert-senderavatar", $comments).attr('src') + '" name="' + $input.attr('name') + '">');
		$input.remove();

		$("img.comments-insert-senderavatar", $comments).click(
			function() {
				$(this).next("#avatar-chooser").show();
			}
		);
		$("#avatar-chooser img", $comments).click(
			function() {
				$("img.comments-insert-senderavatar", $comments).attr('src', $(this).attr('src'));
				$(this).addClass('active');
				$(this).siblings().removeClass('active');
				$("form #comments-website-hidden input", $comments).attr('value', $(this).attr('src'));
				$("#avatar-chooser", $comments).hide();
			}
		);
	}


});
/* ========================================================== */