


// Nothing to see here! ;)
if (window.addEventListener) {
	var keys = [], konamiCode = "38,38,40,40,37,39,37,39,66,65";
	window.addEventListener("keydown", function(e){
		keys.push(e.keyCode);
		if (keys.toString().indexOf( konamiCode ) >= 0)
			window.location = "http://www.youtube.com/watch?v=KXWEM4gZhg4";
		}, true);
}



$(document).ready(function(){
	
	// setup slideshows
	$('div.polaroiderize_display').polaroiderize({
		sourceElement: $('#stage'),
		displayTime: 500
	});	

});


/*
	Polaroid slideshow based on http://polaroiderizer.com
*/
(function($) {

	$.fn.polaroiderize = function(options) {
		
		var opt = $.extend({}, $.fn.polaroiderize.defaults, options);
		var position = 0;
		var ready = $("<div class='polaroiderize_ready'></div>").appendTo($(this));
		
		// put any images already in the display, into the stage and stop hiding displayed images.
		$('div.polaroiderize_display a').show().appendTo(ready);

		// copy images (in their anchors) to the ready area
		if(opt.sourceElement) {
			$("a", opt.sourceElement).appendTo(ready);
		}
		$('a', ready).show();
		
		// start a timer to display another image every few seconds.
		displayNext();

		// Iterate throught the images which are downloaded and ready to display.
		function displayNext(){
			var i = $("a", ready).get(position);
			if(i) {
				$(i).transition();
			}
			position++;
			if(position >= $("a", ready).length) {
				position = 0;
			}
			timer = setTimeout(function() { displayNext(); }, 3000);				
		};
	
		return $(this);
	};

	// choose the transition effect based .
	$.fn.transition = function() {
		var effect = $.fn.polaroiderize.defaults.transition;
		var item = $(this).clone();
		if(effect == 'polaroids'){
			item.polaroidScroll();
		}
		return $(this);
	};

	$.fn.polaroidScroll = function() {
		var display = $('div.polaroiderize_display');
		var frame = $('<div class="polaroid"></div>').append(this);
		var photo = $('img', frame);		
		display.append(frame);

		// set starting point
		var x = 40 + Math.floor(Math.random() * (display.width() - frame.width() - 80));
		var y = frame.height();
		frame.show().css({top: '-'+y+'px', left: x+'px'});
			
		// set opacity of photo
		photo.css({opacity: '0'});
			
		// animate photo opacity and into view.
		frame.animate({top:'15px'}, 400);
		photo.animate({opacity: '1'}, 2000, function(picture){
			// animate slowly out of view and opacity of entire object.
			frame.animate({top: display.height()+'px', opacity:'0'}, 5000, function(){
				frame.remove();
			});	
		});
		return this;				
	};

	// defaults.
	$.fn.polaroiderize.defaults = {
		sourceElement: null,
		json: null,
		displayTime: 1000,
		loop: true,
		transition: 'polaroids'
	};

})(jQuery);
