var param = {};

$(function() {

	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		$(this).html('<a href="mailto:'+address+'">'+address+'</a>');
	});
	
	// open all external links in new window
	$("a.ex").click(function() {
		window.open(this.href);
		return false;
	});

	// fade in sub-menus
	$("ul.fade li").hide();
	(function showel(el) {
		el.fadeIn(500, function() {
			$(this).next().length && showel($(this).next()); 
		});
	})( $("ul.fade li:first") );

	// fade in landing images
	$("#project-image.fade").hide();
	$("#project-image.fade").fadeIn(2000);

	// project slide related functions
	$("#project-images a").not(".button").click(function() {
		MoveTo(this);
		return false;
	});

	$("#project-images a.button").click(function() {
		var direction = $(this).attr("id");
		var prev = $(this).prev(".here");
		var next = $(this).next(".here");

		if( (direction == "next" && prev.length > 0) ) {
			// go to the next link after the previous button
			var loc = $("#project-images a:first").next("a");
			MoveTo(loc);
		} else if( (direction == "prev" && next.length > 0) ) {
			// go to the previous link before the next button
			var loc = $("#project-images a:last").prev("a");
			MoveTo(loc);
		} else {
			Move(direction);
		}
		return false;
	});

	$("#project-menu a.button img").hover(
		function() {
			param.src = $(this).attr("src");
			var parts = param.src.split(".");
			$(this).attr("src", parts[0]+"_hover."+parts[1]);
		},
		function() {
			$(this).attr("src", param.src);
		}
	);

});

function GetProjectImage(img_id) {
	var src = $("#"+img_id).attr("href");
	var alt = $("#"+img_id).attr("title");
	if( src ) {
		$("#project-image img").attr({ "src" : src, "alt" : alt, "title" : alt });
	} else {
		return false;
	}
}

function MoveTo(info) {
	var id = $(info).attr("id");
	$(info).addClass("here");
	GetProjectImage(id);
	$("#project-images a").not("#"+id).removeClass("here");
	return false;
}

function Move(direction) {
	var curr = $("#project-images .here");
	var next = $(curr).next("a");
	var prev = $(curr).prev("a");
	if( next || prev ) {
		if( direction == "next" ) {
			MoveTo(next);
		} else if( direction == "prev" ) {
			MoveTo(prev);
		} else {
			return false;
		}
	} else {
		return false;
	}
}