/* Author: AJ Livingston

*/

try{Typekit.load();}catch(e){}

var AJL = {
	
	currentExhibit : null,
	exhibits : [],
	loadedExhibits : [],
	numExhibits : 0,
	openExhibits : 0,
	preloader : "<div class='preloader' />",
	speed : 400,
	
	init : function () {
		AJL.numExhibits = $("#work .exhibit").length;
				
		/* set jQuery easing method for all animations */
		$.easing.def = "easeOutExpo";
		
		/* Apply class to body for iOS layout adjustments.
			Probably better with media queries, orko orko! */
		if (window.navigator.userAgent.match(/ipad|iphone|ipod/i)) {
			$("body").addClass("ios");
		}
		
		/* Iterate through the exhibits and..
			record them in the exhibits array for hash validation,
			apply a descending z-index for overlapping appearance. */
		$("#work .exhibit").each(function (i, elem) {
			var hash = ("#" + $(elem).attr('id'));
			AJL.exhibits.push(hash);
			$(elem).css('z-index', (100 - i));
		});
		
		/* show the work list, hides initially since JS dependent on image loading.
		 	Turn on your JavaScript you jabronis. */
		$("#work-list").delay(100).fadeIn(800);
		
		/* Append a link button to expand/contract the menu */
		$("#global-header").append("<span id='header-expand-link'>&ndash;</span>")
		
		/* Set up click handler for expand/contract button in the header menu. */
		$("#header-expand-link, #header-collapsed").click(function (e) {
			e.preventDefault();
			AJL.toggleMenu();
		});
		
		/* Click tracking for about/contact section links */
		$("#header-info a").click(function () {
			var title = $(this).attr('title') || $(this).html();
			_gaq.push(["_trackEvent", "About", "Click", title]);
			//console.log("_GAQ Tracking About Click: " + title);
		})
		
		/* Handle clicks for items in the work list.
		 	Updates window's url hash for selected item */
		$("a.slide").click(function (e) {
			e.preventDefault();	
			var hash = this.hash;
			
			if (AJL.exhibits.indexOf(hash) != -1) {
				window.location.hash = AJL.currentExhibit = hash;	
				$(this).addClass("viewed");

				_gaq.push(["_trackEvent", "Exhibit", "Open", $(this).html()]);
				//console.log("_GAQ Tracking Exhibit Open: " + $(this).html());
				//console.log("Text: " + $(this).text());
				//console.log("Html: " + $(this).html());

				AJL.showExhibit();
			}
		});
		
		/* Close all open exhibits. Reset the currentExhibit */
		$(".close-all-link").click(function (e) {
			e.preventDefault();
			
			$("#work .open").slideUp(AJL.speed, function () {
				$(this).removeClass("open");
			});
			AJL.openExhibits = 0;
			AJL.currentExhibit = '';
			window.location.hash = AJL.currentExhibit;
			$(".close-all-link").css("visibility", "hidden");
		})
		
		/* Click handler for close button on each exhibit. */
		$(".exhibit .close-btn").click(function (e) {
			e.preventDefault();
			var id = ('#' + $(this).parent(".exhibit").attr('id')),
				title = $(id).find(".exhibit-title").html();
			
			_gaq.push(["_trackEvent", "Exhibit", "Close", title]);
			//console.log("_GAQ Tracking Exhibit Close: " + title);
			
			AJL.hideExhibit(id);
		});
		
		/* Add the preloader image to each exhibit,
			to be removed after all images are loaded. */
		$("#work .exhibit .content").prepend(AJL.preloader);
		
		/* Handle page load with an existing hash */
		if (window.location.hash) {
			AJL.hashInit();
		}
		
	},
	
	closeMenu : function () {
		$('#header-content').slideUp();
		$('#header-collapsed').slideDown();
	},
	
	hashInit : function () {
		var hash = window.location.hash,
			index = $(hash).index(),
			title = $(hash).find('.exhibit-title').html();
			
		if (title) {
			_gaq.push(["_trackEvent", "Exhibit", "InitOpen", title]);
			//console.log("_GAQ Tracking Exhibit InitOpen: " + title);
		}
			
		/* Simulate the click on the menu work list */
		$("#work-list .slide:eq(" + index + ")").click();
	},
	
	hideExhibit : function (id) {
		var exhibit = $(id),
			next = exhibit.nextAll(".open")[0],
			prev = exhibit.prevAll(".open")[0],
			closest = $(prev).offset() ? $(prev).offset().top : 0;
			
		exhibit.slideUp(AJL.speed, function () {
			AJL.currentExhibit = '';
			window.location.hash = AJL.currentExhibit;
		}).removeClass("open");
		
		AJL.openExhibits --;
		if (AJL.openExhibits <= 0) {
			AJL.openExhibits = 0;
			AJL.toggleMenu(true);
			$(".close-all-link").css("visibility", "hidden");
		} else {
			AJL.scrollWindow(closest);
		}
		
		//console.log("hideExhibit(), openExhibits: " + AJL.openExhibits);
	},
	
	loadImages : function (i) {
		var index = i;
		//console.log("loadImages(" + index + ")");
		//console.log("loadedExhibits[" + index + "]: " + AJL.loadedExhibits[index]);
		
		if (!AJL.loadedExhibits[index]) {
			var numImages = exhibitImages[index].length,
				numLoaded = 0,
				j = 0;
			
			//console.log("loading images...");
			
			for (j; j < numImages; j ++) {
				var imgUrl = exhibitImages[index][j];
				
				$("#work .exhibit:eq(" + index + ") .img-container img:eq(" + j + ")").load(function () {
					//console.log("image loaded.");
					$(this).slideDown();
					onSuccess();
				}).attr('src', imgUrl);
			}
			
			function onSuccess () {
				numLoaded ++;
				if (numLoaded === numImages) {
					//console.log("All images loaded for exhibit #" + index);
					AJL.loadedExhibits[index] = true;
					
					$("#work .exhibit:eq(" + index + ")").find(".preloader").remove();
				}
			}
			
		}
		
		$("#work .exhibit:eq(" + index + ")").each(function (j) {
			
		});
	},
	
	openMenu : function () {
		$('#header-collapsed').slideUp();
		$('#header-content').slideDown();
		
	},
	
	scrollWindow : function (pos) {
		var position = pos || 0;
		$("html, body").animate({scrollTop : position}, AJL.speed);
	},
	
	showExhibit : function (id) {
		var exId = id || AJL.currentExhibit,
			exhibit = $(exId);
		
		//console.log(exhibit);
		
		if (exhibit) {
			
			if (!exhibit.hasClass("open")) {
				AJL.openExhibits ++;
			}
			
			exhibit.css('min-height', window.innerHeight).slideDown(AJL.speed, function () {
				var content = $(this).find(".content");
				
				exhibit.addClass("open")
					.find('.preloader').css({
						'height' : window.innerHeight
					});
					
				AJL.loadImages($(this).index());
				
				$("html, body").animate({
					scrollTop:$(this).offset().top
				}, AJL.speed);
				
				AJL.currentExhibit = exhibit;
			});
			
			//AJL.openExhibits ++;
			AJL.toggleMenu();
			$(".close-all-link").css("visibility", "visible");
		}
		
		
		
		//console.log("showExhibit(), openExhibits: " + AJL.openExhibits);
	},
	
	toggleMenu : function (open) {
		var link = $('#header-expand-link');
		
		if (link.html().match(/\+/i) || open) {
			link.html('–');
			AJL.openMenu();
		} else if (link.html().match(/–/i)) {
			link.html('+');
			AJL.closeMenu();
		} 
	}
	
};

/* Document ready */
$(function () {
	if (exhibitImages) {
		AJL.init();
	}
});




















