$( function() {

	// fix silly 'ol IE
	if( $.browser.msie ) {
		$("head").append('<link type="text/css" rel="stylesheet" href="/css/ie.css" />');
		$(window).resize(function() {
			if( $(this).height() < 655 ) {
				$("#wrapper").addClass("too-small");
			} else {
				$("#wrapper").removeClass("too-small");
			}
		});
	}

	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		var link = $(this).attr("title") || address;
		$(this).html('<a href="mailto:'+address+'">'+link+'</a>');
	});

	// open all external links in new window
	$("a[href^=http], a.external").click(function(e) {
		e.preventDefault();
		window.open(this.href);
	});

	// project type column vars
	var col = {
		step_distance : $("#selected-slider ul:first").outerWidth(true),
		curr_step : 0,
		total : $("#selected-slider ul").length,
		visible : 2
	};

	// show/hide project type controls
	ToggleTypeColumnControls(col);

	// slide columns of projects on project type page
	$("#type-prev, #type-next").live("click", function(e) {
		e.preventDefault();
		var dir = $(this).attr("id").replace("type-", "");

		if( dir === "next" && col.curr_step < col.total - col.visible ) {
			col.curr_step++;
			$("#selected-slider").animate({ left : "-="+(col.step_distance)+"px" });
		} else if( dir === "prev" && col.curr_step > 0 ) {
			col.curr_step--;
			$("#selected-slider").animate({ left : "+="+(col.step_distance)+"px" });
		} else {
			col.curr_step = 0;
			$("#selected-slider").animate({ left : 0 });
		}
		// show/hide project type controls
		ToggleTypeColumnControls(col);
	});

	// fade the project gallery overlay onload
	setTimeout(function() { $(".gallery-overlay").animate({ opacity : 0 }) }, 1500);
	$(".gallery-overlay").hover(
		function() {
			$(this).animate({ opacity : 1 });
		},
		function() {
			$(this).animate({ opacity : 0 });
		}
	);

	var gallery_data = null;
	$(".gallery-overlay").click(function() {
		if( gallery_data == null ) {
			var ID = $(this).attr("id").replace("gallery-", "");
			$.getJSON("/ajax/gallery/"+ID+"/", function(data) {
				gallery_data = data;
				LaunchGalleryPopup();
				AddImagesToGallery(gallery_data);
			});
		} else {
			LaunchGalleryPopup();
			AddImagesToGallery(gallery_data);
		}
	});

	$("#gallery-images img").live("click", function(e) {
		e.preventDefault();
		AdvanceGallery();
	});

	$("#gallery-curtain, #gallery-popup, #gallery-images").live("click", function(e) {
		e.preventDefault();
		// prevent children of these divs from closing the gallery
		if( e.target !== this ) { return; }
		RemoveGalleryPopup();
	});

});

function ToggleTypeColumnControls(col) {
	if( col.curr_step === col.total - col.visible ) {
		$("#type-next").hide();
	} else {
		$("#type-next").show();
	}
	if( col.curr_step === 0 ) {
		$("#type-prev").hide();
	} else {
		$("#type-prev").show();
	}
}

function AddImagesToGallery(data) {
	$.each(data, function(i, item) {
		$('<img title="Click to see the next image ->" stlye="z-index: '+(1005 + i)+';"/>').attr("src", item).appendTo("#gallery-images");
	});
	// show first image
	$("#gallery-images img:first").addClass("show").show();
	$("#gallery-popup").fadeIn();
}

function LaunchGalleryPopup() {
	$("#projects .popout, #project-info").fadeOut("fast");
	$('<div id="gallery-curtain" title="[X] Click to close gallery"/>').appendTo("#main-container");
	$('<div id="gallery-popup" title="[X] Click to close gallery"><div id="gallery-images"/></div>').appendTo("#main-container").hide();
}

function RemoveGalleryPopup() {
	$("#gallery-curtain, #gallery-popup").fadeOut("fast", function() {
		$(this).remove();
		$("#projects .popout, #project-info").fadeIn("fast");
	});
	return false;
}

function AdvanceGallery() {
	var curr = "#gallery-images img.show";
	var next = ($(curr).next().attr("src") != undefined) ? $(curr).next() : $("#gallery-images img:first");
	var fade_time = 200;
	$("#gallery-images").animate({ opacity : 0 }, fade_time, null, function() {
		$(curr).removeClass("show");
		next.addClass("show");
		$(this).animate({ opacity : 1 }, fade_time);
	});
	return false;
}

function FlashIntroComplete() {
	$("#about").fadeIn("fast", function() {
		$("#flash-intro").remove();
	});
}
