jQuery (
	function () {
		
		$('#photographer-www').css ('opacity', 0.5);
		
		var $win	= $(window),
			$doc	= $(document),
			$body	= $('body'),
			$container	= $('#container'),
			$loader	= $('#loader'),
			$swipeable	= $('#swipeable'),
			$swipeLeft	= $('#swipe-left'),
			$swipeRight	= $('#swipe-right'),
			$menuBg	= $('header .background');
		
		var $gallery		= $('#gallery'),
			$box			= $('#box'),
			$boxTitle		= $box.find ('.title'),
			$boxCaption		= $box.find ('.caption'),
			$slides			= $gallery.children ('.slide'),
			$imageholder	= $('div.imageholder'),
			$pages			= $('article.page'),
			$play			= $box.find ('.play');
			$nav			= $('nav a'),
			imagesLoaded	= 0,
			ratio			= 1.5;
				
		function appResize () {
			/* Resize gallery */
			
			app.appWidth		= $win.width ();
			app.appHeight		= $win.height ();
			app.galleryWidth	= app.appWidth * $slides.length;
			app.appRatio		= app.appWidth / app.appHeight;

			$body.removeClass ('width-height height-width');

			if (app.appRatio > ratio) {
				$body.addClass ('width-height');
			} else {
				$body.addClass ('height-width');
			}
			
			/* Set sildes to new stuff */
			$slides.width (app.appWidth).height (app.appHeight);
			$pages.width (app.appWidth).height (app.appHeight);
			$imageholder.width (app.appWidth).height (app.appHeight);
			$gallery.width (app.galleryWidth);
			
			/* Focus on the current slide */
			
		}
		
		function openApp () {
			
			$loader.fadeOut (1000,
				function () {
					$swipeable.delay (3000).fadeOut ();
				}
			);
			
			
		}
		
		function imageLoaded ($slide, image) {
			
			if ($slide.hasClass ('page')) {
				$slide.find ('.imageholder').prepend (image);
			} else {
				$slide.prepend (image);
			}
			
			imagesLoaded++;

			if (imagesLoaded > 10) {
				openApp ();
			}
			
		}
		
		function swipeArrows () {
			if (scroller.currPageX == 0) {
				$swipeLeft.hide ();
			} else {
				$swipeLeft.show ();
			}
			
			
			if (scroller.currPageX == ($slides.length - 1)) {
				$swipeRight.hide ();
			} else {
				$swipeRight.show ();
			}
		}
				
		$win.bind ('resize',
			function () {
				
				appResize ();
				
				setTimeout (
					function () {
						scroller.refresh ();
						scroller.scrollToPage (scroller.currPageX);
					}, 20
				);

			}
		);
		
		appResize ();
		
		/*
		$gallery.draggable ({
			axis: 'x',
			drag: function (e, ui) {
				var position	= ui.offset.left;
				
				if (position > 0) {
					$gallery.css ({left: 0});
					return false;
				}
				
				var maxDrag	= (0 - (app.galleryWidth - app.appWidth));
				if (position <= maxDrag) {
					$gallery.css ({left: maxDrag});
					return false;
				}
			}
		});
		*/
		
		$slides.add ($pages).each (
			function () {
				var $this	= $(this),
					img	= new Image ();
				
				img.onload	= function () {
					imageLoaded ($this, img);
				};
				
				img.src	= site_link + this.getAttribute ('data-image');
				
				
				$this.data ('slide.title', $this.find ('.title').html ());
				$this.data ('slide.caption', $this.find ('.caption').html ());
				$this.data ('slide.video', $this.hasClass ('video'));
			}
		);

		var scrollSettings	= {
						snap: '.slide',
						momentum: false,
						hScrollbar: false,
						vScrollbar: false,
						onScrollEnd: function () {
							var $slide	= $slides.eq (scroller.currPageX),
								title	= $slide.data ('slide.title'),
								caption	= $slide.data ('slide.caption');
							
							$boxTitle.html (title);
							$boxCaption.html (caption);
							
							document.title	= title + ' | ' + site_name;
							
							$box.removeClass ('picture video pos-1 pos-2 pos-3 home');
							
							if ($slide.data ('slide.video') === true) {
								$box.addClass ('video');
							} else {
								$box.addClass ('picture');
							}
						
							$box.addClass ($slide.data ('pos'));
							
							$('#photographer-name').text ($slide.data ('photographer-name'));
							$('#photographer-www').attr ('href', $slide.data ('photographer-www'));
							
							swipeArrows ();
							slideToNext ();
							
						}
					};

		if (Modernizr.touch) {
			
			$.getScript (template_dir + 'assets/js/iscroll.js',
				function () {
					scroller	= new iScroll ('container', scrollSettings);
					
					
					swipeArrows ();
				}
			);
			
		} else {
			$.getScript (template_dir + 'assets/js/scroll.js',
				function () {
					scroller	= new Scroll ('container', scrollSettings);
					
					
					swipeArrows ();
				}
			);
		}
		
		$(document).bind ('keyup',
			function (e) {
				
				if (e.which == 39) {
					$swipeRight.trigger ('click');
				}
				if (e.which == 37) {
					$swipeLeft.trigger ('click');
				}
			}
		);
		
		$swipeLeft.bind ('click',
			function (e) {
				e.preventDefault ();
				if (scroller.currPageX > 0) {
					$('#youtube-player').remove ();
					slideDirection	= 'left';
					scroller.scrollToPage (scroller.currPageX - 1, 0, 400);
				}
			}
		);
		
		$swipeRight.bind ('click',
			function (e) {
				e.preventDefault ();
				if (scroller.currPageX < ($slides.length - 1)) {
					$('#youtube-player').remove ();
					slideDirection	= 'right';
					scroller.scrollToPage (scroller.currPageX + 1, 0, 400);
				}
			}
		);
		
		$pages.css ({top: 0 - app.appWidth, left: 0});
		
		$pages.find ('.close').bind ('click',
			function (e) {
				e.preventDefault ();
				
				var $this	= $(this);
				
				$this.closest ('.page').removeClass ('active')
				.animate ({top: 0 - app.appWidth});
			}
		);
		
		$nav.bind ('click',
			function (e) {
				e.preventDefault ();
				
				var $this		= $(this),
					id			= this.getAttribute ('data-id'),
					$page		= $pages.filter ('[data-id=' + id + ']'),
					topStart	= 0 - app.appWidth;
				
				if ($page.hasClass ('active')) {
					$page.animate ({
						top: topStart
					}).removeClass ('active');
					return false;
				}
				
				$nav.removeClass ('active');
				$this.addClass ('active');
				
				document.title	= $page.data ('slide.title') + ' | ' + site_name;
				
				$pages.filter ('.active')
				.removeClass ('active')
				.animate ({top: topStart});
				
				
				$page.addClass ('active').css ({
					top: topStart
				}).animate ({
					top: 0
				});
				
			}
		);
		
		$('#home-button').bind ('click',
			function (e) {
				e.preventDefault ();
				$pages.filter ('.active').find ('.close').trigger ('click');
				scroller.scrollToPage (0, 0, 1000);
			}
		);
		
		
		$play.bind ('click',
			function (e) {
				e.preventDefault ();
				
				var html	= $('#player-template').html (),
					$slide	= $slides.eq (scroller.currPageX),
					v		= $slide.data ('video');
				
				$('<a title="" href="http://www.youtube.com/watch?v=' + v + '"></a>').prettyPhoto ({
					opacity: 0.80,
					show_title: false,
					theme: 'dark_rounded'
				}).trigger ('click');
				
			}
		);
		
		
		/* Slide show */
		var sliderTimeout, slideDirection = 'right'
		function slideToNext () {
			sliderTimeout && clearTimeout (sliderTimeout);
			
			sliderTimeout	= setTimeout (
				function () {

					if (slideDirection === 'right') {
						$swipeRight.trigger ('click');
					} else {
						$swipeLeft.trigger ('click');
					}
					
					slideToNext ();
				}
			, 8000);
		}
		
		slideToNext();
		
		$win.bind ('blur',
			function () {
				sliderTimeout && clearTimeout (sliderTimeout);
			}
		).bind ('focus',
			function () {
				slideToNext ();
			}
		);
	}
);

var scroller, app	= {appWidth: 0, appHeight: 0, galleryWidth: 0};
