$(document).ready(function() {
	
	/**
	 *
	 * Post Event Overlay
	 *
	 **/
	 
	 // Resize photo
	 $('html').css('overflow', 'hidden');
	 resize();
	 
	 // Hide and show the post event information.
	 $('#postevent_container a.close').click(function(e) {
	 
	 	e.preventDefault();
	 	$('html').css('overflow', 'scroll');
	 
	 	var $container = $(this);
	 	$(this).parent().fadeOut('slow', function() {
	 		$container.hide();
	 		$('#postevent_container').empty();
	 	});
	 }).hover(function() {
	 	$(this).animate({'padding-right': 20}, 'fast');
	 }, function() {
	 	$(this).animate({'padding-right': 10}, 'fast');
	 });
	 
	 // Cycle thru the post event content divs
	 $('#postevent_content').cycle({ 
    	fx:    'fade', 
    	timeout: 6000
	});
	
	/**
	 *
	 * Nav Fixed Dropdown
	 *
	 **/
	var scrollnav = $('<div id="scroll_nav"><div><a href="#container" class="backtotop">Back <em>to</em> Top</a><ul><li><a href="#agenda">Agenda</a></li><li><a href="#attendees">Attendees</a></li></ul><ul><li><a href="#speakers">Speakers</a></li><li><a href="#location">Location</a></li></ul></div></div>');
	
	$(scrollnav).prependTo('#container').hide();
	
	$(window).scroll(function() {
		if ($(document).scrollTop() > 300)
		{
			$('#scroll_nav').slideDown('slow');
		}
		else {
			$('#scroll_nav').slideUp('slow');
		}
	});
	
	
	/**
	 *
	 * Nav Page Scrolling
	 *
	 **/
	$('#nav a, #scroll_nav a').click(function(event) {
		$.scrollTo($(this).attr('href'), 'slow');
		event.preventDefault();
	});
	
	
	/**
	 *
	 * Rollover Background Swapping
	 *
	 **/
	$('#confirm_attendance a, #scroll_nav a.backtotop').hover(function() {
		$(this).parent().addClass('hover');
	}, function() {
		$(this).parent().removeClass('hover');
	});
	
	
	/**
	 *
	 * Footer Slide
	 *
	 **/
	$('#footer_content').hide();
	
	$('#footer_menu').toggle(function() {
		$('#footer_menu p:first-child').fadeOut('fast', function() {
			$(this).text('Click to Hide').fadeIn('fast');
			$(this).next().fadeOut('fast');
		});
		
		$('#footer_content').slideToggle();
	}, function() {
		$('#footer_menu p:first-child').fadeOut('fast', function() {
			$(this).text('Click to Expand').fadeIn('fast');
			$(this).next().fadeIn('fast');
		});
		
		$('#footer_content').slideToggle();
	});
	
	
	/**
	 *
	 * Twitter Feed
	 *
	 **/	
	$.getJSON('http://twitter.com/status/user_timeline/MMGVailSummit.json?count=3&callback=?', function(data) {
	
		$.each(data, function(i, tweet) {
			$('#twitter').append($('<li><p>' + tweet.text + '<br /><img src="/images/dots.png" /></p></li>'));
		});
	
	});
	
	
	/**
	 *
	 * Lightboxes
	 *
	 **/
	 
	// Hide content in p elements with an attribute of title=fullbio
	$('p[title=fullbio]').hide();
	
	$('#speakers li a, #attendees li a').click(function(event) {
		
		// Hide browser scroll bars
		//$('html').css('overflow-y', 'hidden');
		
		// Append background overlay.
		$('<div id="lightbox_overlay"></div>')
			.css('top', $(document).scrollTop())
			.css('opacity', '0')
			.animate({'opacity': '0.8'}, 'slow')
			.appendTo('body');
		
		// Append container.
		$('<div id="lightbox_container"><a class="close"></a></div>')
			.appendTo('body');
		
		$('#lightbox_container a.close').click(function(){
			removeLightbox();
		});
			
		// Append the sibling paragraphs of this link to the lightbox container
		var biotxt = $(this).siblings('p[title=fullbio]').html();
		$(this).siblings('h2, h3').clone().appendTo('#lightbox_container');
		$('#lightbox_container').append($('<p>' + biotxt + '</p>'));
					
		positionLightbox();
		
		// Prevent default link behavior
		event.preventDefault();
		return false;
		
	});
	
	// Reposition container on resize
	$(window).resize(function() {
		positionLightbox();
		resize();
	});
	
	$(document).scroll(function() {
		positionLightbox();
	});
	
});

function positionLightbox() {
			
	var top = ($(window).height() - $('#lightbox_container').height()) / 2;
	var left = ($(window).width() - $('#lightbox_container').width()) / 2;
	
	$('#lightbox_overlay')
		.css({
			'top': 0 + $(document).scrollTop(),
			'left': 0
		});
	
	$('#lightbox_container')
		.css({
			'top': top + $(document).scrollTop(),
			'left': left
		})
		.fadeIn();
		
}
			
function removeLightbox() {
	$('#lightbox_overlay, #lightbox_container')
		.fadeOut('slow', function() {
			// Remove both the overlay bg, and container
			$(this).remove();
			// Show browser scroll bars
			//$('html').css('overflow-y', 'auto'); 
	});
}

function resize() {
	// Define starting width
	var startWidth = 1190;
	var startHeight = 892;
	// Define image ratio
	var ratio = startHeight / startWidth;
	// Gather browser dimensions
	var browserWidth = $(window).width();
	var browserHeight = $(window).height();
	// Resize image to proper ratio
	if ((browserHeight / browserWidth) > ratio)
	{
		$('#postevent_container img.resize').height(browserHeight);
		$('#postevent_container img.resize').width(browserHeight / ratio);	
	} else {
		$('#postevent_container img.resize').width(browserWidth);
		$('#postevent_container img.resize').height(browserWidth * ratio);
	}
	// Center image in window
	// Push image up a bit at small browser height? - zf
	if (browserHeight > 600) {
		$('#postevent_container img.resize').css('left', (browserWidth - $(this).width()) / 2);
		$('#postevent_container img.resize').css('top', ((browserHeight - $(this).height()) / 2) - 50);
	
	} else {
		$('#postevent_container img.resize').css('left', (browserWidth - $(this).width()) / 2);
		$('#postevent_container img.resize').css('top', (browserHeight - $(this).height()) / 2);
	}
	
	if (browserWidth > 1400 && browserHeight < 800) {
		$('#postevent_container img.resize').css('top', ((browserHeight - $(this).height()) / 2) - 300);
	}
	
}
