// JohnDeGroote JS
// Written by Unit Interactive for JohnDeGroote.com
// 02/17/10

$(function () {
	
	$.localScroll({
	
		duration: 300
	
	});
	
	$( '#resumesub' ).scrollFollow({
            speed: 300,
            offset: 60,
            container: 'resume'
    });
	
	loadmodal();
	
	setTimeout(emails, 200);
	
});

emails = function () {

	$('a.eadd').each(function() {
	
		var email = $(this).attr('rel');
		
		email = email.replace(/\s\[at\]\s/gi, '@');
		email = email.replace(/\s\[dot\]\s/gi, '.');
		
		$(this).attr('href', 'mailto:' + email);
		
		if ($(this).html().match(/\s\[at\]\s/gi)) $(this).html(email);
	
	});

}


loadmodal = function () {

	$.ajax({ url:"./templates/modal-directions.php", cache:true, dataType:"html" });
	
	$('.modal').bind('click', function () { modals( $(this).attr('rel') ); });
	
}

modals = function (r) {
	
	$(window).unbind('scroll');
		
	var div = $('<div/>').load('./templates/modal-'+r+'.php', function () {
		
		if($.browser.msie) {
			$('body')[0].innerHTML = $('body').html() + div.html();
		}
		else {
			$('body').append( div.html() );
			$('#overlay, #modal').css('opacity', '0');
		}
		
		modalfade();
		
	});
	
}

modalfade = function () {
	
	var scroll = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	
	if (scroll > 122) {
		var s = parseInt($('#modal').css('top')) + scroll;
		$('#modal').css( 'top', s );
	}
	
	$('#overlay').height( $('body').height() );
	$('#modal').css('margin-left', 0 - ($('#modal').width() / 2) );
	
	if(!$.browser.msie) {
		
		$('#overlay').fadeTo(300, 1.0, function () {
			
			$('#modal').fadeTo(300, 1.0);
			$('#close, #overlay').bind('click', closemodal);
			$('#print').bind('click', printmodal);
		
		});
		
	}
	else {
	
		$('#close, #overlay').bind('click', closemodal);
		$('#print').bind('click', printmodal);
		
	}

}

printmodal = function () {
	
	window.print();
	
}

closemodal = function () {
	
	$('#overlay, #modal, body link').remove();
	$('.modal').unbind('click').bind('click', function () { modals( $(this).attr('rel') ); });
	
	$( '#resumesub' ).scrollFollow({
            speed: 300,
            offset: 60,
            container: 'resume'
    });
	
}

formatTweet = function ( txt ) 
{
	// formatting 'http://www.site.com' to '<a href="http://www.site.com">http://www.site.com</a>'
	var urls = txt.match( /http[^\s]+/gi );
	
	if ( urls ) for( var i = 0; i < urls.length; i++ ) txt = txt.replace( urls[i], '<a href="' + urls[i] + '">' + urls[i] + '</a>');

	// formatting '@username' to '<a href="http://twitter.com/username">@username</a>'
	var users = txt.match( /^@[^\s]+|\s@[^\s]+\b/gi );
	
	if ( users ) {
		for( var i = 0; i < users.length; i++ ) {
			users[i] = users[i].replace( ' ', '' );
			users[i] = users[i].replace( ':', '' );
			var user = users[i].replace( /@/, '' );
			
			txt = txt.replace( users[i], '<a href="http://twitter.com/' + user + '">' + users[i] + '</a>');
		}
	}
	
	return txt;
};

twitterFeed = function ( data ) {
	var tweets = '', counter = 0;
	
	$( data ).each(function ( i ) {
							  
			var txt = this.text;
			
			//if ( txt.match( /#unify/gi ) ) {
				counter++;
				tweets += formatTweet( txt );

				//if ( counter == 5 || i == data.length - 1 ) {
					
				$( '#twitter_update_list' ).html( tweets );
				return false;
					
				//}
		//	}
	});
	
};

