(function($) {
	$.easing.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;
	};

	$.fn.extend({
		myFadeTo	: function(value, duration, callback) {
			return $(this).animate({ opacity	: value }, duration, callback);
		},
		myFadeIn	: function(duration, callback) {
			return $(this).myFadeTo(1.0, duration, callback);
		},
		myFadeOut	: function(duration, callback) {
			return $(this).myFadeTo(0.0, duration, callback);
		}
	});

	// initialize lazy-loader for gallery
	$("ul.gallery img").lazyload({
		event	: "activate-photography"
	});

	// set active navigation link
	$("nav a[href='" + location.hash + "']").addClass("active");
	$.event.trigger("activate-" + location.hash.substring(1));

	// enable scroller
	$.localScroll({
		target		: '#content',
		duration	: 1000,
		easing		: 'easeInOutQuad',
		stop			: true,
		hash			: true,
		onBefore	: function(ev, element, target) {
			$.event.trigger("activate-" + $(element).attr("id"));

			// scroll content element back to top
			$(element).prop('scrollTop', 0);

			// activate the proper navigation link
			$("nav ul li a")
				.removeClass("active")
				.filter("[href='#" + $(element).attr("id") + "']")
				.addClass("active");
		}
	});

	// logo fun
	$("#logo img")
		// fade logo when page is done loading
		.myFadeIn(1000, function() { $(this).myFadeOut(2000); })
		// fade logo on hover (just for kicks)
		.hover(
			function() { $(this).stop().myFadeIn(1000); },
			function() { $(this).stop().myFadeOut(1000); }
		);

	$("#content ul.gallery li img").click(function() {
		var $img			= $(this);
		var $lightbox = $("<div id='lightbox-mask'></div>" 						+
											"<div id='lightbox-clone'></div>" 					+
											"<div id='lightbox'>" 											+
												"<div id='lightbox-close'></div>" 				+
												"<div id='lightbox-content'>&nbsp;</div>" +
											"</div>"
										).appendTo("body");

		// show highlight
		/*
		$("#lightbox-clone")
			.append(
				$img.clone().css({
					zindex	: 1000,
					position: 'fixed',
					width		: $img.outerWidth(false),
					height	: $img.outerHeight(false),
					clip		: $img.css('clip'),
					top			: $img.offset().top,
					left		: $img.offset().left
				})
			);
		*/

		// get dimensions
		var image 		= new Image();
		image.src			= $img.attr("src");
		image.onload 	= function() {
			// determine ratio
			var ratio	= image.width / image.height;

			// landscape vs portrait?
			if (ratio > 1.0)
			{
				width		= Math.min( $(window).width() * 0.7, image.width);
				height	= width / ratio;
			}
			else
			{
				height	= Math.min( $(window).height() * 0.7, image.height);
				width		= height * ratio;
			}

			// clone image, set scaled width/height, and add it to lightbox content
			$img
				.clone()
				.css({ width : width, height : height })
				.appendTo( $("#lightbox-content").empty() );

			// resize lightbox to image dimensions and fade in
			$("#lightbox")
				.css({
					boxShadow	: '0 0 70px ' + $img.attr("data-color"),
					width			: width,
					height		: height,
					marginTop	: height 	/ -2,
					marginLeft: width 	/ -2
				})
				.animate({
					opacity	: 1
				}, 700);
		};

		// add event handlers to close lightbox
		var close = function() { 
			$lightbox.animate({
				opacity	: 0
			}, 250, function() { 
				$lightbox.remove();
			});
			$("body").unbind("keyup.lightbox");
		};

		$("#lightbox-close, #lightbox-mask").click(close);

		$(window).bind("keyup.lightbox", function(ev) { 
			if (ev.keyCode == 27)
				close();
		});
	});

	// active the konami!
	$(window).konami(function() {
		var $lightbox = $("<div id='lightbox-mask'></div>")
			.css({ 
				backgroundImage	: "url(img/nyannyan.gif)",
				opacity					: 0.8
			})
			.appendTo( $("body") );

		$("<audio src='http://nyanit.com/audio/nyanlooped.mp3' loop='true'/>")
			.appendTo( $("body") )
			.get(0)
			.play();

		$(this).bind("keydown", function(ev) {
			if (ev.keyCode == 27)
			{
				var audio = $("audio")[0];

				audio.volume = 0;
				audio.pause();

				$(this).unbind("keyup", arguments.callee);
				$("audio").remove();
				$("#lightbox-mask").animate({
					opacity	: 0
				}, 500, function() {
					$(this).remove();
				});
			}
		});
	});

})(jQuery);

