/* 22Slides */
/* Javascript Functions
----------------------------------------------------------------------------- */


	var rsn = new Object();

	
/* Browser Detector
----------------------------------------------------------------------------- */


	rsn.browserDetect = function() {
	
		// Desktop
		if ($.browser.mozilla) var browser = "mozilla";
		if ($.browser.webkit) var browser = "webkit";
		if ($.browser.msie) var browser = "msie";
		$('body').addClass(browser);

		// iOS
		if (navigator.userAgent.match(/like Mac OS X/i)) $('body').addClass('ios');
				
		// Disallow layouts that don't work properly
		if ($.browser.msie && $.browser.version < 8 && $('body.left').length > 0) {
		
			if ($('.sidescroll').length > 0) {
			
				console.log('found sidescroll');
				$('.sidescroll').addClass('list').removeClass('sidescroll');
				
			}
			
		}

	};
	
	
/* iOS Exceptions
----------------------------------------------------------------------------- */
	
	
	rsn.ios = function() {
	
		if ($('body').hasClass('ios') && $('.sidescroll').length > 0) {
		
			// Make header not have fixed position for ios
			$('body').addClass('static');
		
		}
	
	};
	
	
/* Email
----------------------------------------------------------------------------- */

	
	rsn.email = function() {
	
		$('a[rel="mail"]').each(function() {
		
			var email = $(this).attr('href').split('#');
			var email = email[0] + "@" + email[1] + "." + email[2];
			if ($(this).attr('href') == $(this).text()) $(this).text(email);
			$(this).attr('href', 'mailto:' + email);
					
		});
	
	};
	
	
