/* RESEN */
/* BuchananPhoto.com Javascript
----------------------------------------------------------------------------- */


	// Ready
	$(document).ready(function() {

		homePage();	
		cufon();
		bookmark();
		
		backgroundScale()

	});
	
	// Loaded
	$(window).load(function() {

		image();	
		homePage();
		topOfPage();
		
	});


/* FUNCTION: CUFON
----------------------------------------------------------------------------- */


	function cufon() {
	
		var replace = "h2, #latest h4";
		Cufon.replace(replace, { hover: true });
		$(replace).css('visibility', 'visible');
	
	}


/* FUNCTION: READ BOOKMARK
----------------------------------------------------------------------------- */


	function bookmark() {
	
		if (window.location.hash && window.location.hash != "#none") {
			
			var find = "/_photos/" + set + "/" + window.location.hash.replace('#', '');
										
			$('ul.photos').find('a[href=' + find + ']').each(function() {
			
				var orientation = $(this).parent().attr('class');
				var image = $(this).attr('href');
				var title = $(this).attr('title');
				
				switchImage(image, title, orientation);
				
			});
		
		}
	
	}


/* DISPLAY IMAGE
----------------------------------------------------------------------------- */


	function image() {
	
		$('a[rel=lightbox]').click(function() {
		
		
			// Open
			
			var orientation = $(this).parent().attr('class');
			var image = $(this).attr('href');
			var title = $(this).attr('title');
	
			$('#stage').html('<div id="horizon"><div class="' + orientation + '"><img src="' + image + '" alt="' + title + '" /><ul><li>' + title + '</li><li><a href="mailto:bryan@resenmedia.com?Subject=Media Use&body=Photo: ' + title + ' \n\rfrom set: ' + set + '">Media Use</a></li></ul></div></div><a class="nav prev" href="#">Previous</a><a class="nav next" href="#">Next</a>');
			$('#stage').show();
			var hash = image.split('/').pop();
			window.location.hash = hash;


			// Hide Navigation Buttons
			
			var $current = $('ul.photos').find('a[href="' + $('#stage img').attr('src') + '"]').parent();
			if ($current.next().length == 0) $('#stage a.next').addClass('hide');
			if ($current.prev().length == 0) $('#stage a.prev').addClass('hide');

			
			// Navigation
			
			lightboxNav();
		
			
			return false;
			
		});
	
	}
	
	
/* FUNCTION: LIGHTBOX
----------------------------------------------------------------------------- */

	
	function lightboxNav() {
	
	
		// Keys
		
		$('body').unbind('keydown');
		$('body').keydown(function(e) {
		
			if (e.which == 37 || e.which == 39) {
		
				var $current = $('ul.photos').find('a[href="' + $('#stage img').attr('src') + '"]').parent();
	
				if (e.which == 37) {
				
					if ($current.prev().length > 0) {
					
						var $new = $current.prev();
						$('#stage a.prev').addClass('hover');
						
					} else {
					
						return false;
					
					}
	
				} else if (e.which == 39) {
			
					if ($current.next().length > 0) {
	
						var $new = $current.next();
						$('#stage a.next').addClass('hover');

					} else {
					
						return false;
	
					}
				
				}
	
				var orientation = $new.attr('class');
				var image = $new.children('a').attr('href');
				var title = $new.children('a').attr('title');
				
				switchImage(image, title, orientation);
				
				return false;
				
			}
			
		});
	
		$('body').keyup(function(e) { $('#stage a.nav').removeClass('hover'); });
		
		// Buttons
		
		$('#stage a.nav').click(function() {
			

			// Get Image
			
			var $current = $('ul.photos').find('a[href="' + $('#stage img').attr('src') + '"]').parent();
			var $new = ($(this).hasClass('prev')? $current.prev() : $current.next() );
			
			if ($new.length == 0) return false;
			
			var orientation = $new.attr('class');
			var image = $new.children('a').attr('href');
			var title = $new.children('a').attr('title');
			
			switchImage(image, title, orientation);
			
			return false;
		
		});
		
		
		// Close
		
		$('#stage, #stage img').click(function() { 

			$('#stage').hide();
			window.location.hash = "none";
	
		});
			
	
	}
	
	
