// 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);
}





var theteam = theteam || {};
theteam.heroTimeout = 8; // in seconds to auto rotate hero/homepage carousel

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

	// png fixes
	DD_roundies.addRule('div.notch_narrow');
	// DD_roundies.addRule('div.work-thumbs div.work-thumb div');

	// homepage elements
	if ($('body').hasClass('homepage')) {
		theteam.initHeroCarousel();
	    theteam.initNewsTicker();
	}
});

theteam.initNewsTicker = function() {
	$('ul#newsreel li').css('display','block');

	$('#newsreel').newsTicker({
	    newsList: "#newsreel",
        startDelay: 1000,
        placeHolder1: " ",
		tickerRate: 50,
		stopOnHover: false
    });
};

theteam.initHeroCarousel = function() {
	theteam.$hero = $('ul#hero-carousel').
			wrap('<div id="hero-carousel-container" />').
			css('position', 'absolute');

	theteam.$hero_container = theteam.$hero.parent('#hero-carousel-container').
			append('<img src="/images/notch_single_alpha.png" class="notch">').
			append('<div class="hero-carousel-nav" />');

	theteam.$hero_nav = theteam.$hero_container.find('div.hero-carousel-nav').
			append('<a href="#" class="carousel-back">Previous feature</a><div class="status"></div><a href="#" class="carousel-next">Next feature</a>').
			animate({'top': '0px'}, 1000);

	theteam.$hero_nav.find('a.carousel-next').bind('click', theteam.nextHeroSlide);
	theteam.$hero_nav.find('a.carousel-back').bind('click', theteam.prevHeroSlide);

	theteam.$hero_images = theteam.$hero_container.find('li');
	theteam.$hero.width(theteam.$hero_images.length * theteam.$hero_container.width());

	DD_roundies.addRule('img.notch');
	DD_roundies.addRule('div.progress span');
	DD_roundies.addRule('div.hero-carousel-nav a');

	theteam.setHeroSlide(1);
};

theteam.setHeroSlide = function(pg) {
	// $('div.progress', theteam.$hero_nav).css({'backgroundPosition':'-45px 0px'});
	$('div.status', theteam.$hero_nav).text(pg + '/' + theteam.$hero_images.length);

	theteam.$hero_nav.find('a').addClass('active');

	var position = -(pg * theteam.$hero_container.width()) + theteam.$hero_container.width();
	theteam.$hero.stop().animate({'left':position + 'px'}, 900, "swing", function(){
		theteam.heroCurrent = pg;
		theteam.setHeroTimeout();
	});
};

theteam.nextHeroSlide = function(ev) {
	if ($(ev.target).is('.active')) {
		theteam.heroCurrent = (theteam.heroCurrent >= theteam.$hero_images.length) ? 1 : theteam.heroCurrent + 1;
		theteam.setHeroSlide(theteam.heroCurrent);
	}

	return false;
};

theteam.prevHeroSlide = function(ev) {
	if ($(ev.target).is('.active')) {
		theteam.heroCurrent = (theteam.heroCurrent == 1) ? theteam.$hero_images.length : theteam.heroCurrent - 1;
		theteam.setHeroSlide(theteam.heroCurrent);
	}

	return false;
};

theteam.setHeroTimeout = function() {
	theteam.$hero_nav.find('div.status').stop().css({'backgroundPosition':'-45px 0px'}).animate({'backgroundPosition': '0px 0px'}, theteam.heroTimeout * 1000, "linear", function() {
		theteam.nextHeroSlide({target: theteam.$hero_nav.find('a.carousel-next')});
	});
};


/*
	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);

