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

	function thisMovie(movieName)
	{
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	}

	function changeIndex(index) {
		var movie = thisMovie('swf-diaporama');
		if (movie && movie.changeIndex) {
			movie.changeIndex(index);
		}
		var all = $(".diaporama-projects .image-large");
		var current = $(all[index]);
		$('#image-current').text(index + 1);
		$('.diaporama-overlay .legend').text( current.find('.image-title').text() );
	}
	
	var index = 0;

	function canScrollNext() {
		var containerRight = $(".diaporama").offset().left + $(".diaporama").width();
		var lastProjectleft = $('.diaporama img:last').offset().left;
		// console.log(lastProjectleft + '<' + containerRight);
		if (lastProjectleft < containerRight) return false;
		return true;
	}

	function canScrollPrev() {
		var containerLeft = $(".diaporama").offset().left;
		var firstObjectLeft = $('.diaporama img:first').offset().left;
		// console.log(firstObjectLeft + '>' + containerLeft);
		if (firstObjectLeft >= containerLeft) return false;
		return true;
	}

	function fixArrows() {
		if (canScrollPrev()) {
			$(".project-navigation .prev").removeClass('disabled').removeClass('prev-disabled');
		} else {
			$(".project-navigation .prev").addClass('disabled').addClass('prev-disabled');
		}
		if (canScrollNext()) {
			$(".project-navigation .next").removeClass('disabled').removeClass('next-disabled');
		} else {
			$(".project-navigation .next").addClass('disabled').addClass('next-disabled');
		}
	}

	$(".project-navigation .next").click(function() {
		if (canScrollNext()) {

			$(".diaporama-projects").fadeOut(500, function() {
				$(".diaporama-projects").animate({'left': '-=600px'}, 0, null, function() {
					index++;
					changeIndex(index);
					$(".diaporama-projects").fadeIn(500, fixArrows);
				});
			} );

			// $(".diaporama-projects").animate({'left': '-=600px'}, 1000, null, fixArrows);

		}
		return false;
	});

	$(".project-navigation .prev").click(function() {
		if (canScrollPrev()) {

			$(".diaporama-projects").fadeOut(500, function() {
				$(".diaporama-projects").animate({'left': '+=600px'}, 0, null, function() {
					index--;
					changeIndex(index);
					$(".diaporama-projects").fadeIn(500, fixArrows);
				});
			} );

			// $(".diaporama-projects").animate({'left': '+=600px'}, 1000, null, fixArrows);

		}
		return false;
	});

	$(".button-mosaic").click(function() {
		$(".mosaic").css('min-height', $(".diaporama").height() + 'px');
		$(".diaporama").hide();
		$(".project-navigation").hide();
		$(".mosaic").show();
		$(".video").hide();
		return false;
	});

	$(".button-slide").click(function() {
		diaporama_show_overlay();
		$(this).removeClass('off');
		$(".button-video").addClass('off');
		$(".diaporama").show();
		$(".project-navigation").show();
		$(".mosaic").hide();
		$(".video").hide();
		return false;
	});
	
	$(".button-video").click(function() {
		$(this).removeClass('off');
		$(".button-slide").addClass('off');
		$(".mosaic").hide();
		$(".diaporama").hide();
		$(".project-navigation").hide();
		$(".video").show();
		return false;
	});

	// $(".button-zoom").click(function() {
	//     $(".diaporama").show();
	//     $(".project-navigation").show();
	//     $(".mosaic").hide();
	// });

	// $(".mosaic").hide();

	$(".mosaic a").each(function(i, el) {
		$(el).click(function() {

			index = i;

			diaporama_show_overlay();

			$(".diaporama-projects").animate({'left': '-' + (i * 600) + 'px'}, 0, null, fixArrows);

			$(".mosaic").fadeOut(100, function() {
				$(".diaporama, .project-navigation").fadeIn(200, function() {
					changeIndex(index);
					fixArrows();
				});
			});

			return false;
		});
	});

	var hdlEffect = null, hdlTimeout = null;

	function diaporama_show_overlay()
	{
		hdlEffect = $(".diaporama-overlay").fadeTo(250, 1, function() {
			// hdlTimeout = setTimeout(function() {
			// 	hdlEffect = $(".diaporama-overlay").fadeTo(1000, 0.01);
			// }, 1000);
		});		
	}

	diaporama_show_overlay();

	changeIndex(0);

	$(".diaporama, .diaporama-navigation .prev, .diaporama-navigation .next").mouseenter(function() {
		// console.log('mouseenter');
		if (hdlTimeout) clearTimeout(hdlTimeout);
		if (hdlEffect) hdlEffect.stop();
		hdlEffect = $(".diaporama-overlay").fadeTo(100, 1);
	})
	
	$(".diaporama,.diaporama-navigation .prev, .diaporama-navigation .next").mouseleave(function() {
		// console.log('mouseleave');
		if (hdlTimeout) clearTimeout(hdlTimeout);
		if (hdlEffect) hdlEffect.stop();
		hdlEffect = $(".diaporama-overlay").fadeTo(500, 0.01);
	});

})

})(jQuery);