function incrementGallery() {
	
	var nav = $('.gallery nav ol'),
		next = nav.children('li.current').next();
	
	if(next.length === 0) {
		next = nav.children('li:first');
	}
	
	next.children('a').click();
	
	galleryTimer = setTimeout(function() { incrementGallery(); }, 5000);
	
}

function setWrapDimensions(wrap) {
	wrap.css('width', $('.content .container').width());
	$(window).load(function() {
		wrap.css('width', $('.content .container').width());
	});
}

function bindAlerts() {
	
	$('[data-confirm]').each(function() {
		
		var self = $(this),
			message = self.attr('data-confirm');
		
		if(typeof self.attr('data-click-binded') === 'undefined') {
			
			self.attr('data-click-binded', true);
			
			var form = self.parents('form');
			
			if(self.is('button') && form.length > 0) {
				form.submit(function() {
					return confirm(message);
				});
				return;
			}
			
			self.click(function() {
				return confirm(message);
			});
			
		}
		
	});
	
}

function resetSidebarHeight(animate) {
	
	var sidebar = $('.sidebar'),
		options = sidebar.children('.user-options'),
		offset = parseInt(options.data('paddingTop'), 10) + sidebar.children('.tab-box.current').outerHeight(true);
	
	if(animate) {
		options.animate({paddingTop: offset}, 500, 'easeInOutQuart');
	}
	else {
		options.css('padding-top', offset);
	}
	
}

$(function() {
	$('html').removeClass('no_js').addClass('js');
	
	$.ajaxSetup({'cache':		false,
				 'type':		'POST',
				 'dataType':	'json'});
	
	bindAlerts();
	
	
	// FALLBACK FOR PLACEHOLDER ON INPUT BOXES
	
	if(!('placeholder' in document.createElement('input'))) {
		
		$('input[placeholder]').each(function() {
			$(this).inputHint($(this).attr('placeholder'));
		});
		
	}
	
	// TAB BOXES
	
	$('.tab-box:first').addClass('current');
	
	$(window).load(function() {
		
		$('.sidebar .user-options').each(function() {
			$(this).data('paddingTop', $(this).css('padding-top').replace('px', ''));
		});
		
		resetSidebarHeight(false);
		
	}).resize(function() {
		resetSidebarHeight(false);
	});
	
	
	// SWAP BLOCKS
	
	if(document.location.hash) {
		
		var block = $(document.location.hash + '.block');
		
		if(block.length) {
			$('.block').removeClass('selected');
			block.addClass('selected');
		}
		
	}
	
	$('a.swap-block').click(function() {
		
		var self = $(this),
			block = self.parents('.block');
		
		block.slideUp(200, function() {
			block.removeClass('selected');
		});
		
		$(self.attr('href')).slideDown(200, function() {
			$(this).addClass('selected');
		});
		
		return false;
		
	});
	
	$('a[href^=#]').click(function() {
		
		var anchor = $(this).attr('href'),
			block = $(anchor + '.block');
		
		if(block.length > 0) {
			$('.block').removeClass('selected');
			block.addClass('selected');
		}
		
	});

	// HOME GALLERY
	
	var gallery = $('.gallery');
	
	if(gallery.length > 0) {
		
		gallery.find('nav li:first').addClass('current');
		
		gallery.find('nav a').click(function() {
			
			var self = $(this),
				slider = self.parents('nav').siblings('ol.images'),
				li = self.parent('li'),
				offset = '-' + 100 * li.index() + '%';
			
			if(slider.is(':animated')) {
				return false;
			}
			
			slider.animate({left: offset}, 1500, 'easeInOutQuart'); // easeOutBack
			
			li.addClass('current').siblings().removeClass('current');
			
			clearTimeout(galleryTimer);
			
			return false;
			
		});
		
		galleryTimer = setTimeout(function() { incrementGallery(); }, 5000);
		
		var wrap = $('.home .gallery, .home .gallery .images li');
		
		setWrapDimensions(wrap);
		
		$(window).resize(function() {
		   setWrapDimensions(wrap);
		});
		
	}
	
});
