var Slideshow = {
	
	numberOfSlides: 0,
	currentSlide: 0,
	slideWidth: 0,
	slideHeight: 0,
	textVisible: true,
	
	initialize: function() {
		this.numberOfSlides = $("#slideshow-items li").length;
		$("#text").attr("original-height", $("#text").height());
		this.resize();
		$("#loading").hide();
		$("#slideshow-items").fadeIn(250);
	},
	
	resize: function() {
		var windowHeight = $(window).height();
		var slideHeight = this.slideHeight = windowHeight - 220;
		var slideWidth = this.slideWidth = Math.floor(slideHeight * 1.5);
		var slideshowWidth = this.numberOfSlides * slideWidth;
		var buttonHeight = this.slideHeight - 8;
		$("#slideshow-items").css("width", slideshowWidth+"px");
		$("#slideshow-items h2").each(function(){
			$(this).css("width", (slideWidth-8)+"px");
		});
		// $("#slideshow-items h2, #slideshow-items p.subtitle").each(function(){
		$("#slideshow-items p.subtitle").each(function(){
			// $(this).css("width", (slideWidth-8)+"px");
			$(this).css("width", Math.round((slideWidth-8)/3)+"px");
		});
		$("#slideshow-items img:not(.portrait), #slideshow-items object, #slideshow-items iframe.vimeo").each(function(){
			$(this).css("height", slideHeight+"px").css("width", slideWidth+"px");
		});
		var _margin = Math.floor((slideWidth - (slideHeight / 3 * 4)) / 4);
		$("#slideshow-items img.portrait").css({
			"height": slideHeight,
			"width": Math.floor(slideHeight / 3 * 2),
			"margin-left": _margin,
			"margin-right": _margin
		});
		$("#slideshow-items ul").css("left", this.calculatePosition());
		$("#slideshow-navigation li a").each(function(){
			$(this).css("height", buttonHeight+"px");
		});
		
		// text max height = windowHeight - 32
		var originalHeight = $("#text").attr("original-height");
		// if (windowHeight - 32 < originalHeight) {
		// 	$("#text").css("height", (windowHeight - 32)+"px");
		// } else {
		// 	$("#text").css("height", "auto");
		// }
		if (windowHeight - 56 < originalHeight) {
			$("#text").css("height", (windowHeight - 56)+"px");
		} else {
			$("#text").css("height", "auto");
		}
		
	},
	
	next: function() {
		this.show(this.currentSlide + 1);
	},
	
	previous: function() {
		this.show(this.currentSlide - 1);
	},
	
	calculatePosition: function() {
		var windowWidth = $(window).width();
		if (windowWidth > this.numberOfSlides * this.slideWidth) {
			return 0;
		} else {
			var newPosition = this.currentSlide * this.slideWidth;
			var itemsLeft = this.numberOfSlides - this.currentSlide - 1;
			var whiteSpace = windowWidth - this.slideWidth - (itemsLeft * this.slideWidth);
			if (whiteSpace > 0) {
				return -(newPosition - whiteSpace);
			} else {
				return -newPosition;
			}
		}
	},
	
	show: function(num) {
		if (num >= this.numberOfSlides) num = this.numberOfSlides - 1;
		if (num < 0) num = 0;
		if (num != this.currentSlide) {
			this.currentSlide = num;
			this.currentSlide > 0 ? $("#slideshow-navigation .left").show() : $("#slideshow-navigation .left").hide();
			this.currentSlide < this.numberOfSlides - 1 ? $("#slideshow-navigation .right").show() : $("#slideshow-navigation .right").hide();
			var newPosition = this.calculatePosition();
			$("#slideshow-items ul").animate({left:newPosition},250);
		}
	},
	
	toggleText: function() {
		if (this.textVisible) {
			// $("#text").slideUp(250);
			$("#text").animate({height:'hide', opacity:0.3}, 250);
			this.textVisible = false;
			$("#text-nav").attr("class", "open");
		} else {
			$("#text").animate({height:'show', opacity:1}, 250);
			// $("#text").slideDown(250);
			this.textVisible = true;
			$("#text-nav").attr("class", "close");
		}
	}
	
};

$(window).resize(function(){
	Slideshow.resize();
});
$(window).load(function(){
	Slideshow.initialize();
});