/* External Links
----------------------------------------------------------------------------- */

	
	rsn.links = function() {
	
		$('.body a').each(function() {
		
			var href = $(this).attr('href');
			var domain = document.domain;
			
			if (!href.match(domain) && !href.match(/^\//)) {
			
				$(this).attr('target', '_blank').attr('rel', 'external');
			
			}
					
		});
	
	};	
	
	
/* Home Page Slideshow
----------------------------------------------------------------------------- */


	rsn.homePage = function() {
	
		// Detect Full Bleed
		if ($('body.bleed').length > 0) var bleed = true;
		
		// Detect IE
		if ($.browser.msie) var ie = true;

		if (bleed) {

			// Resize First Image		
			rsn.resizeBackgroundImage('#billboard > img:first-child');
			
		} else if ($('#billboard > img').length > 1) {
		
			// Set Slideshow Height
			var height = parseInt($('#billboard > img:first-child').height());
			$('#billboard').css('height', height + 'px');
					
		}
		
		// Show First Image
		if (!ie) {
		
			$('#billboard > img:first-child').addClass('show');
		
		} else {

			$('#billboard > img:first-child').animate({'opacity': 1}, 1000, function() {
			
				$('#billboard > img:first-child').addClass('show');

			});		
		
		}
	
		// Start Slideshow
		var slideCount = $('#billboard > img:not(.blocker)').length;
		if (slideCount > 1) {
		
			var slideshow = setInterval(function() {
						
				// Get Last and Next Slides
				var $last = $('#billboard .show');
				
				if ($last.next().length > 0 && !$last.next().hasClass('blocker')) {
				
					var $next = $last.next();
	
				} else {
				
					var $next = $('#billboard > img:first-child');
	
				}
				
				// Resize Next Image
				if (bleed) rsn.resizeBackgroundImage($next);
				
				// Show Next Image
				if (!ie) {

					$next.addClass('show');				
					
				} else {
							
					$next.animate({'opacity': 1}, 1000, function() {
					
						$next.addClass('show');				

					});

				}
				
				// Hide Last Image
				setTimeout(function () {
				
					if (!ie) {

						$last.removeClass('show');
						
					} else {
					
						$last.animate({'opacity': 0}, 1000, function() {
						
							$last.removeClass('show');
						
						});
					
					}

				}, 1000);
			
			}, 4500);
		
		}
	
	};
	
	
/* Full-Bleed Background
----------------------------------------------------------------------------- */


	rsn.resizeBackgroundImage = function(image) {
		
		$(image).each(function() {
	
			// Get Dimensions
			var width = parseInt($(this).width());
			var height = parseInt($(this).height());
			var windowWidth = parseInt($(window).width());
			var windowHeight = parseInt($(window).height());
			
			// Get Aspect Ratio
			var aspectRatio = width / height;
	
			// Define New Sizes
			var newWidth = windowWidth;
			var newHeight = newWidth / aspectRatio;
			
			if (newHeight < windowHeight) {
			
				var newHeight = windowHeight;
				var newWidth = newHeight * aspectRatio
			
			}
			
			// Move to Center Top
			var newLeft = (windowWidth - newWidth) / 2;
			
			// Assign Dimensions
			$(this).css({
			
				'width': newWidth + 'px',
				'height': newHeight + 'px',
				'left': newLeft + 'px'
				
			});
			
		});
	
	};
	
	rsn.adjustFooter = function() {

		// Get Dimensions
		var width = parseInt($('#content').width());
		var outerWidth = parseInt($('#content').outerWidth());
		var padding = parseInt((outerWidth - width) / 2);
		var marginLeft = outerWidth / -2;

		// Reposition Footer
		$('.bleed footer').css({
		
			'width': width + 'px',
			'margin-left': marginLeft + 'px',
			'padding-left': padding + 'px',
			'padding-right': padding + 'px'
			
		});
		
		// Show Footer
		$('.bleed footer').addClass('show');
	
	};
	
	
/* Grid
----------------------------------------------------------------------------- */
	
	
	rsn.grid = function() {
	
		// Make grid into a square
		if ($('body:not(.left) div.grid li, div.archive:not(.list) li').length > 0) {
		
			var size = $('div.thumbs li:first-child').width();
			$('div.thumbs li > a').css('height', size);
	
		}
				
		// Identify portrait-oriented photos
		$('#content .grid img, #content .archive img').each(function() {

			var $this = $(this);
			var image = $(this).attr('src');
			var imgLoader = new Image();				
			imgLoader.onload = function(data) {

				if (imgLoader.height > imgLoader.width) $this.addClass('portrait');
				$this.addClass('show');
							
			}
	
			// Has to be after onload function for IE		
			imgLoader.src = image;
		
		});
	
	};
	
	
/* Carousel
----------------------------------------------------------------------------- */
	
	
	rsn.carousel = function() {
	
	
/* Size Adjust */


		var contentWidth = parseInt($('#content').width());		
		var carouselHeight = Math.round((4 * contentWidth) / 6);
		$('.carousel .images').css('height', carouselHeight + 'px');
		$('.carousel .images li').css('line-height', carouselHeight + 'px');
		
	
/* Move Thumbnails Below Stage */


		$('.thumbs').insertAfter('.images').addClass('opaque');


/* Slideshow */

		
		if ($('.images > li').length > 1) {

			$('.images').after('<a class="paging back" href="#back">Back</a><a class="paging next" href="#next">Next</a>').cycle({
			
				fx: 'scrollHorz',
				next: 'a.next',
				prev: 'a.back',
				speed: 400,
				timeout: 0,
				pager: '.thumbs',
				activePagerClass: 'selected',
				pagerAnchorBuilder: function(id, slide) {
				
					// Return 
					var thumb = $(slide).find('img').attr('src').replace('image_', 'thumb_');
					return $('.thumbs img[src="' + thumb + '"]').closest('li');				
				
				}, before: function() {
				
					// Stop thumbnail animation in progress
					$('.thumbs').stop();
				
					// Scroll thumbnails
					setTimeout(function() {
					
						var position = $('.thumbs .selected').position();
										
						if (position) {
						
							var scroll = $('.thumbs').scrollLeft();
							var thumbWidth = $('.thumbs .selected').width();
							var windowWidth = $('.thumbs').width();
							var left = (position.left + scroll) - (windowWidth / 2) + thumbWidth / 2;
							
							$('.thumbs').animate({ scrollLeft: left }, 1000);
						
						}
					
					}, 10);
				
				}
		
			});
			
		} else {
		
			$('.thumbs').hide();
		
		}
		
		// Show Images
		$('.images').addClass('opaque');
		
		
/* Hover */


		$('.carousel').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
		
		
/* Paging */


		$('body').keydown(function(e) {

			if (e.which == 37) {
			
				$('a.back').click();
				return false;

			} else if (e.which == 39) {

				$('a.next').click();
				return false;

			}
		
		});

	};
	
	
/* Side Scroll Layout
----------------------------------------------------------------------------- */
	
	
/* Layout Sizing */

	
	rsn.resizeLayout = function() {
		
		var windowHeight = ($.browser.msie ? document.documentElement.clientHeight : window.innerHeight);
		var windowWidth = ($.browser.msie ? document.body.clientWidth : window.innerWidth);
		var sidebarWidth = $('header').width();
		var lineHeight = $('.images').height();
		var contentWidth = windowWidth - sidebarWidth;
		var imagesHeight = ($.browser.webkit ? windowHeight - 10 : windowHeight );
				
	//	$('header').css('height', windowHeight);
		$('#content').not('#page-index #content').css({ 'max-width': contentWidth });
		
		$('.sidescroll .images').css({
			'line-height': windowHeight - 30 + 'px',
			'height': imagesHeight + 'px'
		});
		
		// Firefox / IE min-height for images in gallery
		
		if ($('.left .sidescroll .images').length > 0) {
		
			if ($.browser.mozilla || $.browser.msie) {
			
				var maxHeight = parseInt(imagesHeight * .96);
				$('.images img:not(.blocker)').css('height', maxHeight + 'px');
			
			}
		
		}
		
	
	};
	
	rsn.sideScroll = function() {


/* Trigger Layout Resizing */


		rsn.resizeLayout();
		$(window).resize(rsn.resizeLayout);
		if (navigator.userAgent.match(/like Mac OS X/i)) {
		
		//	$('body').bind('onorientationchange', function() { rsn.resizeLayout(); });
		
		}
		

/* Show Content Area */


		$('#content').css('visibility', 'visible');

	
/* Scrolling */


		var scrollSpeed = (navigator.appVersion.indexOf("Mac") != -1 ? 25 : 70);

		$('.sidescroll .images').mousewheel(function(event, delta, deltaX, deltaY) {
				
			if (deltaY > 0 && deltaY > deltaX || deltaY < 0 && deltaY < deltaX) {
			
				var speed = deltaY * scrollSpeed;
				var position = $(this).scrollLeft() + -speed;
				$(this)[0].scrollLeft = position;
				return false;
		
			}		
		
		});
		
		
/* Hover */


		$('.left .images li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
		
		
/* Firefox/IE Image Resizing Fix (max-height isn't working for some reason with these browsers) */
	
		
		if ($.browser.mozilla || $.browser.msie) {

			if ($('.sidescroll .images').length > 0) {
			
				$('.images img:not(.blocker)').each(function() {
	
					var img = $(this);
					$(img).addClass('transparent');
					
					$('<img>').attr('src', $(img).attr('src')).load(function() {
					
						$(img).css('maxHeight', this.height + 'px').removeClass('transparent');
						
					});
	
				});
			
			}
		
		}
		
	
	};
	
	
/* Markup Changes
----------------------------------------------------------------------------- */
	
	
	rsn.markup = function() {
	
		$('.archive .thumbs > li:nth-child(5n+1)').addClass('clear');
	
	};
	
	
/* More Images
----------------------------------------------------------------------------- */
	
	
	rsn.pages = function() {
		
		$(window).scroll(function() {
					
			if ($(window).height() + $(window).scrollTop() >= $(document).height() - 10) {
		
				rsn.moreImages();
		
			}
		
		});
	
	};
	
	rsn.moreImages = function() {
				
		var currentPage = $('.images').attr('class').split(' ').pop().replace('page','') * 1;
		var nextPage = currentPage + 1;
		$('.images').removeClass('page' + currentPage).addClass('page' + nextPage);
		
		var url = "/" + tag + "?page=" + nextPage;
		$('<ul id="temporary"></ul>').appendTo('body').load(url + ' .images > li', function() {
							
			$(this).children('li').each(function() {
			
				$(this).appendTo('.images');
			
			});
			
			$(this).remove();
			
		});
		
	};
	
	
/* Forms
----------------------------------------------------------------------------- */
		
	
	rsn.forms = function() {
	
		// Swap actions to trick robots
		var action = "/actions" + $(this).attr('action');
		$(this).attr('action', action);
	
	}
	
	
/* Peek
----------------------------------------------------------------------------- */
		
	
	rsn.peek = function() {
	
		$('a').each(function() {
		
			var href = $(this).attr('href') + "?peek=true";
			$(this).attr('href', href);
		
		});
		
	}
		
	

/* Timeline
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		// Detect browser
		rsn.browserDetect();
		
		// Change markup
		rsn.markup();
		
		// iOS changes
		rsn.ios();
		
		// Home page billboard rotation
		if ($('#billboard').length > 0) rsn.homePage();
	
		// Lightbox
		if ($('#content .grid').length > 0) lightbox.start();
		
		// Decode email addresses
		rsn.email();
		
		// External links
		rsn.links();
		
		// Sideways layout
		if ($('body.left').length > 0) rsn.sideScroll();

		// Carousel
		if ($('div.carousel').length > 0) rsn.carousel();
				
		// Adjust footer on home page
		if ($('.bleed footer').length > 0) rsn.adjustFooter();
		
		// Automatic page loading
		if ($('.list.stream').length > 0) rsn.pages();
		
		// Swap form actions
		$('.contact_form').each(rsn.forms);
		
		// Grid Sizing
		rsn.grid();
		
		// Peek
		if (typeof peek != "undefined") rsn.peek();
		
	});
	
	$(window).load(function() {

		// Full-Bleed Home Page Background
		if ($('.bleed #billboard').length > 0) {
		
			rsn.resizeBackgroundImage('#billboard .show');
			$(window).resize(function() { rsn.resizeBackgroundImage('#billboard .show'); });
			
			if (navigator.userAgent.match(/like Mac OS X/i)) {
							
				$('body').bind('onorientationchange', function() {
				
					rsn.resizeBackgroundImage('#billboard .show')
					
				});
			
			}
		
		}

		// Fix Slideshow Height
		if (!$('body').hasClass('bleed')) {
		
			var height = parseInt($('#billboard > img:first-child').height());
			$('#billboard').css('height', height + 'px');
		
		}

	});
		
		