/* FUNCTION: SWITCH IMAGE
----------------------------------------------------------------------------- */


	function switchImage(image, title, orientation) {
	

			// Show Image
			
			$('#stage').html('<div id="horizon"><div class="' + orientation + '"><img src="' + image + '" alt="' + title + '" /><ul><li>' + title + '</li><li><a href="mailto:bryan@resenmedia.com?Subject=Media Use&body=Photo: ' + title + ' \n\rfrom set: ' + set + '">Media Use</a></li></ul></div></div><a class="nav prev" href="#">Previous</a><a class="nav next" href="#">Next</a>').show();
			var hash = image.split('/').pop();
			window.location.hash = hash;


			// Show/Hide Buttons
			
			var $new = $('ul.photos').find('a[href="' + $('#stage img').attr('src') + '"]').parent();

			if ($new.next().length == 0) {
			
				$('#stage a.next').addClass('hide');
				
			} else {
			
				$('#stage a.next').removeClass('hide');

			}
			
			if ($new.prev().length == 0) {
			
				$('#stage a.prev').addClass('hide');
			
			} else {
			
				$('#stage a.prev').removeClass('hide');

			}
			
			
			// Reassign Actions
			
 			lightboxNav();
				
	}


/* FUNCTION: HOME PAGE FUNCTIONS
----------------------------------------------------------------------------- */


	function homePage() {
	
		if ($('.section-home').length > 0) {
	
	
/* Background Sizing */


			backgroundScale();
			$(window).resize(backgroundScale);

	
/* Animations */	
	
	
			$('.section-home #boss').fadeIn(1500);
			var t = setTimeout(function() {
			
				$('#latest').each(function() {
				
					$(this).animate({
					
						bottom: '45px',
						opacity: 1
					
					}, 500);
					
				
				});
			
			}, 1500);
			
			
/* Latest Shoots */	


			$('#latest li').hover(function() {
			
				$(this).addClass('hover');
			
			}, function() {
			
				$(this).removeClass('hover');
			
			});
			
			$('#latest li').click(function() {
			
				window.location = $(this).find('a.image').attr('href');
				return false;
			
			});

		
		}
		
	}
	
	
/* FUNCTION: RESIZE BACKGROUND
----------------------------------------------------------------------------- */


	function backgroundScale() {
	
		
		// Get Original Dimensions
		
		var windowWidth = parseInt($(window).width());
		var windowHeight = parseInt($(window).height());
		
		var imageWidth = parseInt($('#boss img:first').width());
		var imageHeight = parseInt($('#boss img:first').height());
		

		// Calculate New Dimensions
		
		if (imageHeight != 0 && imageWidth != 0) {
		
			var proportion = imageHeight / imageWidth;	
			var imageHeight = windowWidth * proportion;
		
			if (imageHeight < windowHeight) {
			
				var imageHeight = windowHeight;
				var imageWidth = windowHeight / proportion;
				
			} else {
			
				var imageWidth = windowWidth;
	
			}
	
			var top = (windowHeight - imageHeight) / 2;
			var left = (windowWidth - imageWidth) / 2;
			
			
			// Assign Dimensions
				
			$('#boss img').css('width', imageWidth).css('height', imageHeight).css('top', top).css('left', left);
		
		}
		
	}


/* FUNCTION: GO TO TOP OF PAGE
----------------------------------------------------------------------------- */


	function topOfPage() {
	
		$(window).scroll(function() {
		 
		 	if ($('a.topofpage').length == 0) $('body').append('<a href="#top" class="topofpage">Top</a>');
		 	if ($(window).scrollTop() == 0) {
		 	
		 		$('a.topofpage').remove();
		 	
		 	}
		 	
			$('a.topofpage').click(function() {
			 
				// Scroll to Top
				window.scrollTo(0, 0);
				
				// Remove
				$(this).remove();
				
				return false;
			 
			});
		 
		});
	
	}
