$(function (){
	
	// mail rewrite (href)
	$('a[href*="mailto:"]').each(function() {
		var mailto = $(this).attr('href').split('[at]');
		$(this).attr("href", mailto[0] + "@" + mailto[1]);
	});
	

	// mail rewrite (text node)
	$('a:contains("[at]")').each(function() {
		var mailto = $(this).text().split('[at]');
		$(this).text(mailto[0] + "@" + mailto[1]);
	});
	
	//latest tweet
	$('#footer .twitter p.tweet').load('greenroom/includes/inc_latest_tweet.php');
	
	//Twitter @Anywhere
	twttr.anywhere(function(twitter) {
		twitter("body").hovercards();
		
		// Let's find the elements which has a hovercard class  
		twitter(".hovercard").hovercards({  
			username: function(node){  
				var twitter_regexp = /twitter\.com\/([a-z0-9_]*)\/?(.*)?/gi;  
				if(node.href.match(twitter_regexp) && (twitter_match = twitter_regexp.exec(node.href))){  
					return twitter_match[1];  
				}  
				return '';  
			}  
		});  
     });	
	
	// highlight fades
	$('#highlights .section h3').each(function() {
		var extension = "png";
		var img = $('img', this);
		var src = $(img).attr("src");
		var alt = $(img).attr("alt");
		var srcAlt = src.split('.'+extension)[0] + "-alt." + extension;
	
		$('<img />', {
			src: srcAlt,
			alt: alt
		})
		.css("display", "inline")
		.addClass('alt')
		.insertAfter(img);
	})
	.parent();
	
	
	// IE PNG fix
	if($.fn.pngFix && $('body').hasClass('home')) {
		$(document).pngFix({ blankgif:'../layout/blank.gif' });
	}


	// spotlight navigation
	$('#spotlights').each(function() {
		$hl = $('.highlight', this);
		$inner = $('.inner', this).eq(0);
		$outer = $('.outer', this).eq(0);
		var w = $hl.eq(0).outerWidth();
		var i = $hl.length;
		
		if (i < 4) { return; };

		$outer.append('<div class="nav">\n<a href="#vorig-project" class="prev">Vorig project</a> <a href="#volgend-project" class="next">Volgend project &rsaquo;</a>\n</div>');

		$('.nav .next', this).click(function() {
			$hl = $('.highlight', $inner);
			
			// $(this).animate({
			// 	marginRight: '5px'
			// }, 50, function() {
			// 	$(this).animate({
			// 		marginRight: '0'
			// 	}, 100);
			// });
			
			if ($hl.filter(':animated').length < 1) {
				$hl.filter(':visible:last').next().fadeIn(750);
				$hl.filter(':visible:first').animate({
					marginLeft: '-=' + w,
					opacity: 0
				}, 750, function() {
					$(this)
					.css({
						marginLeft: "0", opacity: "1"
					})
					.hide()
					.appendTo( $inner );
				});
			};			
			
			return false;
		});

		$('.nav .prev', this).click(function() {
			$hl = $('.highlight', $inner);

			// $(this).animate({
			// 	marginLeft: '5px'
			// }, 50, function() {
			// 	$(this).animate({
			// 		marginLeft: '0'
			// 	}, 100);
			// });
			
			if ($hl.filter(':animated').length < 1) {
				$hl.filter(':visible:last').fadeOut(750);
				$hl.filter(':last')
				.insertBefore( $hl.filter(':first') )
				.css({
					marginLeft: -w,
					opacity: '0'
				})
				.show()
				.animate({
					marginLeft: '0',
					opacity: '1'
				}, 750);
			};
			
			return false;
		});

		$hl.slice(3).hide();
		
		$(window).resize(function() {
			$inner.css({
				paddingLeft: $('#main').offset().left + 43,
				margin: "0",
				width: w * i
			});
		}).trigger('resize');

		
	});
	
	
});