(function($){
	$('html').addClass('js-on');
	function initDomReady(){
		moodswitch.create();
		teamswitch.create();
	}
	
	
	var moodswitch = {
		create: function(){
			this.wrapper = $('#mood-wrapper');
			this.buttons = $('div.gallery a').bind('click', $.proxy( moodswitch, 'changeImage'));
			this.caption = $('div.gallery p.caption');
			this.currentIndex = 0;
			$(window).bind('load', $.proxy( moodswitch, 'lazyLoad'));
		},
		lazyLoad: function(){
			if( moodswitch.hasImage ){return;}
			var imgs = '';
			this.buttons.not('[href^=#]')
				.each(function(){
					var smallImg = $('img', this);
					imgs += '<img src="'+ this.href +'" alt="'+ smallImg.attr('alt') +'" title="'+ smallImg.attr('title') +'" class="mood" style="display: none;" />';
				})
			;
			moodswitch.hasImage = true;
			moodswitch.wrapper.append( imgs );
		},
		changeImage: function(e){
			var nextIndex = this.buttons.index( e.currentTarget );
			if( nextIndex === this.currentIndex ){return false;}
			this.lazyLoad();
			
			var img = $('img', this.wrapper)
				.filter(':eq('+ nextIndex +')')
				.fadeOver({
					hideElement: $('img', this.wrapper).filter(':eq('+ this.currentIndex +')')
				})
			;
			this.buttons.removeClass('active');
			$(e.currentTarget).addClass('active');
			this.currentIndex = nextIndex;
			this.caption.html( img.attr('title') );
			return false;
		}
	};
	
	var teamswitch = {
		create: function(){
			this.buttons = $('div.team a');
			this.panels = this.buttons.map(function(){
				return $( $(this).attr('href') )[0];
			});
			this.buttons.bind('click', $.proxy(this, 'switchTeam'));
			//initial
			this.buttons.filter(':first').addClass('active');
			this.panels.filter(':first').addClass('active-sheet').show();
		},
		switchTeam: function(e){
			var activeBtn = this.buttons.filter('.active');
			if( activeBtn[0] === e.currentTarget ){return false;}
			var newBtn = $(e.currentTarget).addClass('active');
			activeBtn.removeClass('active');
			 this.panels
			 	.filter( newBtn.attr('href') )
				.fadeOver({
					hideElement: this.panels.filter( activeBtn.attr('href') ).removeClass('active-sheet'),
					animateHeight: 'sync'
				})
				.addClass('active-sheet')
				.find('h2')
				.setFocus()
			;
			return false;
		}
	};
	
	
	$(initDomReady);
	
	
})(jQuery);
