$(document).ready(function(){


	// Resize the header logo to match the height of the header nav (if over 120px)
	var headerLogoHeight = $('#header-logo a span').first().outerHeight();
	var headerNavHeight = $('#header nav').first().outerHeight();
	if (headerNavHeight >= 120) {
		$('#header-logo a span').height(headerNavHeight); // Adjust the header logo
		$('#header-title').css('marginTop',headerNavHeight-90); // Adjust the header title (if it exists)
	}


	// Initialize jQuery Uniform
	$("input, textarea, select, button.button").uniform();


	// Work > Index code
	if($('#work-index').length != 0) {
		var pageTitle = document.title; // Store the initial page title, since it will change as the user interacts with the page
		$('head').append('<meta property="og:title" content=""/><meta property="og:type" content="article"/><meta property="og:image" content=""/><meta property="og:url" content=""/><meta property="og:site_name" content="Creo Productions"/>'); // As the page loads, add Facebook meta tags
		$('a.iframe').fancybox({
			centerOnScroll: true,
			height: 355, // 535:355
			overlayColor: '#fcfcfa',
			overlayOpacity: '0.94',
			padding: 0,
			speedIn: 300,
			speedOut: 100,
			width: 630, // 950:630
			// When a Fancybox is opened, we need to store a bunch of vars, insert "share this" code above the video, and add new meta data to the page.
			onComplete: function( links, index ){
				var self = $(links[index]); // Store the currently active video's link
				var title = self.find('.title').first().text(); // Get the video's title
				var urlTitle = self.attr('rel'); // Get the video's URL title
				var image = self.find('img').first().attr('src'); // Get the URL of the video's image placeholder
				var block = $('#fancybox-wrap'); // Find the Fancybox element that we need to add "share this" code to.
				var permalinkCode = '<li class="permalink headline"><a href="http://www.creoproductions.com/work/'+urlTitle+'/">Permalink</a></li>'; // Permanent link to this video
				var tweetButtonCode = '<li class="twitter"><a href="http://twitter.com/share?url=http%3A%2F%2Fcreoproductions.com%2Fwork%2F'+urlTitle+'%2F&amp;counturl=http%3A%2F%2Fcreoproductions.com%2Fwork%2F'+urlTitle+'%2F&amp;via=aaron_creo&amp;text=Watching &quot;'+title+'&quot; on Creo\'s website." class="twitter-share-button" data-count="none" data-via="aaron_creo">Tweet</a></li>'; // Twitter "Tweet" button
				var likeButtonCode = '<li class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fcreoproductions.com%2Fwork%2F'+urlTitle+'%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none;overflow:hidden;width:100px;height:21px;margin-bottom:9px;" allowTransparency="true"></iframe></li>'; // Facebook "Like" button
				document.title = title+' - '+pageTitle; // Prepend the page title with the current video's title
				block.outerHeight(block.outerHeight()+30); // Adjust the Fancybox's height to make room for the new code
				block.css('top',block.position().top-50); // Adjust the Fancybox's vertical position to make room for the new code
				block.prepend('<ul id="share-page">'+permalinkCode+tweetButtonCode+likeButtonCode)+'</ul>'; // Insert the new code
				$.getScript("http://platform.twitter.com/widgets.js"); // Load Twitter's widjet JS file
				// Add a bunch of meta tag data for Facebook
				$('meta[property=og:title]').attr('content',title);
				$('meta[property=og:type]').attr('content','article');
				$('meta[property=og:image]').attr('content',image);
				$('meta[property=og:url]').attr('content','http://www.creoproductions.com/work/'+urlTitle+'/');
				$('meta[property=og:site_name]').attr('content','Creo Productions');
				return false;
			},
			// Return the work page to its default state when the user closes Fancybox
			onClosed: function(){
				document.title = pageTitle; // Revert to the initial page title
				$('#share-page').empty(); // Dump all HTML from the "share this" container
				$('meta[property=og:title],meta[property=og:type],meta[property=og:image],meta[property=og:url],meta[property=og:site_name]').attr('content',''); // Clear out all values for the Facebook meta tags.
				return false;
			}
		});
		// If the Work page has a value in the second URL segment, open that video on page load.
		var workIndexWrapper = $('#work-index'); 
		var isPermalinkPage = workIndexWrapper.hasClass('permalink');
		if(isPermalinkPage){
			var targetRel = workIndexWrapper.attr('rel');
			$('#work-list a[rel='+targetRel+']').trigger('click');
		}
	}


	// Hack pasted content and old Blogger text so that all blog posts use our typographic styles
	$('.content [style]').removeAttr('style');


});
