// Tooltips
	(function($) {
		window.qtipOptions = {
			position: {
				my: 'top left',
				at: 'bottom right',
				target: 'mouse',
				viewport: $(window),
				adjust: {
					mouse: false,
					y: 20,
					x: 5
				}
			},
			style: {
				classes: 'ui-tooltip-dudensing',
				tip: {
					offset: 5,
					width: 12,
					mimic: 'center'
				}
			},
			show: {
				delay: 500
			},
			hide: {
				distance: 10
			}
		};
		
		// Remove titles from edit links
		$('.post-edit-link, .comment-edit-link').removeAttr('title');
		
		window.tips = $('[title^=]');
		
		tips.qtip(qtipOptions);
	})(jQuery);
	
// Transcripts
	(function($) {
		var ownWords   = $('#own-words');
		var toggles    = $('a.show-transcript', ownWords);
		var closers    = $('a.hide-transcript', ownWords);
		var containers = $('.transcript-container', ownWords);
		
		// Hide transcripts
		var close = function(selector) {
			selector.height(0).removeClass('open');
		};
		
		// Open
		toggles.click(function(e) {
			e.preventDefault();
			
			var self       = $(this);
			var container  = self.next();
			var transcript = container.children().first();
			var height     = transcript.outerHeight() + 16;
			
			// Close all containers except this one
			close(containers.not(container));
			
			// Show/hide transcript
			if ( ! container.hasClass('open')) {
				setTimeout(function() {
					container.height(height).addClass('open');
					setTimeout(function() {
						$.scrollTo(self, 300, {over : -10});
					}, 300);
				}, 300);
			} else {
				close(container);
			}
		});
		
		// Close
		closers.click(function(e) {
			e.preventDefault();
			
			var self       = $(this);
			var transcript = self.parent();
			var container  = transcript.parent();
			
			close(container);
		});
	})(jQuery);

// Explore more
	(function($) {
		var more = $('.explore-more .inner');
		var anchor = $('<span/>', {
			'class' : 'anchor',
			click   : function(e) {
				if (more.hasClass('open')) {
					more.removeClass('open');
				} else {
					more.addClass('open');
				}
			}
		}).appendTo(more);
	})(jQuery);
	
// Image panning
/*
	(function($) {
		$('img.pan').each(function(i, image) {
			var image  = $(image),
			    width  = image.data('max-width')  || image.width(),
			    height = image.data('max-height') || image.height();
			
			image.panFullSize(width, height, function() {
				var pan = image.next('.panFullSize');
				
				if (width < image.width() && height == image.height())
					pan.css('cursor', 'w-resize');
				else if (height < image.height() && width == image.width())
					pan.css('cursor', 'n-resize');
			});
		});
	})(jQuery);
*/

// Image panning
	(function($) {
		$('img.pan').each(function(i, img) {
			var img       = $(img),
			    width     = img.width(),
				height    = img.height(),
				maxWidth  = img.data('max-width') || width,
				maxHeight = img.data('max-height') || height,
				container = $('<div class="pan-container"></div>');
			
			img.wrap(container);
			
			container = img.parent();
			
			container.css({
				width  : maxWidth,
				height : maxHeight
			});
			
			var offset   = container.offset(),
			    half     = Math.round(maxWidth / 2),
				distance = Math.round(maxWidth * 0.8),
				maxLeft  = width - maxWidth;
			
			container.click(function(e) {
				var clickX  = e.pageX - offset.left,
				    dir     = clickX > half,
				    left    = Number(img.css('left').replace('px', '')),
					newLeft = dir ? (left - distance) : left + distance;
				
				container.removeClass('beginning end');
				
				if (Math.abs(newLeft) > maxLeft) {
					newLeft = -maxLeft;
					container.addClass('end');
				} else if (newLeft > 0) {
					newLeft = 0;
					container.addClass('beginning');
				}
					
				if (Modernizr.csstransitions) {
					img.css('left', newLeft);
				} else {
					img.animate({left:newLeft}, 800, 'easeOutQuad');
				}
			});
		});
	})(jQuery);
