var first_load = true;
var slideshow = '';
var is_mobile = false;

(function($){
	$(document).ready(function(){
		// Vertically center the page
		vCenter();
		$(window).resize(function() {
			vCenter();
		});

		is_mobile = window.__is_mobile;

		loadPage(window.__home_url);

		$(window).load(function(){
			if(! is_mobile) {
				$('#home-animation').remove();
			} else {
				$('#flashvideo').remove();
			}
		})

		// Site navigation
		$('.site-link').live('click', function() {
			if($(this).parents('nav').length) {
				if($(this).parent().hasClass('current') == false) {
					$(this).parent().addClass('current').siblings('.current').removeClass('current');
				}
			} else {
				$('nav .current').removeClass('current');
			}
			loadPage($(this).attr('href'));
			return false;
		});

		$('.comments-link').live('click', function() {
			if(slideshow != '') {
				stopSlideshow();
			}
			var href = $(this).attr('href');
			$.ajax({
				url: href,
				data: { is_ajax_request: true },
				type: 'GET',
				success: function(data) {
					$(data).prependTo('.retreats');
					if($('.slideshow').length) {
						$('.slideshow').fadeOut(500, function() {
							$(this).remove();
							$('.retreats .entry').hide();
							$('.retreats .comments').show();
							$('.retreats').fadeIn(500, function() {
								$('.comments .scrollable').jScrollPane({
									verticalGutter: 12
								});
							});
						})
					} else {
						$('.retreats .entry').fadeOut(500, function() {
							$('.retreats .comments').fadeIn(500, function() {
								$('.comments .scrollable').jScrollPane({
									verticalGutter: 12
								});
							});
						});
					}
				}
			});
			return false;
		});

		$('.close').live('click', function() {
			stopSlideshow();
			$('.slideshow').fadeOut(500, function() {
				$(this).remove();
				$('.retreats').fadeIn(500);
			});
			return false;
		});

		$('.slide-link').live('click', function() {
			var fader_href = $(this).attr('href');
			loadFader(fader_href);
			return false;
		});

		$('.slideshow .prev').live('click', function() {
			if($(this).hasClass('disabled') == false) {
				gotoSlide('prev');
			}
			return false;
		});

		$('.slideshow .next').live('click', function() {
			if($(this).hasClass('disabled') == false) {
				gotoSlide('next');
			}
			return false;
		});

		$('.slideshow .pause').live('click', function() {
			stopSlideshow();
			$(this).removeClass('pause').addClass('play');
			return false;
		});

		$('.slideshow .play').live('click', function() {
			startSlideshow();
			$(this).removeClass('play').addClass('pause');
			return false;
		});

		$('.slideshow .nav .container a').live('click', function() {
			var idx = $(this).parent().index();
			gotoSlide(idx);
			return false;
		});

		$('.slideshow .nav .prev-page').live('click', function() {
			if($(this).hasClass('disabled') == false) {
				prevPage();
			}
			return false;
		});

		$('.slideshow .nav .next-page').live('click', function() {
			if($(this).hasClass('disabled') == false) {
				nextPage();
			}
			return false;
		});
	})

	function loadPage(page_href) {
		$.ajax({
			url: page_href,
			data: { is_ajax_request: true },
			type: 'GET',
			success: function(data) {
				if(first_load) {
					first_load = false;
					$(data).appendTo('#main');
					$('#main .appended').show();
					if($('.home-video').length) {
						if(! is_mobile) {
							$('#home-animation').remove();
						} else {
							$('#flashvideo').remove();
						}
					}
					$('#main .appended').show(0, function() {
						home_video();
					});
				} else {
					$('#main .appended').css({
						position: 'absolute',
						top: 0,
						left: 0
					}).fadeOut(500, function() {
						$(this).remove();
					});
					$(data).appendTo('#main');
					if($('.home-video').length) {
						if(! is_mobile) {
							$('#home-animation').remove();
						} else {
							$('#flashvideo').remove();
						}
					}
					$('#main .appended').show(0, function() {
						home_video();
					});
					$('#main .appended:last').fadeIn(500);
				}
				if ( $('#main .appended:last').hasClass('long-content') ) {
					$('#main .appended:last .content-container').jScrollPane({
						verticalGutter: 12
					});
				};
			}
		});
	}

	function loadFader(fader_href) {
		$.ajax({
			url: fader_href,
			data: { is_ajax_request: true },
			type: 'GET',
			success: function(data) {
				$('.retreats').fadeOut(500, function() {
					$(data).appendTo('.appended');
					$('.slideshow').fadeIn(500);
					buildSlideshow();
				});
			}
		})
	}

	function buildSlideshow() {
		var slides_length = $('.slideshow .container li').length;
		var num_wid = slides_length * 26;
		$('.slideshow .nav .container').css('width', num_wid);
		$('.slideshow p.current span:first').text('1');
		$('.slideshow p.current span:last').text(slides_length);
		$('.slideshow .numbers .container').append('<ul />')
		for(i=1;i<=slides_length;i++) {
			i < 10 ? i = '0' + i : i = i;
			$('.slideshow .numbers ul').append('<li><a href="#">' + i + '</a></li>');
		}
		$('.slideshow .nav .prev, .slideshow .nav .prev-page').addClass('disabled');
		if(slides_length <= 14) {
			$('.slideshow .nav .next-page').addClass('disabled');
		}
		$('.slideshow > .container li:first').fadeIn(500);
		$('.slideshow .nav .container li:first').addClass('current');
		var delay = 100;
		$('.slideshow .nav .container li').each(function() {
			$(this).delay(delay).fadeIn(300);
			delay = delay + 50;
		});
		startSlideshow();
	}

	function startSlideshow() {
		slideshow = setTimeout(function() {
			var current = $('.slideshow > .container li:visible');
			var next = undefined;
			current.next().length ? next = current.next() : next = current.parent().find('li:first');
			current.fadeOut(800);

			var next_idx = next.index();
			$('.slideshow .nav .container .current').removeClass('current');
			$('.slideshow .nav .container li').eq(next_idx).addClass('current');
			$('.slideshow p.current span:first').text(next_idx+1);

			next.fadeIn(800, function() {
				slideshowControls();
			});

			if($('.slideshow .nav .numbers .current').offset().left >= $('.slideshow .nav .numbers').width() + $('.slideshow .nav .numbers').offset().left) {
				nextPage();
			}
			if($('.prev-page').hasClass('disabled') == false && $('.slideshow .nav .numbers li:first').hasClass('current')) {
				prevPage();
			}

			startSlideshow();
		}, 6000);
	}

	function stopSlideshow() {
		clearTimeout(slideshow);
	}

	function gotoSlide(idx) {
		if(slideshow != '') {
			stopSlideshow();
		}
		var current = $('.slideshow > .container li:visible');
		if(idx == 'next') {
			var next = undefined;
			current.next().length ? next = current.next() : next = current.parent().find('li:first');
			current.fadeOut(800);

			var next_idx = next.index();
			$('.slideshow .nav .container .current').removeClass('current');
			$('.slideshow .nav .container li').eq(next_idx).addClass('current');
			$('.slideshow p.current span:first').text(next_idx+1);

			next.fadeIn(800, function() {
				slideshowControls();
			});
		} else if(idx == 'prev') {
			var prev = undefined;
			current.prev().length ? prev = current.prev() : prev = current.parent().find('li:last');
			current.fadeOut(800);

			var prev_idx = prev.index();
			$('.slideshow .nav .container .current').removeClass('current');
			$('.slideshow .nav .container li').eq(prev_idx).addClass('current');
			$('.slideshow p.current span:first').text(prev_idx+1);

			prev.fadeIn(800, function() {
				slideshowControls();
			});
			startSlideshow();
		} else {
			current.fadeOut(800);
			$('.slideshow > .container li').eq(idx).fadeIn(800, function() {
				slideshowControls();
			});
			$('.slideshow .nav .container .current').removeClass('current');
			$('.slideshow .nav .container li').eq(idx).addClass('current');
			$('.slideshow p.current span:first').text(idx+1);
			startSlideshow();
		}
	}

	function slideshowControls() {
		var current = $('.slideshow .nav .container .current').index();
		if(current == 0) {
			$('.slideshow .nav .prev').addClass('disabled');
			$('.slideshow .nav .netx').removeClass('disabled');
		} if(current > 0) {
			$('.slideshow .nav .prev').removeClass('disabled');
		} if(current == $('.slideshow .nav .container li').length - 1) {
			$('.slideshow .nav .next').addClass('disabled');
		}
	}

	function nextPage() {
		$('.slideshow .nav .prev-page').removeClass('disabled');
		var current_pos = parseInt($('.slideshow .nav .container').css('left'));
		var new_pos = current_pos - 360;
		$('.slideshow .nav .numbers .container').animate({
			left: new_pos
		}, function() {
			if($('.slideshow .nav .container').width() - Math.abs(parseInt($('.slideshow .nav .container').css('left'))) < $('.slideshow .nav .numbers').width()) {
				$('.slideshow .nav .next-page').addClass('disabled');
			}
		});
	}

	function prevPage() {
		$('.slideshow .nav .next-page').removeClass('disabled');
		var current_pos = parseInt($('.slideshow .nav .container').css('left'));
		var new_pos = current_pos + 360;
		$('.slideshow .nav .numbers .container').animate({
			left: new_pos
		}, function() {
			if(parseInt($('.slideshow .nav .container').css('left')) == 0) {
				$('.slideshow .nav .prev-page').addClass('disabled');
			}
		});
	}

	function vCenter() {
		if($(window).height() > $('#wrap').outerHeight()) {
			var diff = Math.round(($(window).height() - $('#wrap').outerHeight())/2);
			$('#wrap').css('margin-top', diff);
		} else {
			$('#wrap').removeAttr('style');
		}
	}

	function home_video() {
		setTimeout(function() {
			$('#home-animation span').addClass('show');
		}, 10);
	}
})(jQuery)
